NeteaseMusicLyricDownloader/modules/functions/settings/save_load_settings.py

70 lines
2.8 KiB
Python
Raw Permalink Normal View History

2022-04-02 22:50:52 +08:00
"""加载 or 保存设置文件"""
import json
import os
2022-07-17 18:59:58 +08:00
class Settings(object): # 设定一个基础的存储设置信息的 class ,并设置形参用于 json 导入设置
def __init__(self, l_p="./out/", l_f="%(name)s - %(artists)s", lang="en", a_s=True, s_l_t = True):
2022-04-02 22:50:52 +08:00
self.lyric_path = l_p
self.lyric_format = l_f
2022-04-02 22:50:52 +08:00
self.language = lang
self.auto_save = a_s
self.save_lyrics_time = s_l_t
2022-04-02 22:50:52 +08:00
def class2dict(aclass: Settings): # 让 json.dumps 将 class 转化为一个 dict ,用于保存
2022-04-02 22:50:52 +08:00
return {
"lyric_path": aclass.lyric_path,
"lyric_format": aclass.lyric_format,
2022-04-02 22:50:52 +08:00
"language": aclass.language,
"auto_save": aclass.auto_save,
"save_lyrics_time": aclass.save_lyrics_time
2022-04-02 22:50:52 +08:00
}
def dict2class(adict): # 让 json.load 将读取到的 dict 转化为我们所需要的 class
if len(adict) != 4: # 若检测到多余的设定将抛出异常
2022-04-02 22:50:52 +08:00
raise json.decoder.JSONDecodeError("Too many keys", "none", 0)
else:
return Settings(
l_p=adict["lyric_path"],
l_f=adict["lyric_format"],
lang=adict["language"],
a_s=adict["auto_save"],
s_l_t=adict["save_lyrics_time"]
)
2022-04-02 22:50:52 +08:00
def load_settings() -> Settings: # 加载 的函数
2022-04-02 22:50:52 +08:00
"""加载设置
调用即可无需参数
返回: 设置 class"""
if os.path.exists("settings.json"): # 判断目录下是否存在 settings.json ,若没有则创建,若有则读取
with open("settings.json", 'r', encoding="utf-8") as f:
try:
2022-07-17 18:59:58 +08:00
settings = json.load(f, object_hook=dict2class) # 尝试转换 json 为 dict
path = ""
2022-07-17 18:59:58 +08:00
if not os.path.exists(settings.lyric_path): # 检测输出文件夹,若文件夹不存在则在启动时创建
2024-10-04 11:01:33 +08:00
os.makedirs(settings.lyric_path, exist_ok=True)
2022-07-17 18:59:58 +08:00
return settings
except json.decoder.JSONDecodeError or KeyError: # 如果检测到文件无法读取,将会删除设置文件并重新创建
2022-04-02 22:50:52 +08:00
print("设置文件损坏,重新创建...")
os.remove("settings.json")
return load_settings()
else:
2022-07-17 18:59:58 +08:00
with open("settings.json", 'w', encoding="utf-8") as f: # 当 settings.json 不存在时新建一个 settings.json 并写入默认配置
2022-04-02 22:50:52 +08:00
f.write(json.dumps(Settings(), default=class2dict))
2024-10-04 11:01:33 +08:00
settings = Settings()
os.makedirs(settings.lyric_path, exist_ok=True)
return settings
2022-04-02 22:50:52 +08:00
def save_settings(settings): # 保存 的函数
"""保存设置
``settings`` 传入一个 设置 class 将其序列化为json
返回 done 即为完成理论上不存在报错所以暂时没有做其他处理"""
with open("settings.json", 'w', encoding="utf-8") as f:
f.write(json.dumps(settings, default=class2dict))
return "done"