какая-то магия с загрузкой конфигурации

This commit is contained in:
lulzette 2024-05-20 21:38:39 +03:00
parent cbcf468009
commit 99e8c84ea6
2 changed files with 27 additions and 25 deletions

View File

@ -1,6 +1,7 @@
from dataclasses import dataclass, asdict from dataclasses import dataclass, asdict
import configparser import configparser
@dataclass(slots=True, frozen=True) @dataclass(slots=True, frozen=True)
class Config: class Config:
streamer: str streamer: str
@ -11,26 +12,27 @@ class Config:
check_period: int check_period: int
cleanup_msg: bool cleanup_msg: bool
def load_config(config_path: str = "twitch-tgbot.cfg") -> Config: @classmethod
""" def load(cls, config_path: str = "twitch-tgbot.cfg") -> 'Config':
Эта функция либо читает существующий конфиг, либо создает новый. """
Возвращает объект конфига (configparser.ConfigParser()) Эта функция либо читает существующий конфиг, либо создает новый.
""" Возвращает объект конфига (configparser.ConfigParser())
config_file = 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') if not config_file.read(config_path) or not config_file.has_section('twitch'):
twitch = config_file['twitch'] config_file.add_section('twitch')
config = Config( twitch = config_file['twitch']
streamer=twitch.get('streamer',fallback='the_viox'), config = Config(
app_id=twitch.get('app_id',fallback=''), streamer=twitch.get('streamer',fallback='the_viox'),
app_secret=twitch.get('app_secret',fallback=''), app_id=twitch.get('app_id',fallback=''),
target_tg_chat=twitch.get('target_tg_chat',fallback=''), app_secret=twitch.get('app_secret',fallback=''),
tg_token=twitch.get('tg_token',fallback=''), target_tg_chat=twitch.get('target_tg_chat',fallback=''),
check_period=twitch.getint('check_period',fallback=10), tg_token=twitch.get('tg_token',fallback=''),
cleanup_msg=twitch.getboolean('cleanup_msg',fallback=True) 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)}) with open(config_path, 'w') as cfg_file:
config_file.write(cfg_file) config_file.read_dict({'twitch': asdict(config)})
return config config_file.write(cfg_file)
return config

View File

@ -10,7 +10,7 @@ from datetime import datetime
import requests import requests
import shutil import shutil
from parse_config import Config, load_config from parse_config import Config
from log_msg import MyLog from log_msg import MyLog
@ -128,7 +128,7 @@ def check_alive():
if __name__ == '__main__': if __name__ == '__main__':
log = MyLog().logger log = MyLog().logger
log.info("Запущен") log.info("Запущен")
config_python = load_config() config_python = Config.load()
twitch_client = Twitch(config_python.app_id, config_python.app_secret) twitch_client = Twitch(config_python.app_id, config_python.app_secret)
bot = telebot.TeleBot(config_python.tg_token) bot = telebot.TeleBot(config_python.tg_token)