From 07f78afdc4e34c11aedeb5b8e031e5b813024e0f Mon Sep 17 00:00:00 2001 From: falsycat Date: Sun, 12 Feb 2023 10:30:44 +0900 Subject: [PATCH] implement switching logger level and mods activation by config --- main.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/main.py b/main.py index b6d0ba1..3b07ffb 100644 --- a/main.py +++ b/main.py @@ -44,16 +44,17 @@ class App: def __init__(self): logging.basicConfig() self.logging = logging.getLogger("app") - self.logging.setLevel(logging.DEBUG) with open("config.json", "r") as f: self.CONFIG = json.load(f) + self.logging.setLevel(self.CONFIG["logging"]) self.logging.info("configuration loaded") self.M = self.authenticate() self.logging.info("authentication succeeded") self.mods = {} + self.install("elicense", mods.elicense) self.alive = True signal.signal(signal.SIGTERM, lambda a,b: self.exit()) @@ -90,13 +91,18 @@ class App: return M def install(self, name, mod): + config = self.CONFIG["mods"].get(name) + if config is None: + return + + logger = logging.getLogger("mods."+name) + if "logging" in config: + logger.setLevel(config["logging"]) + + self.mods[name] = mod.init(config, logger) self.logging.info("mod installed, "+name) - logger = logging.getLogger("mod."+name) - self.mods[name] = mod.init(self.CONFIG["mods"][name], logger) - def run(self): - self.install("elicense", mods.elicense) while self.alive: try: self.cycle() @@ -104,13 +110,12 @@ class App: except Exception as e: self.logging.warning("cycle aborted:", e) - for mod in self.mods: - self.mods[mod].cycle() - for i in range(self.CONFIG["interval"]): if not self.alive: break time.sleep(1) + del self.mods + def cycle(self): items = self.M.notifications( account_id = self.CONFIG["master"], @@ -123,7 +128,8 @@ class App: self.reply(st, "handling aborted: {}".format(e)) self.M.notifications_dismiss(id = item["id"]) - # TODO: call mod cycles + for mod in self.mods: + self.mods[mod].cycle() def exit(self): self.alive = False @@ -131,10 +137,11 @@ class App: def reply(self, st, msg): try: + self.M.status_post( + status = "@{} {}".format(st["account"]["acct"], msg), + in_reply_to_id = st["id"], + visibility = "direct") self.logging.debug("reply:", msg) - #self.M.status_post( - # status = "@{} {}".format(st["account"]["acct"], msg), - # in_reply_to_id = st["id"]) except Exception as e: self.logging.error("reply failure:", msg, "(", e, ")")