- 新增自定义域名

- 增加连接报错域名提示 #195
- 修复 Dev-debug 开启的错误 #192
- 重做重命名逻辑,支持合集重命名以及文件夹内重命名,支持字幕重命名。
This commit is contained in:
EstrellaXD
2023-04-23 21:35:57 +08:00
parent 7622010803
commit ee8f7dd1a2
9 changed files with 276 additions and 98 deletions

View File

@@ -2,9 +2,8 @@ import json
import os
import logging
from dataclasses import dataclass
from .const import DEFAULT_SETTINGS, ENV_TO_ATTR
from module.conf.const import ENV_TO_ATTR
from module.models import Config
logger = logging.getLogger(__name__)
@@ -15,70 +14,48 @@ except ImportError:
VERSION = "DEV_VERSION"
class ConfLoad(dict):
def __getattr__(self, item):
return self.get(item)
def __setattr__(self, key, value):
self[key] = value
def save_config_to_file(config: Config, path: str):
with open(path, "w", encoding="utf-8") as f:
json.dump(config, f, indent=4)
logger.info(f"Config saved")
@dataclass
class Settings:
program: ConfLoad
downloader: ConfLoad
rss_parser: ConfLoad
bangumi_manage: ConfLoad
debug: ConfLoad
proxy: ConfLoad
notification: ConfLoad
def load_config_from_file(path: str) -> Config:
with open(path, "r", encoding="utf-8") as f:
config = json.load(f)
return config
def __init__(self, path: str | None):
self.load(path)
def load(self, path: str | None):
if path is None:
conf = DEFAULT_SETTINGS
elif os.path.isfile(path):
with open(path, "r") as f:
# Use utf-8 to avoid encoding error
conf = json.load(f, encoding="utf-8")
def _val_from_env(env: str, attr: tuple):
if isinstance(attr, tuple):
if attr[1] == "bool":
return os.environ[env].lower() == "true"
elif attr[1] == "int":
return int(os.environ[env])
elif attr[1] == "float":
return float(os.environ[env])
else:
conf = self._create_config()
for key, section in conf.items():
setattr(self, key, ConfLoad(section))
return os.environ[env]
else:
return os.environ[env]
@staticmethod
def _val_from_env(env, attr):
val = os.environ[env]
if isinstance(attr, tuple):
conv_func = attr[1]
val = conv_func(val)
return val
def _create_config(self):
_settings = DEFAULT_SETTINGS
for key, section in ENV_TO_ATTR.items():
for env, attr in section.items():
if env in os.environ:
attr_name = attr[0] if isinstance(attr, tuple) else attr
_settings[key][attr_name] = self._val_from_env(env, attr)
with open(CONFIG_PATH, "w") as f:
# Save utf-8 to avoid encoding error
json.dump(_settings, f, indent=4, ensure_ascii=False)
logger.warning(f"Config file had been transferred from environment variables to {CONFIG_PATH}, some settings may be lost.")
logger.warning("Please check the config file and restart the program.")
logger.warning("Please check github wiki (https://github.com/EstrellaXD/Auto_Bangumi/#/wiki) for more information.")
return _settings
def env_to_config() -> Config:
_settings = Config()
for key, section in ENV_TO_ATTR.items():
for env, attr in section.items():
if env in os.environ:
attr_name = attr[0] if isinstance(attr, tuple) else attr
setattr(_settings, attr_name, _val_from_env(env, attr))
return _settings
if os.path.isdir("config") and VERSION == "DEV_VERSION":
CONFIG_PATH = "config/config_dev.json"
settings = load_config_from_file(CONFIG_PATH)
print(dict(settings))
elif os.path.isdir("config") and VERSION != "DEV_VERSION":
CONFIG_PATH = "config/config.json"
else:
CONFIG_PATH = None
settings = Settings(CONFIG_PATH)