-
Notifications
You must be signed in to change notification settings - Fork 0
/
libConfig.py
70 lines (59 loc) · 2.16 KB
/
libConfig.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
#!/usr/bin/env python3
import json, pathlib
class config:
def __init__(self):
self.queryStr = ""
self.folderStr = "config/"
self.storeDict = dict()
self.queryDict = dict()
self.modeStr = "OVERWRITE"
def load(self):
pathStr = "{}{}.json".format(
self.folderStr,self.queryStr
)
pathlib.Path(self.folderStr).mkdir(parents=True,exist_ok=True)
if not pathlib.Path(pathStr).exists():
with open(pathStr,"w") as targetHandle:
json.dump(dict(),targetHandle)
print("[libConfig]Create new config dictionary: "+self.queryStr)
self.storeDict = dict()
else:
self.storeDict = json.load(open(pathStr))
print("[libConfig]Finish loading: "+self.queryStr)
def save(self):
pathStr = "{}{}.json".format(
self.folderStr,self.queryStr
)
if self.modeStr == "UPDATE":
if pathlib.Path(pathStr).exists():
print("[libConfig]Update json: "+self.queryStr)
sourceDict = json.load(open(pathStr))
print("[libConfig]Finish loading: "+self.queryStr)
else:
print("[libConfig]Create new config dictionary: "+self.queryStr)
sourceDict = dict()
else:
print("[libConfig]Overwrite json: "+self.queryStr)
sourceDict = dict()
sourceDict.update(self.queryDict)
pathlib.Path(self.folderStr).mkdir(parents=True,exist_ok=True)
with open(pathStr,"w") as targetHandle:
json.dump(sourceDict,targetHandle,indent=2,sort_keys=True)
print("[libConfig]Finish writing json: "+self.queryStr)
if __name__ == "__main__":
print("__name__ == "+__name__)
Config = config()
Config.queryStr = "testing"
Config.folderStr = "config/"
Config.queryDict = {
"haha" : "ha",
"ho" : "hohoho",
}
Config.modeStr = "OVERWRITE"
Config.save()
Config.load()
tempDict = Config.storeDict
tempDict.update({ "hoha" : "wahahaha"})
Config.modeStr = "UPDATE"
Config.save()
Config.load()