Skip to content

Commit

Permalink
Add log to file and fix several annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Era-Dorta committed Oct 7, 2024
1 parent 6d3210a commit c50fc47
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/signalblast/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ async def create(cls, admin_password: str | None) -> None:
await self.set_hashed_password(admin_password)
return self

def get_hashed_password(self):
def get_hashed_password(self) -> str:
return self._hashed_password

async def set_hashed_password(self, password: str):
async def set_hashed_password(self, password: str) -> None:
if password is None:
self._hashed_password = b""
else:
Expand Down
8 changes: 4 additions & 4 deletions src/signalblast/broadcastbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BroadcasBot:
subscribers_data_path = get_code_data_path() / "subscribers.csv"
banned_users_data_path = get_code_data_path() / "banned_users.csv"

def __init__(self, config: dict):
def __init__(self, config: dict) -> None:
self._bot = SignalBot(config)
self.ping_job: Job | None = None

Expand Down Expand Up @@ -70,10 +70,10 @@ def register(
contacts: list[str] | bool | None = True, # noqa: FBT002
groups: list[str] | bool | None = False, # noqa: FBT002
f: Callable[[Message], bool] | None = None,
):
) -> None:
self._bot.register(command=command, contacts=contacts, groups=groups, f=f)

def start(self):
def start(self) -> None:
self._bot.start()

@property
Expand All @@ -87,7 +87,7 @@ async def load_data(
expiration_time: int | None,
signal_data_path: Path,
welcome_message: str | None = None,
):
) -> None:
self.subscribers = await Users.load_from_file(self.subscribers_data_path)
self.banned_users = await Users.load_from_file(self.banned_users_data_path)

Expand Down
19 changes: 15 additions & 4 deletions src/signalblast/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@
UnsetPing,
Unsubscribe,
)
from signalblast.utils import get_code_data_path
from signalblast.utils import get_code_data_path, get_logger

logging.getLogger().setLevel(logging.WARNING)
logging.getLogger("apscheduler").setLevel(logging.WARNING)
LOGGING_LEVEL = logging.WARNING
LOG_TO_FILE = False
if LOG_TO_FILE:
get_logger(None, Path("signalbot.log"), LOGGING_LEVEL)
else:
logging.getLogger().setLevel(LOGGING_LEVEL)

logging.getLogger("apscheduler").setLevel(LOGGING_LEVEL)


async def initialise_bot( # noqa: PLR0913 Too many arguments in function definition
Expand All @@ -43,10 +49,15 @@ async def initialise_bot( # noqa: PLR0913 Too many arguments in function defini
}

get_code_data_path().mkdir(parents=True, exist_ok=True)
if LOG_TO_FILE:
logger = get_logger("signalblast", Path("signalblast.log"), LOGGING_LEVEL)
else:
logger = logging.getLogger("signalblast")
logger.setLevel(LOGGING_LEVEL)

bot = BroadcasBot(config)
await bot.load_data(
logger=logging.getLogger("signalblast"),
logger=logger,
admin_pass=admin_pass,
expiration_time=expiration_time,
signal_data_path=signal_data_path,
Expand Down
21 changes: 11 additions & 10 deletions src/signalblast/utils.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import functools
import pathlib
from collections.abc import Callable
from logging import Formatter, Logger, getLogger
from logging.handlers import RotatingFileHandler
from pathlib import Path
from re import Pattern
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from signalbot import Context as ChatContext


def _get_rotating_handler(log_file: str) -> RotatingFileHandler:
return RotatingFileHandler(log_file, mode="a", maxBytes=5 * 1024 * 1024, backupCount=1, encoding=None, delay=0)
def _get_rotating_handler(log_file: Path) -> RotatingFileHandler:
return RotatingFileHandler(str(log_file), mode="a", maxBytes=5 * 1024 * 1024, backupCount=1, encoding=None, delay=0)


def get_logger(log_file: str, logging_level) -> Logger:
def get_logger(name: str | None, log_file: Path, logging_level: int) -> Logger:
handler = _get_rotating_handler(log_file)
formatter = Formatter("%(asctime)s %(name)s [%(levelname)s] - %(funcName)s - %(message)s")
handler.setLevel(logging_level)
handler.setFormatter(formatter)
log = getLogger("signalblast")
log = getLogger(name)
log.setLevel(logging_level)
log.addHandler(handler)
return log

Expand All @@ -31,12 +32,12 @@ def redirect_semaphore_logger(log_file: str) -> None:
semaphore_log.addHandler(handler)


def get_code_data_path():
return pathlib.Path(__file__).parent.absolute() / "data"
def get_code_data_path() -> Path:
return Path(__file__).parent.absolute() / "data"


def triggered(pattern: Pattern):
def decorator_triggered(func):
def triggered(pattern: Pattern) -> Callable:
def decorator_triggered(func: Callable) -> Callable:
@functools.wraps(func)
async def wrapper_triggered(*args, **kwargs):
c: ChatContext = args[1]
Expand Down

0 comments on commit c50fc47

Please sign in to comment.