prevent saving settings with bogus values

This commit is contained in:
AUTOMATIC 2022-09-23 17:27:30 +03:00
parent 79e7c39298
commit 6fa20d51dc
3 changed files with 22 additions and 20 deletions

View File

@ -219,6 +219,7 @@ options_templates.update(options_section(('ui', "User interface"), {
class Options: class Options:
data = None data = None
data_labels = options_templates data_labels = options_templates
typemap = {int: float}
def __init__(self): def __init__(self):
self.data = {k: v.default for k, v in self.data_labels.items()} self.data = {k: v.default for k, v in self.data_labels.items()}
@ -244,25 +245,23 @@ class Options:
with open(filename, "w", encoding="utf8") as file: with open(filename, "w", encoding="utf8") as file:
json.dump(self.data, file) json.dump(self.data, file)
def same_type(self, x, y):
if x is None or y is None:
return True
type_x = self.typemap.get(type(x), type(x))
type_y = self.typemap.get(type(y), type(y))
return type_x == type_y
def load(self, filename): def load(self, filename):
with open(filename, "r", encoding="utf8") as file: with open(filename, "r", encoding="utf8") as file:
self.data = json.load(file) self.data = json.load(file)
typemap = {int: float}
def same_type(x, y):
if x is None or y is None:
return True
type_x = typemap.get(type(x), type(x))
type_y = typemap.get(type(y), type(y))
return type_x == type_y
bad_settings = 0 bad_settings = 0
for k, v in self.data.items(): for k, v in self.data.items():
info = self.data_labels.get(k, None) info = self.data_labels.get(k, None)
if info is not None and not same_type(info.default, v): if info is not None and not self.same_type(info.default, v):
print(f"Warning: bad setting value: {k}: {v} ({type(v).__name__}; expected {type(info.default).__name__})", file=sys.stderr) print(f"Warning: bad setting value: {k}: {v} ({type(v).__name__}; expected {type(info.default).__name__})", file=sys.stderr)
bad_settings += 1 bad_settings += 1

View File

@ -37,9 +37,7 @@ def txt2img(prompt: str, negative_prompt: str, prompt_style: str, prompt_style2:
print(f"\ntxt2img: {prompt}", file=shared.progress_print_out) print(f"\ntxt2img: {prompt}", file=shared.progress_print_out)
processed = modules.scripts.scripts_txt2img.run(p, *args) processed = modules.scripts.scripts_txt2img.run(p, *args)
if processed is not None: if processed is None:
pass
else:
processed = process_images(p) processed = process_images(p)
shared.total_tqdm.clear() shared.total_tqdm.clear()

View File

@ -831,7 +831,11 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo):
components = [] components = []
def run_settings(*args): def run_settings(*args):
up = [] changed = 0
for key, value, comp in zip(opts.data_labels.keys(), args, components):
if 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__}"
for key, value, comp in zip(opts.data_labels.keys(), args, components): for key, value, comp in zip(opts.data_labels.keys(), args, components):
comp_args = opts.data_labels[key].component_args comp_args = opts.data_labels[key].component_args
@ -841,14 +845,15 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo):
oldval = opts.data.get(key, None) oldval = opts.data.get(key, None)
opts.data[key] = value opts.data[key] = value
if oldval != value and opts.data_labels[key].onchange is not None: if oldval != value:
opts.data_labels[key].onchange() if opts.data_labels[key].onchange is not None:
opts.data_labels[key].onchange()
up.append(comp.update(value=value)) changed += 1
opts.save(shared.config_filename) opts.save(shared.config_filename)
return 'Settings applied.' return f'{changed} settings changed.'
with gr.Blocks(analytics_enabled=False) as settings_interface: with gr.Blocks(analytics_enabled=False) as settings_interface:
settings_submit = gr.Button(value="Apply settings", variant='primary') settings_submit = gr.Button(value="Apply settings", variant='primary')