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
1 changed files with 19 additions and 12 deletions

31
main.py
View File

@ -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, ")")