From 36ecff71aea2c9c9f296ae1e0b5da00a3aea26d3 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Sun, 20 Aug 2023 15:36:39 +0900 Subject: [PATCH 1/4] catch error when loading config_states and save config_states with indent --- modules/config_states.py | 11 +++++++---- modules/ui_extensions.py | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/modules/config_states.py b/modules/config_states.py index 6f1ab53fc59..62d40106a69 100644 --- a/modules/config_states.py +++ b/modules/config_states.py @@ -28,10 +28,13 @@ def list_config_states(): for filename in os.listdir(config_states_dir): if filename.endswith(".json"): path = os.path.join(config_states_dir, filename) - with open(path, "r", encoding="utf-8") as f: - j = json.load(f) - j["filepath"] = path - config_states.append(j) + try: + with open(path, "r", encoding="utf-8") as f: + j = json.load(f) + j["filepath"] = path + config_states.append(j) + except Exception as e: + print(f'[ERROR]{path}, {e}') config_states = sorted(config_states, key=lambda cs: cs["created_at"], reverse=True) diff --git a/modules/ui_extensions.py b/modules/ui_extensions.py index 15a8b0bf4e3..c5a04d6b2f6 100644 --- a/modules/ui_extensions.py +++ b/modules/ui_extensions.py @@ -65,7 +65,7 @@ def save_config_state(name): filename = os.path.join(config_states_dir, f"{timestamp}_{name}.json") print(f"Saving backup of webui/extension state to {filename}.") with open(filename, "w", encoding="utf-8") as f: - json.dump(current_config_state, f) + json.dump(current_config_state, f, indent=4) config_states.list_config_states() new_value = next(iter(config_states.all_config_states.keys()), "Current") new_choices = ["Current"] + list(config_states.all_config_states.keys()) From e0e64bcdf606b0c5f0e543b53f7e63a94ec325de Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Sun, 20 Aug 2023 16:13:26 +0900 Subject: [PATCH 2/4] assert key created_at exist in config_states --- modules/config_states.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/config_states.py b/modules/config_states.py index 62d40106a69..331c7b90ec0 100644 --- a/modules/config_states.py +++ b/modules/config_states.py @@ -31,10 +31,11 @@ def list_config_states(): try: with open(path, "r", encoding="utf-8") as f: j = json.load(f) + assert "created_at" in j, '"created_at" does not exist' j["filepath"] = path config_states.append(j) except Exception as e: - print(f'[ERROR]{path}, {e}') + print(f'[ERROR]: Config states {path}, {e}') config_states = sorted(config_states, key=lambda cs: cs["created_at"], reverse=True) From 7ca20adc6dc7797502170648d9737c7d01c04adc Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Sun, 20 Aug 2023 18:18:45 +0900 Subject: [PATCH 3/4] no need to use OrderedDict --- modules/config_states.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/modules/config_states.py b/modules/config_states.py index 331c7b90ec0..b766aef11d8 100644 --- a/modules/config_states.py +++ b/modules/config_states.py @@ -8,14 +8,12 @@ import tqdm from datetime import datetime -from collections import OrderedDict import git from modules import shared, extensions, errors from modules.paths_internal import script_path, config_states_dir - -all_config_states = OrderedDict() +all_config_states = {} def list_config_states(): From 2c10fda39900adcf9aca1f69c7ef023a2d85e2a9 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Sun, 20 Aug 2023 17:34:20 +0900 Subject: [PATCH 4/4] make it obvious that a config_status is corrupted also format HTML removing unnecessary text blocks --- modules/ui_extensions.py | 224 ++++++++++++++++++++------------------- 1 file changed, 117 insertions(+), 107 deletions(-) diff --git a/modules/ui_extensions.py b/modules/ui_extensions.py index c5a04d6b2f6..e01382676a2 100644 --- a/modules/ui_extensions.py +++ b/modules/ui_extensions.py @@ -200,119 +200,129 @@ def update_config_states_table(state_name): created_date = time.asctime(time.gmtime(config_state["created_at"])) filepath = config_state.get("filepath", "") - code = f"""""" - - webui_remote = config_state["webui"]["remote"] or "" - webui_branch = config_state["webui"]["branch"] - webui_commit_hash = config_state["webui"]["commit_hash"] or "" - webui_commit_date = config_state["webui"]["commit_date"] - if webui_commit_date: - webui_commit_date = time.asctime(time.gmtime(webui_commit_date)) - else: - webui_commit_date = "" - - remote = f"""{html.escape(webui_remote or '')}""" - commit_link = make_commit_link(webui_commit_hash, webui_remote) - date_link = make_commit_link(webui_commit_hash, webui_remote, webui_commit_date) - - current_webui = config_states.get_webui_config() - - style_remote = "" - style_branch = "" - style_commit = "" - if current_webui["remote"] != webui_remote: - style_remote = STYLE_PRIMARY - if current_webui["branch"] != webui_branch: - style_branch = STYLE_PRIMARY - if current_webui["commit_hash"] != webui_commit_hash: - style_commit = STYLE_PRIMARY - - code += f"""

Config Backup: {config_name}

-
Filepath: {filepath}
-
Created at: {created_date}
""" - - code += f"""

WebUI State

- - - - - - - - - - - - - - - - - -
URLBranchCommitDate
{remote}{webui_branch}{commit_link}{date_link}
- """ - - code += """

Extension State

- - - - - - - - - - - - """ - - ext_map = {ext.name: ext for ext in extensions.extensions} - - for ext_name, ext_conf in config_state["extensions"].items(): - ext_remote = ext_conf["remote"] or "" - ext_branch = ext_conf["branch"] or "" - ext_enabled = ext_conf["enabled"] - ext_commit_hash = ext_conf["commit_hash"] or "" - ext_commit_date = ext_conf["commit_date"] - if ext_commit_date: - ext_commit_date = time.asctime(time.gmtime(ext_commit_date)) + try: + webui_remote = config_state["webui"]["remote"] or "" + webui_branch = config_state["webui"]["branch"] + webui_commit_hash = config_state["webui"]["commit_hash"] or "" + webui_commit_date = config_state["webui"]["commit_date"] + if webui_commit_date: + webui_commit_date = time.asctime(time.gmtime(webui_commit_date)) else: - ext_commit_date = "" + webui_commit_date = "" + + remote = f"""{html.escape(webui_remote or '')}""" + commit_link = make_commit_link(webui_commit_hash, webui_remote) + date_link = make_commit_link(webui_commit_hash, webui_remote, webui_commit_date) - remote = f"""{html.escape(ext_remote or '')}""" - commit_link = make_commit_link(ext_commit_hash, ext_remote) - date_link = make_commit_link(ext_commit_hash, ext_remote, ext_commit_date) + current_webui = config_states.get_webui_config() - style_enabled = "" style_remote = "" style_branch = "" style_commit = "" - if ext_name in ext_map: - current_ext = ext_map[ext_name] - current_ext.read_info_from_repo() - if current_ext.enabled != ext_enabled: - style_enabled = STYLE_PRIMARY - if current_ext.remote != ext_remote: - style_remote = STYLE_PRIMARY - if current_ext.branch != ext_branch: - style_branch = STYLE_PRIMARY - if current_ext.commit_hash != ext_commit_hash: - style_commit = STYLE_PRIMARY - - code += f""" - - - - - - - - """ - - code += """ - -
ExtensionURLBranchCommitDate
{html.escape(ext_name)}{remote}{ext_branch}{commit_link}{date_link}
- """ + if current_webui["remote"] != webui_remote: + style_remote = STYLE_PRIMARY + if current_webui["branch"] != webui_branch: + style_branch = STYLE_PRIMARY + if current_webui["commit_hash"] != webui_commit_hash: + style_commit = STYLE_PRIMARY + + code = f""" +

Config Backup: {config_name}

+
Filepath: {filepath}
+
Created at: {created_date}
+

WebUI State

+ + + + + + + + + + + + + + + + + +
URLBranchCommitDate
+ {remote} + + {webui_branch} + + {commit_link} + + {date_link} +
+

Extension State

+ + + + + + + + + + + +""" + + ext_map = {ext.name: ext for ext in extensions.extensions} + + for ext_name, ext_conf in config_state["extensions"].items(): + ext_remote = ext_conf["remote"] or "" + ext_branch = ext_conf["branch"] or "" + ext_enabled = ext_conf["enabled"] + ext_commit_hash = ext_conf["commit_hash"] or "" + ext_commit_date = ext_conf["commit_date"] + if ext_commit_date: + ext_commit_date = time.asctime(time.gmtime(ext_commit_date)) + else: + ext_commit_date = "" + + remote = f"""{html.escape(ext_remote or '')}""" + commit_link = make_commit_link(ext_commit_hash, ext_remote) + date_link = make_commit_link(ext_commit_hash, ext_remote, ext_commit_date) + + style_enabled = "" + style_remote = "" + style_branch = "" + style_commit = "" + if ext_name in ext_map: + current_ext = ext_map[ext_name] + current_ext.read_info_from_repo() + if current_ext.enabled != ext_enabled: + style_enabled = STYLE_PRIMARY + if current_ext.remote != ext_remote: + style_remote = STYLE_PRIMARY + if current_ext.branch != ext_branch: + style_branch = STYLE_PRIMARY + if current_ext.commit_hash != ext_commit_hash: + style_commit = STYLE_PRIMARY + + code += f""" + + + + + + +""" + + code += """ +
ExtensionURLBranchCommitDate
{html.escape(ext_name)}{remote}{ext_branch}{commit_link}{date_link}
""" + + except Exception as e: + print(f"[ERROR]: Config states {filepath}, {e}") + code = f""" +

Config Backup: {config_name}

+
Filepath: {filepath}
+
Created at: {created_date}
+

This file is corrupted

""" return code