From e513bc8f7cacc21e65c646f70df50a9c79c6ebb5 Mon Sep 17 00:00:00 2001 From: RF-Tar-Railt Date: Fri, 7 Feb 2025 00:56:17 +0800 Subject: [PATCH] :beers: `entari new` edited .env --- arclet/entari/__main__.py | 17 +++++++++++++++++ arclet/entari/core.py | 19 ++++++++++--------- arclet/entari/logger.py | 22 +++++++++++++++------- 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/arclet/entari/__main__.py b/arclet/entari/__main__.py index 7e70649..066001f 100644 --- a/arclet/entari/__main__.py +++ b/arclet/entari/__main__.py @@ -114,6 +114,21 @@ """ +def check_env(file: Path): + env = Path.cwd() / ".env" + if env.exists(): + lines = env.read_text(encoding="utf-8").splitlines() + for i, line in enumerate(lines): + if line.startswith("ENTARI_CONFIG_FILE"): + lines[i] = f"ENTARI_CONFIG_FILE='{file.resolve().as_posix()}'" + with env.open("w", encoding="utf-8") as f: + f.write("\n".join(lines)) + break + else: + with env.open("w+", encoding="utf-8") as f: + f.write(f"\nENTARI_CONFIG_FILE='{file.resolve().as_posix()}'") + + def main(): res = alc() if (not res.matched or res.non_component) and not res.error_info: @@ -142,6 +157,7 @@ def main(): with _path.open("w", encoding="utf-8") as f: f.write(JSON_BASIC_TEMPLATE + PT) + check_env(_path) print(f"Config file created at {_path}") return if _path.suffix in (".yaml", ".yml"): @@ -154,6 +170,7 @@ def main(): with _path.open("w", encoding="utf-8") as f: f.write(YAML_BASIC_TEMPLATE + PT) + check_env(_path) print(f"Config file created at {_path}") return print(f"Unsupported file extension: {_path.suffix}") diff --git a/arclet/entari/core.py b/arclet/entari/core.py index a8127de..364ea61 100644 --- a/arclet/entari/core.py +++ b/arclet/entari/core.py @@ -138,15 +138,6 @@ def __init__( alconna_config.command_max_count = EntariConfig.instance.basic.get("cmd_count", 4096) log.set_level(log_level) log.core.debug(f"Log level set to {log_level}") - requires(*EntariConfig.instance.prelude_plugin) - for plug in EntariConfig.instance.prelude_plugin: - load_plugin(plug, prelude=True) - plugins = [ - plug for plug in EntariConfig.instance.plugin if not plug.startswith("~") and not plug.startswith("$") - ] - requires(*plugins) - for plug in plugins: - load_plugin(plug) self.ignore_self_message = ignore_self_message self.register(self.handle_event) self.lifecycle(self.account_hook) @@ -181,6 +172,16 @@ def ensure_manager(self, manager: Launart): self.manager = manager manager.add_component(plugin_service) + requires(*EntariConfig.instance.prelude_plugin) + for plug in EntariConfig.instance.prelude_plugin: + load_plugin(plug, prelude=True) + plugins = [ + plug for plug in EntariConfig.instance.plugin if not plug.startswith("~") and not plug.startswith("$") + ] + requires(*plugins) + for plug in plugins: + load_plugin(plug) + async def handle_event(self, account: Account, event: Event): with suppress(NotImplementedError): ev = event_parse(account, event) diff --git a/arclet/entari/logger.py b/arclet/entari/logger.py index ed2fb72..de0d76c 100644 --- a/arclet/entari/logger.py +++ b/arclet/entari/logger.py @@ -17,6 +17,7 @@ def __init__(self): self.fork("[core]") self.fork("[plugin]") self.fork("[message]") + self.log_level = "INFO" def fork(self, child_name: str): patched = logger.patch(lambda r: r.update(name=child_name)) @@ -43,19 +44,19 @@ def wrapper(self, name: str, color: str = "blue"): ) ) patched = patched.bind(name=f"plugins.{name}") - self.loggers[f"plugin.{name}"] = patched + self.loggers[name] = patched return patched - @staticmethod - def set_level(level: str | int): + def set_level(self, level: str | int): if isinstance(level, str): level = level.upper() logging.basicConfig( handlers=[LoguruHandler()], level=level, format="%(asctime)s | %(name)s[%(levelname)s]: %(message)s", + force=True, ) - logger.configure(extra={"entari_log_level": level}, patcher=_hidden_upsteam) + self.log_level = level class LoguruHandler(logging.Handler): # pragma: no cover @@ -79,14 +80,16 @@ def emit(self, record: logging.LogRecord): handlers=[LoguruHandler()], level="INFO", format="%(asctime)s | %(name)s[%(levelname)s]: %(message)s", + force=True, ) +log = LoggerManager() + def default_filter(record): if record["name"].startswith("launart"): return record["level"].no >= logger.level("SUCCESS").no - log_level = record["extra"].get("entari_log_level", "INFO") - levelno = logger.level(log_level).no if isinstance(log_level, str) else log_level + levelno = logger.level(log.log_level).no if isinstance(log.log_level, str) else log.log_level return record["level"].no >= levelno @@ -123,10 +126,15 @@ def _hidden_upsteam(record: Record): record["name"] = "satori" if record["name"].startswith("launart"): # type: ignore record["name"] = "launart" + if record["name"].startswith("uvicorn"): # type: ignore + record["name"] = "uvicorn" + if record["name"].startswith("starlette"): # type: ignore + record["name"] = "starlette" + if record["name"].startswith("graia.amnesia"): # type: ignore + record["name"] = "graia.amnesia" logger.configure(patcher=_hidden_upsteam) -log = LoggerManager() __all__ = ["log"]