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 = 3001 teletoken: str = '' bot_owner: int = 0 class ConfigError(Exception): """Config error exception.""" pass def _get_teletoken(cmd_arguments: Namespace) -> str: """ Get Telegram bot token from command line arguments or **LTLNOTIFYER_TELETOKEN** environment variable. 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 from command line arguments or **LTLNOTIFYER_BOT_OWNER** environment variable. Can raise **ConfigError** if bot owner id not set or incorrect. :param cmd_arguments: command line arguments object. :return: bot owner Telegram id. """ bot_owner = cmd_arguments.owner or 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 from command line arguments or **LTLNOTIFYER_BASE_URL** environment variable. Can raise **Config error** if base url not set. :param cmd_arguments: command line arguments object. :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.') return base_url def _get_port(cmd_arguments: Namespace) -> int: """ Get server port from command line arguments or **LTLNOTIFYER_PORT** environment variable. :param cmd_arguments: command line arguments object. :return: port number or 3001 if port not set or incorrect. """ port = cmd_arguments.port or getenv('LTLNOTIFIER_PORT', default='') port = int(port) if port.isdigit() else 3001 return port def _init_config(arguments: Namespace) -> None: """ Initialisation Config singleton. :param arguments: command line arguments object. """ teletoken = _get_teletoken(arguments) bot_owner = _get_bot_owner_id(arguments) base_url = _get_base_url(arguments) port = _get_port(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',)