Fixes.
This commit is contained in:
46
config.py
46
config.py
@@ -15,10 +15,10 @@ class Config(metaclass=Singleton):
|
||||
* `teletoken`: telegram api token.
|
||||
* `bot_owner`: telegram id of bot owner.
|
||||
"""
|
||||
base_url: str
|
||||
port: int
|
||||
teletoken: str
|
||||
bot_owner: int
|
||||
base_url: str = ''
|
||||
port: int = 3001
|
||||
teletoken: str = ''
|
||||
bot_owner: int = 0
|
||||
|
||||
|
||||
class ConfigError(Exception):
|
||||
@@ -28,7 +28,8 @@ class ConfigError(Exception):
|
||||
|
||||
def _get_teletoken(cmd_arguments: Namespace) -> str:
|
||||
"""
|
||||
Get Telegram bot token.
|
||||
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.
|
||||
@@ -46,13 +47,14 @@ def _get_teletoken(cmd_arguments: Namespace) -> str:
|
||||
|
||||
def _get_bot_owner_id(cmd_arguments: Namespace) -> int:
|
||||
"""
|
||||
Get bot owner telegram id.
|
||||
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.
|
||||
:param cmd_arguments: command line arguments object.
|
||||
:return: bot owner Telegram id.
|
||||
"""
|
||||
bot_owner = getenv('LTLNOTIFYER_BOT_OWNER')
|
||||
bot_owner = cmd_arguments.owner or getenv('LTLNOTIFYER_BOT_OWNER')
|
||||
if not bot_owner:
|
||||
raise ConfigError('Bot owner telegram id not set.')
|
||||
try:
|
||||
@@ -66,32 +68,42 @@ def _get_bot_owner_id(cmd_arguments: Namespace) -> int:
|
||||
|
||||
def _get_base_url(cmd_arguments: Namespace) -> str:
|
||||
"""
|
||||
Get server base url.
|
||||
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.
|
||||
: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.
|
||||
Get server port from command line arguments or
|
||||
**LTLNOTIFYER_PORT** environment variable.
|
||||
|
||||
:param cmd_arguments: command line arguments.
|
||||
:return:
|
||||
"""
|
||||
: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) -> None:
|
||||
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, port = _get_base_url(arguments)
|
||||
base_url = _get_base_url(arguments)
|
||||
port = _get_port(arguments)
|
||||
Config(teletoken=teletoken,
|
||||
port=port,
|
||||
bot_owner=bot_owner,
|
||||
@@ -114,7 +126,7 @@ _parser.add_argument('--base_url', default='',
|
||||
_parser.add_argument('--port', default='',
|
||||
help='Server port (default = 3001. \n'
|
||||
'Can be set in LTLNOTIFYER_PORT environment '
|
||||
'variable.')
|
||||
'variable.)')
|
||||
|
||||
_init_config(_parser.parse_args())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user