From abe38388b6da62786ccd36fc346ad4d213f8fc79 Mon Sep 17 00:00:00 2001 From: Von Random Date: Wed, 23 Oct 2024 14:26:09 +0300 Subject: [PATCH] configs and defaults --- config.dist.yml => configs/pgbot.dist.yml | 0 sched.dist.yml => configs/sched.dist.yml | 0 tokens.dist.yml => configs/tokens.dist.yml | 0 pgbot | 2 +- pgbotlib/cron.py | 4 +++- pgbotlib/misc.py | 10 ++++++++++ pgbotlib/response.py | 4 +++- 7 files changed, 17 insertions(+), 3 deletions(-) rename config.dist.yml => configs/pgbot.dist.yml (100%) rename sched.dist.yml => configs/sched.dist.yml (100%) rename tokens.dist.yml => configs/tokens.dist.yml (100%) diff --git a/config.dist.yml b/configs/pgbot.dist.yml similarity index 100% rename from config.dist.yml rename to configs/pgbot.dist.yml diff --git a/sched.dist.yml b/configs/sched.dist.yml similarity index 100% rename from sched.dist.yml rename to configs/sched.dist.yml diff --git a/tokens.dist.yml b/configs/tokens.dist.yml similarity index 100% rename from tokens.dist.yml rename to configs/tokens.dist.yml diff --git a/pgbot b/pgbot index bf9ab03..752932a 100755 --- a/pgbot +++ b/pgbot @@ -15,7 +15,7 @@ import pgbotlib.response def init(args: list) -> tuple: - conf_path = args[0] if args else 'config.yml' + conf_path = args[0] if args else pgbotlib.misc.Paths.CONFIG try: with open(conf_path, 'r', encoding='utf-8') as data: config = yaml.safe_load(data.read()) diff --git a/pgbotlib/cron.py b/pgbotlib/cron.py index 1959293..529cfa0 100644 --- a/pgbotlib/cron.py +++ b/pgbotlib/cron.py @@ -5,6 +5,7 @@ import yaml import aiocron import telethon import pgbotlib.response +import pgbotlib.misc class Cron: @@ -12,7 +13,8 @@ class Cron: config: dict, client: telethon.TelegramClient, responder: pgbotlib.response.Responder) -> None: - with open(config['schedule'], 'r', encoding='utf-8') as data: + schedule_conf = config.get('schedule', pgbot.misc.Paths.SCHEDULE) + with open(schedule_conf, 'r', encoding='utf-8') as data: self.sched = yaml.safe_load(data.read()) self.responder = responder self.client = client diff --git a/pgbotlib/misc.py b/pgbotlib/misc.py index 47eda4a..836dfd3 100644 --- a/pgbotlib/misc.py +++ b/pgbotlib/misc.py @@ -1,7 +1,17 @@ +import enum +import os + import telethon import pgbotlib.dbstuff +class Paths(enum.Enum): + CONFDIR = '/etc/pgbot' + CONFIG = os.path.join(CONFDIR, 'pgbot.yml') + TOKENS = os.path.join(CONFDIR, 'tokens.yml') + SCHEDULE = os.path.join(CONFDIR, '/sched.yml') + + class NameGenerator: def __init__(self, config: dict, db: pgbotlib.dbstuff.DBConn) -> None: self.db = db diff --git a/pgbotlib/response.py b/pgbotlib/response.py index 9c02643..0a67689 100644 --- a/pgbotlib/response.py +++ b/pgbotlib/response.py @@ -30,7 +30,9 @@ class Responder: self.enabled = True self.apiregex = re.compile(r'^\{(\w+)\}(.+)?$') self.namegen = pgbotlib.misc.NameGenerator(config, db_connection) - self.tokens = get_tokens(config['response_tokens']) + self.tokens = get_tokens( + config.get('response_tokens', pgbotlib.misc.Paths.TOKENS) + ) self.chats = config['chats'] self.api = pgbotlib.api.ApiWrapper(self.tokens, db_connection) self.db_connection = db_connection