From 8b2bcd29e5d57cc4bc8608323e123b82ce61f459 Mon Sep 17 00:00:00 2001 From: Alexey Sokolov Date: Wed, 2 Mar 2022 15:52:27 +0300 Subject: [PATCH] Fixes. --- config.py | 46 ++++++++++++++++++++++++++++---------------- start.py | 3 ++- telegram/__init__.py | 5 +++-- web/routes.py | 3 ++- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/config.py b/config.py index a0a679b..da5c6cd 100644 --- a/config.py +++ b/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()) diff --git a/start.py b/start.py index 996010f..0cfc298 100644 --- a/start.py +++ b/start.py @@ -2,11 +2,12 @@ import logging from aiohttp import web -from config import config +from config import Config from telegram import telegram_bot, telegram_dispatcher import db from web import create_app +config = Config() app = create_app(telegram_bot, telegram_dispatcher, config) logging.basicConfig(level=logging.INFO) diff --git a/telegram/__init__.py b/telegram/__init__.py index 7a625cc..1c9f102 100644 --- a/telegram/__init__.py +++ b/telegram/__init__.py @@ -2,9 +2,10 @@ from aiogram import Bot, Dispatcher from aiogram.dispatcher.webhook import SendMessage from aiogram.contrib.middlewares.logging import LoggingMiddleware -from config import config +from config import Config +config = Config() telegram_bot = Bot(token=config.teletoken) telegram_dispatcher = Dispatcher(telegram_bot) telegram_dispatcher.middleware.setup(LoggingMiddleware()) @@ -15,4 +16,4 @@ async def test_telegram(message): return SendMessage(message.chat.id, 'passed') -__all__ = ('telegram_bot', telegram_dispatcher) \ No newline at end of file +__all__ = ('telegram_bot', 'telegram_dispatcher') \ No newline at end of file diff --git a/web/routes.py b/web/routes.py index 0aa55d0..f9d2793 100644 --- a/web/routes.py +++ b/web/routes.py @@ -1,9 +1,10 @@ from aiohttp import web -from config import config +from config import Config from .views import test_route +config = Config() TELEGRAM_WEBHOOK_ROUTE = '/' + config.teletoken routes = [