Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
feat: add logger config
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinNitroG committed Dec 23, 2024
1 parent f42286a commit 8bbfe77
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 14 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@
{
"plate": "30F88251"
}
],
"notify": [
{
"telegram": {
"bot_token": "2780473231:weiruAShGUUx4oLOMoUhd0GiREXSZcCq-uB",
"chat_id": "-1001790012349"
}
}
]
}
2 changes: 1 addition & 1 deletion schemas/config.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"$defs": {"PlateInfo": {"properties": {"plate": {"title": "Plate", "type": "string"}, "owner": {"anyOf": [{"type": "string"}, {"type": "null"}], "default": null, "title": "Owner"}}, "required": ["plate"], "title": "PlateInfo", "type": "object"}}, "properties": {"data": {"items": {"$ref": "#/$defs/PlateInfo"}, "title": "Data", "type": "array"}}, "required": ["data"], "title": "Config", "type": "object"}
{"$defs": {"LogLevel": {"enum": ["NOTSET", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], "title": "LogLevel", "type": "string"}, "PlateInfo": {"properties": {"plate": {"title": "Plate", "type": "string"}, "owner": {"anyOf": [{"type": "string"}, {"type": "null"}], "default": null, "title": "Owner"}}, "required": ["plate"], "title": "PlateInfo", "type": "object"}, "Telegram": {"properties": {"bot_token": {"title": "Bot Token", "type": "string"}, "chat_id": {"title": "Chat Id", "type": "string"}}, "required": ["bot_token", "chat_id"], "title": "Telegram", "type": "object"}, "TelegramNotify": {"properties": {"telegram": {"$ref": "#/$defs/Telegram"}}, "required": ["telegram"], "title": "TelegramNotify", "type": "object"}}, "properties": {"data": {"default": [], "items": {"$ref": "#/$defs/PlateInfo"}, "title": "Data", "type": "array"}, "notify": {"default": [], "items": {"$ref": "#/$defs/TelegramNotify"}, "title": "Notify", "type": "array"}, "log_level": {"$ref": "#/$defs/LogLevel", "default": "WARNING"}}, "title": "Config", "type": "object"}
11 changes: 8 additions & 3 deletions src/check_phat_nguoi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from check_phat_nguoi.modules.config_reader import config_reader
from check_phat_nguoi.modules.config_reader import config
from check_phat_nguoi.modules.logger import setup_logger


def main():
print(config_reader("config.json"))
def main() -> None:
setup_logger()
print(config)


__all__ = ["main"]
2 changes: 2 additions & 0 deletions src/check_phat_nguoi/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

if __name__ == "__main__":
main()

__all__ = []
13 changes: 10 additions & 3 deletions src/check_phat_nguoi/models/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict

from check_phat_nguoi.models.log_level import LogLevel
from check_phat_nguoi.models.notify.telegram_notify import TelegramNotify
from check_phat_nguoi.models.plate_info import PlateInfo
from check_phat_nguoi.models.telegram import Telegram


class Config(BaseModel):
model_config = ConfigDict(use_enum_values=True, validate_default=True)

data: list[PlateInfo] = []
notify: list[Telegram] = []
notify: list[TelegramNotify] = []
log_level: LogLevel = LogLevel.warning


__all__ = ["Config"]
13 changes: 13 additions & 0 deletions src/check_phat_nguoi/models/log_level.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from enum import Enum


class LogLevel(str, Enum):
notset = "NOTSET"
debug = "DEBUG"
info = "INFO"
warning = "WARNING"
error = "ERROR"
critical = "CRITICAL"


__all__ = ["LogLevel"]
3 changes: 3 additions & 0 deletions src/check_phat_nguoi/models/notify/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from check_phat_nguoi.models.notify.telegram_notify import TelegramNotify

__all__ = ["TelegramNotify"]
25 changes: 25 additions & 0 deletions src/check_phat_nguoi/models/notify/telegram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from re import match as re_match

from pydantic import BaseModel, field_validator


class Telegram(BaseModel):
bot_token: str
chat_id: str

@field_validator("bot_token", mode="after")
@classmethod
def validate_bot_token(cls, _bot_token: str) -> str:
if not re_match(r"^[0-9]+:.+$", _bot_token):
raise ValueError("Bot token is not valid")
return _bot_token

@field_validator("chat_id", mode="after")
@classmethod
def validate_chat_id(cls, _chat_id: str) -> str:
if not re_match(r"^[+-]?[0-9]+$", _chat_id):
raise ValueError("Bot token is not valid")
return _chat_id


__all__ = ["Telegram"]
10 changes: 10 additions & 0 deletions src/check_phat_nguoi/models/notify/telegram_notify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from pydantic import BaseModel

from check_phat_nguoi.models.notify.telegram import Telegram


class TelegramNotify(BaseModel):
telegram: Telegram


__all__ = ["TelegramNotify"]
3 changes: 3 additions & 0 deletions src/check_phat_nguoi/models/plate_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
class PlateInfo(BaseModel):
plate: str
owner: str | None = None


__all__ = ["PlateInfo"]
6 changes: 0 additions & 6 deletions src/check_phat_nguoi/models/telegram.py

This file was deleted.

8 changes: 7 additions & 1 deletion src/check_phat_nguoi/modules/config_reader.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
from json import load

from check_phat_nguoi.models.config import Config
from check_phat_nguoi.utils.constants import CONFIG_PATH


def config_reader(config_path) -> Config:
def _config_reader(config_path) -> Config:
with open(config_path, "r", encoding="utf8") as config:
data = load(config)
return Config(**data)


config: Config = _config_reader(CONFIG_PATH)

__all__ = ["config"]
13 changes: 13 additions & 0 deletions src/check_phat_nguoi/modules/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from logging import basicConfig

from check_phat_nguoi.modules.config_reader import config


def setup_logger() -> None:
basicConfig(
level=config.log_level,
format="%(asctime)s [%(levelname)-8s] - %(message)s (%(filename)s:%(lineno)d)",
)


__all__ = ["setup_logger"]
1 change: 1 addition & 0 deletions src/check_phat_nguoi/utils/constants.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
URL: str = "https://api.checkphatnguoi.vn/phatnguoi"
CONFIG_PATH: str = "config.json"

0 comments on commit 8bbfe77

Please sign in to comment.