Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance file listing functions with sorting and prefix filtering #16526

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions modules/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,34 @@ def list_files_with_name(filename):
return res


def custom_sort_key(filename):
# Regular files come first, followed by those with - and then alphanumeric
match = re.match(r"(.+?)(-\d{2}|-[A-Z]|)(\.\w+)", filename)
base = match.group(1) if match else filename
suffix = match.group(2) if match else ''
extension = match.group(3) if match else ''

# 1. Base name
# 2. Suffix (optional)
return (base, suffix, extension)

def list_files_with_prefix(prefix, file_extension):
res = []
dirs = [paths.script_path] + [ext.path for ext in extensions.active()]

for dirpath in dirs:
if not os.path.isdir(dirpath):
continue

for filename in os.listdir(dirpath):
if filename.startswith(prefix) and filename.endswith(file_extension):
res.append(os.path.join(dirpath, filename))

res.sort(key=lambda x: (os.path.dirname(x), custom_sort_key(os.path.basename(x))))

return res


def load_scripts():
global current_basedir
scripts_data.clear()
Expand Down
2 changes: 1 addition & 1 deletion modules/ui_gradio_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def css_html():
def stylesheet(fn):
return f'<link rel="stylesheet" property="stylesheet" href="{webpath(fn)}">'

for cssfile in scripts.list_files_with_name("style.css"):
for cssfile in scripts.list_files_with_prefix("style", ".css"):
head += stylesheet(cssfile)

user_css = os.path.join(data_path, "user.css")
Expand Down