From 2dd4419bb126dbba46828b2a195e1995d757a9d3 Mon Sep 17 00:00:00 2001 From: misyaguziya <53165965+misyaguziya@users.noreply.github.com> Date: Sun, 27 Oct 2024 15:12:04 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=8D=EF=B8=8F[Update]=20Config=20:=20co?= =?UTF-8?q?nfig.json=E3=81=AE=E4=BF=9D=E5=AD=98=E3=82=BF=E3=82=A4=E3=83=9F?= =?UTF-8?q?=E3=83=B3=E3=82=B0=E3=82=92fire=20and=20forget=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-python/config.py | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src-python/config.py b/src-python/config.py index 3f1e24a3..8d719516 100644 --- a/src-python/config.py +++ b/src-python/config.py @@ -3,7 +3,7 @@ import inspect from os import path as os_path, makedirs as os_makedirs from json import load as json_load from json import dump as json_dump - +from asyncio import new_event_loop from device_manager import device_manager from models.transcription.transcription_languages import transcription_lang from utils import generatePercentageStringsList, isUniqueStrings @@ -15,12 +15,14 @@ def json_serializable(var_name): return func return decorator -def saveJson(path, key, value): - with open(path, "r", encoding="utf-8") as fp: - json_data = json_load(fp) - json_data[key] = value +config_data = {} +def saveJsonData(path, key, value): + config_data[key] = value with open(path, "w", encoding="utf-8") as fp: - json_dump(json_data, fp, indent=4, ensure_ascii=False) + json_dump(config_data, fp, indent=4, ensure_ascii=False) + +def saveJson(path, key, value): + new_event_loop().run_in_executor(None, saveJsonData, path, key, value) class Config: _instance = None @@ -1163,21 +1165,16 @@ class Config: def load_config(self): if os_path.isfile(self.PATH_CONFIG) is not False: with open(self.PATH_CONFIG, 'r', encoding="utf-8") as fp: - config = json_load(fp) + if fp.readable() and fp.seek(0, 2) > 0: + fp.seek(0) + config_data = json_load(fp) - old_message_format = None - for key in config.keys(): - if key == "MESSAGE_FORMAT": - old_message_format = config[key] - setattr(self, key, config[key]) - - if old_message_format is not None: - setattr(self, "SEND_MESSAGE_FORMAT_WITH_T", old_message_format) + for key, value in config_data.items(): + setattr(self, key, value) with open(self.PATH_CONFIG, 'w', encoding="utf-8") as fp: - config = {} for var_name, var_func in json_serializable_vars.items(): - config[var_name] = var_func(self) - json_dump(config, fp, indent=4, ensure_ascii=False) + config_data[var_name] = var_func(self) + json_dump(config_data, fp, indent=4, ensure_ascii=False) config = Config() \ No newline at end of file