Added command line arguments.

This commit is contained in:
Alexey Sokolov
2022-02-18 17:23:23 +03:00
parent 0a08b23b72
commit f70500a519
2 changed files with 95 additions and 28 deletions

2
.gitignore vendored
View File

@@ -1 +1,3 @@
/res/db/database.db /res/db/database.db
/res/db/
*__pycache__/

121
config.py
View File

@@ -1,9 +1,12 @@
from os import getenv from os import getenv
from dataclasses import dataclass from dataclasses import dataclass
from argparse import ArgumentParser, Namespace
from utils import Singleton
@dataclass @dataclass
class Config: class Config(metaclass=Singleton):
""" """
Notifier config dataclass. Notifier config dataclass.
@@ -19,38 +22,100 @@ class Config:
class ConfigError(Exception): class ConfigError(Exception):
"""Config error exception."""
pass pass
_teletoken = getenv('LTLNOTIFIER_TELETOKEN') def _get_teletoken(cmd_arguments: Namespace) -> str:
# Check api token """
if not _teletoken: Get Telegram bot token.
raise ConfigError('virtual environment LTLNOTIFIER_TELETOKEN not set.') Can raise **ConfigError** if Telegram token not set or incorrect.
if len(_teletoken) != 45 or _teletoken[9] != ':' or not _teletoken[:9].isdigit():
raise ConfigError('virtual environment LTLNOTIFIER_TELETOKEN incorrect.')
_bot_owner = getenv('LTLNOTIFIER_BOT_OWNER') :param cmd_arguments: command line arguments.
# Check bot owner :return: Telegram bot token.
if not _bot_owner: """
raise ConfigError('virtual environment LTLNOTIFIER_BOT_OWNER not set.') teletoken = cmd_arguments.teletoken or getenv('LTLNOTIFYER_TELETOKEN')
try: if not teletoken:
if len(_bot_owner) != 9: raise ConfigError('Telegram token not set.')
raise ValueError if len(teletoken) != 45 or \
_bot_owner = int(_bot_owner) teletoken[9] != ':' \
except ValueError: or not teletoken[:9].isdigit():
raise ConfigError('virtual environment LTLNOTIFIER_BOT_OWNER incorrect.') raise ConfigError('Telegram token incorrect.')
return teletoken
_base_url = getenv('LTLNOTIFIER_BASE_URL')
# Check base url
if not _base_url:
raise ConfigError('virtual environment LTLNOTIFIER_BASE_URL not set')
_port = getenv('LTLNOTIFIER_PORT', default='') def _get_bot_owner_id(cmd_arguments: Namespace) -> int:
_port = int(_port) if _port.isdigit() else 3001 """
Get bot owner telegram id.
Can raise **ConfigError** if bot owner id not set or incorrect.
config = Config(teletoken=_teletoken, :param cmd_arguments: command line arguments.
port=_port, :return: bot owner Telegram id.
bot_owner=_bot_owner, """
base_url=_base_url) 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
__all__ = ('Config', 'config')
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',)