Skip to content

Commit

Permalink
refactoring bot main for composite in future
Browse files Browse the repository at this point in the history
  • Loading branch information
bomzheg committed Oct 28, 2023
1 parent 5c305db commit fef7bc3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 45 deletions.
6 changes: 6 additions & 0 deletions shvatka/core/interfaces/scheduler/sheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ async def plain_hint(
async def cancel_scheduled_game(self, game: dto.Game):
raise NotImplementedError

async def __aenter__(self):
raise NotImplementedError

async def __aexit__(self, exc_type, exc_val, exc_tb):
raise NotImplementedError


class LevelTestScheduler(Protocol):
async def plain_test_hint(
Expand Down
47 changes: 3 additions & 44 deletions shvatka/tgbot/__main__.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
import asyncio
import logging

from aiogram_dialog.manager.message_manager import MessageManager

from shvatka.common.config.parser.logging_config import setup_logging
from shvatka.common.factory import create_telegraph, create_dataclass_factory
from shvatka.infrastructure.clients.factory import create_file_storage
from shvatka.infrastructure.db.factory import (
create_lock_factory,
create_level_test_dao,
create_session_maker,
create_engine,
)
from shvatka.infrastructure.scheduler.factory import create_scheduler
from shvatka.tgbot.config.parser.main import load_config
from shvatka.tgbot.main_factory import (
create_bot,
create_dispatcher,
get_paths,
create_redis,
prepare_dp_full,
)
from shvatka.tgbot.username_resolver.user_getter import UserGetter
from shvatka.tgbot.utils.router import print_router_tree
from shvatka.tgbot.views.jinja_filters import setup_jinja

logger = logging.getLogger(__name__)

Expand All @@ -32,42 +22,11 @@ async def main():

setup_logging(paths)
config = load_config(paths)
dcf = create_dataclass_factory()
file_storage = create_file_storage(config.file_storage_config)
engine = create_engine(config.db)
pool = create_session_maker(engine)
bot = create_bot(config)
setup_jinja(bot=bot)
level_test_dao = create_level_test_dao()

async with (
UserGetter(config.tg_client) as user_getter,
create_redis(config.redis) as redis,
create_scheduler(
pool=pool,
redis=redis,
bot=bot,
redis_config=config.redis,
game_log_chat=config.bot.log_chat,
file_storage=file_storage,
level_test_dao=level_test_dao,
) as scheduler,
bot.context(),
):
dp = create_dispatcher(
config=config,
user_getter=user_getter,
dcf=dcf,
pool=pool,
redis=redis,
scheduler=scheduler,
locker=create_lock_factory(),
file_storage=file_storage,
level_test_dao=level_test_dao,
telegraph=create_telegraph(config.bot),
message_manager=MessageManager(),
)
file_storage = create_file_storage(config.file_storage_config)

async with prepare_dp_full(config, pool, file_storage) as (bot, dp):
logger.info("started with configured routers \n%s", print_router_tree(dp))
try:
await dp.start_polling(
Expand Down
48 changes: 47 additions & 1 deletion shvatka/tgbot/main_factory.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from contextlib import asynccontextmanager

from aiogram import Bot, Dispatcher
from aiogram.enums import ParseMode
Expand All @@ -10,23 +11,68 @@
from redis.asyncio.client import Redis
from sqlalchemy.ext.asyncio import async_sessionmaker, AsyncSession

from shvatka.common import create_dataclass_factory, create_telegraph
from shvatka.common.config.models.paths import Paths
from shvatka.common.config.parser.paths import common_get_paths
from shvatka.core.interfaces.clients.file_storage import FileStorage
from shvatka.core.interfaces.scheduler import Scheduler
from shvatka.core.utils.key_checker_lock import KeyCheckerFactory
from shvatka.infrastructure.db.config.models.storage import StorageConfig, StorageType
from shvatka.infrastructure.db.dao.memory.level_testing import LevelTestingData
from shvatka.infrastructure.db.factory import create_redis
from shvatka.infrastructure.db.factory import (
create_redis,
create_level_test_dao,
create_lock_factory,
)
from shvatka.infrastructure.scheduler.factory import create_scheduler
from shvatka.tgbot.config.models.main import TgBotConfig
from shvatka.tgbot.handlers import setup_handlers
from shvatka.tgbot.middlewares import setup_middlewares
from shvatka.tgbot.username_resolver.user_getter import UserGetter
from shvatka.tgbot.views.jinja_filters import setup_jinja
from shvatka.tgbot.views.telegraph import Telegraph

logger = logging.getLogger(__name__)


@asynccontextmanager
async def prepare_dp_full(
config: TgBotConfig, pool: async_sessionmaker[AsyncSession], file_storage: FileStorage
):
dcf = create_dataclass_factory()
bot = create_bot(config)
setup_jinja(bot=bot)
level_test_dao = create_level_test_dao()

async with (
UserGetter(config.tg_client) as user_getter,
create_redis(config.redis) as redis,
create_scheduler(
pool=pool,
redis=redis,
bot=bot,
redis_config=config.redis,
game_log_chat=config.bot.log_chat,
file_storage=file_storage,
level_test_dao=level_test_dao,
) as scheduler,
bot.context(),
):
yield bot, create_dispatcher(
config=config,
user_getter=user_getter,
dcf=dcf,
pool=pool,
redis=redis,
scheduler=scheduler,
locker=create_lock_factory(),
file_storage=file_storage,
level_test_dao=level_test_dao,
telegraph=create_telegraph(config.bot),
message_manager=MessageManager(),
)


def create_bot(config: TgBotConfig) -> Bot:
return Bot(
token=config.bot.token,
Expand Down

0 comments on commit fef7bc3

Please sign in to comment.