-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
37 lines (27 loc) · 974 Bytes
/
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
from contextlib import suppress
from dataclasses import dataclass
import jsons
import yaml
from database_observe_config import DatabaseObserverConfig
from mysql_config import MySQLConfig
@dataclass
class Config:
mysql: MySQLConfig = MySQLConfig()
database_observer: DatabaseObserverConfig = DatabaseObserverConfig()
def make_config(filename: str) -> Config:
# noinspection PyUnusedLocal
config = None
with suppress(FileNotFoundError, jsons.DecodeError):
config = load_config(filename)
if config is None:
config = Config()
save_config(config, filename)
return config
def load_config(filename: str):
with open(filename, "r", encoding="utf-8-sig") as f:
content = yaml.load(f, yaml.SafeLoader)
if content:
return jsons.load(content, Config)
def save_config(config: Config, filename: str):
with open(filename, "w", encoding="utf-8") as f:
yaml.dump(jsons.dump(config), f)