summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyomotoi <[email protected]>2022-10-13 16:09:38 +0800
committerKyomotoi <[email protected]>2022-10-13 16:09:38 +0800
commite4ffbcfb34f56d4ba3330f3e85912a8af30a322c (patch)
treeb09daaea8f28c9e444485db410291ac6fad409ab
parentfa2cdee3c8d4bb103e225416d3322d990a0272fb (diff)
downloadATRI-e4ffbcfb34f56d4ba3330f3e85912a8af30a322c.tar.gz
ATRI-e4ffbcfb34f56d4ba3330f3e85912a8af30a322c.tar.bz2
ATRI-e4ffbcfb34f56d4ba3330f3e85912a8af30a322c.zip
♻️ 重构 config 加载项
-rw-r--r--ATRI/configs/__init__.py1
-rw-r--r--ATRI/configs/config.py53
-rw-r--r--ATRI/configs/default_config.yml32
-rw-r--r--ATRI/configs/models.py60
4 files changed, 146 insertions, 0 deletions
diff --git a/ATRI/configs/__init__.py b/ATRI/configs/__init__.py
new file mode 100644
index 0000000..3558f42
--- /dev/null
+++ b/ATRI/configs/__init__.py
@@ -0,0 +1 @@
+from .config import Config \ No newline at end of file
diff --git a/ATRI/configs/config.py b/ATRI/configs/config.py
new file mode 100644
index 0000000..57a94e9
--- /dev/null
+++ b/ATRI/configs/config.py
@@ -0,0 +1,53 @@
+import yaml
+from pathlib import Path
+
+from ATRI.log import log
+
+from .models import (
+ BotConfig,
+ ConfigModel,
+ WithGoCQHTTP,
+ RuntimeConfig
+)
+
+
+_DEFAULT_CONFIG_PATH = Path(".") / "ATRI" / "configs" / "default_config.yml"
+
+
+class Config:
+ def __init__(self, config_path: Path):
+ if not config_path.is_file():
+ try:
+ log.warning("未检测到 config.yml, 已于当前目录生成, 请参照文档进行填写并重新启动")
+ log.warning("文档地址: https://atri.imki.moe/install/configuration-bot/")
+ with open(config_path, "w", encoding="utf-8") as w:
+ w.write(_DEFAULT_CONFIG_PATH.read_text("utf-8"))
+ exit(0)
+ except Exception as err:
+ log.error(f"写入文件 config.yml 失败, 请确认是否给足权限: {err}")
+ exit(-1)
+
+ self.config_path = config_path
+ with open(self.config_path, "r", encoding="utf-8") as f:
+ self.config = yaml.safe_load(f)
+
+ def parse(self) -> ConfigModel:
+ return ConfigModel.parse_obj(self.config)
+
+ def get_runtime_conf(self) -> dict:
+ bot_conf = BotConfig.parse_obj(self.config["BotConfig"])
+ gocq_conf = WithGoCQHTTP.parse_obj(self.config["WithGoCQHTTP"])
+
+ return RuntimeConfig(
+ host=bot_conf.host,
+ port=bot_conf.port,
+ debug=bot_conf.debug,
+ superusers=bot_conf.superusers,
+ nickname=bot_conf.nickname,
+ command_start=bot_conf.command_start,
+ command_sep=bot_conf.command_sep,
+ session_expire_timeout=bot_conf.session_expire_timeout,
+ gocq_accoutns=gocq_conf.accounts,
+ gocq_download_domain=gocq_conf.download_version,
+ gocq_version=gocq_conf.download_version
+ ).dict()
diff --git a/ATRI/configs/default_config.yml b/ATRI/configs/default_config.yml
new file mode 100644
index 0000000..fbc6720
--- /dev/null
+++ b/ATRI/configs/default_config.yml
@@ -0,0 +1,32 @@
+# 设置参考文档: https://atri.imki.moe/install/configuration-bot/
+ConfigVersion: "1.0.0"
+
+BotConfig:
+ host: "127.0.0.1"
+ port: 20000
+ debug: false
+ superusers: {"1234567890"}
+ nickname: {"ATRI", "Atri", "atri", "亚托莉", "アトリ"}
+ command_start: {"", "/"}
+ command_sep: {"."}
+ session_expire_timeout: 60
+ access_token: ""
+ proxy: ""
+ request_timeout: 5
+
+WithGoCQHTTP:
+ enabled: false
+ accounts:
+ - uin: 1234567890
+ password: ""
+ protocol: 5
+
+ download_domain: "github.com"
+ download_version: "v1.0.0-rc1"
+
+SauceNAO:
+ key: ""
+
+Setu:
+ reverse_proxy: true
+ reverse_proxy_domain: "i.pixiv.re"
diff --git a/ATRI/configs/models.py b/ATRI/configs/models.py
new file mode 100644
index 0000000..8043f8a
--- /dev/null
+++ b/ATRI/configs/models.py
@@ -0,0 +1,60 @@
+from typing import List
+from pydantic import BaseModel
+
+
+class BotConfig(BaseModel):
+ host: str
+ port: int
+ debug: bool
+ superusers: set
+ nickname: set
+ command_start: set
+ command_sep: set
+ session_expire_timeout: int
+ access_token: str
+ proxy: str
+ request_timeout: int
+
+
+class GoCQHTTPAccountList(BaseModel):
+ uin: int
+ password: str
+ protocol: int
+
+
+class WithGoCQHTTP(BaseModel):
+ enabled: bool
+ accounts: List[GoCQHTTPAccountList]
+ download_domain: str
+ download_version: str
+
+
+class SauceNAO(BaseModel):
+ key: str
+
+
+class Setu(BaseModel):
+ reverse_proxy: bool
+ reverse_proxy_domain: str
+
+
+class ConfigModel(BaseModel):
+ ConfigVersion: str
+ BotConfig: BotConfig
+ WithGoCQHTTP: WithGoCQHTTP
+ SauceNAO: SauceNAO
+ Setu: Setu
+
+
+class RuntimeConfig(BaseModel):
+ host: str
+ port: int
+ debug: bool
+ superusers: set
+ nickname: set
+ command_start: set
+ command_sep: set
+ session_expire_timeout: int
+ gocq_accoutns: list
+ gocq_download_domain: str
+ gocq_version: str