-
Notifications
You must be signed in to change notification settings - Fork 124
/
pickledb.py
298 lines (248 loc) · 8.97 KB
/
pickledb.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2019 Harrison Erd
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import sys
import os
import signal
import shutil
import json
from tempfile import NamedTemporaryFile
from threading import Thread
def load(location, auto_dump, sig=True):
'''Return a pickledb object. location is the path to the json file.'''
return PickleDB(location, auto_dump, sig)
class PickleDB(object):
key_string_error = TypeError('Key/name must be a string!')
def __init__(self, location, auto_dump, sig):
'''Creates a database object and loads the data from the location path.
If the file does not exist it will be created on the first update.
'''
self.load(location, auto_dump)
self.dthread = None
if sig:
self.set_sigterm_handler()
def __getitem__(self, item):
'''Syntax sugar for get()'''
return self.get(item)
def __setitem__(self, key, value):
'''Sytax sugar for set()'''
return self.set(key, value)
def __delitem__(self, key):
'''Sytax sugar for rem()'''
return self.rem(key)
def set_sigterm_handler(self):
'''Assigns sigterm_handler for graceful shutdown during dump()'''
def sigterm_handler(*args, **kwargs):
if self.dthread is not None:
self.dthread.join()
sys.exit(0)
signal.signal(signal.SIGTERM, sigterm_handler)
def load(self, location, auto_dump):
'''Loads, reloads or changes the path to the db file'''
location = os.path.expanduser(location)
self.loco = location
self.auto_dump = auto_dump
if os.path.exists(location):
self._loaddb()
else:
self.db = {}
return True
def _dump(self):
'''Dump to a temporary file, and then move to the actual location'''
with NamedTemporaryFile(mode='wt', delete=False) as f:
json.dump(self.db, f)
if os.stat(f.name).st_size != 0:
shutil.move(f.name, self.loco)
def dump(self):
'''Force dump memory db to file'''
self.dthread = Thread(target=self._dump)
self.dthread.start()
self.dthread.join()
return True
def _loaddb(self):
'''Load or reload the json info from the file'''
try:
self.db = json.load(open(self.loco, 'rt'))
except ValueError:
if os.stat(self.loco).st_size == 0: # Error raised because file is empty
self.db = {}
else:
raise # File is not empty, avoid overwriting it
def _autodumpdb(self):
'''Write/save the json dump into the file if auto_dump is enabled'''
if self.auto_dump:
self.dump()
def set(self, key, value):
'''Set the str value of a key'''
if isinstance(key, str):
self.db[key] = value
self._autodumpdb()
return True
else:
raise self.key_string_error
def get(self, key):
'''Get the value of a key'''
try:
return self.db[key]
except KeyError:
return False
def getall(self):
'''Return a list of all keys in db'''
return self.db.keys()
def exists(self, key):
'''Return True if key exists in db, return False if not'''
return key in self.db
def rem(self, key):
'''Delete a key'''
if not key in self.db: # return False instead of an exception
return False
del self.db[key]
self._autodumpdb()
return True
def totalkeys(self, name=None):
'''Get a total number of keys, lists, and dicts inside the db'''
if name is None:
total = len(self.db)
return total
else:
total = len(self.db[name])
return total
def append(self, key, more):
'''Add more to a key's value'''
tmp = self.db[key]
self.db[key] = tmp + more
self._autodumpdb()
return True
def lcreate(self, name):
'''Create a list, name must be str'''
if isinstance(name, str):
self.db[name] = []
self._autodumpdb()
return True
else:
raise self.key_string_error
def ladd(self, name, value):
'''Add a value to a list'''
self.db[name].append(value)
self._autodumpdb()
return True
def lextend(self, name, seq):
'''Extend a list with a sequence'''
self.db[name].extend(seq)
self._autodumpdb()
return True
def lgetall(self, name):
'''Return all values in a list'''
return self.db[name]
def lget(self, name, pos):
'''Return one value in a list'''
return self.db[name][pos]
def lrange(self, name, start=None, end=None):
'''Return range of values in a list '''
return self.db[name][start:end]
def lremlist(self, name):
'''Remove a list and all of its values'''
number = len(self.db[name])
del self.db[name]
self._autodumpdb()
return number
def lremvalue(self, name, value):
'''Remove a value from a certain list'''
self.db[name].remove(value)
self._autodumpdb()
return True
def lpop(self, name, pos):
'''Remove one value in a list'''
value = self.db[name][pos]
del self.db[name][pos]
self._autodumpdb()
return value
def llen(self, name):
'''Returns the length of the list'''
return len(self.db[name])
def lappend(self, name, pos, more):
'''Add more to a value in a list'''
tmp = self.db[name][pos]
self.db[name][pos] = tmp + more
self._autodumpdb()
return True
def lexists(self, name, value):
'''Determine if a value exists in a list'''
return value in self.db[name]
def dcreate(self, name):
'''Create a dict, name must be str'''
if isinstance(name, str):
self.db[name] = {}
self._autodumpdb()
return True
else:
raise self.key_string_error
def dadd(self, name, pair):
'''Add a key-value pair to a dict, "pair" is a tuple'''
self.db[name][pair[0]] = pair[1]
self._autodumpdb()
return True
def dget(self, name, key):
'''Return the value for a key in a dict'''
return self.db[name][key]
def dgetall(self, name):
'''Return all key-value pairs from a dict'''
return self.db[name]
def drem(self, name):
'''Remove a dict and all of its pairs'''
del self.db[name]
self._autodumpdb()
return True
def dpop(self, name, key):
'''Remove one key-value pair in a dict'''
value = self.db[name][key]
del self.db[name][key]
self._autodumpdb()
return value
def dkeys(self, name):
'''Return all the keys for a dict'''
return self.db[name].keys()
def dvals(self, name):
'''Return all the values for a dict'''
return self.db[name].values()
def dexists(self, name, key):
'''Determine if a key exists or not in a dict'''
return key in self.db[name]
def dmerge(self, name1, name2):
'''Merge two dicts together into name1'''
first = self.db[name1]
second = self.db[name2]
first.update(second)
self._autodumpdb()
return True
def deldb(self):
'''Delete everything from the database'''
self.db = {}
self._autodumpdb()
return True