From f2b69709eaff88fc3a2bd49585556ec0883bf5ea Mon Sep 17 00:00:00 2001 From: AUTOMATIC <16777216c@gmail.com> Date: Fri, 4 Nov 2022 09:42:25 +0300 Subject: [PATCH] move option access checking to options class out of various places scattered through code --- modules/processing.py | 4 ++-- modules/shared.py | 11 +++++++++++ modules/ui.py | 20 +++++--------------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/modules/processing.py b/modules/processing.py index 2168208c4..a46e592da 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -418,13 +418,13 @@ def process_images(p: StableDiffusionProcessing) -> Processed: try: for k, v in p.override_settings.items(): - opts.data[k] = v # we don't call onchange for simplicity which makes changing model, hypernet impossible + setattr(opts, k, v) # we don't call onchange for simplicity which makes changing model, hypernet impossible res = process_images_inner(p) finally: for k, v in stored_opts.items(): - opts.data[k] = v + setattr(opts, k, v) return res diff --git a/modules/shared.py b/modules/shared.py index d8e99f857..024c771ae 100644 --- a/modules/shared.py +++ b/modules/shared.py @@ -396,6 +396,15 @@ class Options: def __setattr__(self, key, value): if self.data is not None: if key in self.data or key in self.data_labels: + assert not cmd_opts.freeze_settings, "changing settings is disabled" + + comp_args = opts.data_labels[key].component_args + if isinstance(comp_args, dict) and comp_args.get('visible', True) is False: + raise RuntimeError(f"not possible to set {key} because it is restricted") + + if cmd_opts.hide_ui_dir_config and key in restricted_opts: + raise RuntimeError(f"not possible to set {key} because it is restricted") + self.data[key] = value return @@ -412,6 +421,8 @@ class Options: return super(Options, self).__getattribute__(item) def save(self, filename): + assert not cmd_opts.freeze_settings, "saving settings is disabled" + with open(filename, "w", encoding="utf8") as file: json.dump(self.data, file, indent=4) diff --git a/modules/ui.py b/modules/ui.py index b2b1c854e..633b56efa 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -1438,8 +1438,6 @@ def create_ui(wrap_gradio_gpu_call): def run_settings(*args): changed = 0 - assert not shared.cmd_opts.freeze_settings, "changing settings is disabled" - for key, value, comp in zip(opts.data_labels.keys(), args, components): if comp != dummy_component and not opts.same_type(value, opts.data_labels[key].default): return f"Bad value for setting {key}: {value}; expecting {type(opts.data_labels[key].default).__name__}", opts.dumpjson() @@ -1448,15 +1446,9 @@ def create_ui(wrap_gradio_gpu_call): if comp == dummy_component: continue - comp_args = opts.data_labels[key].component_args - if comp_args and isinstance(comp_args, dict) and comp_args.get('visible') is False: - continue - - if cmd_opts.hide_ui_dir_config and key in restricted_opts: - continue - oldval = opts.data.get(key, None) - opts.data[key] = value + + setattr(opts, key, value) if oldval != value: if opts.data_labels[key].onchange is not None: @@ -1469,17 +1461,15 @@ def create_ui(wrap_gradio_gpu_call): return f'{changed} settings changed.', opts.dumpjson() def run_settings_single(value, key): - assert not shared.cmd_opts.freeze_settings, "changing settings is disabled" - if not opts.same_type(value, opts.data_labels[key].default): return gr.update(visible=True), opts.dumpjson() oldval = opts.data.get(key, None) - if cmd_opts.hide_ui_dir_config and key in restricted_opts: + try: + setattr(opts, key, value) + except Exception: return gr.update(value=oldval), opts.dumpjson() - opts.data[key] = value - if oldval != value: if opts.data_labels[key].onchange is not None: opts.data_labels[key].onchange()