breadtube-bot/breadtube_bot/config.py
2025-10-04 16:51:08 +09:00

21 lines
722 B
Python

from dataclasses import asdict, dataclass
@dataclass
class Config:
bot_channel: str = 'breadtube-bot'
bot_role: str = 'BreadTube'
bot_channel_init_retries: int = 3
def to_str(self) -> str:
return '\n'.join(['config', *[f'{k}={v}' for k, v in asdict(self).items()]])
def from_str(self, text: str):
lines = text.strip().splitlines()
if not lines:
raise RuntimeError('Config cannot load: empty input')
if lines[0] != 'config':
raise RuntimeError('Config cannot load: first line is not "config"')
for line in lines[1:]:
key, value = line.split('=', maxsplit=1)
setattr(self, key, self.__annotations__[key](value))