Skip to content

Commit

Permalink
🍻 entari new edited .env
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Feb 6, 2025
1 parent 041ed55 commit e513bc8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
17 changes: 17 additions & 0 deletions arclet/entari/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"):
Expand All @@ -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}")
Expand Down
19 changes: 10 additions & 9 deletions arclet/entari/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <y><c>{log_level}</c></y>")
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)
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 15 additions & 7 deletions arclet/entari/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand All @@ -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


Expand Down Expand Up @@ -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"]

0 comments on commit e513bc8

Please sign in to comment.