diff --git a/database/__init__.py b/database/__init__.py index 766c4da..ae0cdc6 100644 --- a/database/__init__.py +++ b/database/__init__.py @@ -11,9 +11,19 @@ from .async_db import AsyncBotDB from .base import DatabaseConnection -from .models import (Admin, AudioListenRecord, AudioMessage, AudioModerate, - BlacklistUser, MessageContentLink, Migration, PostContent, - TelegramPost, User, UserMessage) +from .models import ( + Admin, + AudioListenRecord, + AudioMessage, + AudioModerate, + BlacklistUser, + MessageContentLink, + Migration, + PostContent, + TelegramPost, + User, + UserMessage, +) from .repository_factory import RepositoryFactory # Для обратной совместимости экспортируем старый интерфейс diff --git a/database/async_db.py b/database/async_db.py index 83057bc..086204d 100644 --- a/database/async_db.py +++ b/database/async_db.py @@ -3,9 +3,16 @@ from typing import Any, Dict, List, Optional, Tuple import aiosqlite -from database.models import (Admin, AudioMessage, BlacklistHistoryRecord, - BlacklistUser, PostContent, TelegramPost, User, - UserMessage) +from database.models import ( + Admin, + AudioMessage, + BlacklistHistoryRecord, + BlacklistUser, + PostContent, + TelegramPost, + User, + UserMessage, +) from database.repository_factory import RepositoryFactory diff --git a/database/repository_factory.py b/database/repository_factory.py index 5d4ce65..18c3aae 100644 --- a/database/repository_factory.py +++ b/database/repository_factory.py @@ -2,8 +2,9 @@ from typing import Optional from database.repositories.admin_repository import AdminRepository from database.repositories.audio_repository import AudioRepository -from database.repositories.blacklist_history_repository import \ - BlacklistHistoryRepository +from database.repositories.blacklist_history_repository import ( + BlacklistHistoryRepository, +) from database.repositories.blacklist_repository import BlacklistRepository from database.repositories.message_repository import MessageRepository from database.repositories.post_repository import PostRepository diff --git a/helper_bot/handlers/admin/__init__.py b/helper_bot/handlers/admin/__init__.py index dbcf322..4b9d6f2 100644 --- a/helper_bot/handlers/admin/__init__.py +++ b/helper_bot/handlers/admin/__init__.py @@ -1,10 +1,20 @@ from .admin_handlers import admin_router from .dependencies import AdminAccessMiddleware, BotDB, Settings -from .exceptions import (AdminAccessDeniedError, AdminError, InvalidInputError, - UserAlreadyBannedError, UserNotFoundError) +from .exceptions import ( + AdminAccessDeniedError, + AdminError, + InvalidInputError, + UserAlreadyBannedError, + UserNotFoundError, +) from .services import AdminService, BannedUser, User -from .utils import (escape_html, format_ban_confirmation, format_user_info, - handle_admin_error, return_to_admin_menu) +from .utils import ( + escape_html, + format_ban_confirmation, + format_user_info, + handle_admin_error, + return_to_admin_menu, +) __all__ = [ "admin_router", diff --git a/helper_bot/handlers/admin/admin_handlers.py b/helper_bot/handlers/admin/admin_handlers.py index 1bbafb6..e711abb 100644 --- a/helper_bot/handlers/admin/admin_handlers.py +++ b/helper_bot/handlers/admin/admin_handlers.py @@ -4,19 +4,26 @@ from aiogram.fsm.context import FSMContext from helper_bot.filters.main import ChatTypeFilter from helper_bot.handlers.admin.dependencies import AdminAccessMiddleware -from helper_bot.handlers.admin.exceptions import (InvalidInputError, - UserAlreadyBannedError) +from helper_bot.handlers.admin.exceptions import ( + InvalidInputError, + UserAlreadyBannedError, +) from helper_bot.handlers.admin.services import AdminService -from helper_bot.handlers.admin.utils import (escape_html, - format_ban_confirmation, - format_user_info, - handle_admin_error, - return_to_admin_menu) -from helper_bot.keyboards.keyboards import (create_keyboard_for_approve_ban, - create_keyboard_for_ban_days, - create_keyboard_for_ban_reason, - create_keyboard_with_pagination, - get_reply_keyboard_admin) +from helper_bot.handlers.admin.utils import ( + escape_html, + format_ban_confirmation, + format_user_info, + handle_admin_error, + return_to_admin_menu, +) +from helper_bot.keyboards.keyboards import ( + create_keyboard_for_approve_ban, + create_keyboard_for_ban_days, + create_keyboard_for_ban_reason, + create_keyboard_with_pagination, + get_reply_keyboard_admin, +) + # Local imports - metrics from helper_bot.utils.metrics import db_query_time, track_errors, track_time from logs.custom_logger import logger diff --git a/helper_bot/handlers/admin/rate_limit_handlers.py b/helper_bot/handlers/admin/rate_limit_handlers.py index 1358a12..2837121 100644 --- a/helper_bot/handlers/admin/rate_limit_handlers.py +++ b/helper_bot/handlers/admin/rate_limit_handlers.py @@ -8,14 +8,18 @@ from aiogram.fsm.context import FSMContext from aiogram.types import FSInputFile from helper_bot.filters.main import ChatTypeFilter -from helper_bot.middlewares.dependencies_middleware import \ - DependenciesMiddleware +from helper_bot.middlewares.dependencies_middleware import DependenciesMiddleware + # Local imports - metrics from helper_bot.utils.metrics import track_errors, track_time from helper_bot.utils.rate_limit_metrics import ( - get_rate_limit_metrics_summary, update_rate_limit_gauges) -from helper_bot.utils.rate_limit_monitor import (get_rate_limit_summary, - rate_limit_monitor) + get_rate_limit_metrics_summary, + update_rate_limit_gauges, +) +from helper_bot.utils.rate_limit_monitor import ( + get_rate_limit_summary, + rate_limit_monitor, +) from logs.custom_logger import logger diff --git a/helper_bot/handlers/admin/services.py b/helper_bot/handlers/admin/services.py index e8b760f..6eb625e 100644 --- a/helper_bot/handlers/admin/services.py +++ b/helper_bot/handlers/admin/services.py @@ -1,11 +1,16 @@ from datetime import datetime from typing import List, Optional -from helper_bot.handlers.admin.exceptions import (InvalidInputError, - UserAlreadyBannedError) -from helper_bot.utils.helper_func import (add_days_to_date, - get_banned_users_buttons, - get_banned_users_list) +from helper_bot.handlers.admin.exceptions import ( + InvalidInputError, + UserAlreadyBannedError, +) +from helper_bot.utils.helper_func import ( + add_days_to_date, + get_banned_users_buttons, + get_banned_users_list, +) + # Local imports - metrics from helper_bot.utils.metrics import track_errors, track_time from logs.custom_logger import logger diff --git a/helper_bot/handlers/callback/__init__.py b/helper_bot/handlers/callback/__init__.py index 0feae62..ccb7ddd 100644 --- a/helper_bot/handlers/callback/__init__.py +++ b/helper_bot/handlers/callback/__init__.py @@ -1,8 +1,19 @@ from .callback_handlers import callback_router -from .constants import (CALLBACK_BAN, CALLBACK_DECLINE, CALLBACK_PAGE, - CALLBACK_PUBLISH, CALLBACK_RETURN, CALLBACK_UNLOCK) -from .exceptions import (BanError, PostNotFoundError, PublishError, - UserBlockedBotError, UserNotFoundError) +from .constants import ( + CALLBACK_BAN, + CALLBACK_DECLINE, + CALLBACK_PAGE, + CALLBACK_PUBLISH, + CALLBACK_RETURN, + CALLBACK_UNLOCK, +) +from .exceptions import ( + BanError, + PostNotFoundError, + PublishError, + UserBlockedBotError, + UserNotFoundError, +) from .services import BanService, PostPublishService __all__ = [ diff --git a/helper_bot/handlers/callback/callback_handlers.py b/helper_bot/handlers/callback/callback_handlers.py index ab8551a..d156184 100644 --- a/helper_bot/handlers/callback/callback_handlers.py +++ b/helper_bot/handlers/callback/callback_handlers.py @@ -11,25 +11,45 @@ from aiogram.types import CallbackQuery from helper_bot.handlers.admin.utils import format_user_info from helper_bot.handlers.voice.constants import CALLBACK_DELETE, CALLBACK_SAVE from helper_bot.handlers.voice.services import AudioFileService -from helper_bot.keyboards.keyboards import (create_keyboard_for_ban_reason, - create_keyboard_with_pagination, - get_reply_keyboard_admin) +from helper_bot.keyboards.keyboards import ( + create_keyboard_for_ban_reason, + create_keyboard_with_pagination, + get_reply_keyboard_admin, +) from helper_bot.utils.base_dependency_factory import get_global_instance -from helper_bot.utils.helper_func import (get_banned_users_buttons, - get_banned_users_list) +from helper_bot.utils.helper_func import get_banned_users_buttons, get_banned_users_list + # Local imports - metrics -from helper_bot.utils.metrics import (db_query_time, track_errors, - track_file_operations, track_time) +from helper_bot.utils.metrics import ( + db_query_time, + track_errors, + track_file_operations, + track_time, +) from logs.custom_logger import logger -from .constants import (CALLBACK_BAN, CALLBACK_DECLINE, CALLBACK_PAGE, - CALLBACK_PUBLISH, CALLBACK_RETURN, CALLBACK_UNLOCK, - ERROR_BOT_BLOCKED, MESSAGE_DECLINED, MESSAGE_ERROR, - MESSAGE_PUBLISHED, MESSAGE_USER_BANNED, - MESSAGE_USER_UNLOCKED) +from .constants import ( + CALLBACK_BAN, + CALLBACK_DECLINE, + CALLBACK_PAGE, + CALLBACK_PUBLISH, + CALLBACK_RETURN, + CALLBACK_UNLOCK, + ERROR_BOT_BLOCKED, + MESSAGE_DECLINED, + MESSAGE_ERROR, + MESSAGE_PUBLISHED, + MESSAGE_USER_BANNED, + MESSAGE_USER_UNLOCKED, +) from .dependency_factory import get_ban_service, get_post_publish_service -from .exceptions import (BanError, PostNotFoundError, PublishError, - UserBlockedBotError, UserNotFoundError) +from .exceptions import ( + BanError, + PostNotFoundError, + PublishError, + UserBlockedBotError, + UserNotFoundError, +) callback_router = Router() diff --git a/helper_bot/handlers/callback/services.py b/helper_bot/handlers/callback/services.py index b4c2253..140ba90 100644 --- a/helper_bot/handlers/callback/services.py +++ b/helper_bot/handlers/callback/services.py @@ -6,27 +6,47 @@ from aiogram import Bot, types from aiogram.types import CallbackQuery from helper_bot.keyboards.keyboards import create_keyboard_for_ban_reason -from helper_bot.utils.helper_func import (delete_user_blacklist, - get_text_message, send_audio_message, - send_media_group_to_channel, - send_photo_message, - send_text_message, - send_video_message, - send_video_note_message, - send_voice_message) +from helper_bot.utils.helper_func import ( + delete_user_blacklist, + get_text_message, + send_audio_message, + send_media_group_to_channel, + send_photo_message, + send_text_message, + send_video_message, + send_video_note_message, + send_voice_message, +) + # Local imports - metrics -from helper_bot.utils.metrics import (db_query_time, track_errors, - track_media_processing, track_time) +from helper_bot.utils.metrics import ( + db_query_time, + track_errors, + track_media_processing, + track_time, +) from logs.custom_logger import logger -from .constants import (CONTENT_TYPE_AUDIO, CONTENT_TYPE_MEDIA_GROUP, - CONTENT_TYPE_PHOTO, CONTENT_TYPE_TEXT, - CONTENT_TYPE_VIDEO, CONTENT_TYPE_VIDEO_NOTE, - CONTENT_TYPE_VOICE, ERROR_BOT_BLOCKED, - MESSAGE_POST_DECLINED, MESSAGE_POST_PUBLISHED, - MESSAGE_USER_BANNED_SPAM) -from .exceptions import (BanError, PostNotFoundError, PublishError, - UserBlockedBotError, UserNotFoundError) +from .constants import ( + CONTENT_TYPE_AUDIO, + CONTENT_TYPE_MEDIA_GROUP, + CONTENT_TYPE_PHOTO, + CONTENT_TYPE_TEXT, + CONTENT_TYPE_VIDEO, + CONTENT_TYPE_VIDEO_NOTE, + CONTENT_TYPE_VOICE, + ERROR_BOT_BLOCKED, + MESSAGE_POST_DECLINED, + MESSAGE_POST_PUBLISHED, + MESSAGE_USER_BANNED_SPAM, +) +from .exceptions import ( + BanError, + PostNotFoundError, + PublishError, + UserBlockedBotError, + UserNotFoundError, +) class PostPublishService: diff --git a/helper_bot/handlers/group/__init__.py b/helper_bot/handlers/group/__init__.py index e838c10..a060f32 100644 --- a/helper_bot/handlers/group/__init__.py +++ b/helper_bot/handlers/group/__init__.py @@ -6,6 +6,7 @@ from .constants import ERROR_MESSAGES, FSM_STATES from .decorators import error_handler from .exceptions import NoReplyToMessageError, UserNotFoundError from .group_handlers import GroupHandlers, create_group_handlers, group_router + # Local imports - services from .services import AdminReplyService, DatabaseProtocol diff --git a/helper_bot/handlers/group/decorators.py b/helper_bot/handlers/group/decorators.py index 0d298ca..b1511a0 100644 --- a/helper_bot/handlers/group/decorators.py +++ b/helper_bot/handlers/group/decorators.py @@ -25,8 +25,9 @@ def error_handler(func: Callable[..., Any]) -> Callable[..., Any]: (arg for arg in args if isinstance(arg, types.Message)), None ) if message and hasattr(message, "bot"): - from helper_bot.utils.base_dependency_factory import \ - get_global_instance + from helper_bot.utils.base_dependency_factory import ( + get_global_instance, + ) bdf = get_global_instance() important_logs = bdf.settings["Telegram"]["important_logs"] diff --git a/helper_bot/handlers/group/group_handlers.py b/helper_bot/handlers/group/group_handlers.py index d3f0709..c21fddc 100644 --- a/helper_bot/handlers/group/group_handlers.py +++ b/helper_bot/handlers/group/group_handlers.py @@ -7,8 +7,10 @@ from aiogram.fsm.context import FSMContext # Local imports - filters from database.async_db import AsyncBotDB from helper_bot.filters.main import ChatTypeFilter + # Local imports - metrics from helper_bot.utils.metrics import metrics, track_errors, track_time + # Local imports - utilities from logs.custom_logger import logger diff --git a/helper_bot/handlers/group/services.py b/helper_bot/handlers/group/services.py index 9a0ee77..934887b 100644 --- a/helper_bot/handlers/group/services.py +++ b/helper_bot/handlers/group/services.py @@ -8,6 +8,7 @@ from aiogram import types # Local imports from helper_bot.utils.helper_func import send_text_message + # Local imports - metrics from helper_bot.utils.metrics import db_query_time, track_errors, track_time from logs.custom_logger import logger diff --git a/helper_bot/handlers/private/__init__.py b/helper_bot/handlers/private/__init__.py index e5c549b..0a8be50 100644 --- a/helper_bot/handlers/private/__init__.py +++ b/helper_bot/handlers/private/__init__.py @@ -4,8 +4,8 @@ # Local imports - constants and utilities from .constants import BUTTON_TEXTS, ERROR_MESSAGES, FSM_STATES from .decorators import error_handler -from .private_handlers import (PrivateHandlers, create_private_handlers, - private_router) +from .private_handlers import PrivateHandlers, create_private_handlers, private_router + # Local imports - services from .services import BotSettings, PostService, StickerService, UserService diff --git a/helper_bot/handlers/private/decorators.py b/helper_bot/handlers/private/decorators.py index 2546567..1adabc0 100644 --- a/helper_bot/handlers/private/decorators.py +++ b/helper_bot/handlers/private/decorators.py @@ -25,8 +25,9 @@ def error_handler(func: Callable[..., Any]) -> Callable[..., Any]: (arg for arg in args if isinstance(arg, types.Message)), None ) if message and hasattr(message, "bot"): - from helper_bot.utils.base_dependency_factory import \ - get_global_instance + from helper_bot.utils.base_dependency_factory import ( + get_global_instance, + ) bdf = get_global_instance() important_logs = bdf.settings["Telegram"]["important_logs"] diff --git a/helper_bot/handlers/private/private_handlers.py b/helper_bot/handlers/private/private_handlers.py index 72d7c71..e69d681 100644 --- a/helper_bot/handlers/private/private_handlers.py +++ b/helper_bot/handlers/private/private_handlers.py @@ -12,15 +12,19 @@ from aiogram.fsm.context import FSMContext # Local imports - filters and middlewares from database.async_db import AsyncBotDB from helper_bot.filters.main import ChatTypeFilter + # Local imports - utilities -from helper_bot.keyboards import (get_reply_keyboard, - get_reply_keyboard_for_post) +from helper_bot.keyboards import get_reply_keyboard, get_reply_keyboard_for_post from helper_bot.keyboards.keyboards import get_reply_keyboard_leave_chat from helper_bot.middlewares.album_middleware import AlbumMiddleware from helper_bot.middlewares.blacklist_middleware import BlacklistMiddleware from helper_bot.utils import messages -from helper_bot.utils.helper_func import (check_user_emoji, get_first_name, - update_user_info) +from helper_bot.utils.helper_func import ( + check_user_emoji, + get_first_name, + update_user_info, +) + # Local imports - metrics from helper_bot.utils.metrics import db_query_time, track_errors, track_time diff --git a/helper_bot/handlers/private/services.py b/helper_bot/handlers/private/services.py index 42be11e..cda68b2 100644 --- a/helper_bot/handlers/private/services.py +++ b/helper_bot/handlers/private/services.py @@ -15,17 +15,32 @@ from aiogram.types import FSInputFile from database.models import TelegramPost, User from helper_bot.keyboards import get_reply_keyboard_for_post + # Local imports - utilities from helper_bot.utils.helper_func import ( - add_in_db_media, check_username_and_full_name, determine_anonymity, - get_first_name, get_text_message, prepare_media_group_from_middlewares, - send_audio_message, send_media_group_message_to_private_chat, - send_photo_message, send_text_message, send_video_message, - send_video_note_message, send_voice_message) + add_in_db_media, + check_username_and_full_name, + determine_anonymity, + get_first_name, + get_text_message, + prepare_media_group_from_middlewares, + send_audio_message, + send_media_group_message_to_private_chat, + send_photo_message, + send_text_message, + send_video_message, + send_video_note_message, + send_voice_message, +) + # Local imports - metrics -from helper_bot.utils.metrics import (db_query_time, track_errors, - track_file_operations, - track_media_processing, track_time) +from helper_bot.utils.metrics import ( + db_query_time, + track_errors, + track_file_operations, + track_media_processing, + track_time, +) from logs.custom_logger import logger diff --git a/helper_bot/handlers/voice/services.py b/helper_bot/handlers/voice/services.py index e1d8140..36808ee 100644 --- a/helper_bot/handlers/voice/services.py +++ b/helper_bot/handlers/voice/services.py @@ -8,16 +8,23 @@ from typing import List, Optional, Tuple from aiogram.types import FSInputFile -from helper_bot.handlers.voice.constants import (MESSAGE_DELAY_1, - MESSAGE_DELAY_2, - MESSAGE_DELAY_3, - MESSAGE_DELAY_4, STICK_DIR, - STICK_PATTERN, STICKER_DELAY, - VOICE_USERS_DIR) -from helper_bot.handlers.voice.exceptions import (AudioProcessingError, - DatabaseError, - FileOperationError, - VoiceMessageError) +from helper_bot.handlers.voice.constants import ( + MESSAGE_DELAY_1, + MESSAGE_DELAY_2, + MESSAGE_DELAY_3, + MESSAGE_DELAY_4, + STICK_DIR, + STICK_PATTERN, + STICKER_DELAY, + VOICE_USERS_DIR, +) +from helper_bot.handlers.voice.exceptions import ( + AudioProcessingError, + DatabaseError, + FileOperationError, + VoiceMessageError, +) + # Local imports - metrics from helper_bot.utils.metrics import db_query_time, track_errors, track_time from logs.custom_logger import logger diff --git a/helper_bot/handlers/voice/voice_handler.py b/helper_bot/handlers/voice/voice_handler.py index fc24b2d..3ea223e 100644 --- a/helper_bot/handlers/voice/voice_handler.py +++ b/helper_bot/handlers/voice/voice_handler.py @@ -11,21 +11,33 @@ from helper_bot.filters.main import ChatTypeFilter from helper_bot.handlers.private.constants import BUTTON_TEXTS, FSM_STATES from helper_bot.handlers.voice.constants import * from helper_bot.handlers.voice.services import VoiceBotService -from helper_bot.handlers.voice.utils import (get_last_message_text, - get_user_emoji_safe, - validate_voice_message) +from helper_bot.handlers.voice.utils import ( + get_last_message_text, + get_user_emoji_safe, + validate_voice_message, +) from helper_bot.keyboards import get_reply_keyboard -from helper_bot.keyboards.keyboards import (get_main_keyboard, - get_reply_keyboard_for_voice) +from helper_bot.keyboards.keyboards import ( + get_main_keyboard, + get_reply_keyboard_for_voice, +) from helper_bot.middlewares.blacklist_middleware import BlacklistMiddleware -from helper_bot.middlewares.dependencies_middleware import \ - DependenciesMiddleware +from helper_bot.middlewares.dependencies_middleware import DependenciesMiddleware from helper_bot.utils import messages -from helper_bot.utils.helper_func import (check_user_emoji, get_first_name, - send_voice_message, update_user_info) +from helper_bot.utils.helper_func import ( + check_user_emoji, + get_first_name, + send_voice_message, + update_user_info, +) + # Local imports - metrics -from helper_bot.utils.metrics import (db_query_time, track_errors, - track_file_operations, track_time) +from helper_bot.utils.metrics import ( + db_query_time, + track_errors, + track_file_operations, + track_time, +) from logs.custom_logger import logger diff --git a/helper_bot/main.py b/helper_bot/main.py index 1b637a5..6066604 100644 --- a/helper_bot/main.py +++ b/helper_bot/main.py @@ -13,13 +13,13 @@ from helper_bot.handlers.group import group_router from helper_bot.handlers.private import private_router from helper_bot.handlers.voice import VoiceHandlers from helper_bot.middlewares.blacklist_middleware import BlacklistMiddleware -from helper_bot.middlewares.dependencies_middleware import \ - DependenciesMiddleware -from helper_bot.middlewares.metrics_middleware import (ErrorMetricsMiddleware, - MetricsMiddleware) +from helper_bot.middlewares.dependencies_middleware import DependenciesMiddleware +from helper_bot.middlewares.metrics_middleware import ( + ErrorMetricsMiddleware, + MetricsMiddleware, +) from helper_bot.middlewares.rate_limit_middleware import RateLimitMiddleware -from helper_bot.server_prometheus import (start_metrics_server, - stop_metrics_server) +from helper_bot.server_prometheus import start_metrics_server, stop_metrics_server async def start_bot_with_retry( diff --git a/helper_bot/middlewares/metrics_middleware.py b/helper_bot/middlewares/metrics_middleware.py index b5465a9..2564b86 100644 --- a/helper_bot/middlewares/metrics_middleware.py +++ b/helper_bot/middlewares/metrics_middleware.py @@ -16,16 +16,16 @@ from ..utils.metrics import metrics # Import button command mapping try: - from ..handlers.admin.constants import (ADMIN_BUTTON_COMMAND_MAPPING, - ADMIN_COMMANDS) + from ..handlers.admin.constants import ADMIN_BUTTON_COMMAND_MAPPING, ADMIN_COMMANDS from ..handlers.callback.constants import CALLBACK_COMMAND_MAPPING from ..handlers.private.constants import BUTTON_COMMAND_MAPPING - from ..handlers.voice.constants import \ - BUTTON_COMMAND_MAPPING as VOICE_BUTTON_COMMAND_MAPPING - from ..handlers.voice.constants import \ - CALLBACK_COMMAND_MAPPING as VOICE_CALLBACK_COMMAND_MAPPING - from ..handlers.voice.constants import \ - COMMAND_MAPPING as VOICE_COMMAND_MAPPING + from ..handlers.voice.constants import ( + BUTTON_COMMAND_MAPPING as VOICE_BUTTON_COMMAND_MAPPING, + ) + from ..handlers.voice.constants import ( + CALLBACK_COMMAND_MAPPING as VOICE_CALLBACK_COMMAND_MAPPING, + ) + from ..handlers.voice.constants import COMMAND_MAPPING as VOICE_COMMAND_MAPPING except ImportError: # Fallback if constants not available BUTTON_COMMAND_MAPPING = {} diff --git a/helper_bot/middlewares/rate_limit_middleware.py b/helper_bot/middlewares/rate_limit_middleware.py index 4e8be94..c50ef88 100644 --- a/helper_bot/middlewares/rate_limit_middleware.py +++ b/helper_bot/middlewares/rate_limit_middleware.py @@ -6,8 +6,7 @@ from typing import Any, Awaitable, Callable, Dict, Union from aiogram import BaseMiddleware from aiogram.exceptions import TelegramAPIError, TelegramRetryAfter -from aiogram.types import (CallbackQuery, ChatMemberUpdated, InlineQuery, - Message, Update) +from aiogram.types import CallbackQuery, ChatMemberUpdated, InlineQuery, Message, Update from helper_bot.utils.rate_limiter import telegram_rate_limiter from logs.custom_logger import logger diff --git a/helper_bot/utils/helper_func.py b/helper_bot/utils/helper_func.py index 4e5dc4c..dac4040 100644 --- a/helper_bot/utils/helper_func.py +++ b/helper_bot/utils/helper_func.py @@ -17,17 +17,29 @@ except ImportError: _emoji_lib_available = False from aiogram import types -from aiogram.types import (FSInputFile, InputMediaAudio, InputMediaDocument, - InputMediaPhoto, InputMediaVideo) +from aiogram.types import ( + FSInputFile, + InputMediaAudio, + InputMediaDocument, + InputMediaPhoto, + InputMediaVideo, +) from database.models import TelegramPost -from helper_bot.utils.base_dependency_factory import (BaseDependencyFactory, - get_global_instance) +from helper_bot.utils.base_dependency_factory import ( + BaseDependencyFactory, + get_global_instance, +) from logs.custom_logger import logger # Local imports - metrics -from .metrics import (db_query_time, track_errors, track_file_operations, - track_media_processing, track_time) +from .metrics import ( + db_query_time, + track_errors, + track_file_operations, + track_media_processing, + track_time, +) bdf = get_global_instance() # TODO: поменять архитектуру и подключить правильный BotDB diff --git a/helper_bot/utils/metrics.py b/helper_bot/utils/metrics.py index 18c2f42..b416b8c 100644 --- a/helper_bot/utils/metrics.py +++ b/helper_bot/utils/metrics.py @@ -10,8 +10,13 @@ from contextlib import asynccontextmanager from functools import wraps from typing import Any, Dict, Optional -from prometheus_client import (CONTENT_TYPE_LATEST, Counter, Gauge, Histogram, - generate_latest) +from prometheus_client import ( + CONTENT_TYPE_LATEST, + Counter, + Gauge, + Histogram, + generate_latest, +) from prometheus_client.core import CollectorRegistry # Метрики rate limiter теперь создаются в основном классе diff --git a/helper_bot/utils/rate_limiter.py b/helper_bot/utils/rate_limiter.py index f80c963..78d891f 100644 --- a/helper_bot/utils/rate_limiter.py +++ b/helper_bot/utils/rate_limiter.py @@ -188,8 +188,7 @@ class TelegramRateLimiter: # Глобальный экземпляр rate limiter -from helper_bot.config.rate_limit_config import (RateLimitSettings, - get_rate_limit_config) +from helper_bot.config.rate_limit_config import RateLimitSettings, get_rate_limit_config def _create_rate_limit_config(settings: RateLimitSettings) -> RateLimitConfig: diff --git a/run_helper.py b/run_helper.py index 98222fa..8715db1 100644 --- a/run_helper.py +++ b/run_helper.py @@ -68,8 +68,7 @@ async def main(): # Останавливаем планировщик метрик try: - from helper_bot.utils.metrics_scheduler import \ - stop_metrics_scheduler + from helper_bot.utils.metrics_scheduler import stop_metrics_scheduler stop_metrics_scheduler() logger.info("Планировщик метрик остановлен") diff --git a/tests/test_audio_file_service.py b/tests/test_audio_file_service.py index a4441f5..7d298ed 100644 --- a/tests/test_audio_file_service.py +++ b/tests/test_audio_file_service.py @@ -4,8 +4,7 @@ from unittest.mock import AsyncMock, MagicMock, Mock, mock_open, patch import pytest -from helper_bot.handlers.voice.exceptions import (DatabaseError, - FileOperationError) +from helper_bot.handlers.voice.exceptions import DatabaseError, FileOperationError from helper_bot.handlers.voice.services import AudioFileService diff --git a/tests/test_auto_unban_scheduler.py b/tests/test_auto_unban_scheduler.py index df5a9dd..7f64bef 100644 --- a/tests/test_auto_unban_scheduler.py +++ b/tests/test_auto_unban_scheduler.py @@ -4,8 +4,10 @@ from unittest.mock import AsyncMock, Mock, patch import pytest -from helper_bot.utils.auto_unban_scheduler import (AutoUnbanScheduler, - get_auto_unban_scheduler) +from helper_bot.utils.auto_unban_scheduler import ( + AutoUnbanScheduler, + get_auto_unban_scheduler, +) class TestAutoUnbanScheduler: diff --git a/tests/test_blacklist_history_repository.py b/tests/test_blacklist_history_repository.py index da43184..828a222 100644 --- a/tests/test_blacklist_history_repository.py +++ b/tests/test_blacklist_history_repository.py @@ -5,8 +5,9 @@ from unittest.mock import AsyncMock, Mock, patch import pytest from database.models import BlacklistHistoryRecord -from database.repositories.blacklist_history_repository import \ - BlacklistHistoryRepository +from database.repositories.blacklist_history_repository import ( + BlacklistHistoryRepository, +) class TestBlacklistHistoryRepository: diff --git a/tests/test_callback_handlers.py b/tests/test_callback_handlers.py index 8c59703..2cf6125 100644 --- a/tests/test_callback_handlers.py +++ b/tests/test_callback_handlers.py @@ -5,7 +5,9 @@ from unittest.mock import AsyncMock, MagicMock, Mock, patch import pytest from helper_bot.handlers.callback.callback_handlers import ( - delete_voice_message, save_voice_message) + delete_voice_message, + save_voice_message, +) from helper_bot.handlers.voice.constants import CALLBACK_DELETE, CALLBACK_SAVE diff --git a/tests/test_improved_media_processing.py b/tests/test_improved_media_processing.py index 46caa80..05d78e7 100644 --- a/tests/test_improved_media_processing.py +++ b/tests/test_improved_media_processing.py @@ -10,8 +10,11 @@ import pytest from aiogram import types from helper_bot.utils.helper_func import ( - add_in_db_media, add_in_db_media_mediagroup, download_file, - send_media_group_message_to_private_chat) + add_in_db_media, + add_in_db_media_mediagroup, + download_file, + send_media_group_message_to_private_chat, +) class TestDownloadFile: diff --git a/tests/test_keyboards_and_filters.py b/tests/test_keyboards_and_filters.py index 0068702..cb25041 100644 --- a/tests/test_keyboards_and_filters.py +++ b/tests/test_keyboards_and_filters.py @@ -1,16 +1,22 @@ from unittest.mock import AsyncMock, Mock, patch import pytest -from aiogram.types import (InlineKeyboardButton, InlineKeyboardMarkup, - KeyboardButton, ReplyKeyboardMarkup) +from aiogram.types import ( + InlineKeyboardButton, + InlineKeyboardMarkup, + KeyboardButton, + ReplyKeyboardMarkup, +) from database.async_db import AsyncBotDB from helper_bot.filters.main import ChatTypeFilter -from helper_bot.keyboards.keyboards import (create_keyboard_with_pagination, - get_reply_keyboard, - get_reply_keyboard_admin, - get_reply_keyboard_for_post, - get_reply_keyboard_leave_chat) +from helper_bot.keyboards.keyboards import ( + create_keyboard_with_pagination, + get_reply_keyboard, + get_reply_keyboard_admin, + get_reply_keyboard_for_post, + get_reply_keyboard_leave_chat, +) class TestKeyboards: diff --git a/tests/test_rate_limiter.py b/tests/test_rate_limiter.py index b6ea61f..ed3e7bd 100644 --- a/tests/test_rate_limiter.py +++ b/tests/test_rate_limiter.py @@ -8,15 +8,20 @@ from unittest.mock import AsyncMock, MagicMock, patch import pytest -from helper_bot.config.rate_limit_config import (RateLimitSettings, - get_rate_limit_config) -from helper_bot.utils.rate_limit_monitor import (RateLimitMonitor, - RateLimitStats, - record_rate_limit_request) -from helper_bot.utils.rate_limiter import (ChatRateLimiter, GlobalRateLimiter, - RateLimitConfig, RetryHandler, - TelegramRateLimiter, - send_with_rate_limit) +from helper_bot.config.rate_limit_config import RateLimitSettings, get_rate_limit_config +from helper_bot.utils.rate_limit_monitor import ( + RateLimitMonitor, + RateLimitStats, + record_rate_limit_request, +) +from helper_bot.utils.rate_limiter import ( + ChatRateLimiter, + GlobalRateLimiter, + RateLimitConfig, + RetryHandler, + TelegramRateLimiter, + send_with_rate_limit, +) class TestRateLimitConfig: diff --git a/tests/test_refactored_admin_handlers.py b/tests/test_refactored_admin_handlers.py index 1f77055..0ed8a43 100644 --- a/tests/test_refactored_admin_handlers.py +++ b/tests/test_refactored_admin_handlers.py @@ -4,9 +4,11 @@ import pytest from aiogram import types from aiogram.fsm.context import FSMContext -from helper_bot.handlers.admin.exceptions import (InvalidInputError, - UserAlreadyBannedError, - UserNotFoundError) +from helper_bot.handlers.admin.exceptions import ( + InvalidInputError, + UserAlreadyBannedError, + UserNotFoundError, +) from helper_bot.handlers.admin.services import AdminService, BannedUser, User diff --git a/tests/test_refactored_group_handlers.py b/tests/test_refactored_group_handlers.py index bee38fb..464c95f 100644 --- a/tests/test_refactored_group_handlers.py +++ b/tests/test_refactored_group_handlers.py @@ -7,10 +7,14 @@ from aiogram import types from aiogram.fsm.context import FSMContext from helper_bot.handlers.group.constants import ERROR_MESSAGES, FSM_STATES -from helper_bot.handlers.group.exceptions import (NoReplyToMessageError, - UserNotFoundError) -from helper_bot.handlers.group.group_handlers import (GroupHandlers, - create_group_handlers) +from helper_bot.handlers.group.exceptions import ( + NoReplyToMessageError, + UserNotFoundError, +) +from helper_bot.handlers.group.group_handlers import ( + GroupHandlers, + create_group_handlers, +) from helper_bot.handlers.group.services import AdminReplyService diff --git a/tests/test_refactored_private_handlers.py b/tests/test_refactored_private_handlers.py index b9d35c3..abfca8d 100644 --- a/tests/test_refactored_private_handlers.py +++ b/tests/test_refactored_private_handlers.py @@ -8,7 +8,9 @@ from aiogram.fsm.context import FSMContext from helper_bot.handlers.private.constants import BUTTON_TEXTS, FSM_STATES from helper_bot.handlers.private.private_handlers import ( - PrivateHandlers, create_private_handlers) + PrivateHandlers, + create_private_handlers, +) from helper_bot.handlers.private.services import BotSettings diff --git a/tests/test_utils.py b/tests/test_utils.py index fe89ab0..d511122 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -6,18 +6,37 @@ import pytest import helper_bot.utils.messages as messages # Import for patching constants from database.async_db import AsyncBotDB -from helper_bot.utils.base_dependency_factory import (BaseDependencyFactory, - get_global_instance) +from helper_bot.utils.base_dependency_factory import ( + BaseDependencyFactory, + get_global_instance, +) from helper_bot.utils.helper_func import ( - add_days_to_date, add_in_db_media, add_in_db_media_mediagroup, - check_access, check_user_emoji, check_username_and_full_name, - delete_user_blacklist, determine_anonymity, download_file, - get_banned_users_buttons, get_banned_users_list, get_first_name, - get_random_emoji, get_text_message, prepare_media_group_from_middlewares, - safe_html_escape, send_audio_message, - send_media_group_message_to_private_chat, send_media_group_to_channel, - send_photo_message, send_text_message, send_video_message, - send_video_note_message, send_voice_message, update_user_info) + add_days_to_date, + add_in_db_media, + add_in_db_media_mediagroup, + check_access, + check_user_emoji, + check_username_and_full_name, + delete_user_blacklist, + determine_anonymity, + download_file, + get_banned_users_buttons, + get_banned_users_list, + get_first_name, + get_random_emoji, + get_text_message, + prepare_media_group_from_middlewares, + safe_html_escape, + send_audio_message, + send_media_group_message_to_private_chat, + send_media_group_to_channel, + send_photo_message, + send_text_message, + send_video_message, + send_video_note_message, + send_voice_message, + update_user_info, +) from helper_bot.utils.messages import get_message diff --git a/tests/test_voice_bot_architecture.py b/tests/test_voice_bot_architecture.py index e956b46..9f7b7bc 100644 --- a/tests/test_voice_bot_architecture.py +++ b/tests/test_voice_bot_architecture.py @@ -4,12 +4,13 @@ from unittest.mock import AsyncMock, Mock, patch import pytest -from helper_bot.handlers.voice.exceptions import (AudioProcessingError, - VoiceMessageError) +from helper_bot.handlers.voice.exceptions import AudioProcessingError, VoiceMessageError from helper_bot.handlers.voice.services import VoiceBotService -from helper_bot.handlers.voice.utils import (get_last_message_text, - get_user_emoji_safe, - validate_voice_message) +from helper_bot.handlers.voice.utils import ( + get_last_message_text, + get_user_emoji_safe, + validate_voice_message, +) class TestVoiceBotService: diff --git a/tests/test_voice_constants.py b/tests/test_voice_constants.py index 744b2d7..b6774ef 100644 --- a/tests/test_voice_constants.py +++ b/tests/test_voice_constants.py @@ -1,15 +1,22 @@ import pytest -from helper_bot.handlers.voice.constants import (BTN_LISTEN, BTN_SPEAK, - BUTTON_COMMAND_MAPPING, - CALLBACK_COMMAND_MAPPING, - CALLBACK_DELETE, - CALLBACK_SAVE, CMD_EMOJI, - CMD_HELP, CMD_REFRESH, - CMD_RESTART, CMD_START, - COMMAND_MAPPING, - STATE_STANDUP_WRITE, - STATE_START, VOICE_BOT_NAME) +from helper_bot.handlers.voice.constants import ( + BTN_LISTEN, + BTN_SPEAK, + BUTTON_COMMAND_MAPPING, + CALLBACK_COMMAND_MAPPING, + CALLBACK_DELETE, + CALLBACK_SAVE, + CMD_EMOJI, + CMD_HELP, + CMD_REFRESH, + CMD_RESTART, + CMD_START, + COMMAND_MAPPING, + STATE_STANDUP_WRITE, + STATE_START, + VOICE_BOT_NAME, +) class TestVoiceConstants: diff --git a/tests/test_voice_exceptions.py b/tests/test_voice_exceptions.py index 5dd4f7f..38d8150 100644 --- a/tests/test_voice_exceptions.py +++ b/tests/test_voice_exceptions.py @@ -1,8 +1,10 @@ import pytest -from helper_bot.handlers.voice.exceptions import (AudioProcessingError, - VoiceBotError, - VoiceMessageError) +from helper_bot.handlers.voice.exceptions import ( + AudioProcessingError, + VoiceBotError, + VoiceMessageError, +) class TestVoiceExceptions: diff --git a/tests/test_voice_handler.py b/tests/test_voice_handler.py index e5f9914..beaf8eb 100644 --- a/tests/test_voice_handler.py +++ b/tests/test_voice_handler.py @@ -4,8 +4,7 @@ import pytest from aiogram import types from aiogram.fsm.context import FSMContext -from helper_bot.handlers.voice.constants import (STATE_STANDUP_WRITE, - STATE_START) +from helper_bot.handlers.voice.constants import STATE_STANDUP_WRITE, STATE_START from helper_bot.handlers.voice.voice_handler import VoiceHandlers diff --git a/tests/test_voice_services.py b/tests/test_voice_services.py index 7301806..dfd37fd 100644 --- a/tests/test_voice_services.py +++ b/tests/test_voice_services.py @@ -4,8 +4,7 @@ from unittest.mock import AsyncMock, MagicMock, Mock, patch import pytest -from helper_bot.handlers.voice.exceptions import (AudioProcessingError, - VoiceMessageError) +from helper_bot.handlers.voice.exceptions import AudioProcessingError, VoiceMessageError from helper_bot.handlers.voice.services import VoiceBotService diff --git a/tests/test_voice_utils.py b/tests/test_voice_utils.py index 8ab862c..50de559 100644 --- a/tests/test_voice_utils.py +++ b/tests/test_voice_utils.py @@ -4,10 +4,13 @@ from unittest.mock import Mock, patch import pytest from aiogram import types -from helper_bot.handlers.voice.utils import (format_time_ago, - get_last_message_text, - get_user_emoji_safe, plural_time, - validate_voice_message) +from helper_bot.handlers.voice.utils import ( + format_time_ago, + get_last_message_text, + get_user_emoji_safe, + plural_time, + validate_voice_message, +) class TestVoiceUtils: