From 948325cb4291d78bb2a82968e3f685bf64cceee5 Mon Sep 17 00:00:00 2001 From: Davte Date: Sun, 3 May 2020 16:00:01 +0200 Subject: [PATCH] Helper module deprecated (use davtelepot.helper instead), typos and minor refactoring --- ciclopibot/__init__.py | 2 +- ciclopibot/bot.py | 33 +++++++++++++++++++-------------- ciclopibot/ciclopi.py | 6 ++++-- ciclopibot/helper.py | 19 ------------------- 4 files changed, 24 insertions(+), 36 deletions(-) delete mode 100644 ciclopibot/helper.py diff --git a/ciclopibot/__init__.py b/ciclopibot/__init__.py index 00bb1a5..cf7aa23 100644 --- a/ciclopibot/__init__.py +++ b/ciclopibot/__init__.py @@ -3,7 +3,7 @@ __author__ = "Davide Testa" __email__ = "davide@davte.it" __license__ = "GNU General Public License v3.0" -__version__ = "1.1.16" +__version__ = "1.1.17" __maintainer__ = "Davide Testa" __contact__ = "t.me/davte" diff --git a/ciclopibot/bot.py b/ciclopibot/bot.py index d6ad3bf..ea6aa53 100644 --- a/ciclopibot/bot.py +++ b/ciclopibot/bot.py @@ -15,7 +15,8 @@ from .messages import ( default_help_messages, language_messages, supported_languages ) -if __name__ == '__main__': + +def main(): path = os.path.dirname( os.path.abspath( __file__ @@ -43,6 +44,7 @@ if __name__ == '__main__': # Outputs the log in console, log_file and errors_file # Log formatter: datetime, module name (filled with spaces up to 15 # characters), logging level name (filled to 8), message + # noinspection SpellCheckingInspection log_formatter = logging.Formatter( "%(asctime)s [%(module)-15s %(levelname)-8s] %(message)s", style='%' @@ -60,10 +62,10 @@ if __name__ == '__main__': file_handler.setLevel(logging.ERROR) root_logger.addHandler(file_handler) - consoleHandler = logging.StreamHandler() - consoleHandler.setFormatter(log_formatter) - consoleHandler.setLevel(logging.DEBUG) - root_logger.addHandler(consoleHandler) + console_handler = logging.StreamHandler() + console_handler.setFormatter(log_formatter) + console_handler.setLevel(logging.DEBUG) + root_logger.addHandler(console_handler) # Instantiate bot bot = davtelepot.bot.Bot(token=bot_token, @@ -78,14 +80,13 @@ if __name__ == '__main__': bot.set_authorization_denied_message( messages.authorization_denied_message ) - with bot.db as db: - db['users'].upsert( - dict( - telegram_id=63538990, - privileges=1 - ), - ['telegram_id'] - ) + bot.db['users'].upsert( + dict( + telegram_id=63538990, + privileges=1 + ), + ['telegram_id'] + ) davtelepot.administration_tools.init(bot) ciclopi.init(bot) davtelepot.authorization.init(bot) @@ -101,4 +102,8 @@ if __name__ == '__main__': local_host=local_host, port=port ) - sys.exit(exit_state) + return exit_state + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/ciclopibot/ciclopi.py b/ciclopibot/ciclopi.py index 515c3a6..c2ce40f 100644 --- a/ciclopibot/ciclopi.py +++ b/ciclopibot/ciclopi.py @@ -21,7 +21,7 @@ default_location = None _URL = "http://www.ciclopi.eu/frmLeStazioni.aspx" -ciclopi_webpage = CachedPage.get( +ciclopi_web_page = CachedPage.get( _URL, datetime.timedelta(seconds=15), mode='html' @@ -569,6 +569,8 @@ async def cancel_ciclopi_location(bot, update, user_record): ) +# The service is currently suspended: code is unreachable of course +# noinspection PyUnreachableCode,PyUnusedLocal async def _ciclopi_command(bot: davtelepot.bot.Bot, update, user_record, sent_message=None, show_all=False): return { @@ -593,7 +595,7 @@ async def _ciclopi_command(bot: davtelepot.bot.Bot, update, user_record, sent_me # ) # ) ) - ciclopi_data = await ciclopi_webpage.get_page() + ciclopi_data = await ciclopi_web_page.get_page() if ciclopi_data is None or isinstance(ciclopi_data, Exception): text = bot.get_message( 'ciclopi', 'command', 'unavailable_website', diff --git a/ciclopibot/helper.py b/ciclopibot/helper.py deleted file mode 100644 index c9e6eca..0000000 --- a/ciclopibot/helper.py +++ /dev/null @@ -1,19 +0,0 @@ -"""Make a self-consistent bot help section.""" - -# Third party modules -import davtelepot.helper -from davtelepot.utilities import json_read - -# Project modules -from .messages import default_help_messages - - -def init(bot, help_messages=None, - help_buttons=[]): - """Assign parsers, commands, buttons and queries to given `bot`.""" - if help_messages is None: - help_messages = default_help_messages - davtelepot.helper.init( - bot=bot, - help_messages=help_messages, - )