2022-10-16 10:15:23 +00:00
|
|
|
from enum import Enum
|
2022-09-10 15:30:13 +00:00
|
|
|
from pathlib import Path
|
2022-09-08 01:08:37 +00:00
|
|
|
from typing import (
|
|
|
|
List,
|
|
|
|
Optional,
|
|
|
|
Union,
|
|
|
|
)
|
|
|
|
|
|
|
|
import dotenv
|
|
|
|
import ujson as json
|
2022-10-13 13:01:45 +00:00
|
|
|
from pydantic import BaseModel, BaseSettings, validator
|
2022-09-08 01:08:37 +00:00
|
|
|
|
2022-09-10 15:30:13 +00:00
|
|
|
from utils.const import PROJECT_ROOT
|
|
|
|
|
2022-10-16 10:15:23 +00:00
|
|
|
__all__ = ["BotConfig", "config", "JoinGroups"]
|
2022-10-13 13:01:45 +00:00
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
dotenv.load_dotenv()
|
|
|
|
|
|
|
|
|
2022-10-16 10:15:23 +00:00
|
|
|
class JoinGroups(str, Enum):
|
|
|
|
NO_ALLOW = "NO_ALLOW"
|
|
|
|
ALLOW_AUTH_USER = "ALLOW_AUTH_USER"
|
|
|
|
ALLOW_ALL = "ALLOW_ALL"
|
|
|
|
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
class BotConfig(BaseSettings):
|
|
|
|
debug: bool = False
|
|
|
|
|
2022-10-11 04:00:55 +00:00
|
|
|
db_host: str = ""
|
|
|
|
db_port: int = 0
|
|
|
|
db_username: str = ""
|
|
|
|
db_password: str = ""
|
|
|
|
db_database: str = ""
|
2022-09-08 01:08:37 +00:00
|
|
|
|
2022-10-11 04:00:55 +00:00
|
|
|
redis_host: str = ""
|
|
|
|
redis_port: int = 0
|
|
|
|
redis_db: int = 0
|
2022-09-08 01:08:37 +00:00
|
|
|
|
2022-10-11 04:00:55 +00:00
|
|
|
bot_token: str = ""
|
2022-10-21 11:07:49 +00:00
|
|
|
|
|
|
|
error_notification_chat_id: Optional[str] = None
|
2022-09-08 01:08:37 +00:00
|
|
|
|
2022-10-11 04:00:55 +00:00
|
|
|
api_id: Optional[int] = None
|
|
|
|
api_hash: Optional[str] = None
|
2022-09-11 07:08:02 +00:00
|
|
|
|
2022-10-10 05:34:06 +00:00
|
|
|
channels: List["ConfigChannel"] = []
|
|
|
|
admins: List["ConfigUser"] = []
|
2022-09-08 01:08:37 +00:00
|
|
|
verify_groups: List[Union[int, str]] = []
|
2022-10-16 10:15:23 +00:00
|
|
|
join_groups: Optional[JoinGroups] = JoinGroups.NO_ALLOW
|
2022-09-08 01:08:37 +00:00
|
|
|
|
2022-09-10 15:30:13 +00:00
|
|
|
logger_width: int = 180
|
2022-10-10 05:34:06 +00:00
|
|
|
logger_log_path: str = "./logs"
|
2022-09-10 15:30:13 +00:00
|
|
|
logger_time_format: str = "[%Y-%m-%d %X]"
|
|
|
|
logger_traceback_max_frames: int = 20
|
2022-10-10 05:34:06 +00:00
|
|
|
logger_render_keywords: List[str] = ["BOT"]
|
2022-10-13 13:01:45 +00:00
|
|
|
logger_locals_max_depth: Optional[int] = 0
|
|
|
|
logger_locals_max_length: int = 10
|
|
|
|
logger_locals_max_string: int = 80
|
2022-09-10 15:30:13 +00:00
|
|
|
|
2022-10-07 05:55:50 +00:00
|
|
|
enka_network_api_agent: str = ""
|
2022-10-10 05:34:06 +00:00
|
|
|
pass_challenge_api: str = ""
|
|
|
|
pass_challenge_app_key: str = ""
|
2022-10-07 05:55:50 +00:00
|
|
|
|
2022-10-12 13:39:47 +00:00
|
|
|
web_url: str = "http://localhost:8080/"
|
|
|
|
web_host: str = "localhost"
|
|
|
|
web_port: int = 8080
|
|
|
|
|
2022-10-19 12:22:24 +00:00
|
|
|
error_pb_url: str = ""
|
|
|
|
error_pb_sunset: int = 43200
|
|
|
|
error_pb_max_lines: int = 1000
|
|
|
|
error_sentry_dsn: str = ""
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
class Config:
|
|
|
|
case_sensitive = False
|
|
|
|
json_loads = json.loads
|
|
|
|
json_dumps = json.dumps
|
|
|
|
|
|
|
|
@property
|
|
|
|
def mysql(self) -> "MySqlConfig":
|
|
|
|
return MySqlConfig(
|
|
|
|
host=self.db_host,
|
|
|
|
port=self.db_port,
|
|
|
|
username=self.db_username,
|
|
|
|
password=self.db_password,
|
|
|
|
database=self.db_database,
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def redis(self) -> "RedisConfig":
|
|
|
|
return RedisConfig(
|
|
|
|
host=self.redis_host,
|
|
|
|
port=self.redis_port,
|
|
|
|
database=self.redis_db,
|
|
|
|
)
|
|
|
|
|
2022-09-10 15:30:13 +00:00
|
|
|
@property
|
|
|
|
def logger(self) -> "LoggerConfig":
|
|
|
|
return LoggerConfig(
|
|
|
|
width=self.logger_width,
|
|
|
|
traceback_max_frames=self.logger_traceback_max_frames,
|
|
|
|
path=PROJECT_ROOT.joinpath(self.logger_log_path).resolve(),
|
|
|
|
time_format=self.logger_time_format,
|
|
|
|
render_keywords=self.logger_render_keywords,
|
2022-10-13 13:01:45 +00:00
|
|
|
locals_max_length=self.logger_locals_max_length,
|
|
|
|
locals_max_string=self.logger_locals_max_string,
|
|
|
|
locals_max_depth=self.logger_locals_max_depth,
|
2022-09-10 15:30:13 +00:00
|
|
|
)
|
|
|
|
|
2022-09-11 07:08:02 +00:00
|
|
|
@property
|
|
|
|
def mtproto(self) -> "MTProtoConfig":
|
|
|
|
return MTProtoConfig(
|
|
|
|
api_id=self.api_id,
|
|
|
|
api_hash=self.api_hash,
|
|
|
|
)
|
|
|
|
|
2022-10-12 13:39:47 +00:00
|
|
|
@property
|
|
|
|
def webserver(self) -> "WebServerConfig":
|
|
|
|
return WebServerConfig(
|
|
|
|
host=self.web_host,
|
|
|
|
port=self.web_port,
|
|
|
|
)
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
|
|
|
class ConfigChannel(BaseModel):
|
|
|
|
name: str
|
|
|
|
chat_id: int
|
|
|
|
|
|
|
|
|
|
|
|
class ConfigUser(BaseModel):
|
|
|
|
username: Optional[str]
|
|
|
|
user_id: int
|
|
|
|
|
|
|
|
|
|
|
|
class MySqlConfig(BaseModel):
|
|
|
|
host: str = "127.0.0.1"
|
|
|
|
port: int = 3306
|
|
|
|
username: str
|
|
|
|
password: str
|
|
|
|
database: str
|
|
|
|
|
|
|
|
|
|
|
|
class RedisConfig(BaseModel):
|
2022-10-10 05:34:06 +00:00
|
|
|
host: str = "127.0.0.1"
|
2022-09-08 01:08:37 +00:00
|
|
|
port: int
|
|
|
|
database: int = 0
|
|
|
|
|
|
|
|
|
2022-09-10 15:30:13 +00:00
|
|
|
class LoggerConfig(BaseModel):
|
|
|
|
width: int = 180
|
|
|
|
time_format: str = "[%Y-%m-%d %X]"
|
|
|
|
traceback_max_frames: int = 20
|
2022-10-10 05:34:06 +00:00
|
|
|
path: Path = PROJECT_ROOT / "logs"
|
|
|
|
render_keywords: List[str] = ["BOT"]
|
2022-10-13 13:01:45 +00:00
|
|
|
locals_max_length: int = 10
|
|
|
|
locals_max_string: int = 80
|
|
|
|
locals_max_depth: Optional[int] = None
|
|
|
|
|
|
|
|
@validator("locals_max_depth", pre=True, check_fields=False)
|
|
|
|
def locals_max_depth_validator(cls, value) -> Optional[int]: # pylint: disable=R0201
|
|
|
|
if value <= 0:
|
|
|
|
return None
|
|
|
|
return value
|
2022-09-10 15:30:13 +00:00
|
|
|
|
|
|
|
|
2022-09-11 07:08:02 +00:00
|
|
|
class MTProtoConfig(BaseModel):
|
|
|
|
api_id: Optional[int]
|
|
|
|
api_hash: Optional[str]
|
|
|
|
|
|
|
|
|
2022-10-12 13:39:47 +00:00
|
|
|
class WebServerConfig(BaseModel):
|
|
|
|
host: Optional[str]
|
|
|
|
port: Optional[int]
|
|
|
|
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
BotConfig.update_forward_refs()
|
|
|
|
config = BotConfig()
|