implement switching logger level and mods activation by config

This commit is contained in:
falsycat 2023-02-12 10:30:44 +09:00
parent d61c1d0f1b
commit 07f78afdc4

31
main.py
View File

@ -44,16 +44,17 @@ class App:
def __init__(self): def __init__(self):
logging.basicConfig() logging.basicConfig()
self.logging = logging.getLogger("app") self.logging = logging.getLogger("app")
self.logging.setLevel(logging.DEBUG)
with open("config.json", "r") as f: with open("config.json", "r") as f:
self.CONFIG = json.load(f) self.CONFIG = json.load(f)
self.logging.setLevel(self.CONFIG["logging"])
self.logging.info("configuration loaded") self.logging.info("configuration loaded")
self.M = self.authenticate() self.M = self.authenticate()
self.logging.info("authentication succeeded") self.logging.info("authentication succeeded")
self.mods = {} self.mods = {}
self.install("elicense", mods.elicense)
self.alive = True self.alive = True
signal.signal(signal.SIGTERM, lambda a,b: self.exit()) signal.signal(signal.SIGTERM, lambda a,b: self.exit())
@ -90,13 +91,18 @@ class App:
return M return M
def install(self, name, mod): 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) self.logging.info("mod installed, "+name)
logger = logging.getLogger("mod."+name)
self.mods[name] = mod.init(self.CONFIG["mods"][name], logger)
def run(self): def run(self):
self.install("elicense", mods.elicense)
while self.alive: while self.alive:
try: try:
self.cycle() self.cycle()
@ -104,13 +110,12 @@ class App:
except Exception as e: except Exception as e:
self.logging.warning("cycle aborted:", e) self.logging.warning("cycle aborted:", e)
for mod in self.mods:
self.mods[mod].cycle()
for i in range(self.CONFIG["interval"]): for i in range(self.CONFIG["interval"]):
if not self.alive: break if not self.alive: break
time.sleep(1) time.sleep(1)
del self.mods
def cycle(self): def cycle(self):
items = self.M.notifications( items = self.M.notifications(
account_id = self.CONFIG["master"], account_id = self.CONFIG["master"],
@ -123,7 +128,8 @@ class App:
self.reply(st, "handling aborted: {}".format(e)) self.reply(st, "handling aborted: {}".format(e))
self.M.notifications_dismiss(id = item["id"]) self.M.notifications_dismiss(id = item["id"])
# TODO: call mod cycles for mod in self.mods:
self.mods[mod].cycle()
def exit(self): def exit(self):
self.alive = False self.alive = False
@ -131,10 +137,11 @@ class App:
def reply(self, st, msg): def reply(self, st, msg):
try: 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.logging.debug("reply:", msg)
#self.M.status_post(
# status = "@{} {}".format(st["account"]["acct"], msg),
# in_reply_to_id = st["id"])
except Exception as e: except Exception as e:
self.logging.error("reply failure:", msg, "(", e, ")") self.logging.error("reply failure:", msg, "(", e, ")")