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