Fix hidden directory and model settings.

This commit is contained in:
Sj-Si 2024-04-22 09:55:48 -04:00
parent ba365e4f6b
commit f051937623
2 changed files with 27 additions and 7 deletions

View File

@ -513,6 +513,7 @@ class ExtraNetworksClusterizeCardsList extends ExtraNetworksClusterize {
/*Expects an object like the following: /*Expects an object like the following:
{ {
search_keys: array of strings, search_keys: array of strings,
search_only: bool,
sort_<mode>: string, (for various sort modes) sort_<mode>: string, (for various sort modes)
} }
*/ */

View File

@ -51,6 +51,7 @@ class CardListItem(ListItem):
self.visible: bool = False self.visible: bool = False
self.sort_keys = {} self.sort_keys = {}
self.search_terms = "" self.search_terms = ""
self.search_only = False
class TreeListItem(ListItem): class TreeListItem(ListItem):
@ -105,20 +106,24 @@ class DirectoryTreeNode:
def add_child(self, child: "DirectoryTreeNode") -> None: def add_child(self, child: "DirectoryTreeNode") -> None:
self.children.append(child) self.children.append(child)
def build(self, items: dict[str, dict]) -> None: def build(self, items: dict[str, dict], include_hidden: bool = False) -> None:
"""Builds a tree of nodes as children of this instance. """Builds a tree of nodes as children of this instance.
Args: Args:
items: A dictionary where keys are absolute filepaths for directories/files. items: A dictionary where keys are absolute filepaths for directories/files.
The values are dictionaries representing extra networks items. The values are dictionaries representing extra networks items.
include_hidden: Whether to include hidden directories in the tree.
""" """
self.is_dir = os.path.isdir(self.abspath) self.is_dir = os.path.isdir(self.abspath)
if self.is_dir: if self.is_dir:
for x in os.listdir(self.abspath): for x in os.listdir(self.abspath):
child_path = os.path.join(self.abspath, x) child_path = os.path.join(self.abspath, x)
# Skip hidden directories if include_hidden is False
if os.path.isdir(child_path) and os.path.basename(child_path).startswith(".") and not include_hidden:
continue
# Add all directories but only add files if they are in the items dict. # Add all directories but only add files if they are in the items dict.
if os.path.isdir(child_path) or child_path in items: if os.path.isdir(child_path) or child_path in items:
DirectoryTreeNode(self.root_dir, child_path, self).build(items) DirectoryTreeNode(self.root_dir, child_path, self).build(items, include_hidden)
else: else:
self.item = items.get(self.abspath, None) self.item = items.get(self.abspath, None)
@ -418,12 +423,14 @@ class ExtraNetworksPage:
filename = item.get("filename", "") filename = item.get("filename", "")
# if this is true, the item must not be shown in the default view, # if this is true, the item must not be shown in the default view,
# and must instead only be shown when searching for it # and must instead only be shown when searching for it
if shared.opts.extra_networks_hidden_models == "Always": show_hidden_models = str(shared.opts.extra_networks_hidden_models).strip().lower()
if show_hidden_models == "always":
search_only = False search_only = False
else: else:
search_only = filename.startswith(".") # If any parent dirs are hidden, the model is also hidden.
search_only = any(os.path.basename(x).startswith(".") for x in Path(filename).parents)
if search_only and shared.opts.extra_networks_hidden_models == "Never": if search_only and show_hidden_models == "never":
return "" return ""
sort_keys = {} sort_keys = {}
@ -497,9 +504,17 @@ class ExtraNetworksPage:
card_html = self.create_card_html(tabname=tabname, item=item, div_id=div_id) card_html = self.create_card_html(tabname=tabname, item=item, div_id=div_id)
sort_keys = {k.strip().lower().replace(" ", "_"): html.escape(str(v)) for k, v in item.get("sort_keys", {}).items()} sort_keys = {k.strip().lower().replace(" ", "_"): html.escape(str(v)) for k, v in item.get("sort_keys", {}).items()}
search_terms = item.get("search_terms", []) search_terms = item.get("search_terms", [])
show_hidden_models = str(shared.opts.extra_networks_hidden_models).strip().lower()
if show_hidden_models == "always":
search_only = False
else:
# If any parent dirs are hidden, the model is also hidden.
filename = item.get("filename", "")
search_only = any(os.path.basename(x).startswith(".") for x in Path(filename).parents)
self.cards[div_id] = CardListItem(div_id, card_html) self.cards[div_id] = CardListItem(div_id, card_html)
self.cards[div_id].sort_keys = sort_keys self.cards[div_id].sort_keys = sort_keys
self.cards[div_id].search_terms = " ".join(search_terms) self.cards[div_id].search_terms = " ".join(search_terms)
self.cards[div_id].search_only = search_only
# Sort cards for all sort modes # Sort cards for all sort modes
sort_modes = ["name", "path", "date_created", "date_modified"] sort_modes = ["name", "path", "date_created", "date_modified"]
@ -514,7 +529,8 @@ class ExtraNetworksPage:
res[div_id] = { res[div_id] = {
**{f"sort_{mode}": key for mode, key in card_item.sort_keys.items()}, **{f"sort_{mode}": key for mode, key in card_item.sort_keys.items()},
"search_terms": card_item.search_terms, "search_terms": card_item.search_terms,
"visible": True, "search_only": card_item.search_only,
"visible": not card_item.search_only,
} }
return res return res
@ -707,7 +723,10 @@ class ExtraNetworksPage:
if not os.path.exists(abspath): if not os.path.exists(abspath):
continue continue
self.tree_roots[abspath] = DirectoryTreeNode(os.path.dirname(abspath), abspath, None) self.tree_roots[abspath] = DirectoryTreeNode(os.path.dirname(abspath), abspath, None)
self.tree_roots[abspath].build(tree_items if shared.opts.extra_networks_tree_view_show_files else {}) self.tree_roots[abspath].build(
tree_items if shared.opts.extra_networks_tree_view_show_files else {},
include_hidden=shared.opts.extra_networks_show_hidden_directories,
)
# Generate the html for displaying directory buttons # Generate the html for displaying directory buttons
dirs_html = self.create_dirs_view_html(tabname) dirs_html = self.create_dirs_view_html(tabname)