|
2 | 2 | from pathlib import Path |
3 | 3 |
|
4 | 4 | import yaml |
| 5 | +from pydantic import BaseModel, Field |
5 | 6 |
|
6 | 7 |
|
7 | | -@dataclass(frozen=True) |
8 | | -class Conf: |
9 | | - api_id: int |
10 | | - api_hash: str |
| 8 | +class APIConf(BaseModel): |
| 9 | + id: int |
| 10 | + hash: str |
11 | 11 |
|
12 | | - clients_bots: str |
13 | | - clients_userbots: str |
14 | 12 |
|
15 | | - heap_chat: int |
16 | | - heap_page_fullness: float |
| 13 | +class ClientsConf(BaseModel): |
| 14 | + bots: Path |
| 15 | + userbots: Path |
17 | 16 |
|
18 | | - relations_chat: int |
19 | | - relations_vacuum_window_delay_seconds: int |
20 | | - relations_vacuum_min_message_count: int |
21 | 17 |
|
22 | | - buffer_chat: int |
23 | | - buffer_overflow_len: int |
24 | | - buffer_overflow_timeout_seconds: float |
25 | | - buffer_vacuum_window_delay_seconds: int |
26 | | - buffer_vacuum_min_message_count: int |
| 18 | +class TransactionConf(BaseModel): |
| 19 | + max_age_seconds: int = Field(..., alias="max_age_seconds") |
27 | 20 |
|
28 | | - @classmethod |
29 | | - def load(cls, path: Path) -> "Conf": |
30 | | - with path.open("r") as file: |
31 | | - data = yaml.safe_load(file) |
32 | 21 |
|
33 | | - conf = data["conf"] |
| 22 | +class HorizonConf(BaseModel): |
| 23 | + max_len: int |
| 24 | + transaction: TransactionConf |
| 25 | + |
| 26 | + |
| 27 | +class MessageCacheConf(BaseModel): |
| 28 | + max_len: int |
| 29 | + |
| 30 | + |
| 31 | +class PageConf(BaseModel): |
| 32 | + fullness: float |
| 33 | + |
| 34 | + |
| 35 | +class HeapConf(BaseModel): |
| 36 | + chat: int |
| 37 | + page: PageConf |
| 38 | + |
34 | 39 |
|
35 | | - return cls( |
36 | | - api_id=conf["api"]["id"], |
37 | | - api_hash=conf["api"]["hash"], |
| 40 | +class VacuumConf(BaseModel): |
| 41 | + window_delay_seconds: int | float |
| 42 | + min_message_count: int |
| 43 | + max_workers: int |
38 | 44 |
|
39 | | - clients_bots=conf["clients"]["bots"], |
40 | | - clients_userbots=conf["clients"]["userbots"], |
41 | 45 |
|
42 | | - heap_chat=conf["heap"]["chat"], |
43 | | - heap_page_fullness=conf["heap"]["page"]["fullness"], |
| 46 | +class RelationsConf(BaseModel): |
| 47 | + chat: int |
| 48 | + vacuum: VacuumConf |
44 | 49 |
|
45 | | - relations_chat=conf["relations"]["chat"], |
46 | | - relations_vacuum_window_delay_seconds=conf["relations"]["vacuum"]["window_delay_seconds"], |
47 | | - relations_vacuum_min_message_count=conf["relations"]["vacuum"]["min_message_count"], |
48 | 50 |
|
49 | | - buffer_chat=conf["buffer"]["chat"], |
50 | | - buffer_overflow_len=conf["buffer"]["overflow"]["len"], |
51 | | - buffer_overflow_timeout_seconds=conf["buffer"]["overflow"]["timeout_seconds"], |
52 | | - buffer_vacuum_window_delay_seconds=conf["buffer"]["vacuum"]["window_delay_seconds"], |
53 | | - buffer_vacuum_min_message_count=conf["buffer"]["vacuum"]["min_message_count"], |
54 | | - ) |
| 51 | +class OverflowConf(BaseModel): |
| 52 | + len: int |
| 53 | + timeout_seconds: int | float |
| 54 | + |
| 55 | + |
| 56 | +class BufferConf(BaseModel): |
| 57 | + chat: int |
| 58 | + overflow: OverflowConf |
| 59 | + vacuum: VacuumConf |
| 60 | + |
| 61 | + |
| 62 | +class Conf(BaseModel): |
| 63 | + api: APIConf |
| 64 | + clients: ClientsConf |
| 65 | + horizon: HorizonConf |
| 66 | + message_cache: MessageCacheConf |
| 67 | + heap: HeapConf |
| 68 | + relations: RelationsConf |
| 69 | + buffer: BufferConf |
| 70 | + |
| 71 | + @classmethod |
| 72 | + def load(cls, path: Path) -> "Conf": |
| 73 | + with path.open() as file: |
| 74 | + data = yaml.safe_load(file) |
| 75 | + |
| 76 | + conf = data["conf"] |
| 77 | + return Conf(**conf) |
0 commit comments