Skip to content

Commit 88a64bc

Browse files
committed
Add json dump and load opts
1 parent 68b24d6 commit 88a64bc

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

pickledb.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,22 @@
4040
from threading import Thread
4141

4242

43-
def load(location, auto_dump, sig=True):
43+
def load(location, auto_dump, sig=True, dump_opts=None, load_opts=None):
4444
'''Return a pickledb object. location is the path to the json file.'''
45-
return PickleDB(location, auto_dump, sig)
45+
return PickleDB(location, auto_dump, sig, dump_opts or {}, load_opts or {})
4646

4747

4848
class PickleDB(object):
4949

5050
key_string_error = TypeError('Key/name must be a string!')
5151

52-
def __init__(self, location, auto_dump, sig):
52+
def __init__(self, location, auto_dump, sig, dump_opts, load_opts):
5353
'''Creates a database object and loads the data from the location path.
5454
If the file does not exist it will be created on the first update.
5555
'''
56+
self.dump_opts = dump_opts
57+
self.load_opts = load_opts
58+
5659
self.load(location, auto_dump)
5760
self.dthread = None
5861
if sig:
@@ -92,7 +95,7 @@ def load(self, location, auto_dump):
9295
def _dump(self):
9396
'''Dump to a temporary file, and then move to the actual location'''
9497
with NamedTemporaryFile(mode='wt', delete=False) as f:
95-
json.dump(self.db, f)
98+
json.dump(self.db, f, **self.dump_opts)
9699
if os.stat(f.name).st_size != 0:
97100
shutil.move(f.name, self.loco)
98101

@@ -106,7 +109,7 @@ def dump(self):
106109
def _loaddb(self):
107110
'''Load or reload the json info from the file'''
108111
try:
109-
self.db = json.load(open(self.loco, 'rt'))
112+
self.db = json.load(open(self.loco, 'rt'), **self.load_opts)
110113
except ValueError:
111114
if os.stat(self.loco).st_size == 0: # Error raised because file is empty
112115
self.db = {}

0 commit comments

Comments
 (0)