blob: e3f6d4c987da5360913bda90b04a31fbba2247d9 [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 :
46 # When we drop support for python 2.3 and 2.4
47 # 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
54 # in python 2.3, 2.4 and (with no __future__) 2.5.
55 # 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
Jesus Ceac5a11fa2008-07-23 11:38:42 +000068#At version 2.3 cPickle switched to using protocol instead of bin
Jesus Cea6557aac2010-03-22 14:22:26 +000069if sys.version_info >= (2, 3):
Gregory P. Smithb7de61b2007-10-09 07:19:11 +000070 HIGHEST_PROTOCOL = cPickle.HIGHEST_PROTOCOL
Jesus Cea18eb1fa2008-05-13 20:57:59 +000071# In python 2.3.*, "cPickle.dumps" accepts no
72# named parameters. "pickle.dumps" accepts them,
73# so this seems a bug.
Jesus Cea6557aac2010-03-22 14:22:26 +000074 if sys.version_info < (2, 4):
Jesus Cea18eb1fa2008-05-13 20:57:59 +000075 def _dumps(object, protocol):
76 return cPickle.dumps(object, protocol)
77 else :
78 def _dumps(object, protocol):
79 return cPickle.dumps(object, protocol=protocol)
80
Gregory P. Smithb7de61b2007-10-09 07:19:11 +000081else:
82 HIGHEST_PROTOCOL = None
83 def _dumps(object, protocol):
84 return cPickle.dumps(object, bin=protocol)
Jesus Ceac5a11fa2008-07-23 11:38:42 +000085
86
Jesus Cea6557aac2010-03-22 14:22:26 +000087if sys.version_info < (2, 6) :
88 try:
89 from UserDict import DictMixin
90 except ImportError:
91 # DictMixin is new in Python 2.3
92 class DictMixin: pass
93 MutableMapping = DictMixin
94else :
95 import collections
96 MutableMapping = collections.MutableMapping
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +000097
98#------------------------------------------------------------------------
99
100
101def open(filename, flags=db.DB_CREATE, mode=0660, filetype=db.DB_HASH,
102 dbenv=None, dbname=None):
103 """
104 A simple factory function for compatibility with the standard
105 shleve.py module. It can be used like this, where key is a string
106 and data is a pickleable object:
107
Barry Warsaw9a0d7792002-12-30 20:53:52 +0000108 from bsddb import dbshelve
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000109 db = dbshelve.open(filename)
110
111 db[key] = data
112
113 db.close()
114 """
115 if type(flags) == type(''):
116 sflag = flags
117 if sflag == 'r':
118 flags = db.DB_RDONLY
119 elif sflag == 'rw':
120 flags = 0
121 elif sflag == 'w':
122 flags = db.DB_CREATE
123 elif sflag == 'c':
124 flags = db.DB_CREATE
125 elif sflag == 'n':
126 flags = db.DB_TRUNCATE | db.DB_CREATE
127 else:
Gregory P. Smith1281f762004-03-16 18:50:26 +0000128 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 +0000129
130 d = DBShelf(dbenv)
131 d.open(filename, dbname, filetype, flags, mode)
132 return d
133
134#---------------------------------------------------------------------------
135
Gregory P. Smithd40f1262007-10-12 18:44:06 +0000136class DBShelveError(db.DBError): pass
137
138
Jesus Cea6557aac2010-03-22 14:22:26 +0000139class DBShelf(MutableMapping):
Barry Warsaw99142272003-02-08 03:18:58 +0000140 """A shelf to hold pickled objects, built upon a bsddb DB object. It
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000141 automatically pickles/unpickles data objects going to/from the DB.
142 """
143 def __init__(self, dbenv=None):
144 self.db = db.DB(dbenv)
Gregory P. Smith5d743fd2007-10-13 23:02:05 +0000145 self._closed = True
Gregory P. Smithb7de61b2007-10-09 07:19:11 +0000146 if HIGHEST_PROTOCOL:
147 self.protocol = HIGHEST_PROTOCOL
148 else:
149 self.protocol = 1
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000150
151
152 def __del__(self):
153 self.close()
154
155
156 def __getattr__(self, name):
Barry Warsaw99142272003-02-08 03:18:58 +0000157 """Many methods we can just pass through to the DB object.
158 (See below)
159 """
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000160 return getattr(self.db, name)
161
162
163 #-----------------------------------
164 # Dictionary access methods
165
166 def __len__(self):
167 return len(self.db)
168
169
170 def __getitem__(self, key):
171 data = self.db[key]
172 return cPickle.loads(data)
173
174
175 def __setitem__(self, key, value):
Gregory P. Smithb7de61b2007-10-09 07:19:11 +0000176 data = _dumps(value, self.protocol)
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000177 self.db[key] = data
178
179
180 def __delitem__(self, key):
181 del self.db[key]
182
183
184 def keys(self, txn=None):
Ezio Melotti8d3f1302010-02-02 15:57:45 +0000185 if txn is not None:
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000186 return self.db.keys(txn)
187 else:
188 return self.db.keys()
189
Jesus Cea6557aac2010-03-22 14:22:26 +0000190 if sys.version_info >= (2, 6) :
191 def __iter__(self) : # XXX: Load all keys in memory :-(
192 for k in self.db.keys() :
193 yield k
194
195 # Do this when "DB" support iteration
196 # Or is it enough to pass thru "getattr"?
197 #
198 # def __iter__(self) :
199 # return self.db.__iter__()
200
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000201
Gregory P. Smith5d743fd2007-10-13 23:02:05 +0000202 def open(self, *args, **kwargs):
203 self.db.open(*args, **kwargs)
204 self._closed = False
205
206
207 def close(self, *args, **kwargs):
208 self.db.close(*args, **kwargs)
209 self._closed = True
210
211
212 def __repr__(self):
213 if self._closed:
214 return '<DBShelf @ 0x%x - closed>' % (id(self))
215 else:
216 return repr(dict(self.iteritems()))
217
218
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000219 def items(self, txn=None):
Ezio Melotti8d3f1302010-02-02 15:57:45 +0000220 if txn is not None:
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000221 items = self.db.items(txn)
222 else:
223 items = self.db.items()
224 newitems = []
225
226 for k, v in items:
227 newitems.append( (k, cPickle.loads(v)) )
228 return newitems
229
230 def values(self, txn=None):
Ezio Melotti8d3f1302010-02-02 15:57:45 +0000231 if txn is not None:
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000232 values = self.db.values(txn)
233 else:
234 values = self.db.values()
235
236 return map(cPickle.loads, values)
237
238 #-----------------------------------
239 # Other methods
240
Gregory P. Smith1281f762004-03-16 18:50:26 +0000241 def __append(self, value, txn=None):
Gregory P. Smithb7de61b2007-10-09 07:19:11 +0000242 data = _dumps(value, self.protocol)
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000243 return self.db.append(data, txn)
244
Gregory P. Smith1281f762004-03-16 18:50:26 +0000245 def append(self, value, txn=None):
Gregory P. Smithd40f1262007-10-12 18:44:06 +0000246 if self.get_type() == db.DB_RECNO:
Gregory P. Smith5d743fd2007-10-13 23:02:05 +0000247 return self.__append(value, txn=txn)
Gregory P. Smithd40f1262007-10-12 18:44:06 +0000248 raise DBShelveError, "append() only supported when dbshelve opened with filetype=dbshelve.db.DB_RECNO"
Gregory P. Smith1281f762004-03-16 18:50:26 +0000249
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000250
251 def associate(self, secondaryDB, callback, flags=0):
252 def _shelf_callback(priKey, priData, realCallback=callback):
Jesus Cea4907d272008-08-31 14:00:51 +0000253 # Safe in Python 2.x because expresion short circuit
254 if sys.version_info[0] < 3 or isinstance(priData, bytes) :
255 data = cPickle.loads(priData)
256 else :
257 data = cPickle.loads(bytes(priData, "iso8859-1")) # 8 bits
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000258 return realCallback(priKey, data)
Jesus Cea4907d272008-08-31 14:00:51 +0000259
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000260 return self.db.associate(secondaryDB, _shelf_callback, flags)
261
262
263 #def get(self, key, default=None, txn=None, flags=0):
264 def get(self, *args, **kw):
265 # We do it with *args and **kw so if the default value wasn't
266 # given nothing is passed to the extension module. That way
267 # an exception can be raised if set_get_returns_none is turned
268 # off.
Antoine Pitrou63b0cb22009-10-14 18:01:33 +0000269 data = self.db.get(*args, **kw)
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000270 try:
271 return cPickle.loads(data)
Jesus Cea4907d272008-08-31 14:00:51 +0000272 except (EOFError, TypeError, cPickle.UnpicklingError):
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000273 return data # we may be getting the default value, or None,
274 # so it doesn't need unpickled.
275
276 def get_both(self, key, value, txn=None, flags=0):
Gregory P. Smithb7de61b2007-10-09 07:19:11 +0000277 data = _dumps(value, self.protocol)
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000278 data = self.db.get(key, data, txn, flags)
279 return cPickle.loads(data)
280
281
282 def cursor(self, txn=None, flags=0):
283 c = DBShelfCursor(self.db.cursor(txn, flags))
Gregory P. Smithb7de61b2007-10-09 07:19:11 +0000284 c.protocol = self.protocol
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000285 return c
286
287
288 def put(self, key, value, txn=None, flags=0):
Gregory P. Smithb7de61b2007-10-09 07:19:11 +0000289 data = _dumps(value, self.protocol)
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000290 return self.db.put(key, data, txn, flags)
291
292
293 def join(self, cursorList, flags=0):
294 raise NotImplementedError
295
296
297 #----------------------------------------------
298 # Methods allowed to pass-through to self.db
299 #
300 # close, delete, fd, get_byteswapped, get_type, has_key,
301 # key_range, open, remove, rename, stat, sync,
302 # upgrade, verify, and all set_* methods.
303
304
305#---------------------------------------------------------------------------
306
307class DBShelfCursor:
308 """
309 """
310 def __init__(self, cursor):
311 self.dbc = cursor
312
313 def __del__(self):
314 self.close()
315
316
317 def __getattr__(self, name):
318 """Some methods we can just pass through to the cursor object. (See below)"""
319 return getattr(self.dbc, name)
320
321
322 #----------------------------------------------
323
324 def dup(self, flags=0):
Gregory P. Smithb7de61b2007-10-09 07:19:11 +0000325 c = DBShelfCursor(self.dbc.dup(flags))
326 c.protocol = self.protocol
327 return c
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000328
329
330 def put(self, key, value, flags=0):
Gregory P. Smithb7de61b2007-10-09 07:19:11 +0000331 data = _dumps(value, self.protocol)
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000332 return self.dbc.put(key, data, flags)
333
334
335 def get(self, *args):
336 count = len(args) # a method overloading hack
337 method = getattr(self, 'get_%d' % count)
Antoine Pitrou63b0cb22009-10-14 18:01:33 +0000338 method(*args)
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000339
340 def get_1(self, flags):
341 rec = self.dbc.get(flags)
342 return self._extract(rec)
343
344 def get_2(self, key, flags):
345 rec = self.dbc.get(key, flags)
346 return self._extract(rec)
347
348 def get_3(self, key, value, flags):
Gregory P. Smithb7de61b2007-10-09 07:19:11 +0000349 data = _dumps(value, self.protocol)
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000350 rec = self.dbc.get(key, flags)
351 return self._extract(rec)
352
353
354 def current(self, flags=0): return self.get_1(flags|db.DB_CURRENT)
355 def first(self, flags=0): return self.get_1(flags|db.DB_FIRST)
356 def last(self, flags=0): return self.get_1(flags|db.DB_LAST)
357 def next(self, flags=0): return self.get_1(flags|db.DB_NEXT)
358 def prev(self, flags=0): return self.get_1(flags|db.DB_PREV)
359 def consume(self, flags=0): return self.get_1(flags|db.DB_CONSUME)
360 def next_dup(self, flags=0): return self.get_1(flags|db.DB_NEXT_DUP)
361 def next_nodup(self, flags=0): return self.get_1(flags|db.DB_NEXT_NODUP)
362 def prev_nodup(self, flags=0): return self.get_1(flags|db.DB_PREV_NODUP)
363
364
365 def get_both(self, key, value, flags=0):
Gregory P. Smithb7de61b2007-10-09 07:19:11 +0000366 data = _dumps(value, self.protocol)
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000367 rec = self.dbc.get_both(key, flags)
368 return self._extract(rec)
369
370
371 def set(self, key, flags=0):
372 rec = self.dbc.set(key, flags)
373 return self._extract(rec)
374
375 def set_range(self, key, flags=0):
376 rec = self.dbc.set_range(key, flags)
377 return self._extract(rec)
378
379 def set_recno(self, recno, flags=0):
380 rec = self.dbc.set_recno(recno, flags)
381 return self._extract(rec)
382
383 set_both = get_both
384
385 def _extract(self, rec):
386 if rec is None:
387 return None
388 else:
389 key, data = rec
Jesus Cea4907d272008-08-31 14:00:51 +0000390 # Safe in Python 2.x because expresion short circuit
391 if sys.version_info[0] < 3 or isinstance(data, bytes) :
392 return key, cPickle.loads(data)
393 else :
394 return key, cPickle.loads(bytes(data, "iso8859-1")) # 8 bits
Martin v. Löwis6aa4a1f2002-11-19 08:09:52 +0000395
396 #----------------------------------------------
397 # Methods allowed to pass-through to self.dbc
398 #
399 # close, count, delete, get_recno, join_item
400
401
402#---------------------------------------------------------------------------