From 99e8c84ea6905956b91d4696ee7aa31e9746ec70 Mon Sep 17 00:00:00 2001 From: lulzette Date: Mon, 20 May 2024 21:38:39 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BA=D0=B0=D0=BA=D0=B0=D1=8F-=D1=82=D0=BE=20?= =?UTF-8?q?=D0=BC=D0=B0=D0=B3=D0=B8=D1=8F=20=D1=81=20=D0=B7=D0=B0=D0=B3?= =?UTF-8?q?=D1=80=D1=83=D0=B7=D0=BA=D0=BE=D0=B9=20=D0=BA=D0=BE=D0=BD=D1=84?= =?UTF-8?q?=D0=B8=D0=B3=D1=83=D1=80=D0=B0=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/parse_config.py | 48 +++++++++++++++++++++++---------------------- src/twitch-tgbot | 4 ++-- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/parse_config.py b/src/parse_config.py index b376bcd..60d61a8 100644 --- a/src/parse_config.py +++ b/src/parse_config.py @@ -1,6 +1,7 @@ from dataclasses import dataclass, asdict import configparser + @dataclass(slots=True, frozen=True) class Config: streamer: str @@ -11,26 +12,27 @@ class Config: check_period: int cleanup_msg: bool -def load_config(config_path: str = "twitch-tgbot.cfg") -> Config: - """ - Эта функция либо читает существующий конфиг, либо создает новый. - Возвращает объект конфига (configparser.ConfigParser()) - """ - config_file = configparser.ConfigParser() - # Читаем конфиг, если пустой - заполняем - if not config_file.read(config_path) or not config_file.has_section('twitch'): - config_file.add_section('twitch') - twitch = config_file['twitch'] - config = Config( - streamer=twitch.get('streamer',fallback='the_viox'), - app_id=twitch.get('app_id',fallback=''), - app_secret=twitch.get('app_secret',fallback=''), - target_tg_chat=twitch.get('target_tg_chat',fallback=''), - tg_token=twitch.get('tg_token',fallback=''), - check_period=twitch.getint('check_period',fallback=10), - cleanup_msg=twitch.getboolean('cleanup_msg',fallback=True) - ) - with open(config_path, 'w') as cfg_file: - config_file.read_dict({'twitch': asdict(config)}) - config_file.write(cfg_file) - return config \ No newline at end of file + @classmethod + def load(cls, config_path: str = "twitch-tgbot.cfg") -> 'Config': + """ + Эта функция либо читает существующий конфиг, либо создает новый. + Возвращает объект конфига (configparser.ConfigParser()) + """ + config_file = configparser.ConfigParser() + # Читаем конфиг, если пустой - заполняем + if not config_file.read(config_path) or not config_file.has_section('twitch'): + config_file.add_section('twitch') + twitch = config_file['twitch'] + config = Config( + streamer=twitch.get('streamer',fallback='the_viox'), + app_id=twitch.get('app_id',fallback=''), + app_secret=twitch.get('app_secret',fallback=''), + target_tg_chat=twitch.get('target_tg_chat',fallback=''), + tg_token=twitch.get('tg_token',fallback=''), + check_period=twitch.getint('check_period',fallback=10), + cleanup_msg=twitch.getboolean('cleanup_msg',fallback=True) + ) + with open(config_path, 'w') as cfg_file: + config_file.read_dict({'twitch': asdict(config)}) + config_file.write(cfg_file) + return config diff --git a/src/twitch-tgbot b/src/twitch-tgbot index 2bbc0a9..184d6ec 100755 --- a/src/twitch-tgbot +++ b/src/twitch-tgbot @@ -10,7 +10,7 @@ from datetime import datetime import requests import shutil -from parse_config import Config, load_config +from parse_config import Config from log_msg import MyLog @@ -128,7 +128,7 @@ def check_alive(): if __name__ == '__main__': log = MyLog().logger log.info("Запущен") - config_python = load_config() + config_python = Config.load() twitch_client = Twitch(config_python.app_id, config_python.app_secret) bot = telebot.TeleBot(config_python.tg_token)