From 837613e072327e8151cdb75391ad8b69792bc241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=A1=D0=BB=D0=B8?= =?UTF-8?q?=D0=BF=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sun, 7 Jul 2024 21:25:10 +0300 Subject: [PATCH] Merged with chore/refactor-core --- .gitignore | 3 ++- src/core/config.yaml | 13 ------------ src/core/database/.gitkeep | 0 src/core/logger.py | 43 +++++++++++++++++++++----------------- src/core/main.py | 23 +++++++++++++------- src/core/routers.py | 4 +--- 6 files changed, 42 insertions(+), 44 deletions(-) delete mode 100644 src/core/config.yaml create mode 100644 src/core/database/.gitkeep diff --git a/.gitignore b/.gitignore index cac6286..c7200b5 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ venv __pycache__ OCAB.db src/paths.json -src/core/config.yaml \ No newline at end of file +src/core/config.yaml +src/core/log/**/* diff --git a/src/core/config.yaml b/src/core/config.yaml deleted file mode 100644 index 7e138bd..0000000 --- a/src/core/config.yaml +++ /dev/null @@ -1,13 +0,0 @@ -TELEGRAM: - TOKEN: - -YANDEXGPT: - TOKEN: - CATALOGID: - PROMPT: - -ROLES: - ADMIN: 0 - MODERATOR: 1 - USER: 2 - BOT: 3 \ No newline at end of file diff --git a/src/core/database/.gitkeep b/src/core/database/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/core/logger.py b/src/core/logger.py index 4b190eb..3fef59c 100644 --- a/src/core/logger.py +++ b/src/core/logger.py @@ -1,24 +1,29 @@ +import logging import os import time -async def check_log_file(): - # Проверка наличия файла для логов в формате log-dd-mm-yyyy.log - # Если файл существует, то pass - # Если файл не существует, то создаём его. файл лежит в директории src.core.log - current_data = time.strftime("%d-%m-%Y") - log_file = os.path.join(os.path.dirname(__file__), "log/", f"log-{current_data}.log") - if not os.path.exists(log_file): - with open(log_file, 'w') as file: - file.write("Log file created\n") - file.close() - else: - pass + +def setup_logger(): + """ + Настройка логирования + """ + current_date = time.strftime("%d-%m-%Y") + log_dir = os.path.join(os.path.dirname(__file__), "log") + os.makedirs(log_dir, exist_ok=True) + log_file = os.path.join(log_dir, f"log-{current_date}.log") + + logging.basicConfig( + filename=log_file, + level=logging.INFO, + format="%(asctime)s %(message)s", + datefmt="%H:%M:%S" + ) + async def log(message): - await check_log_file() - current_data = time.strftime("%d-%m-%Y") - log_file = os.path.join(os.path.dirname(__file__), "log/", f"log-{current_data}.log") - # print(log_file) - with open(log_file, 'a') as file: - file.write(f"{time.strftime('%H:%M:%S')} {message}\n") - file.close() + """ + Функция для логирования сообщений + + Она асинхронная, хотя таковой на самом деле не является. + """ + logging.info(message) diff --git a/src/core/main.py b/src/core/main.py index 610bbee..5de2aa0 100644 --- a/src/core/main.py +++ b/src/core/main.py @@ -1,13 +1,18 @@ from routers import include_routers +from src.core.logger import log, setup_logger from src.modules.standard.config.config import get_telegram_token from src.modules.standard.database.db_api import connect_database, create_tables - -from asyncio import run +import asyncio from aiogram import Bot, Dispatcher -async def main(bot: Bot): +async def main(): + bot = None + database = None + setup_logger() + try: + bot = Bot(token=get_telegram_token()) database, path = connect_database() database.connect() create_tables(database) @@ -15,12 +20,14 @@ async def main(bot: Bot): dp = Dispatcher() await include_routers(dp) await dp.start_polling(bot) - + except Exception as e: + log(e) finally: - await bot.session.close() - database.close() + if bot is not None: + await bot.session.close() + if database is not None: + database.close() if __name__ == "__main__": - bot = Bot(token=get_telegram_token()) - run(main(bot)) + asyncio.run(main()) diff --git a/src/core/routers.py b/src/core/routers.py index 2957e57..4c10732 100644 --- a/src/core/routers.py +++ b/src/core/routers.py @@ -1,10 +1,8 @@ -from aiogram import Dispatcher, Router, F, Bot -from aiogram.types import Message +from aiogram import Dispatcher from src.modules.standard.info.routers import router as info_router from src.modules.standard.admin.routers import router as admin_router from src.modules.standard.message_processing.message_api import router as process_message -from src.modules.standard.welcome.routers import router as welcome_router async def include_routers(dp: Dispatcher):