Added command line arguments.
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1,3 @@
|
||||
/res/db/database.db
|
||||
/res/db/
|
||||
*__pycache__/
|
||||
|
||||
119
config.py
119
config.py
@@ -1,9 +1,12 @@
|
||||
from os import getenv
|
||||
from dataclasses import dataclass
|
||||
from argparse import ArgumentParser, Namespace
|
||||
|
||||
from utils import Singleton
|
||||
|
||||
|
||||
@dataclass
|
||||
class Config:
|
||||
class Config(metaclass=Singleton):
|
||||
"""
|
||||
Notifier config dataclass.
|
||||
|
||||
@@ -19,38 +22,100 @@ class Config:
|
||||
|
||||
|
||||
class ConfigError(Exception):
|
||||
"""Config error exception."""
|
||||
pass
|
||||
|
||||
|
||||
_teletoken = getenv('LTLNOTIFIER_TELETOKEN')
|
||||
# Check api token
|
||||
if not _teletoken:
|
||||
raise ConfigError('virtual environment LTLNOTIFIER_TELETOKEN not set.')
|
||||
if len(_teletoken) != 45 or _teletoken[9] != ':' or not _teletoken[:9].isdigit():
|
||||
raise ConfigError('virtual environment LTLNOTIFIER_TELETOKEN incorrect.')
|
||||
def _get_teletoken(cmd_arguments: Namespace) -> str:
|
||||
"""
|
||||
Get Telegram bot token.
|
||||
Can raise **ConfigError** if Telegram token not set or incorrect.
|
||||
|
||||
_bot_owner = getenv('LTLNOTIFIER_BOT_OWNER')
|
||||
# Check bot owner
|
||||
if not _bot_owner:
|
||||
raise ConfigError('virtual environment LTLNOTIFIER_BOT_OWNER not set.')
|
||||
try:
|
||||
if len(_bot_owner) != 9:
|
||||
: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('virtual environment LTLNOTIFIER_BOT_OWNER incorrect.')
|
||||
bot_owner = int(bot_owner)
|
||||
except ValueError:
|
||||
raise ConfigError('Bot owner telegram id incorrect.')
|
||||
return bot_owner
|
||||
|
||||
_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='')
|
||||
_port = int(_port) if _port.isdigit() else 3001
|
||||
def _get_base_url(cmd_arguments: Namespace) -> str:
|
||||
"""
|
||||
Get server base url.
|
||||
Can raise **Config error** if base url not set.
|
||||
|
||||
config = Config(teletoken=_teletoken,
|
||||
port=_port,
|
||||
bot_owner=_bot_owner,
|
||||
base_url=_base_url)
|
||||
: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.')
|
||||
|
||||
__all__ = ('Config', 'config')
|
||||
|
||||
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',)
|
||||
|
||||
Reference in New Issue
Block a user