-
Notifications
You must be signed in to change notification settings - Fork 0
/
jconfig.py
93 lines (64 loc) · 1.72 KB
/
jconfig.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
import json
class JConfig:
def __init__(self):
self._dict = {}
self._path = None
def set(self, key, value):
self._dict[key] = value
def get(self, key):
d = self.getConfig()
try:
value = d[key]
except KeyError:
value = None
return value
def getConfig(self):
return self._dict
def count(self):
return len(self.getConfig())
def toString(self):
content = '{\n'
d = self.getConfig()
if d != None:
i = 0
for (key, value) in d.items():
content += b'\"' + key + '\":\"'
content += '' + str(value) + '\"'
if i != len(d) - 1:
content += ','
content += '\n'
i += 1
content += '}\n'
return content
def getPath(self):
return self._path
def loadFromFile(self, file):
self._path = file
with open(file) as f:
res = True
try:
config = json.load(f)
self._dict = config
f.close()
except ValueError:
res = False
return res
def saveToFile(self, file = None):
self._path = file
content = self.toString()
path = file
if file == None:
return False
with open(file, 'w') as f:
try:
f.write(content)
res = True
f.close()
except:
res = False
return res
if __name__ == '__main__':
c = JConfig()
c.loadFromFile('test.user')
print(c.toString())
c.saveToFile('out.txt')