From 4fbdb198aed316b140b82a65e1cb46f7b786ba53 Mon Sep 17 00:00:00 2001 From: mctaylors Date: Wed, 26 Mar 2025 14:28:15 +0300 Subject: [PATCH] refactor: move imports in main.py this change allows to avoid multiple and very specific imports in other python files Signed-off-by: mctaylors --- commands.py | 8 ++++---- inline_query.py | 10 ++++------ main.py | 14 +++++++------- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/commands.py b/commands.py index 567f1e2..48ddc3d 100644 --- a/commands.py +++ b/commands.py @@ -1,9 +1,11 @@ -import html_parser - from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup from telegram.constants import ParseMode from telegram.ext import ContextTypes +import html_parser +import main +from main import config + async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: await update.message.reply_text( @@ -26,8 +28,6 @@ async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No async def about_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - import main - from main import config reply_markup = None source_url = config.get('General', 'SourceUrl') if source_url.startswith('http://') or source_url.startswith('https://'): diff --git a/inline_query.py b/inline_query.py index 489624b..65e1c98 100644 --- a/inline_query.py +++ b/inline_query.py @@ -1,4 +1,3 @@ -from configparser import ConfigParser from uuid import uuid4 import requests @@ -7,7 +6,9 @@ from telegram import Update, InlineKeyboardButton, InlineQueryResultArticle, Inp from telegram.constants import ParseMode from telegram.ext import ContextTypes +import main from extensions import humanize_tags_from_json, format_rating +from main import config # noinspection PyUnusedLocal @@ -20,7 +21,6 @@ async def inline_query(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No if not query.isdigit(): return - from main import config response = requests.get(f"http://{config.get('App', 'Host')}/posts/{query}.json") if response.status_code != 200: @@ -29,12 +29,10 @@ async def inline_query(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No data = response.json() - await answer_query(update, query, config, data) + await answer_query(update, query, data) -async def answer_query(update: Update, query: str, - config: ConfigParser, data) -> None: - import main +async def answer_query(update: Update, query: str, data) -> None: characters = humanize_tags_from_json(data['tag_string_character'], "no characters") copyrights = humanize_tags_from_json(data['tag_string_copyright'], "unknown copyright") artists = humanize_tags_from_json(data['tag_string_artist'], "unknown artist") diff --git a/main.py b/main.py index 5f1685d..eb36f96 100644 --- a/main.py +++ b/main.py @@ -5,9 +5,6 @@ import os import requests from telegram.ext import Application, CommandHandler, InlineQueryHandler -from commands import * -from inline_query import inline_query - logging.basicConfig( format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO ) @@ -22,17 +19,20 @@ config.read("config.ini") hostname = config.get('App', 'HostName') hostname = hostname if hostname != "" else config.get('App', 'Host') + def main() -> None: validate_config() application = Application.builder().token(get_token()).build() - application.add_handler(CommandHandler("start", start_command)) - application.add_handler(CommandHandler("help", help_command)) - application.add_handler(CommandHandler("about", about_command)) + import commands + application.add_handler(CommandHandler("start", commands.start_command)) + application.add_handler(CommandHandler("help", commands.help_command)) + application.add_handler(CommandHandler("about", commands.about_command)) + from inline_query import inline_query application.add_handler(InlineQueryHandler(inline_query)) - application.run_polling(allowed_updates=Update.ALL_TYPES) + application.run_polling(allowed_updates=commands.Update.ALL_TYPES) def validate_config() -> None: