blob: 85deb13a510f49cf0d5478622c20479c7378bf46 [file] [log] [blame]
Benjamin Peterson651bc322010-03-11 21:53:25 +00001#!/usr/bin/env python
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +00002#------------------------------------------------------------------------
3# Copyright (c) 1997-2001 by Total Control Software
4# All Rights Reserved
5#------------------------------------------------------------------------
6#
7# Module Name: dbShelve.py
8#
9# Description: A reimplementation of the standard shelve.py that
10# forces the use of cPickle, and DB.
11#
12# Creation Date: 11/3/97 3:39:04PM
13#
14# License: This is free software. You may use this software for any
15# purpose including modification/redistribution, so long as
16# this header remains intact and that you do not claim any
17# rights of ownership or authorship of this software. This
18# software has been tested, but no warranty is expressed or
19# implied.
20#
21# 13-Dec-2000: Updated to be used with the new bsddb3 package.
22# Added DBShelfCursor class.
23#
24#------------------------------------------------------------------------
25
Barry Warsaw9a0d7792002-12-30 20:53:52 +000026"""Manage shelves of pickled objects using bsddb database files for the
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000027storage.
28"""
29
30#------------------------------------------------------------------------
31
Jesus Ceac5a11fa2008-07-23 11:38:42 +000032import sys
33absolute_import = (sys.version_info[0] >= 3)
34if absolute_import :
35 # Because this syntaxis is not valid before Python 2.5
36 exec("from . import db")
37else :
38 import db
39
Jesus Cea6557aac2010-03-22 14:22:26 +000040if sys.version_info[0] >= 3 :
41 import cPickle # Will be converted to "pickle" by "2to3"
42else :
43 if sys.version_info < (2, 6) :
44 import cPickle
45 else :
doko@ubuntu.com4950a3b2013-03-19 14:46:29 -070046 # When we drop support for python 2.4
Jesus Cea6557aac2010-03-22 14:22:26 +000047 # we could use: (in 2.5 we need a __future__ statement)
48 #
49 # with warnings.catch_warnings():
50 # warnings.filterwarnings(...)
51 # ...
52 #
53 # We can not use "with" as is, because it would be invalid syntax
doko@ubuntu.com4950a3b2013-03-19 14:46:29 -070054 # in python 2.4 and (with no __future__) 2.5.
Jesus Cea6557aac2010-03-22 14:22:26 +000055 # Here we simulate "with" following PEP 343 :
56 import warnings
57 w = warnings.catch_warnings()
58 w.__enter__()
59 try :
60 warnings.filterwarnings('ignore',
61 message='the cPickle module has been removed in Python 3.0',
62 category=DeprecationWarning)
63 import cPickle
64 finally :
65 w.__exit__()
66 del w
67
doko@ubuntu.com4950a3b2013-03-19 14:46:29 -070068HIGHEST_PROTOCOL = cPickle.HIGHEST_PROTOCOL
69def _dumps(object, protocol):
70 return cPickle.dumps(object, protocol=protocol)
Jesus Ceac5a11fa2008-07-23 11:38:42 +000071
Jesus Cea6557aac2010-03-22 14:22:26 +000072if sys.version_info < (2, 6) :
doko@ubuntu.com4950a3b2013-03-19 14:46:29 -070073 from UserDict import DictMixin as MutableMapping
Jesus Cea6557aac2010-03-22 14:22:26 +000074else :
75 import collections
76 MutableMapping = collections.MutableMapping
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000077
78#------------------------------------------------------------------------
79
80
81def open(filename, flags=db.DB_CREATE, mode=0660, filetype=db.DB_HASH,
82 dbenv=None, dbname=None):
83 """
84 A simple factory function for compatibility with the standard
85 shleve.py module. It can be used like this, where key is a string
86 and data is a pickleable object:
87
Barry Warsaw9a0d7792002-12-30 20:53:52 +000088 from bsddb import dbshelve
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000089 db = dbshelve.open(filename)
90
91 db[key] = data
92
93 db.close()
94 """
95 if type(flags) == type(''):
96 sflag = flags
97 if sflag == 'r':
98 flags = db.DB_RDONLY
99 elif sflag == 'rw':
100 flags = 0
101 elif sflag == 'w':
102 flags = db.DB_CREATE
103 elif sflag == 'c':
104 flags = db.DB_CREATE
105 elif sflag == 'n':
106 flags = db.DB_TRUNCATE | db.DB_CREATE
107 else:
Gregory P. Smith1281f762004-03-16 18:50:26 +0000108 raise db.DBError, "flags should be one of 'r', 'w', 'c' or 'n' or use the bsddb.db.DB_* flags"
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000109
110 d = DBShelf(dbenv)
111 d.open(filename, dbname, filetype, flags, mode)
112 return d
113
114#---------------------------------------------------------------------------
115
Gregory P. Smithd40f1262007-10-12 18:44:06 +0000116class DBShelveError(db.DBError): pass
117
118
Jesus Cea6557aac2010-03-22 14:22:26 +0000119class DBShelf(MutableMapping):
Barry Warsaw99142272003-02-08 03:18:58 +0000120 """A shelf to hold pickled objects, built upon a bsddb DB object. It
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000121 automatically pickles/unpickles data objects going to/from the DB.
122 """
123 def __init__(self, dbenv=None):
124 self.db = db.DB(dbenv)
Gregory P. Smith5d743fd2007-10-13 23:02:05 +0000125 self._closed = True
Gregory P. Smithb7de61b2007-10-09 07:19:11 +0000126 if HIGHEST_PROTOCOL:
127 self.protocol = HIGHEST_PROTOCOL
128 else:
129 self.protocol = 1
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000130
131
132 def __del__(self):
133 self.close()
134
135
136 def __getattr__(self, name):
Barry Warsaw99142272003-02-08 03:18:58 +0000137 """Many methods we can just pass through to the DB object.
138 (See below)
139 """
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000140 return getattr(self.db, name)
141
142
143 #-----------------------------------
144 # Dictionary access methods
145
146 def __len__(self):
147 return len(self.db)
148
149
150 def __getitem__(self, key):
151 data = self.db[key]
152 return cPickle.loads(data)
153
154
155 def __setitem__(self, key, value):
Gregory P. Smithb7de61b2007-10-09 07:19:11 +0000156 data = _dumps(value, self.protocol)
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000157 self.db[key] = data
158
159
160 def __delitem__(self, key):
161 del self.db[key]
162
163
164 def keys(self, txn=None):
Ezio Melotti8d3f1302010-02-02 15:57:45 +0000165 if txn is not None:
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000166 return self.db.keys(txn)
167 else:
168 return self.db.keys()
169
Jesus Cea6557aac2010-03-22 14:22:26 +0000170 if sys.version_info >= (2, 6) :
171 def __iter__(self) : # XXX: Load all keys in memory :-(
172 for k in self.db.keys() :
173 yield k
174
175 # Do this when "DB" support iteration
176 # Or is it enough to pass thru "getattr"?
177 #
178 # def __iter__(self) :
179 # return self.db.__iter__()
180
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000181
Gregory P. Smith5d743fd2007-10-13 23:02:05 +0000182 def open(self, *args, **kwargs):
183 self.db.open(*args, **kwargs)
184 self._closed = False
185
186
187 def close(self, *args, **kwargs):
188 self.db.close(*args, **kwargs)
189 self._closed = True
190
191
192 def __repr__(self):
193 if self._closed:
194 return '<DBShelf @ 0x%x - closed>' % (id(self))
195 else:
196 return repr(dict(self.iteritems()))
197
198
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000199 def items(self, txn=None):
Ezio Melotti8d3f1302010-02-02 15:57:45 +0000200 if txn is not None:
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000201 items = self.db.items(txn)
202 else:
203 items = self.db.items()
204 newitems = []
205
206 for k, v in items:
207 newitems.append( (k, cPickle.loads(v)) )
208 return newitems
209
210 def values(self, txn=None):
Ezio Melotti8d3f1302010-02-02 15:57:45 +0000211 if txn is not None:
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000212 values = self.db.values(txn)
213 else:
214 values = self.db.values()
215
216 return map(cPickle.loads, values)
217
218 #-----------------------------------
219 # Other methods
220
Gregory P. Smith1281f762004-03-16 18:50:26 +0000221 def __append(self, value, txn=None):
Gregory P. Smithb7de61b2007-10-09 07:19:11 +0000222 data = _dumps(value, self.protocol)
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000223 return self.db.append(data, txn)
224
Gregory P. Smith1281f762004-03-16 18:50:26 +0000225 def append(self, value, txn=None):
Gregory P. Smithd40f1262007-10-12 18:44:06 +0000226 if self.get_type() == db.DB_RECNO:
Gregory P. Smith5d743fd2007-10-13 23:02:05 +0000227 return self.__append(value, txn=txn)
Gregory P. Smithd40f1262007-10-12 18:44:06 +0000228 raise DBShelveError, "append() only supported when dbshelve opened with filetype=dbshelve.db.DB_RECNO"
Gregory P. Smith1281f762004-03-16 18:50:26 +0000229
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000230
231 def associate(self, secondaryDB, callback, flags=0):
232 def _shelf_callback(priKey, priData, realCallback=callback):
Jesus Cea4907d272008-08-31 14:00:51 +0000233 # Safe in Python 2.x because expresion short circuit
234 if sys.version_info[0] < 3 or isinstance(priData, bytes) :
235 data = cPickle.loads(priData)
236 else :
237 data = cPickle.loads(bytes(priData, "iso8859-1")) # 8 bits
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000238 return realCallback(priKey, data)
Jesus Cea4907d272008-08-31 14:00:51 +0000239
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000240 return self.db.associate(secondaryDB, _shelf_callback, flags)
241
242
243 #def get(self, key, default=None, txn=None, flags=0):
244 def get(self, *args, **kw):
245 # We do it with *args and **kw so if the default value wasn't
246 # given nothing is passed to the extension module. That way
247 # an exception can be raised if set_get_returns_none is turned
248 # off.
Antoine Pitrou63b0cb22009-10-14 18:01:33 +0000249 data = self.db.get(*args, **kw)
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000250 try:
251 return cPickle.loads(data)
Jesus Cea4907d272008-08-31 14:00:51 +0000252 except (EOFError, TypeError, cPickle.UnpicklingError):
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000253 return data # we may be getting the default value, or None,
254 # so it doesn't need unpickled.
255
256 def get_both(self, key, value, txn=None, flags=0):
Gregory P. Smithb7de61b2007-10-09 07:19:11 +0000257 data = _dumps(value, self.protocol)
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000258 data = self.db.get(key, data, txn, flags)
259 return cPickle.loads(data)
260
261
262 def cursor(self, txn=None, flags=0):
263 c = DBShelfCursor(self.db.cursor(txn, flags))
Gregory P. Smithb7de61b2007-10-09 07:19:11 +0000264 c.protocol = self.protocol
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000265 return c
266
267
268 def put(self, key, value, txn=None, flags=0):
Gregory P. Smithb7de61b2007-10-09 07:19:11 +0000269 data = _dumps(value, self.protocol)
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000270 return self.db.put(key, data, txn, flags)
271
272
273 def join(self, cursorList, flags=0):
274 raise NotImplementedError
275
276
277 #----------------------------------------------
278 # Methods allowed to pass-through to self.db
279 #
280 # close, delete, fd, get_byteswapped, get_type, has_key,
281 # key_range, open, remove, rename, stat, sync,
282 # upgrade, verify, and all set_* methods.
283
284
285#---------------------------------------------------------------------------
286
287class DBShelfCursor:
288 """
289 """
290 def __init__(self, cursor):
291 self.dbc = cursor
292
293 def __del__(self):
294 self.close()
295
296
297 def __getattr__(self, name):
298 """Some methods we can just pass through to the cursor object. (See below)"""
299 return getattr(self.dbc, name)
300
301
302 #----------------------------------------------
303
304 def dup(self, flags=0):
Gregory P. Smithb7de61b2007-10-09 07:19:11 +0000305 c = DBShelfCursor(self.dbc.dup(flags))
306 c.protocol = self.protocol
307 return c
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000308
309
310 def put(self, key, value, flags=0):
Gregory P. Smithb7de61b2007-10-09 07:19:11 +0000311 data = _dumps(value, self.protocol)
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000312 return self.dbc.put(key, data, flags)
313
314
315 def get(self, *args):
316 count = len(args) # a method overloading hack
317 method = getattr(self, 'get_%d' % count)
Antoine Pitrou63b0cb22009-10-14 18:01:33 +0000318 method(*args)
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000319
320 def get_1(self, flags):
321 rec = self.dbc.get(flags)
322 return self._extract(rec)
323
324 def get_2(self, key, flags):
325 rec = self.dbc.get(key, flags)
326 return self._extract(rec)
327
328 def get_3(self, key, value, flags):
Gregory P. Smithb7de61b2007-10-09 07:19:11 +0000329 data = _dumps(value, self.protocol)
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000330 rec = self.dbc.get(key, flags)
331 return self._extract(rec)
332
333
334 def current(self, flags=0): return self.get_1(flags|db.DB_CURRENT)
335 def first(self, flags=0): return self.get_1(flags|db.DB_FIRST)
336 def last(self, flags=0): return self.get_1(flags|db.DB_LAST)
337 def next(self, flags=0): return self.get_1(flags|db.DB_NEXT)
338 def prev(self, flags=0): return self.get_1(flags|db.DB_PREV)
339 def consume(self, flags=0): return self.get_1(flags|db.DB_CONSUME)
340 def next_dup(self, flags=0): return self.get_1(flags|db.DB_NEXT_DUP)
341 def next_nodup(self, flags=0): return self.get_1(flags|db.DB_NEXT_NODUP)
342 def prev_nodup(self, flags=0): return self.get_1(flags|db.DB_PREV_NODUP)
343
344
345 def get_both(self, key, value, flags=0):
Gregory P. Smithb7de61b2007-10-09 07:19:11 +0000346 data = _dumps(value, self.protocol)
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000347 rec = self.dbc.get_both(key, flags)
348 return self._extract(rec)
349
350
351 def set(self, key, flags=0):
352 rec = self.dbc.set(key, flags)
353 return self._extract(rec)
354
355 def set_range(self, key, flags=0):
356 rec = self.dbc.set_range(key, flags)
357 return self._extract(rec)
358
359 def set_recno(self, recno, flags=0):
360 rec = self.dbc.set_recno(recno, flags)
361 return self._extract(rec)
362
363 set_both = get_both
364
365 def _extract(self, rec):
366 if rec is None:
367 return None
368 else:
369 key, data = rec
Jesus Cea4907d272008-08-31 14:00:51 +0000370 # Safe in Python 2.x because expresion short circuit
371 if sys.version_info[0] < 3 or isinstance(data, bytes) :
372 return key, cPickle.loads(data)
373 else :
374 return key, cPickle.loads(bytes(data, "iso8859-1")) # 8 bits
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000375
376 #----------------------------------------------
377 # Methods allowed to pass-through to self.dbc
378 #
379 # close, count, delete, get_recno, join_item
380
381
382#---------------------------------------------------------------------------