|
|
|
import json |
|
import typing |
|
|
|
|
|
import gradio as gr |
|
|
|
|
|
from modules.common.file_ensurer import FileEnsurer |
|
|
|
from handlers.json_handler import JsonHandler |
|
|
|
class GuiJsonUtil: |
|
|
|
current_translation_settings = dict() |
|
|
|
|
|
|
|
@staticmethod |
|
def fetch_translation_settings_key_values(header:str, key_name:str) -> str: |
|
|
|
""" |
|
|
|
Fetches the default values for the settings tab from the translation_settings.json file. |
|
|
|
Parameters: |
|
key_name (str) : Which value to fetch. |
|
|
|
Returns: |
|
(str) : The default value for the specified key. |
|
|
|
""" |
|
|
|
|
|
return GuiJsonUtil.current_translation_settings[header].get(key_name, "None") |
|
|
|
|
|
|
|
@staticmethod |
|
def update_translation_settings_with_new_values(gradio_translation_settings:gr.File, new_values:typing.List[typing.Tuple[str,str]]) -> None: |
|
|
|
""" |
|
|
|
Dumps the new values for the settings tab into the translation_settings.json file. |
|
|
|
Parameters: |
|
new_values (typing.List[typing.Tuple[str,str]]) : A list of tuples containing the key and value to be updated. |
|
|
|
""" |
|
|
|
|
|
old_rules = GuiJsonUtil.current_translation_settings |
|
new_rules = old_rules.copy() |
|
|
|
try: |
|
|
|
for header in new_rules.keys(): |
|
for key, value in new_values: |
|
if(key in new_rules[header]): |
|
new_rules[header][key] = JsonHandler.convert_to_correct_type(key, str(value)) |
|
|
|
JsonHandler.current_translation_settings = new_rules |
|
JsonHandler.validate_json() |
|
|
|
|
|
assert JsonHandler.current_translation_settings != FileEnsurer.INVALID_TRANSLATION_SETTINGS_PLACEHOLDER |
|
|
|
|
|
|
|
with open(FileEnsurer.config_translation_settings_path, "w") as file: |
|
json.dump(new_rules, file) |
|
|
|
with open(gradio_translation_settings.name, "w") as file: |
|
json.dump(new_rules, file) |
|
|
|
GuiJsonUtil.current_translation_settings = new_rules |
|
|
|
except Exception as e: |
|
|
|
|
|
with open(FileEnsurer.config_translation_settings_path, "w") as file: |
|
json.dump(old_rules, file) |
|
|
|
with open(gradio_translation_settings.name, "w") as file: |
|
json.dump(old_rules, file) |
|
|
|
GuiJsonUtil.current_translation_settings = old_rules |
|
|
|
|
|
raise e |