-
Notifications
You must be signed in to change notification settings - Fork 0
/
keywordUtility.py
41 lines (38 loc) · 1.33 KB
/
keywordUtility.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
import json
class keywordUtility:
'''a VERY thin wrapper around JSON's stored in files.
Provides an interface to add, remove, modify, etc keywords from a json.
To use this, you MUST MUST MUST call the finish() function. If you do not call the finish function your changes will be lost.
As I said earlier, this is a VERY VERY VERY thin wrapper.
'''
def __init__(self, **kwargs):
'''you MUST provide a key "filename=something" in kwargs.'''
self.args = kwargs
if not "filename" in kwargs:
raise KeyError
elif "filename" in kwargs:
self.d = json.load(open(kwargs["filename"], "r"))
elif "file" in kwargs:
self.d = json.load(kwargs["file"])
else:
raise KeyError
def addKW(self, kw, val):
self.d[kw] = val
def rmKW(self, kw):
try:
self.d.pop(kw)
except KeyError:
#well, its gone
pass
def addVal(self, kw, val):
self.d[kw].append(val)
def rmVal(self, kw, val):
self.d[kw].remove(val)
def finish(self):
f = 0
f = open(self.args["filename"], "w")
f.write(json.dumps(self.d))
f.flush()
if __name__ == "__main__":
print("Please don't use this as a command-line utility, call it from somewhere else instead")
exit(1)