Benjamin Peterson | 651bc32 | 2010-03-11 21:53:25 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2 | #------------------------------------------------------------------------ |
| 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 Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 26 | """Manage shelves of pickled objects using bsddb database files for the |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 27 | storage. |
| 28 | """ |
| 29 | |
| 30 | #------------------------------------------------------------------------ |
| 31 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 32 | import sys |
| 33 | absolute_import = (sys.version_info[0] >= 3) |
| 34 | if absolute_import : |
| 35 | # Because this syntaxis is not valid before Python 2.5 |
| 36 | exec("from . import db") |
| 37 | else : |
| 38 | import db |
| 39 | |
Jesus Cea | 6557aac | 2010-03-22 14:22:26 +0000 | [diff] [blame] | 40 | if sys.version_info[0] >= 3 : |
| 41 | import cPickle # Will be converted to "pickle" by "2to3" |
| 42 | else : |
| 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 Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 68 | #At version 2.3 cPickle switched to using protocol instead of bin |
Jesus Cea | 6557aac | 2010-03-22 14:22:26 +0000 | [diff] [blame] | 69 | if sys.version_info >= (2, 3): |
Gregory P. Smith | b7de61b | 2007-10-09 07:19:11 +0000 | [diff] [blame] | 70 | HIGHEST_PROTOCOL = cPickle.HIGHEST_PROTOCOL |
Jesus Cea | 18eb1fa | 2008-05-13 20:57:59 +0000 | [diff] [blame] | 71 | # In python 2.3.*, "cPickle.dumps" accepts no |
| 72 | # named parameters. "pickle.dumps" accepts them, |
| 73 | # so this seems a bug. |
Jesus Cea | 6557aac | 2010-03-22 14:22:26 +0000 | [diff] [blame] | 74 | if sys.version_info < (2, 4): |
Jesus Cea | 18eb1fa | 2008-05-13 20:57:59 +0000 | [diff] [blame] | 75 | 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. Smith | b7de61b | 2007-10-09 07:19:11 +0000 | [diff] [blame] | 81 | else: |
| 82 | HIGHEST_PROTOCOL = None |
| 83 | def _dumps(object, protocol): |
| 84 | return cPickle.dumps(object, bin=protocol) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 85 | |
| 86 | |
Jesus Cea | 6557aac | 2010-03-22 14:22:26 +0000 | [diff] [blame] | 87 | if 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 |
| 94 | else : |
| 95 | import collections |
| 96 | MutableMapping = collections.MutableMapping |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 97 | |
| 98 | #------------------------------------------------------------------------ |
| 99 | |
| 100 | |
| 101 | def 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 Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 108 | from bsddb import dbshelve |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 109 | 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. Smith | 1281f76 | 2004-03-16 18:50:26 +0000 | [diff] [blame] | 128 | raise db.DBError, "flags should be one of 'r', 'w', 'c' or 'n' or use the bsddb.db.DB_* flags" |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 129 | |
| 130 | d = DBShelf(dbenv) |
| 131 | d.open(filename, dbname, filetype, flags, mode) |
| 132 | return d |
| 133 | |
| 134 | #--------------------------------------------------------------------------- |
| 135 | |
Gregory P. Smith | d40f126 | 2007-10-12 18:44:06 +0000 | [diff] [blame] | 136 | class DBShelveError(db.DBError): pass |
| 137 | |
| 138 | |
Jesus Cea | 6557aac | 2010-03-22 14:22:26 +0000 | [diff] [blame] | 139 | class DBShelf(MutableMapping): |
Barry Warsaw | 9914227 | 2003-02-08 03:18:58 +0000 | [diff] [blame] | 140 | """A shelf to hold pickled objects, built upon a bsddb DB object. It |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 141 | 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. Smith | 5d743fd | 2007-10-13 23:02:05 +0000 | [diff] [blame] | 145 | self._closed = True |
Gregory P. Smith | b7de61b | 2007-10-09 07:19:11 +0000 | [diff] [blame] | 146 | if HIGHEST_PROTOCOL: |
| 147 | self.protocol = HIGHEST_PROTOCOL |
| 148 | else: |
| 149 | self.protocol = 1 |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 150 | |
| 151 | |
| 152 | def __del__(self): |
| 153 | self.close() |
| 154 | |
| 155 | |
| 156 | def __getattr__(self, name): |
Barry Warsaw | 9914227 | 2003-02-08 03:18:58 +0000 | [diff] [blame] | 157 | """Many methods we can just pass through to the DB object. |
| 158 | (See below) |
| 159 | """ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 160 | 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. Smith | b7de61b | 2007-10-09 07:19:11 +0000 | [diff] [blame] | 176 | data = _dumps(value, self.protocol) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 177 | 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 Melotti | 8d3f130 | 2010-02-02 15:57:45 +0000 | [diff] [blame] | 185 | if txn is not None: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 186 | return self.db.keys(txn) |
| 187 | else: |
| 188 | return self.db.keys() |
| 189 | |
Jesus Cea | 6557aac | 2010-03-22 14:22:26 +0000 | [diff] [blame] | 190 | 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öwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 201 | |
Gregory P. Smith | 5d743fd | 2007-10-13 23:02:05 +0000 | [diff] [blame] | 202 | 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öwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 219 | def items(self, txn=None): |
Ezio Melotti | 8d3f130 | 2010-02-02 15:57:45 +0000 | [diff] [blame] | 220 | if txn is not None: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 221 | 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 Melotti | 8d3f130 | 2010-02-02 15:57:45 +0000 | [diff] [blame] | 231 | if txn is not None: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 232 | 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. Smith | 1281f76 | 2004-03-16 18:50:26 +0000 | [diff] [blame] | 241 | def __append(self, value, txn=None): |
Gregory P. Smith | b7de61b | 2007-10-09 07:19:11 +0000 | [diff] [blame] | 242 | data = _dumps(value, self.protocol) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 243 | return self.db.append(data, txn) |
| 244 | |
Gregory P. Smith | 1281f76 | 2004-03-16 18:50:26 +0000 | [diff] [blame] | 245 | def append(self, value, txn=None): |
Gregory P. Smith | d40f126 | 2007-10-12 18:44:06 +0000 | [diff] [blame] | 246 | if self.get_type() == db.DB_RECNO: |
Gregory P. Smith | 5d743fd | 2007-10-13 23:02:05 +0000 | [diff] [blame] | 247 | return self.__append(value, txn=txn) |
Gregory P. Smith | d40f126 | 2007-10-12 18:44:06 +0000 | [diff] [blame] | 248 | raise DBShelveError, "append() only supported when dbshelve opened with filetype=dbshelve.db.DB_RECNO" |
Gregory P. Smith | 1281f76 | 2004-03-16 18:50:26 +0000 | [diff] [blame] | 249 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 250 | |
| 251 | def associate(self, secondaryDB, callback, flags=0): |
| 252 | def _shelf_callback(priKey, priData, realCallback=callback): |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 253 | # 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öwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 258 | return realCallback(priKey, data) |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 259 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 260 | 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 Pitrou | 63b0cb2 | 2009-10-14 18:01:33 +0000 | [diff] [blame] | 269 | data = self.db.get(*args, **kw) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 270 | try: |
| 271 | return cPickle.loads(data) |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 272 | except (EOFError, TypeError, cPickle.UnpicklingError): |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 273 | 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. Smith | b7de61b | 2007-10-09 07:19:11 +0000 | [diff] [blame] | 277 | data = _dumps(value, self.protocol) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 278 | 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. Smith | b7de61b | 2007-10-09 07:19:11 +0000 | [diff] [blame] | 284 | c.protocol = self.protocol |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 285 | return c |
| 286 | |
| 287 | |
| 288 | def put(self, key, value, txn=None, flags=0): |
Gregory P. Smith | b7de61b | 2007-10-09 07:19:11 +0000 | [diff] [blame] | 289 | data = _dumps(value, self.protocol) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 290 | 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 | |
| 307 | class 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. Smith | b7de61b | 2007-10-09 07:19:11 +0000 | [diff] [blame] | 325 | c = DBShelfCursor(self.dbc.dup(flags)) |
| 326 | c.protocol = self.protocol |
| 327 | return c |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 328 | |
| 329 | |
| 330 | def put(self, key, value, flags=0): |
Gregory P. Smith | b7de61b | 2007-10-09 07:19:11 +0000 | [diff] [blame] | 331 | data = _dumps(value, self.protocol) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 332 | 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 Pitrou | 63b0cb2 | 2009-10-14 18:01:33 +0000 | [diff] [blame] | 338 | method(*args) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 339 | |
| 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. Smith | b7de61b | 2007-10-09 07:19:11 +0000 | [diff] [blame] | 349 | data = _dumps(value, self.protocol) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 350 | 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. Smith | b7de61b | 2007-10-09 07:19:11 +0000 | [diff] [blame] | 366 | data = _dumps(value, self.protocol) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 367 | 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 Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 390 | # 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öwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 395 | |
| 396 | #---------------------------------------------- |
| 397 | # Methods allowed to pass-through to self.dbc |
| 398 | # |
| 399 | # close, count, delete, get_recno, join_item |
| 400 | |
| 401 | |
| 402 | #--------------------------------------------------------------------------- |