-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
70 lines (61 loc) · 1.89 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
import json
from rpc_service import DEFAULT_GRPC_PORT
import locale
class Config:
def __init__(self) -> None:
self.ip = ""
self.filepath = ""
self.listen_port = DEFAULT_GRPC_PORT
self.x = None
self.y = None
self.lang = None
try:
with open("config.json") as fd:
d = json.loads(fd.read())
self.ip = d["ip"]
self.filepath = d["filepath"]
try:
self.listen_port = int(d["listen_port"])
except Exception as e:
print(e)
try:
self.x = int(d["x"])
except Exception as e:
print(e)
try:
self.y = int(d["y"])
except Exception as e:
print(e)
try:
self.lang = d["lang"]
except Exception as e:
print(e)
self._try_get_lang()
except Exception as e:
self._try_get_lang()
def _try_get_lang(self):
language, _ = locale.getdefaultlocale()
if language.lower().find("cn"):
self.lang = "cn"
else:
self.lang = "en"
def save(self):
try:
with open("config.json", "w") as fd:
fd.write(
json.dumps(
{
"ip": self.ip,
"filepath": self.filepath,
"listen_port": self.listen_port,
"x": self.x,
"y": self.y,
"lang": self.lang,
}
)
)
except Exception as e:
print(e)
if __name__ == "__main__":
config = Config()
config.save()