Skip to content

Commit

Permalink
Merge branch 'main' into feature-scan-extra-folders
Browse files Browse the repository at this point in the history
  • Loading branch information
hayden-fr authored Nov 28, 2024
2 parents 5a93893 + e891630 commit 894351c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
6 changes: 3 additions & 3 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ async def read_models(request):
Scan all models and read their information.
"""
try:
result = services.scan_models()
result = services.scan_models(request)
return web.json_response({"success": True, "data": result})
except Exception as e:
error_msg = f"Read models failed: {str(e)}"
Expand Down Expand Up @@ -231,7 +231,7 @@ async def download_model_info(request):
post = await utils.get_request_body(request)
try:
scan_mode = post.get("scanMode", "diff")
await services.download_model_info(scan_mode)
await services.download_model_info(scan_mode, request)
return web.json_response({"success": True})
except Exception as e:
error_msg = f"Download model info failed: {str(e)}"
Expand Down Expand Up @@ -287,7 +287,7 @@ async def migrate_legacy_information(request):
Migrate legacy information.
"""
try:
await services.migrate_legacy_information()
await services.migrate_legacy_information(request)
return web.json_response({"success": True})
except Exception as e:
error_msg = f"Migrate model info failed: {str(e)}"
Expand Down
3 changes: 3 additions & 0 deletions py/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"download": {
"max_task_count": "ModelManager.Download.MaxTaskCount",
},
"scan": {
"include_hidden_files": "ModelManager.Scan.IncludeHiddenFiles"
},
}

user_agent = "Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148"
Expand Down
12 changes: 6 additions & 6 deletions py/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
from . import searcher


def scan_models():
def scan_models(request):
result = []
model_base_paths = utils.resolve_model_base_paths()
for model_type in model_base_paths:

folders, extensions = folder_paths.folder_names_and_paths[model_type]
for path_index, base_path in enumerate(folders):
files = utils.recursive_search_files(base_path)
files = utils.recursive_search_files(base_path, request)

models = folder_paths.filter_files_extensions(files, folder_paths.supported_pt_extensions)

Expand Down Expand Up @@ -136,14 +136,14 @@ def fetch_model_info(model_page: str):
return result


async def download_model_info(scan_mode: str):
async def download_model_info(scan_mode: str, request):
utils.print_info(f"Download model info for {scan_mode}")
model_base_paths = utils.resolve_model_base_paths()
for model_type in model_base_paths:

folders, extensions = folder_paths.folder_names_and_paths[model_type]
for path_index, base_path in enumerate(folders):
files = utils.recursive_search_files(base_path)
files = utils.recursive_search_files(base_path, request)

models = folder_paths.filter_files_extensions(files, folder_paths.supported_pt_extensions)

Expand Down Expand Up @@ -192,7 +192,7 @@ async def download_model_info(scan_mode: str):
utils.print_debug("Completed scan model information.")


async def migrate_legacy_information():
async def migrate_legacy_information(request):
import json
import yaml
from PIL import Image
Expand All @@ -204,7 +204,7 @@ async def migrate_legacy_information():

folders, extensions = folder_paths.folder_names_and_paths[model_type]
for path_index, base_path in enumerate(folders):
files = utils.recursive_search_files(base_path)
files = utils.recursive_search_files(base_path, request)

models = folder_paths.filter_files_extensions(files, folder_paths.supported_pt_extensions)

Expand Down
26 changes: 23 additions & 3 deletions py/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,29 @@ def get_download_path():
return download_path


def recursive_search_files(directory: str):
files, folder_all = folder_paths.recursive_search(directory, excluded_dir_names=[".git"])
return [normalize_path(f) for f in files]
def recursive_search_files(directory: str, request):
if not os.path.isdir(directory):
return []

excluded_dir_names = [".git"]
result = []
include_hidden_files = get_setting_value(request, "scan.include_hidden_files", False)

for dirpath, subdirs, filenames in os.walk(directory, followlinks=True, topdown=True):
subdirs[:] = [d for d in subdirs if d not in excluded_dir_names]
if not include_hidden_files:
subdirs[:] = [d for d in subdirs if not d.startswith(".")]
filenames[:] = [f for f in filenames if not f.startswith(".")]

for file_name in filenames:
try:
relative_path = os.path.relpath(os.path.join(dirpath, file_name), directory)
result.append(relative_path)
except:
logging.warning(f"Warning: Unable to access {file_name}. Skipping this file.")
continue

return [normalize_path(f) for f in result]


def search_files(directory: str):
Expand Down
7 changes: 7 additions & 0 deletions src/hooks/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,12 @@ function useAddConfigSettings(store: import('hooks/store').StoreProvider) {
})
},
})

app.ui?.settings.addSetting({
id: 'ModelManager.Scan.IncludeHiddenFiles',
name: 'Include hidden files(start with .)',
defaultValue: false,
type: 'boolean',
})
})
}

0 comments on commit 894351c

Please sign in to comment.