from os import getenv from dataclasses import dataclass from argparse import ArgumentParser, Namespace from utils import Singleton @dataclass class Config(metaclass=Singleton): """ Notifier config dataclass. * `base_url`: url of domain * `port`: server port (default **3001**) * `teletoken`: telegram api token. * `bot_owner`: telegram id of bot owner. """ base_url: str port: int teletoken: str bot_owner: int class ConfigError(Exception): """Config error exception.""" pass def _get_teletoken(cmd_arguments: Namespace) -> str: """ Get Telegram bot token. Can raise **ConfigError** if Telegram token not set or incorrect. :param cmd_arguments: command line arguments. :return: Telegram bot token. """ teletoken = cmd_arguments.teletoken or getenv('LTLNOTIFYER_TELETOKEN') if not teletoken: raise ConfigError('Telegram token not set.') if len(teletoken) != 45 or \ teletoken[9] != ':' \ or not teletoken[:9].isdigit(): raise ConfigError('Telegram token incorrect.') return teletoken def _get_bot_owner_id(cmd_arguments: Namespace) -> int: """ Get bot owner telegram id. Can raise **ConfigError** if bot owner id not set or incorrect. :param cmd_arguments: command line arguments. :return: bot owner Telegram id. """ bot_owner = getenv('LTLNOTIFYER_BOT_OWNER') if not bot_owner: raise ConfigError('Bot owner telegram id not set.') try: if len(bot_owner) != 9: raise ValueError bot_owner = int(bot_owner) except ValueError: raise ConfigError('Bot owner telegram id incorrect.') return bot_owner def _get_base_url(cmd_arguments: Namespace) -> str: """ Get server base url. Can raise **Config error** if base url not set. :param cmd_arguments: command line arguments. :return: server base url. """ base_url = cmd_arguments.base_url or getenv('LTLNOTIFYER_BASE_URL') if not base_url: raise ConfigError('Server base url not set.') def _get_port(cmd_arguments: Namespace) -> int: """ Get server port. :param cmd_arguments: command line arguments. :return: """ port = cmd_arguments.port or getenv('LTLNOTIFIER_PORT', default='') port = int(port) if port.isdigit() else 3001 def _init_config(arguments) -> None: teletoken = _get_teletoken(arguments) bot_owner = _get_bot_owner_id(arguments) base_url, port = _get_base_url(arguments) Config(teletoken=teletoken, port=port, bot_owner=bot_owner, base_url=base_url) _parser = ArgumentParser(description="ltlNotifyer server.") _parser.add_argument('--teletoken', default='', help='Telegram bot token.\n' 'Must be set if LTLNOTIFYER_TELETOKEN environment' 'variable not set.') _parser.add_argument('--owner', default='', help='Bot owner Telegram id.\n' 'Must be set if LTLNOTIFYER_BOT_OWNER environment' 'variable not set.') _parser.add_argument('--base_url', default='', help='Server base url.\n' 'Must be set if LTLNOTIFIER_BASE_URL environment' 'variable not set.') _parser.add_argument('--port', default='', help='Server port (default = 3001. \n' 'Can be set in LTLNOTIFYER_PORT environment ' 'variable.') _init_config(_parser.parse_args()) __all__ = ('Config',)