From 93c17b6026e226665e6b0e58401284c8c1ed0792 Mon Sep 17 00:00:00 2001 From: Snowykami Date: Mon, 2 Dec 2024 21:46:29 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=B7=BB=E5=8A=A0=E6=96=B0?= =?UTF-8?q?=E7=9A=84=E9=85=8D=E7=BD=AE=E5=8A=A0=E8=BD=BD=E5=99=A8=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BB=8EYAML=E3=80=81TOML=E3=80=81JSON?= =?UTF-8?q?=E5=92=8C=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=EF=BC=8C=E5=B9=B6=E4=BF=AE=E6=94=B9=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E4=B8=BB=E6=9C=BA=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/default.yml | 5 ++-- liteyuki/config.py | 45 ++++++++++++++++++++++++++++++++++++ liteyuki/session/__init__.py | 1 + 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/config/default.yml b/config/default.yml index 13a74c03..e278f19a 100644 --- a/config/default.yml +++ b/config/default.yml @@ -1,9 +1,10 @@ nonebot: - host: 127.0.0.1 + host: 0.0.0.0 port: 20216 command_start: ["", "/"] nickname: [ "liteyuki" ] default_language: zh driver: ~fastapi+~httpx+~websockets alconna_use_command_start: true - gotify_token: "empty token" \ No newline at end of file + gotify_token: "empty token" + diff --git a/liteyuki/config.py b/liteyuki/config.py index 39c09dea..718bad7e 100644 --- a/liteyuki/config.py +++ b/liteyuki/config.py @@ -132,3 +132,48 @@ def load_config_in_default(no_waring: bool = False) -> dict[str, Any]: ) ) return config + +# new config loader +class Loader: + def __init__(self): + self.config = {} + + def load_from_yaml(self, fp: str) -> "Loader": + """从yaml文件加载配置 + Args: + fp + """ + with open(fp, 'r') as file: + self.config.update(yaml.safe_load(file)) + return self + + def load_from_toml(self, fp: str) -> "Loader": + """从toml文件加载配置""" + with open(fp, 'r') as file: + self.config.update(toml.load(file)) + return self + + def load_from_json(self, fp: str) -> "Loader": + """从json文件加载配置""" + with open(fp, 'r') as file: + self.config.update(json.load(file)) + return self + + def load_from_env(self, prefix: str = "") -> "Loader": + """从环境变量加载配置""" + for key, value in os.environ.items(): + if key.startswith(prefix): + self.config[key[len(prefix):]] = value + return self + + def merge(self, loader: "Loader") -> "Loader": + """合并两个Loader键值对树""" + self.config.update(loader.config) + return self + + def get(self, key: str, default: Any = None) -> Any: + """获取配置值""" + return self.config.get(key, default) + + def __repr__(self) -> str: + return f"Loader(config={self.config})" \ No newline at end of file diff --git a/liteyuki/session/__init__.py b/liteyuki/session/__init__.py index a2004c05..1bd343e2 100644 --- a/liteyuki/session/__init__.py +++ b/liteyuki/session/__init__.py @@ -21,3 +21,4 @@ def message_handler_thread(i_chans: Iterable[Chan[Any]]): for msg in select(*i_chans): logger.debug(f"Recv from anybot {msg}") logger.info(f"Recv from anybot {msg}") + pass