Commit 910a097a authored by AUTOMATIC's avatar AUTOMATIC
Browse files

add initial version of the extensions tab

fix broken Restart Gradio button
parent 9b384dfb
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line

function extensions_apply(_, _){
    disable = []
    update = []
    gradioApp().querySelectorAll('#extensions input[type="checkbox"]').forEach(function(x){
        if(x.name.startsWith("enable_") && ! x.checked)
            disable.push(x.name.substr(7))

        if(x.name.startsWith("update_") && x.checked)
            update.push(x.name.substr(7))
    })

    restart_reload()

    return [JSON.stringify(disable), JSON.stringify(update)]
}

function extensions_check(){
    gradioApp().querySelectorAll('#extensions .extension_status').forEach(function(x){
        x.innerHTML = "Loading..."
    })

    return []
}
 No newline at end of file

modules/extensions.py

0 → 100644
+83 −0
Original line number Diff line number Diff line
import os
import sys
import traceback

import git

from modules import paths, shared


extensions = []
extensions_dir = os.path.join(paths.script_path, "extensions")


def active():
    return [x for x in extensions if x.enabled]


class Extension:
    def __init__(self, name, path, enabled=True):
        self.name = name
        self.path = path
        self.enabled = enabled
        self.status = ''
        self.can_update = False

        repo = None
        try:
            if os.path.exists(os.path.join(path, ".git")):
                repo = git.Repo(path)
        except Exception:
            print(f"Error reading github repository info from {path}:", file=sys.stderr)
            print(traceback.format_exc(), file=sys.stderr)

        if repo is None or repo.bare:
            self.remote = None
        else:
            self.remote = next(repo.remote().urls, None)
            self.status = 'unknown'

    def list_files(self, subdir, extension):
        from modules import scripts

        dirpath = os.path.join(self.path, subdir)
        if not os.path.isdir(dirpath):
            return []

        res = []
        for filename in sorted(os.listdir(dirpath)):
            res.append(scripts.ScriptFile(dirpath, filename, os.path.join(dirpath, filename)))

        res = [x for x in res if os.path.splitext(x.path)[1].lower() == extension and os.path.isfile(x.path)]

        return res

    def check_updates(self):
        repo = git.Repo(self.path)
        for fetch in repo.remote().fetch("--dry-run"):
            if fetch.flags != fetch.HEAD_UPTODATE:
                self.can_update = True
                self.status = "behind"
                return

        self.can_update = False
        self.status = "latest"

    def pull(self):
        repo = git.Repo(self.path)
        repo.remotes.origin.pull()


def list_extensions():
    extensions.clear()

    if not os.path.isdir(extensions_dir):
        return

    for dirname in sorted(os.listdir(extensions_dir)):
        path = os.path.join(extensions_dir, dirname)
        if not os.path.isdir(path):
            continue

        extension = Extension(name=dirname, path=path, enabled=dirname not in shared.opts.disabled_extensions)
        extensions.append(extension)
+5 −0
Original line number Diff line number Diff line
@@ -17,6 +17,11 @@ paste_fields = {}
bind_list = []


def reset():
    paste_fields.clear()
    bind_list.clear()


def quote(text):
    if ',' not in str(text):
        return text
+4 −17
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ import modules.ui as ui
import gradio as gr

from modules.processing import StableDiffusionProcessing
from modules import shared, paths, script_callbacks
from modules import shared, paths, script_callbacks, extensions

AlwaysVisible = object()

@@ -107,17 +107,8 @@ def list_scripts(scriptdirname, extension):
        for filename in sorted(os.listdir(basedir)):
            scripts_list.append(ScriptFile(paths.script_path, filename, os.path.join(basedir, filename)))

    extdir = os.path.join(paths.script_path, "extensions")
    if os.path.exists(extdir):
        for dirname in sorted(os.listdir(extdir)):
            dirpath = os.path.join(extdir, dirname)
            scriptdirpath = os.path.join(dirpath, scriptdirname)

            if not os.path.isdir(scriptdirpath):
                continue

            for filename in sorted(os.listdir(scriptdirpath)):
                scripts_list.append(ScriptFile(dirpath, filename, os.path.join(scriptdirpath, filename)))
    for ext in extensions.active():
        scripts_list += ext.list_files(scriptdirname, extension)

    scripts_list = [x for x in scripts_list if os.path.splitext(x.path)[1].lower() == extension and os.path.isfile(x.path)]

@@ -127,11 +118,7 @@ def list_scripts(scriptdirname, extension):
def list_files_with_name(filename):
    res = []

    dirs = [paths.script_path]

    extdir = os.path.join(paths.script_path, "extensions")
    if os.path.exists(extdir):
        dirs += [os.path.join(extdir, d) for d in sorted(os.listdir(extdir))]
    dirs = [paths.script_path] + [ext.path for ext in extensions.active()]

    for dirpath in dirs:
        if not os.path.isdir(dirpath):
+9 −1
Original line number Diff line number Diff line
@@ -132,6 +132,7 @@ class State:
    current_image = None
    current_image_sampling_step = 0
    textinfo = None
    need_restart = False

    def skip(self):
        self.skipped = True
@@ -354,6 +355,12 @@ options_templates.update(options_section(('sampler-params', "Sampler parameters"
    'eta_noise_seed_delta': OptionInfo(0, "Eta noise seed delta", gr.Number, {"precision": 0}),
}))

options_templates.update(options_section((None, "Hidden options"), {
    "disabled_extensions": OptionInfo([], "Disable those extensions"),
}))

options_templates.update()


class Options:
    data = None
@@ -365,8 +372,9 @@ class Options:

    def __setattr__(self, key, value):
        if self.data is not None:
            if key in self.data:
            if key in self.data or key in self.data_labels:
                self.data[key] = value
                return

        return super(Options, self).__setattr__(key, value)

Loading