Skip to content

Commit

Permalink
feat: 兼容已生成的配置文件
Browse files Browse the repository at this point in the history
  • Loading branch information
ikun0014 authored and helloplhm-qwq committed Feb 22, 2024
1 parent 27e2f66 commit 1d640f8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
21 changes: 15 additions & 6 deletions common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import traceback
import sys
import sqlite3
import shutil
from . import variable
from .log import log
import threading
Expand All @@ -22,9 +23,17 @@
# 创建线程本地存储对象
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'):
Expand Down Expand Up @@ -322,7 +331,7 @@ class ConfigReadException(Exception):


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()
Expand Down Expand Up @@ -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('.')
Expand All @@ -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()
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion common/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

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
Expand Down
8 changes: 4 additions & 4 deletions webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,19 @@ def openFailedDialog():
# 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
Expand Down Expand Up @@ -1648,7 +1648,7 @@ def route_change(route):

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")
Expand Down

0 comments on commit 1d640f8

Please sign in to comment.