From 1d640f8dd46b21f4304d0ca792177193d2be5637 Mon Sep 17 00:00:00 2001 From: ikun0014 Date: Thu, 22 Feb 2024 20:37:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=85=BC=E5=AE=B9=E5=B7=B2=E7=94=9F?= =?UTF-8?q?=E6=88=90=E7=9A=84=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/config.py | 21 +++++++++++++++------ common/variable.py | 2 +- webui.py | 8 ++++---- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/common/config.py b/common/config.py index b1a417a..cb68647 100644 --- a/common/config.py +++ b/common/config.py @@ -13,6 +13,7 @@ import os import traceback import sys import sqlite3 +import shutil from . import variable from .log import log import threading @@ -22,9 +23,17 @@ logger = log('config_manager') # 创建线程本地存储对象 local_data = threading.local() +if not os.path.exists('config'): + os.mkdir('config') + if not os.path.exists('data'): os.mkdir('data') +if os.path.exists('config.json' and 'data.db' and 'cache.db'): + shutil.move('config.json','config') + shutil.move('cache.db','data') + shutil.move('data.db','data') + def get_data_connection(): # 检查线程本地存储对象是否存在连接对象,如果不存在则创建一个新的连接对象 if not hasattr(local_data, 'connection'): @@ -322,7 +331,7 @@ default = { def handle_default_config(): - with open("./data/config.json", "w", encoding="utf-8") as f: + with open("./config/config.json", "w", encoding="utf-8") as f: f.write(json.dumps(default, indent=2, ensure_ascii=False, escape_forward_slashes=False)) f.close() @@ -514,7 +523,7 @@ def push_to_list(key, obj): def write_config(key, value): config = None - with open('./data/config.json', 'r', encoding='utf-8') as f: + with open('./config/config.json', 'r', encoding='utf-8') as f: config = json.load(f) keys = key.split('.') @@ -526,7 +535,7 @@ def write_config(key, value): current[keys[-1]] = value variable.config = config - with open('./data/config.json', 'w', encoding='utf-8') as f: + with open('./config/config.json', 'w', encoding='utf-8') as f: json.dump(config, f, indent=2, ensure_ascii=False, escape_forward_slashes=False) f.close() @@ -623,18 +632,18 @@ def write_data(key, value): def initConfig(): try: - with open("./data/config.json", "r", encoding="utf-8") as f: + with open("./config/config.json", "r", encoding="utf-8") as f: try: variable.config = json.loads(f.read()) if (not isinstance(variable.config, dict)): logger.warning('配置文件并不是一个有效的字典,使用默认值') variable.config = default - with open("./data/config.json", "w", encoding="utf-8") as f: + with open("./config/config.json", "w", encoding="utf-8") as f: f.write(json.dumps(variable.config, indent=2, ensure_ascii=False, escape_forward_slashes=False)) f.close() except: - if os.path.getsize("./data/config.json") != 0: + if os.path.getsize("./config/config.json") != 0: logger.error("配置文件加载失败,请检查是否遵循JSON语法规范") sys.exit(1) else: diff --git a/common/variable.py b/common/variable.py index 4c02183..c4bb2fb 100644 --- a/common/variable.py +++ b/common/variable.py @@ -12,7 +12,7 @@ import ujson as _json def _read_config_file(): try: - with open("./data/config.json", "r", encoding = "utf-8") as f: + with open("./config/config.json", "r", encoding = "utf-8") as f: return _json.load(f) except: pass diff --git a/webui.py b/webui.py index 42d656a..549b2cb 100644 --- a/webui.py +++ b/webui.py @@ -93,19 +93,19 @@ def _Save(page: flet.Page, args): # print(args) try: - with open("./data/config.json", "r+", encoding="utf-8") as f: + with open("./config/config.json", "r+", encoding="utf-8") as f: config = json.loads(f.read()) config = update_nested_json(config, args) i = 1 while 1: try: - os.rename("./data/config.json", f"config{i}.bak") + os.rename("./config/config.json", f"config{i}.bak") break except: i += 1 - with open("./data/config.json", "w+", encoding="utf-8") as f: + with open("./config/config.json", "w+", encoding="utf-8") as f: f.write( json.dumps( config, indent=4, ensure_ascii=False, escape_forward_slashes=False @@ -1648,7 +1648,7 @@ def main(page: flet.Page): if __name__ == "__main__": try: - with open("./data/config.json", "r+", encoding="utf-8") as f: + with open("./config/config.json", "r+", encoding="utf-8") as f: config = json.loads(f.read()) except: print("无法找到config.json,请先运行main.py生成config.json")