Repository navigation
Expand file tree
/
Copy pathconfig.py
More file actions
101 lines (86 loc) · 2.71 KB
/
Copy pathconfig.py
File metadata and controls
101 lines (86 loc) · 2.71 KB
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
import json
import os
def checktyp(obj: object, typ: type):
assert isinstance(obj, typ)
return obj
class MysqlConfig:
host: str = "127.0.0.1"
port: int = 3306
user: str = "username"
password: str = "password"
database: str = "database"
@classmethod
def to_dict(cls):
return {
"host": cls.host,
"port": cls.port,
"user": cls.user,
"password": cls.password,
"database": cls.database,
}
@classmethod
def save(cls, target="./config/mysql.config.json"):
os.makedirs("config", exist_ok=True)
with open(target, "w") as fd:
json.dump(cls.to_dict(), fd)
@classmethod
def load(cls, target="./config/mysql.config.json"):
if not os.path.exists(target):
cls.save(target=target)
return
data: dict
with open(target, "r") as fd:
data = json.load(fd)
cls.host = checktyp(data.get("host"), str)
cls.port = checktyp(data.get("port"), int)
cls.user = checktyp(data.get("user"), str)
cls.password = checktyp(data.get("password"), str)
cls.database = checktyp(data.get("database"), str)
class WebConfig:
host: str = "0.0.0.0"
port: int = 8000
@classmethod
def to_dict(cls):
return {
"host": cls.host,
"port": cls.port,
}
@classmethod
def save(cls, target="./config/web.config.json"):
os.makedirs("config", exist_ok=True)
with open(target, "w") as fd:
json.dump(cls.to_dict(), fd)
@classmethod
def load(cls, target="./config/web.config.json"):
if not os.path.exists(target):
cls.save(target=target)
return
data: dict
with open(target, "r") as fd:
data = json.load(fd)
cls.host = checktyp(data.get("host"), str)
cls.port = checktyp(data.get("port"), int)
class CDNConfig:
private_key: str = ""
public_key: str = ""
@classmethod
def to_dict(cls):
return {
"private_key": cls.private_key,
"public_key": cls.public_key,
}
@classmethod
def save(cls, target="./config/cdn.config.json"):
os.makedirs("config", exist_ok=True)
with open(target, "w") as fd:
json.dump(cls.to_dict(), fd)
@classmethod
def load(cls, target="./config/cdn.config.json"):
if not os.path.exists(target):
cls.save(target=target)
return
data: dict
with open(target, "r") as fd:
data = json.load(fd)
cls.private_key = checktyp(data.get("private_key"), str)
cls.public_key = checktyp(data.get("public_key"), str)