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