-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyndb.py
More file actions
196 lines (164 loc) · 4.5 KB
/
syndb.py
File metadata and controls
196 lines (164 loc) · 4.5 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import json
import os
import signal
import sys
from threading import Thread
import sqlite3
def load(location, auto_dump):
return SynDB(location, auto_dump)
class SynDB(object):
key_error = TypeError('Key must be a string!')
def __init__(self , location, auto_dump):
self.location = os.path.expanduser(location)
self.load(self.location, auto_dump)
self.dthread = Thread(target=self._autodump)
if auto_dump is True:
self.dthread.start()
self.set_sigterm_handler()
def __del__(self):
if self.dthread is not None:
self.dthread.join()
def __repr__(self):
return str(self.db)
def __str__(self):
return str(self.db)
def load(self , location, auto_dump):
self.loco = location
self.auto_dump = auto_dump
if os.path.exists(location):
self._load()
else:
self.db = {}
return True
def _autodump(self):
if self.auto_dump:
self.dump()
def _load(self):
try:
self.db = json.load(open(self.location, "r"))
except ValueError:
self.db = {}
def set_sigterm_handler(self):
def sigterm_handler():
if self.dthread is not None:
self.dthread.join()
sys.exit(0)
signal.signal(signal.SIGTERM, sigterm_handler)
def dump(self):
try:
json.dump(self.db, open(self.location, "w"))
self.dthread = Thread(
target=json.dump,
args=(self.db, open(self.loco, 'wt')))
self.dthread.start()
self.dthread.join()
return True
except:
return False
def set(self , key, value):
if isinstance(key, str):
self.db[key] = value
self._autodump()
return True
else:
raise self.key_error
def ping(self):
try:
self._load()
return True
except:
return False
def update(self, key, value):
if key in self.db:
self.db[key] = value
self._autodump()
return True
else:
return False
def get(self , key):
try:
return self.db[key]
except KeyError:
return False
def get_all(self):
return self.db.keys()
def delete(self , key):
if not key in self.db:
return False
del self.db[key]
self.dump()
return True
def resetdb(self):
self.db={}
self.dump()
return True
def loadall(self):
if not self.db:
print("No Values Found")
return False
else:
print(self.db)
return True
def append(self, key, value):
if not key in self.db:
self.db[key] = []
self.db[key].append(value)
self._autodump()
return True
def exists(self, key):
if key in self.db:
return True
else:
return False
## Must read, All of the code under this line is being tested.
def list_create(self, key):
if not key in self.db:
self.db[key] = []
self._autodump()
return True
else:
return False
def list_add(self, key, value):
if key in self.db:
self.db[key].append(value)
self._autodump()
return True
else:
return False
def list_get(self, key):
if key in self.db:
return self.db[key]
else:
return False
def list_delete(self, key, value):
if key in self.db:
self.db[key].remove(value)
self._autodump()
return True
else:
return False
def dcreate(self, key):
if isinstance(key, str):
self.db[key] = {}
self._autodump()
return True
else:
raise self.key_error
def dadd(self, key, value, value2):
if key in self.db:
self.db[key][value] = value2
self._autodump()
return True
else:
return False
def dget(self, key, value):
if key in self.db:
return self.db[key][value]
else:
return False
def dmerge(self, name, name2):
name1 = self.db[name]
name_2 = self.db[name2]
name1.update(name_2)
self._autodump()
return True