-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
196 lines (162 loc) · 4.41 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from dataclasses import dataclass
from os import getenv
from pathlib import Path
from zoneinfo import available_timezones
from dotenv import load_dotenv
load_dotenv(verbose=True)
@dataclass(frozen=True, eq=False)
class Administrators:
"""
Administrators:
name - telegram id
"""
creator: str
@dataclass(frozen=True, eq=False)
class MainRedis:
"""
Base settings Redis.
"""
port: int | str
password: str
prefix: str
host: str = 'localhost'
def as_dict(self):
return {
'host': self.host,
'port': self.port,
'password': self.password,
'prefix': self.prefix,
}
@dataclass(frozen=True, eq=False)
class CacheRedis:
"""
Redis settings for cache.
"""
db: int
password: str
url: str = f"redis://{MainRedis.host}"
def as_dict(self):
return {
'url': self.url,
'db': self.db,
'password': self.password,
}
@dataclass(frozen=True, eq=False)
class ConfigRedis:
"""
All Redis settings.
"""
redis_for_bot: MainRedis
redis_data_cache: CacheRedis
@dataclass(frozen=True, eq=False)
class WebHook:
host: str
port: int
webhook_path: str = Path(__file__).parent
def as_dict(self):
return {
'webhook_path': self.webhook_path,
'host': self.host,
'port': self.port,
}
def __post_init__(self):
if not isinstance(self.port, int):
raise TypeError('type PORT must be integer!')
@dataclass(frozen=True, eq=False)
class ConfigHook:
"""
Settings for start bot (webhook).
"""
WEBHOOK: WebHook
WEBHOOK_URL: str
@dataclass(frozen=True, eq=False)
class YandexAPI:
"""
Yandex API info.
"""
FOLDER_ID: str
API_YA_STT: str
API_YA_TTS: str
@dataclass(frozen=True, eq=False)
class OtherAPI:
HORO_XML: str
HORO_EN: str
HAIRCUT_PARSE: str
OCR_URL: str | None
@dataclass(frozen=True, eq=False)
class WeatherAPI:
"""
Weather API's info.
"""
API_WEATHER: str
API_WEATHER2: str
CITY_WEATHER: str
@dataclass(frozen=True, eq=False)
class ConfigAPI:
yandex: YandexAPI
weather: WeatherAPI
other: OtherAPI
@dataclass(frozen=True, eq=False, slots=True)
class ConfigBot:
BOT_TOKEN: str
bot_administrators: Administrators
redis: ConfigRedis
hook_info: ConfigHook
work_with_api: ConfigAPI
time_zone: str = 'UTC'
def __post_init__(self):
if self.time_zone not in available_timezones():
raise ValueError(
'You can choose only available timezones!\n'
'more info > https://docs.python.org/3/library/zoneinfo.html#zoneinfo.available_timezones'
)
def create_config() -> ConfigBot:
"""
Create config.
:return: ConfigBot object
"""
return ConfigBot(
time_zone=getenv('TIMEZONE'),
BOT_TOKEN=getenv('BOT_TOKEN'),
bot_administrators=Administrators(creator=getenv('CREATOR')),
redis=ConfigRedis(
redis_for_bot=MainRedis(
host=getenv('HOST_REDIS'),
port=getenv('PORT_REDIS'),
password=getenv('PASS_REDIS'),
prefix='fsm_key',
),
redis_data_cache=CacheRedis(
url=f"redis://{getenv('HOST_REDIS')}",
db=1,
password=getenv('PASS_REDIS'),
)
),
hook_info=ConfigHook(
WEBHOOK=WebHook(
webhook_path=getenv('WEBHOOK_PATH'),
host=getenv('WEBAPP_HOST'),
port=3001,
),
WEBHOOK_URL=f"{getenv('WEBHOOK_HOST')}{getenv('WEBHOOK_PATH')}"
),
work_with_api=ConfigAPI(
yandex=YandexAPI(
FOLDER_ID=getenv('FOLDER_ID'),
API_YA_STT=getenv('API_YA_STT'),
API_YA_TTS=getenv('API_YA_TTS')
),
weather=WeatherAPI(
API_WEATHER=getenv('API_WEATHER'),
API_WEATHER2=getenv('API_WEATHER2'),
CITY_WEATHER=getenv('CITY_WEATHER'),
),
other=OtherAPI(
HORO_XML=getenv('HORO_XML'),
HORO_EN=getenv('HORO_EN'),
HAIRCUT_PARSE=getenv('HAIRCUT_PARSE'),
OCR_URL=getenv('OCR_URL'),
),
)
)
bot_config: ConfigBot = create_config()