-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove load_envs module, refactor database implementations and upgrad…
…e pydantic Deleted the 'load_envs.py' module as its functions are no longer needed. Modified the 'reporter.py' and 'media_cache.py' classes in the database package with refactoring to improve readability and structure. Upgraded the Pydantic package to a newer version for improved data validation and settings management. The 'poetry.lock' file updated to reflect the package changes.
- Loading branch information
Showing
32 changed files
with
438 additions
and
310 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from app.commands.commands import commands | ||
from .commands import commands | ||
|
||
connect_commands = commands.connect_commands |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,5 @@ | ||
import json | ||
import os | ||
import sys | ||
from pathlib import Path | ||
|
||
import pytz | ||
|
||
from .init_logger import init_logger_config | ||
from .json_logger import CONTEXT_VARS | ||
from .load_envs import load_envs | ||
from .context_vars import * | ||
from .init_logger import * | ||
from .paths import * | ||
from .settings import * | ||
from .types import * | ||
|
||
# region Base paths | ||
APP_PATH = Path(__file__).resolve().parent.parent | ||
PROJECT_PATH = APP_PATH.parent | ||
BASE_PATH = Path(os.getenv("BASE_PATH", PROJECT_PATH)) | ||
|
||
CONFIG_PATH = BASE_PATH / "config" | ||
DATA_PATH = BASE_PATH / "data" | ||
LOG_PATH = DATA_PATH / "logs" | ||
|
||
CONFIG_PATH.mkdir(parents=True, exist_ok=True) | ||
DATA_PATH.mkdir(parents=True, exist_ok=True) | ||
LOG_PATH.mkdir(parents=True, exist_ok=True) | ||
# endregion | ||
|
||
# Load envs | ||
load_envs(BASE_PATH, CONFIG_PATH) | ||
|
||
# region Localizations | ||
LOCALE_PATH = PROJECT_PATH / "locales" | ||
DEFAULT_LOCALE = os.getenv("DEFAULT_LOCALE", "en") | ||
DOMAIN = os.getenv("LOCALE_DOMAIN", "messages") | ||
# endregion | ||
|
||
# region Other | ||
TOKEN = os.getenv("TG_TOKEN") # Telegram token | ||
TIME_ZONE = pytz.timezone(os.getenv("TZ", "Europe/Moscow")) | ||
|
||
# Contacts for help command | ||
CONTACTS_PATH = Path(os.getenv("CONTACTS_PATH", CONFIG_PATH / "contacts.json")) | ||
REPORT_PATH = Path(os.getenv("REPORT_PATH", CONFIG_PATH / "report.json")) | ||
NOTIFY_PATH = Path(os.getenv("NOTIFY_PATH", CONFIG_PATH / "notify.json")) | ||
|
||
if not REPORT_PATH.parent.exists(): | ||
REPORT_PATH.parent.mkdir(parents=True) | ||
|
||
CONTACTS: list[CONTACT] = [] | ||
|
||
if CONTACTS_PATH.exists(): | ||
with open(CONTACTS_PATH) as f: | ||
CONTACTS = json.load(f) | ||
|
||
# Telegram file limit | ||
TG_FILE_LIMIT = 20 * 1024 * 1024 # 20 MB | ||
|
||
# LamadavaSaas API Token | ||
LAMADAVA_SAAS_TOKEN = os.getenv("LAMADAVA_SAAS_TOKEN", None) | ||
|
||
# region MongoDB support | ||
# mongodb://user:password@localhost:27017/database | ||
# mongodb://user:password@localhost:27017/ | ||
MONGO_URL = os.getenv("MONGO_URL", None) | ||
MONGO_DB = os.getenv("MONGO_DB", None) | ||
|
||
ENABLE_MONGO = MONGO_URL and MONGO_DB | ||
if not ENABLE_MONGO: | ||
print( | ||
"Bot requires MongoDB to work.\n" "Please, set MONGO_URL and MONGO_DB envs.", | ||
file=sys.stderr, | ||
) | ||
exit(1) | ||
# endregion | ||
|
||
# Load custom logger config | ||
init_logger_config(LOG_PATH, TIME_ZONE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from contextvars import ContextVar | ||
|
||
__all__ = ( | ||
"USER_ID", | ||
"USERNAME", | ||
"QUERY", | ||
"DATA_TYPE", | ||
"CONTEXT_VARS", | ||
) | ||
|
||
USER_ID: ContextVar[int] = ContextVar("USER_ID", default=0) | ||
USERNAME: ContextVar[str] = ContextVar("USERNAME", default="") | ||
QUERY: ContextVar[str] = ContextVar("QUERY", default="") | ||
DATA_TYPE: ContextVar[str] = ContextVar("DATA_TYPE", default="") | ||
CONTEXT_VARS: list[ContextVar] = [ | ||
USER_ID, | ||
USERNAME, | ||
QUERY, | ||
DATA_TYPE, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import json | ||
|
||
from .json_logger import JsonFormatter | ||
|
||
__all__ = ("FmtFormatter",) | ||
|
||
|
||
def escape_string(string: str) -> str: | ||
if " " in string or '"' in string or "'" in string: | ||
return json.dumps(string) | ||
return string | ||
|
||
|
||
class FmtFormatter(JsonFormatter): | ||
def format(self, record) -> str: | ||
""" | ||
Mostly the same as the parent's class method, the difference | ||
being that a dict is manipulated and dumped as JSON | ||
instead of a string. | ||
""" | ||
message_dict: dict[str, str] = json.loads(super().format(record)) | ||
|
||
return " ".join(f"{k}={escape_string(v)}" for k, v in message_dict.items() if v) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import os | ||
from pathlib import Path | ||
|
||
__all__ = ( | ||
"APP_PATH", | ||
"PROJECT_PATH", | ||
"BASE_PATH", | ||
"DATA_PATH", | ||
"LOG_PATH", | ||
"CONFIG_PATH", | ||
"LOCALE_PATH", | ||
) | ||
|
||
APP_PATH = Path(__file__).resolve().parent.parent | ||
PROJECT_PATH = APP_PATH.parent | ||
BASE_PATH = Path(os.getenv("BASE_PATH", PROJECT_PATH)) | ||
|
||
DATA_PATH = BASE_PATH / "data" | ||
LOG_PATH = DATA_PATH / "logs" | ||
CONFIG_PATH = BASE_PATH / "config" | ||
LOCALE_PATH = PROJECT_PATH / "locales" | ||
|
||
DATA_PATH.mkdir(parents=True, exist_ok=True) | ||
LOG_PATH.mkdir(parents=True, exist_ok=True) | ||
CONFIG_PATH.mkdir(parents=True, exist_ok=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import functools | ||
import json | ||
from pathlib import Path | ||
from typing import Self | ||
|
||
import pytz | ||
from pydantic import ByteSize, Field, MongoDsn, SecretStr, model_validator | ||
from pydantic_settings import BaseSettings, SettingsConfigDict | ||
|
||
from .paths import BASE_PATH, CONFIG_PATH | ||
from .types import CONTACT | ||
|
||
__all__ = ( | ||
"Settings", | ||
"settings", | ||
) | ||
|
||
|
||
class Settings(BaseSettings): | ||
model_config = SettingsConfigDict( | ||
env_file=[ | ||
CONFIG_PATH / ".env.local", | ||
CONFIG_PATH / ".env", | ||
BASE_PATH / ".env.local", | ||
BASE_PATH / ".env", | ||
], | ||
env_ignore_empty=True, | ||
extra="ignore", | ||
) | ||
|
||
default_locale: str = Field("en") | ||
domain: str = Field("messages") | ||
|
||
token: SecretStr = Field(alias="TG_TOKEN", description="Telegram Token") | ||
time_zone: pytz.tzinfo.BaseTzInfo = Field(pytz.timezone("Europe/Moscow"), alias="TZ") | ||
|
||
tg_file_size: ByteSize = Field("20 MiB", description="Telegram file size") | ||
|
||
report_path: Path = Field(CONFIG_PATH / "report.json") | ||
contacts_path: Path = Field(CONFIG_PATH / "contacts.json") | ||
|
||
@functools.cached_property | ||
def contacts(self) -> list[CONTACT]: | ||
if self.contacts_path.exists(): | ||
with self.contacts_path.open() as f: | ||
return json.load(f) | ||
return [] | ||
|
||
mongo_url: MongoDsn | None = Field(None) | ||
mongo_db: str | None = Field(None) | ||
|
||
@model_validator(mode="after") | ||
def mongo_require(self) -> Self: | ||
if self.mongo_url is None or self.mongo_db is None: | ||
raise ValueError("Bot requires MongoDB to work. Please, set MONGO_URL and MONGO_DB.") | ||
self.report_path.parent.mkdir(exist_ok=True, parents=True) | ||
return self | ||
|
||
|
||
settings = Settings() | ||
|
||
if __name__ == "__main__": | ||
print(repr(settings)) |
Oops, something went wrong.