generated from geekiot/python-template
feature/docs - add project docstrings & todos & comments #6
6 changed files with 61 additions and 12 deletions
|
|
@ -1 +1,4 @@
|
||||||
__all__ = []
|
__all__ = []
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: Add automatic import all modules from this folder.
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
__all__ = []
|
__all__ = []
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: Add automatic import all modules from this folder.
|
||||||
|
|
||||||
from aiogram.types import Message
|
from aiogram.types import Message
|
||||||
|
|
||||||
from bot.handlers import registry
|
from bot.handlers import registry
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,42 @@ from pydantic import Field
|
||||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||||
|
|
||||||
|
|
||||||
|
# Constant paths for project.
|
||||||
|
LOG_DIR = Path("logs")
|
||||||
|
LOG_DIR.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
# Log format for loguru logger.
|
||||||
|
LOG_FORMAT = (
|
||||||
|
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
|
||||||
|
"<level>{level:<8}</level> | "
|
||||||
|
"<cyan>{process}</cyan>.<cyan>{thread.name}</cyan> | "
|
||||||
|
"<magenta>{module}.{function}:{line}</magenta> – "
|
||||||
|
"<level>{message}</level>"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class Settings(BaseSettings):
|
class Settings(BaseSettings):
|
||||||
|
"""
|
||||||
|
Class to specify env settings for the project.
|
||||||
|
|
||||||
|
The .env file is located in the project root.
|
||||||
|
|
||||||
|
Env Vars:
|
||||||
|
bot_token (str):
|
||||||
|
Telegram API bot token.
|
||||||
|
listen_logging (bool):
|
||||||
|
intercepting logs from logging.
|
||||||
|
Defaults to False.
|
||||||
|
file_log_level (Literal):
|
||||||
|
logging level for the .log file.
|
||||||
|
"DEBUG", "INFO", "WARNING" or "ERROR".
|
||||||
|
Defaults to "DEBUG".
|
||||||
|
console_log_level (Literal):
|
||||||
|
logging level for the console.
|
||||||
|
"DEBUG", "INFO", "WARNING" or "ERROR".
|
||||||
|
Defaults to "INFO".
|
||||||
|
"""
|
||||||
|
|
||||||
bot_token: str = Field(frozen=True)
|
bot_token: str = Field(frozen=True)
|
||||||
|
|
||||||
listen_logging: bool = Field(
|
listen_logging: bool = Field(
|
||||||
|
|
@ -43,20 +78,18 @@ class Settings(BaseSettings):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
LOG_DIR = Path("logs")
|
|
||||||
LOG_DIR.mkdir(parents=True, exist_ok=True)
|
|
||||||
|
|
||||||
LOG_FORMAT = (
|
|
||||||
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
|
|
||||||
"<level>{level:<8}</level> | "
|
|
||||||
"<cyan>{process}</cyan>.<cyan>{thread.name}</cyan> | "
|
|
||||||
"<magenta>{module}.{function}:{line}</magenta> – "
|
|
||||||
"<level>{message}</level>"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class InterceptHandler(logging.Handler):
|
class InterceptHandler(logging.Handler):
|
||||||
|
"""
|
||||||
|
Class for intercepting logs from logging, including Aiogram.
|
||||||
|
"""
|
||||||
|
|
||||||
def emit(self, record: logging.LogRecord) -> None:
|
def emit(self, record: logging.LogRecord) -> None:
|
||||||
|
"""
|
||||||
|
Try to intercepting logs from logging.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
record (logging.LogRecord): record from logging.
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
level = loguru_logger.level(record.levelname).name
|
level = loguru_logger.level(record.levelname).name
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
|
@ -82,6 +115,14 @@ def configure_logger(
|
||||||
listen_logging: bool,
|
listen_logging: bool,
|
||||||
console_log_level: str,
|
console_log_level: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
"""
|
||||||
|
Configure loguru logger.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
file_log_level (str): logging level for the .log file.
|
||||||
|
listen_logging (bool): intercepting logs from logging.
|
||||||
|
console_log_level (str): logging level for the console.
|
||||||
|
"""
|
||||||
if listen_logging:
|
if listen_logging:
|
||||||
logging.root.handlers.clear()
|
logging.root.handlers.clear()
|
||||||
logging.root.setLevel(logging.DEBUG)
|
logging.root.setLevel(logging.DEBUG)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
# TODO: Add database models.
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
# TODO: Add database session.
|
||||||
session: AsyncSession = ...
|
session: AsyncSession = ...
|
||||||
|
|
|
||||||
|
|
@ -3,4 +3,5 @@ __all__ = ["client"]
|
||||||
|
|
||||||
from redis.asyncio.client import Redis
|
from redis.asyncio.client import Redis
|
||||||
|
|
||||||
|
# TODO: Add redis client.
|
||||||
client: Redis = ...
|
client: Redis = ...
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue