| 1 | n/a | #!/usr/bin/env python |
|---|
| 2 | n/a | #------------------------------------------------------------------------ |
|---|
| 3 | n/a | # Copyright (c) 1997-2001 by Total Control Software |
|---|
| 4 | n/a | # All Rights Reserved |
|---|
| 5 | n/a | #------------------------------------------------------------------------ |
|---|
| 6 | n/a | # |
|---|
| 7 | n/a | # Module Name: dbShelve.py |
|---|
| 8 | n/a | # |
|---|
| 9 | n/a | # Description: A reimplementation of the standard shelve.py that |
|---|
| 10 | n/a | # forces the use of cPickle, and DB. |
|---|
| 11 | n/a | # |
|---|
| 12 | n/a | # Creation Date: 11/3/97 3:39:04PM |
|---|
| 13 | n/a | # |
|---|
| 14 | n/a | # License: This is free software. You may use this software for any |
|---|
| 15 | n/a | # purpose including modification/redistribution, so long as |
|---|
| 16 | n/a | # this header remains intact and that you do not claim any |
|---|
| 17 | n/a | # rights of ownership or authorship of this software. This |
|---|
| 18 | n/a | # software has been tested, but no warranty is expressed or |
|---|
| 19 | n/a | # implied. |
|---|
| 20 | n/a | # |
|---|
| 21 | n/a | # 13-Dec-2000: Updated to be used with the new bsddb3 package. |
|---|
| 22 | n/a | # Added DBShelfCursor class. |
|---|
| 23 | n/a | # |
|---|
| 24 | n/a | #------------------------------------------------------------------------ |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | """Manage shelves of pickled objects using bsddb database files for the |
|---|
| 27 | n/a | storage. |
|---|
| 28 | n/a | """ |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | #------------------------------------------------------------------------ |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | import sys |
|---|
| 33 | n/a | absolute_import = (sys.version_info[0] >= 3) |
|---|
| 34 | n/a | if absolute_import : |
|---|
| 35 | n/a | # Because this syntaxis is not valid before Python 2.5 |
|---|
| 36 | n/a | exec("from . import db") |
|---|
| 37 | n/a | else : |
|---|
| 38 | n/a | import db |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | if sys.version_info[0] >= 3 : |
|---|
| 41 | n/a | import cPickle # Will be converted to "pickle" by "2to3" |
|---|
| 42 | n/a | else : |
|---|
| 43 | n/a | if sys.version_info < (2, 6) : |
|---|
| 44 | n/a | import cPickle |
|---|
| 45 | n/a | else : |
|---|
| 46 | n/a | # When we drop support for python 2.3 and 2.4 |
|---|
| 47 | n/a | # we could use: (in 2.5 we need a __future__ statement) |
|---|
| 48 | n/a | # |
|---|
| 49 | n/a | # with warnings.catch_warnings(): |
|---|
| 50 | n/a | # warnings.filterwarnings(...) |
|---|
| 51 | n/a | # ... |
|---|
| 52 | n/a | # |
|---|
| 53 | n/a | # We can not use "with" as is, because it would be invalid syntax |
|---|
| 54 | n/a | # in python 2.3, 2.4 and (with no __future__) 2.5. |
|---|
| 55 | n/a | # Here we simulate "with" following PEP 343 : |
|---|
| 56 | n/a | import warnings |
|---|
| 57 | n/a | w = warnings.catch_warnings() |
|---|
| 58 | n/a | w.__enter__() |
|---|
| 59 | n/a | try : |
|---|
| 60 | n/a | warnings.filterwarnings('ignore', |
|---|
| 61 | n/a | message='the cPickle module has been removed in Python 3.0', |
|---|
| 62 | n/a | category=DeprecationWarning) |
|---|
| 63 | n/a | import cPickle |
|---|
| 64 | n/a | finally : |
|---|
| 65 | n/a | w.__exit__() |
|---|
| 66 | n/a | del w |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | #At version 2.3 cPickle switched to using protocol instead of bin |
|---|
| 69 | n/a | if sys.version_info >= (2, 3): |
|---|
| 70 | n/a | HIGHEST_PROTOCOL = cPickle.HIGHEST_PROTOCOL |
|---|
| 71 | n/a | # In python 2.3.*, "cPickle.dumps" accepts no |
|---|
| 72 | n/a | # named parameters. "pickle.dumps" accepts them, |
|---|
| 73 | n/a | # so this seems a bug. |
|---|
| 74 | n/a | if sys.version_info < (2, 4): |
|---|
| 75 | n/a | def _dumps(object, protocol): |
|---|
| 76 | n/a | return cPickle.dumps(object, protocol) |
|---|
| 77 | n/a | else : |
|---|
| 78 | n/a | def _dumps(object, protocol): |
|---|
| 79 | n/a | return cPickle.dumps(object, protocol=protocol) |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | else: |
|---|
| 82 | n/a | HIGHEST_PROTOCOL = None |
|---|
| 83 | n/a | def _dumps(object, protocol): |
|---|
| 84 | n/a | return cPickle.dumps(object, bin=protocol) |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | if sys.version_info < (2, 6) : |
|---|
| 88 | n/a | try: |
|---|
| 89 | n/a | from UserDict import DictMixin |
|---|
| 90 | n/a | except ImportError: |
|---|
| 91 | n/a | # DictMixin is new in Python 2.3 |
|---|
| 92 | n/a | class DictMixin: pass |
|---|
| 93 | n/a | MutableMapping = DictMixin |
|---|
| 94 | n/a | else : |
|---|
| 95 | n/a | import collections |
|---|
| 96 | n/a | MutableMapping = collections.MutableMapping |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | #------------------------------------------------------------------------ |
|---|
| 99 | n/a | |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | def open(filename, flags=db.DB_CREATE, mode=0660, filetype=db.DB_HASH, |
|---|
| 102 | n/a | dbenv=None, dbname=None): |
|---|
| 103 | n/a | """ |
|---|
| 104 | n/a | A simple factory function for compatibility with the standard |
|---|
| 105 | n/a | shleve.py module. It can be used like this, where key is a string |
|---|
| 106 | n/a | and data is a pickleable object: |
|---|
| 107 | n/a | |
|---|
| 108 | n/a | from bsddb import dbshelve |
|---|
| 109 | n/a | db = dbshelve.open(filename) |
|---|
| 110 | n/a | |
|---|
| 111 | n/a | db[key] = data |
|---|
| 112 | n/a | |
|---|
| 113 | n/a | db.close() |
|---|
| 114 | n/a | """ |
|---|
| 115 | n/a | if type(flags) == type(''): |
|---|
| 116 | n/a | sflag = flags |
|---|
| 117 | n/a | if sflag == 'r': |
|---|
| 118 | n/a | flags = db.DB_RDONLY |
|---|
| 119 | n/a | elif sflag == 'rw': |
|---|
| 120 | n/a | flags = 0 |
|---|
| 121 | n/a | elif sflag == 'w': |
|---|
| 122 | n/a | flags = db.DB_CREATE |
|---|
| 123 | n/a | elif sflag == 'c': |
|---|
| 124 | n/a | flags = db.DB_CREATE |
|---|
| 125 | n/a | elif sflag == 'n': |
|---|
| 126 | n/a | flags = db.DB_TRUNCATE | db.DB_CREATE |
|---|
| 127 | n/a | else: |
|---|
| 128 | n/a | raise db.DBError, "flags should be one of 'r', 'w', 'c' or 'n' or use the bsddb.db.DB_* flags" |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | d = DBShelf(dbenv) |
|---|
| 131 | n/a | d.open(filename, dbname, filetype, flags, mode) |
|---|
| 132 | n/a | return d |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | #--------------------------------------------------------------------------- |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | class DBShelveError(db.DBError): pass |
|---|
| 137 | n/a | |
|---|
| 138 | n/a | |
|---|
| 139 | n/a | class DBShelf(MutableMapping): |
|---|
| 140 | n/a | """A shelf to hold pickled objects, built upon a bsddb DB object. It |
|---|
| 141 | n/a | automatically pickles/unpickles data objects going to/from the DB. |
|---|
| 142 | n/a | """ |
|---|
| 143 | n/a | def __init__(self, dbenv=None): |
|---|
| 144 | n/a | self.db = db.DB(dbenv) |
|---|
| 145 | n/a | self._closed = True |
|---|
| 146 | n/a | if HIGHEST_PROTOCOL: |
|---|
| 147 | n/a | self.protocol = HIGHEST_PROTOCOL |
|---|
| 148 | n/a | else: |
|---|
| 149 | n/a | self.protocol = 1 |
|---|
| 150 | n/a | |
|---|
| 151 | n/a | |
|---|
| 152 | n/a | def __del__(self): |
|---|
| 153 | n/a | self.close() |
|---|
| 154 | n/a | |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | def __getattr__(self, name): |
|---|
| 157 | n/a | """Many methods we can just pass through to the DB object. |
|---|
| 158 | n/a | (See below) |
|---|
| 159 | n/a | """ |
|---|
| 160 | n/a | return getattr(self.db, name) |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | |
|---|
| 163 | n/a | #----------------------------------- |
|---|
| 164 | n/a | # Dictionary access methods |
|---|
| 165 | n/a | |
|---|
| 166 | n/a | def __len__(self): |
|---|
| 167 | n/a | return len(self.db) |
|---|
| 168 | n/a | |
|---|
| 169 | n/a | |
|---|
| 170 | n/a | def __getitem__(self, key): |
|---|
| 171 | n/a | data = self.db[key] |
|---|
| 172 | n/a | return cPickle.loads(data) |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | |
|---|
| 175 | n/a | def __setitem__(self, key, value): |
|---|
| 176 | n/a | data = _dumps(value, self.protocol) |
|---|
| 177 | n/a | self.db[key] = data |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | def __delitem__(self, key): |
|---|
| 181 | n/a | del self.db[key] |
|---|
| 182 | n/a | |
|---|
| 183 | n/a | |
|---|
| 184 | n/a | def keys(self, txn=None): |
|---|
| 185 | n/a | if txn is not None: |
|---|
| 186 | n/a | return self.db.keys(txn) |
|---|
| 187 | n/a | else: |
|---|
| 188 | n/a | return self.db.keys() |
|---|
| 189 | n/a | |
|---|
| 190 | n/a | if sys.version_info >= (2, 6) : |
|---|
| 191 | n/a | def __iter__(self) : # XXX: Load all keys in memory :-( |
|---|
| 192 | n/a | for k in self.db.keys() : |
|---|
| 193 | n/a | yield k |
|---|
| 194 | n/a | |
|---|
| 195 | n/a | # Do this when "DB" support iteration |
|---|
| 196 | n/a | # Or is it enough to pass thru "getattr"? |
|---|
| 197 | n/a | # |
|---|
| 198 | n/a | # def __iter__(self) : |
|---|
| 199 | n/a | # return self.db.__iter__() |
|---|
| 200 | n/a | |
|---|
| 201 | n/a | |
|---|
| 202 | n/a | def open(self, *args, **kwargs): |
|---|
| 203 | n/a | self.db.open(*args, **kwargs) |
|---|
| 204 | n/a | self._closed = False |
|---|
| 205 | n/a | |
|---|
| 206 | n/a | |
|---|
| 207 | n/a | def close(self, *args, **kwargs): |
|---|
| 208 | n/a | self.db.close(*args, **kwargs) |
|---|
| 209 | n/a | self._closed = True |
|---|
| 210 | n/a | |
|---|
| 211 | n/a | |
|---|
| 212 | n/a | def __repr__(self): |
|---|
| 213 | n/a | if self._closed: |
|---|
| 214 | n/a | return '<DBShelf @ 0x%x - closed>' % (id(self)) |
|---|
| 215 | n/a | else: |
|---|
| 216 | n/a | return repr(dict(self.iteritems())) |
|---|
| 217 | n/a | |
|---|
| 218 | n/a | |
|---|
| 219 | n/a | def items(self, txn=None): |
|---|
| 220 | n/a | if txn is not None: |
|---|
| 221 | n/a | items = self.db.items(txn) |
|---|
| 222 | n/a | else: |
|---|
| 223 | n/a | items = self.db.items() |
|---|
| 224 | n/a | newitems = [] |
|---|
| 225 | n/a | |
|---|
| 226 | n/a | for k, v in items: |
|---|
| 227 | n/a | newitems.append( (k, cPickle.loads(v)) ) |
|---|
| 228 | n/a | return newitems |
|---|
| 229 | n/a | |
|---|
| 230 | n/a | def values(self, txn=None): |
|---|
| 231 | n/a | if txn is not None: |
|---|
| 232 | n/a | values = self.db.values(txn) |
|---|
| 233 | n/a | else: |
|---|
| 234 | n/a | values = self.db.values() |
|---|
| 235 | n/a | |
|---|
| 236 | n/a | return map(cPickle.loads, values) |
|---|
| 237 | n/a | |
|---|
| 238 | n/a | #----------------------------------- |
|---|
| 239 | n/a | # Other methods |
|---|
| 240 | n/a | |
|---|
| 241 | n/a | def __append(self, value, txn=None): |
|---|
| 242 | n/a | data = _dumps(value, self.protocol) |
|---|
| 243 | n/a | return self.db.append(data, txn) |
|---|
| 244 | n/a | |
|---|
| 245 | n/a | def append(self, value, txn=None): |
|---|
| 246 | n/a | if self.get_type() == db.DB_RECNO: |
|---|
| 247 | n/a | return self.__append(value, txn=txn) |
|---|
| 248 | n/a | raise DBShelveError, "append() only supported when dbshelve opened with filetype=dbshelve.db.DB_RECNO" |
|---|
| 249 | n/a | |
|---|
| 250 | n/a | |
|---|
| 251 | n/a | def associate(self, secondaryDB, callback, flags=0): |
|---|
| 252 | n/a | def _shelf_callback(priKey, priData, realCallback=callback): |
|---|
| 253 | n/a | # Safe in Python 2.x because expresion short circuit |
|---|
| 254 | n/a | if sys.version_info[0] < 3 or isinstance(priData, bytes) : |
|---|
| 255 | n/a | data = cPickle.loads(priData) |
|---|
| 256 | n/a | else : |
|---|
| 257 | n/a | data = cPickle.loads(bytes(priData, "iso8859-1")) # 8 bits |
|---|
| 258 | n/a | return realCallback(priKey, data) |
|---|
| 259 | n/a | |
|---|
| 260 | n/a | return self.db.associate(secondaryDB, _shelf_callback, flags) |
|---|
| 261 | n/a | |
|---|
| 262 | n/a | |
|---|
| 263 | n/a | #def get(self, key, default=None, txn=None, flags=0): |
|---|
| 264 | n/a | def get(self, *args, **kw): |
|---|
| 265 | n/a | # We do it with *args and **kw so if the default value wasn't |
|---|
| 266 | n/a | # given nothing is passed to the extension module. That way |
|---|
| 267 | n/a | # an exception can be raised if set_get_returns_none is turned |
|---|
| 268 | n/a | # off. |
|---|
| 269 | n/a | data = self.db.get(*args, **kw) |
|---|
| 270 | n/a | try: |
|---|
| 271 | n/a | return cPickle.loads(data) |
|---|
| 272 | n/a | except (EOFError, TypeError, cPickle.UnpicklingError): |
|---|
| 273 | n/a | return data # we may be getting the default value, or None, |
|---|
| 274 | n/a | # so it doesn't need unpickled. |
|---|
| 275 | n/a | |
|---|
| 276 | n/a | def get_both(self, key, value, txn=None, flags=0): |
|---|
| 277 | n/a | data = _dumps(value, self.protocol) |
|---|
| 278 | n/a | data = self.db.get(key, data, txn, flags) |
|---|
| 279 | n/a | return cPickle.loads(data) |
|---|
| 280 | n/a | |
|---|
| 281 | n/a | |
|---|
| 282 | n/a | def cursor(self, txn=None, flags=0): |
|---|
| 283 | n/a | c = DBShelfCursor(self.db.cursor(txn, flags)) |
|---|
| 284 | n/a | c.protocol = self.protocol |
|---|
| 285 | n/a | return c |
|---|
| 286 | n/a | |
|---|
| 287 | n/a | |
|---|
| 288 | n/a | def put(self, key, value, txn=None, flags=0): |
|---|
| 289 | n/a | data = _dumps(value, self.protocol) |
|---|
| 290 | n/a | return self.db.put(key, data, txn, flags) |
|---|
| 291 | n/a | |
|---|
| 292 | n/a | |
|---|
| 293 | n/a | def join(self, cursorList, flags=0): |
|---|
| 294 | n/a | raise NotImplementedError |
|---|
| 295 | n/a | |
|---|
| 296 | n/a | |
|---|
| 297 | n/a | #---------------------------------------------- |
|---|
| 298 | n/a | # Methods allowed to pass-through to self.db |
|---|
| 299 | n/a | # |
|---|
| 300 | n/a | # close, delete, fd, get_byteswapped, get_type, has_key, |
|---|
| 301 | n/a | # key_range, open, remove, rename, stat, sync, |
|---|
| 302 | n/a | # upgrade, verify, and all set_* methods. |
|---|
| 303 | n/a | |
|---|
| 304 | n/a | |
|---|
| 305 | n/a | #--------------------------------------------------------------------------- |
|---|
| 306 | n/a | |
|---|
| 307 | n/a | class DBShelfCursor: |
|---|
| 308 | n/a | """ |
|---|
| 309 | n/a | """ |
|---|
| 310 | n/a | def __init__(self, cursor): |
|---|
| 311 | n/a | self.dbc = cursor |
|---|
| 312 | n/a | |
|---|
| 313 | n/a | def __del__(self): |
|---|
| 314 | n/a | self.close() |
|---|
| 315 | n/a | |
|---|
| 316 | n/a | |
|---|
| 317 | n/a | def __getattr__(self, name): |
|---|
| 318 | n/a | """Some methods we can just pass through to the cursor object. (See below)""" |
|---|
| 319 | n/a | return getattr(self.dbc, name) |
|---|
| 320 | n/a | |
|---|
| 321 | n/a | |
|---|
| 322 | n/a | #---------------------------------------------- |
|---|
| 323 | n/a | |
|---|
| 324 | n/a | def dup(self, flags=0): |
|---|
| 325 | n/a | c = DBShelfCursor(self.dbc.dup(flags)) |
|---|
| 326 | n/a | c.protocol = self.protocol |
|---|
| 327 | n/a | return c |
|---|
| 328 | n/a | |
|---|
| 329 | n/a | |
|---|
| 330 | n/a | def put(self, key, value, flags=0): |
|---|
| 331 | n/a | data = _dumps(value, self.protocol) |
|---|
| 332 | n/a | return self.dbc.put(key, data, flags) |
|---|
| 333 | n/a | |
|---|
| 334 | n/a | |
|---|
| 335 | n/a | def get(self, *args): |
|---|
| 336 | n/a | count = len(args) # a method overloading hack |
|---|
| 337 | n/a | method = getattr(self, 'get_%d' % count) |
|---|
| 338 | n/a | method(*args) |
|---|
| 339 | n/a | |
|---|
| 340 | n/a | def get_1(self, flags): |
|---|
| 341 | n/a | rec = self.dbc.get(flags) |
|---|
| 342 | n/a | return self._extract(rec) |
|---|
| 343 | n/a | |
|---|
| 344 | n/a | def get_2(self, key, flags): |
|---|
| 345 | n/a | rec = self.dbc.get(key, flags) |
|---|
| 346 | n/a | return self._extract(rec) |
|---|
| 347 | n/a | |
|---|
| 348 | n/a | def get_3(self, key, value, flags): |
|---|
| 349 | n/a | data = _dumps(value, self.protocol) |
|---|
| 350 | n/a | rec = self.dbc.get(key, flags) |
|---|
| 351 | n/a | return self._extract(rec) |
|---|
| 352 | n/a | |
|---|
| 353 | n/a | |
|---|
| 354 | n/a | def current(self, flags=0): return self.get_1(flags|db.DB_CURRENT) |
|---|
| 355 | n/a | def first(self, flags=0): return self.get_1(flags|db.DB_FIRST) |
|---|
| 356 | n/a | def last(self, flags=0): return self.get_1(flags|db.DB_LAST) |
|---|
| 357 | n/a | def next(self, flags=0): return self.get_1(flags|db.DB_NEXT) |
|---|
| 358 | n/a | def prev(self, flags=0): return self.get_1(flags|db.DB_PREV) |
|---|
| 359 | n/a | def consume(self, flags=0): return self.get_1(flags|db.DB_CONSUME) |
|---|
| 360 | n/a | def next_dup(self, flags=0): return self.get_1(flags|db.DB_NEXT_DUP) |
|---|
| 361 | n/a | def next_nodup(self, flags=0): return self.get_1(flags|db.DB_NEXT_NODUP) |
|---|
| 362 | n/a | def prev_nodup(self, flags=0): return self.get_1(flags|db.DB_PREV_NODUP) |
|---|
| 363 | n/a | |
|---|
| 364 | n/a | |
|---|
| 365 | n/a | def get_both(self, key, value, flags=0): |
|---|
| 366 | n/a | data = _dumps(value, self.protocol) |
|---|
| 367 | n/a | rec = self.dbc.get_both(key, flags) |
|---|
| 368 | n/a | return self._extract(rec) |
|---|
| 369 | n/a | |
|---|
| 370 | n/a | |
|---|
| 371 | n/a | def set(self, key, flags=0): |
|---|
| 372 | n/a | rec = self.dbc.set(key, flags) |
|---|
| 373 | n/a | return self._extract(rec) |
|---|
| 374 | n/a | |
|---|
| 375 | n/a | def set_range(self, key, flags=0): |
|---|
| 376 | n/a | rec = self.dbc.set_range(key, flags) |
|---|
| 377 | n/a | return self._extract(rec) |
|---|
| 378 | n/a | |
|---|
| 379 | n/a | def set_recno(self, recno, flags=0): |
|---|
| 380 | n/a | rec = self.dbc.set_recno(recno, flags) |
|---|
| 381 | n/a | return self._extract(rec) |
|---|
| 382 | n/a | |
|---|
| 383 | n/a | set_both = get_both |
|---|
| 384 | n/a | |
|---|
| 385 | n/a | def _extract(self, rec): |
|---|
| 386 | n/a | if rec is None: |
|---|
| 387 | n/a | return None |
|---|
| 388 | n/a | else: |
|---|
| 389 | n/a | key, data = rec |
|---|
| 390 | n/a | # Safe in Python 2.x because expresion short circuit |
|---|
| 391 | n/a | if sys.version_info[0] < 3 or isinstance(data, bytes) : |
|---|
| 392 | n/a | return key, cPickle.loads(data) |
|---|
| 393 | n/a | else : |
|---|
| 394 | n/a | return key, cPickle.loads(bytes(data, "iso8859-1")) # 8 bits |
|---|
| 395 | n/a | |
|---|
| 396 | n/a | #---------------------------------------------- |
|---|
| 397 | n/a | # Methods allowed to pass-through to self.dbc |
|---|
| 398 | n/a | # |
|---|
| 399 | n/a | # close, count, delete, get_recno, join_item |
|---|
| 400 | n/a | |
|---|
| 401 | n/a | |
|---|
| 402 | n/a | #--------------------------------------------------------------------------- |
|---|