Skip to content

Commit

Permalink
chore: 把配置文件生成的地方换个位置,对Docker用户更友好
Browse files Browse the repository at this point in the history
ps: Docker只能挂载目录,所以更友好
  • Loading branch information
ikun0014 authored and helloplhm-qwq committed Feb 22, 2024
1 parent de3e8da commit f884960
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
def get_data_connection():
# 检查线程本地存储对象是否存在连接对象,如果不存在则创建一个新的连接对象
if not hasattr(local_data, 'connection'):
local_data.connection = sqlite3.connect('data.db')
local_data.connection = sqlite3.connect('./data/data.db')
return local_data.connection


Expand All @@ -37,7 +37,7 @@ def get_data_connection():
def get_cache_connection():
# 检查线程本地存储对象是否存在连接对象,如果不存在则创建一个新的连接对象
if not hasattr(local_cache, 'connection'):
local_cache.connection = sqlite3.connect('cache.db')
local_cache.connection = sqlite3.connect('./data/cache.db')
return local_cache.connection


Expand Down Expand Up @@ -320,7 +320,7 @@ class ConfigReadException(Exception):


def handle_default_config():
with open("./config.json", "w", encoding="utf-8") as f:
with open("./data/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 @@ -512,7 +512,7 @@ def push_to_list(key, obj):

def write_config(key, value):
config = None
with open('config.json', 'r', encoding='utf-8') as f:
with open('./data/config.json', 'r', encoding='utf-8') as f:
config = json.load(f)

keys = key.split('.')
Expand All @@ -524,7 +524,7 @@ def write_config(key, value):

current[keys[-1]] = value
variable.config = config
with open('config.json', 'w', encoding='utf-8') as f:
with open('./data/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 @@ -621,18 +621,18 @@ def write_data(key, value):

def initConfig():
try:
with open("./config.json", "r", encoding="utf-8") as f:
with open("./data/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("./config.json", "w", encoding="utf-8") as f:
with open("./data/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("./config.json") != 0:
if os.path.getsize("./data/config.json") != 0:
logger.error("配置文件加载失败,请检查是否遵循JSON语法规范")
sys.exit(1)
else:
Expand Down

0 comments on commit f884960

Please sign in to comment.