Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python2....
File: dumbdbm.py
"""A dumb and slow but simple dbm clone.
[0] Fix | Delete
[1] Fix | Delete
For database spam, spam.dir contains the index (a text file),
[2] Fix | Delete
spam.bak *may* contain a backup of the index (also a text file),
[3] Fix | Delete
while spam.dat contains the data (a binary file).
[4] Fix | Delete
[5] Fix | Delete
XXX TO DO:
[6] Fix | Delete
[7] Fix | Delete
- seems to contain a bug when updating...
[8] Fix | Delete
[9] Fix | Delete
- reclaim free space (currently, space once occupied by deleted or expanded
[10] Fix | Delete
items is never reused)
[11] Fix | Delete
[12] Fix | Delete
- support concurrent access (currently, if two processes take turns making
[13] Fix | Delete
updates, they can mess up the index)
[14] Fix | Delete
[15] Fix | Delete
- support efficient access to large databases (currently, the whole index
[16] Fix | Delete
is read when the database is opened, and some updates rewrite the whole index)
[17] Fix | Delete
[18] Fix | Delete
- support opening for read-only (flag = 'm')
[19] Fix | Delete
[20] Fix | Delete
"""
[21] Fix | Delete
[22] Fix | Delete
import ast as _ast
[23] Fix | Delete
import os as _os
[24] Fix | Delete
import __builtin__
[25] Fix | Delete
import UserDict
[26] Fix | Delete
[27] Fix | Delete
_open = __builtin__.open
[28] Fix | Delete
[29] Fix | Delete
_BLOCKSIZE = 512
[30] Fix | Delete
[31] Fix | Delete
error = IOError # For anydbm
[32] Fix | Delete
[33] Fix | Delete
class _Database(UserDict.DictMixin):
[34] Fix | Delete
[35] Fix | Delete
# The on-disk directory and data files can remain in mutually
[36] Fix | Delete
# inconsistent states for an arbitrarily long time (see comments
[37] Fix | Delete
# at the end of __setitem__). This is only repaired when _commit()
[38] Fix | Delete
# gets called. One place _commit() gets called is from __del__(),
[39] Fix | Delete
# and if that occurs at program shutdown time, module globals may
[40] Fix | Delete
# already have gotten rebound to None. Since it's crucial that
[41] Fix | Delete
# _commit() finish successfully, we can't ignore shutdown races
[42] Fix | Delete
# here, and _commit() must not reference any globals.
[43] Fix | Delete
_os = _os # for _commit()
[44] Fix | Delete
_open = _open # for _commit()
[45] Fix | Delete
[46] Fix | Delete
def __init__(self, filebasename, mode):
[47] Fix | Delete
self._mode = mode
[48] Fix | Delete
[49] Fix | Delete
# The directory file is a text file. Each line looks like
[50] Fix | Delete
# "%r, (%d, %d)\n" % (key, pos, siz)
[51] Fix | Delete
# where key is the string key, pos is the offset into the dat
[52] Fix | Delete
# file of the associated value's first byte, and siz is the number
[53] Fix | Delete
# of bytes in the associated value.
[54] Fix | Delete
self._dirfile = filebasename + _os.extsep + 'dir'
[55] Fix | Delete
[56] Fix | Delete
# The data file is a binary file pointed into by the directory
[57] Fix | Delete
# file, and holds the values associated with keys. Each value
[58] Fix | Delete
# begins at a _BLOCKSIZE-aligned byte offset, and is a raw
[59] Fix | Delete
# binary 8-bit string value.
[60] Fix | Delete
self._datfile = filebasename + _os.extsep + 'dat'
[61] Fix | Delete
self._bakfile = filebasename + _os.extsep + 'bak'
[62] Fix | Delete
[63] Fix | Delete
# The index is an in-memory dict, mirroring the directory file.
[64] Fix | Delete
self._index = None # maps keys to (pos, siz) pairs
[65] Fix | Delete
[66] Fix | Delete
# Mod by Jack: create data file if needed
[67] Fix | Delete
try:
[68] Fix | Delete
f = _open(self._datfile, 'r')
[69] Fix | Delete
except IOError:
[70] Fix | Delete
with _open(self._datfile, 'w') as f:
[71] Fix | Delete
self._chmod(self._datfile)
[72] Fix | Delete
else:
[73] Fix | Delete
f.close()
[74] Fix | Delete
self._update()
[75] Fix | Delete
[76] Fix | Delete
# Read directory file into the in-memory index dict.
[77] Fix | Delete
def _update(self):
[78] Fix | Delete
self._index = {}
[79] Fix | Delete
try:
[80] Fix | Delete
f = _open(self._dirfile)
[81] Fix | Delete
except IOError:
[82] Fix | Delete
pass
[83] Fix | Delete
else:
[84] Fix | Delete
with f:
[85] Fix | Delete
for line in f:
[86] Fix | Delete
line = line.rstrip()
[87] Fix | Delete
key, pos_and_siz_pair = _ast.literal_eval(line)
[88] Fix | Delete
self._index[key] = pos_and_siz_pair
[89] Fix | Delete
[90] Fix | Delete
# Write the index dict to the directory file. The original directory
[91] Fix | Delete
# file (if any) is renamed with a .bak extension first. If a .bak
[92] Fix | Delete
# file currently exists, it's deleted.
[93] Fix | Delete
def _commit(self):
[94] Fix | Delete
# CAUTION: It's vital that _commit() succeed, and _commit() can
[95] Fix | Delete
# be called from __del__(). Therefore we must never reference a
[96] Fix | Delete
# global in this routine.
[97] Fix | Delete
if self._index is None:
[98] Fix | Delete
return # nothing to do
[99] Fix | Delete
[100] Fix | Delete
try:
[101] Fix | Delete
self._os.unlink(self._bakfile)
[102] Fix | Delete
except self._os.error:
[103] Fix | Delete
pass
[104] Fix | Delete
[105] Fix | Delete
try:
[106] Fix | Delete
self._os.rename(self._dirfile, self._bakfile)
[107] Fix | Delete
except self._os.error:
[108] Fix | Delete
pass
[109] Fix | Delete
[110] Fix | Delete
with self._open(self._dirfile, 'w') as f:
[111] Fix | Delete
self._chmod(self._dirfile)
[112] Fix | Delete
for key, pos_and_siz_pair in self._index.iteritems():
[113] Fix | Delete
f.write("%r, %r\n" % (key, pos_and_siz_pair))
[114] Fix | Delete
[115] Fix | Delete
sync = _commit
[116] Fix | Delete
[117] Fix | Delete
def __getitem__(self, key):
[118] Fix | Delete
pos, siz = self._index[key] # may raise KeyError
[119] Fix | Delete
with _open(self._datfile, 'rb') as f:
[120] Fix | Delete
f.seek(pos)
[121] Fix | Delete
dat = f.read(siz)
[122] Fix | Delete
return dat
[123] Fix | Delete
[124] Fix | Delete
# Append val to the data file, starting at a _BLOCKSIZE-aligned
[125] Fix | Delete
# offset. The data file is first padded with NUL bytes (if needed)
[126] Fix | Delete
# to get to an aligned offset. Return pair
[127] Fix | Delete
# (starting offset of val, len(val))
[128] Fix | Delete
def _addval(self, val):
[129] Fix | Delete
with _open(self._datfile, 'rb+') as f:
[130] Fix | Delete
f.seek(0, 2)
[131] Fix | Delete
pos = int(f.tell())
[132] Fix | Delete
npos = ((pos + _BLOCKSIZE - 1) // _BLOCKSIZE) * _BLOCKSIZE
[133] Fix | Delete
f.write('\0'*(npos-pos))
[134] Fix | Delete
pos = npos
[135] Fix | Delete
f.write(val)
[136] Fix | Delete
return (pos, len(val))
[137] Fix | Delete
[138] Fix | Delete
# Write val to the data file, starting at offset pos. The caller
[139] Fix | Delete
# is responsible for ensuring that there's enough room starting at
[140] Fix | Delete
# pos to hold val, without overwriting some other value. Return
[141] Fix | Delete
# pair (pos, len(val)).
[142] Fix | Delete
def _setval(self, pos, val):
[143] Fix | Delete
with _open(self._datfile, 'rb+') as f:
[144] Fix | Delete
f.seek(pos)
[145] Fix | Delete
f.write(val)
[146] Fix | Delete
return (pos, len(val))
[147] Fix | Delete
[148] Fix | Delete
# key is a new key whose associated value starts in the data file
[149] Fix | Delete
# at offset pos and with length siz. Add an index record to
[150] Fix | Delete
# the in-memory index dict, and append one to the directory file.
[151] Fix | Delete
def _addkey(self, key, pos_and_siz_pair):
[152] Fix | Delete
self._index[key] = pos_and_siz_pair
[153] Fix | Delete
with _open(self._dirfile, 'a') as f:
[154] Fix | Delete
self._chmod(self._dirfile)
[155] Fix | Delete
f.write("%r, %r\n" % (key, pos_and_siz_pair))
[156] Fix | Delete
[157] Fix | Delete
def __setitem__(self, key, val):
[158] Fix | Delete
if not type(key) == type('') == type(val):
[159] Fix | Delete
raise TypeError, "keys and values must be strings"
[160] Fix | Delete
if key not in self._index:
[161] Fix | Delete
self._addkey(key, self._addval(val))
[162] Fix | Delete
else:
[163] Fix | Delete
# See whether the new value is small enough to fit in the
[164] Fix | Delete
# (padded) space currently occupied by the old value.
[165] Fix | Delete
pos, siz = self._index[key]
[166] Fix | Delete
oldblocks = (siz + _BLOCKSIZE - 1) // _BLOCKSIZE
[167] Fix | Delete
newblocks = (len(val) + _BLOCKSIZE - 1) // _BLOCKSIZE
[168] Fix | Delete
if newblocks <= oldblocks:
[169] Fix | Delete
self._index[key] = self._setval(pos, val)
[170] Fix | Delete
else:
[171] Fix | Delete
# The new value doesn't fit in the (padded) space used
[172] Fix | Delete
# by the old value. The blocks used by the old value are
[173] Fix | Delete
# forever lost.
[174] Fix | Delete
self._index[key] = self._addval(val)
[175] Fix | Delete
[176] Fix | Delete
# Note that _index may be out of synch with the directory
[177] Fix | Delete
# file now: _setval() and _addval() don't update the directory
[178] Fix | Delete
# file. This also means that the on-disk directory and data
[179] Fix | Delete
# files are in a mutually inconsistent state, and they'll
[180] Fix | Delete
# remain that way until _commit() is called. Note that this
[181] Fix | Delete
# is a disaster (for the database) if the program crashes
[182] Fix | Delete
# (so that _commit() never gets called).
[183] Fix | Delete
[184] Fix | Delete
def __delitem__(self, key):
[185] Fix | Delete
# The blocks used by the associated value are lost.
[186] Fix | Delete
del self._index[key]
[187] Fix | Delete
# XXX It's unclear why we do a _commit() here (the code always
[188] Fix | Delete
# XXX has, so I'm not changing it). _setitem__ doesn't try to
[189] Fix | Delete
# XXX keep the directory file in synch. Why should we? Or
[190] Fix | Delete
# XXX why shouldn't __setitem__?
[191] Fix | Delete
self._commit()
[192] Fix | Delete
[193] Fix | Delete
def keys(self):
[194] Fix | Delete
return self._index.keys()
[195] Fix | Delete
[196] Fix | Delete
def has_key(self, key):
[197] Fix | Delete
return key in self._index
[198] Fix | Delete
[199] Fix | Delete
def __contains__(self, key):
[200] Fix | Delete
return key in self._index
[201] Fix | Delete
[202] Fix | Delete
def iterkeys(self):
[203] Fix | Delete
return self._index.iterkeys()
[204] Fix | Delete
__iter__ = iterkeys
[205] Fix | Delete
[206] Fix | Delete
def __len__(self):
[207] Fix | Delete
return len(self._index)
[208] Fix | Delete
[209] Fix | Delete
def close(self):
[210] Fix | Delete
try:
[211] Fix | Delete
self._commit()
[212] Fix | Delete
finally:
[213] Fix | Delete
self._index = self._datfile = self._dirfile = self._bakfile = None
[214] Fix | Delete
[215] Fix | Delete
__del__ = close
[216] Fix | Delete
[217] Fix | Delete
def _chmod (self, file):
[218] Fix | Delete
if hasattr(self._os, 'chmod'):
[219] Fix | Delete
self._os.chmod(file, self._mode)
[220] Fix | Delete
[221] Fix | Delete
[222] Fix | Delete
def open(file, flag=None, mode=0666):
[223] Fix | Delete
"""Open the database file, filename, and return corresponding object.
[224] Fix | Delete
[225] Fix | Delete
The flag argument, used to control how the database is opened in the
[226] Fix | Delete
other DBM implementations, is ignored in the dumbdbm module; the
[227] Fix | Delete
database is always opened for update, and will be created if it does
[228] Fix | Delete
not exist.
[229] Fix | Delete
[230] Fix | Delete
The optional mode argument is the UNIX mode of the file, used only when
[231] Fix | Delete
the database has to be created. It defaults to octal code 0666 (and
[232] Fix | Delete
will be modified by the prevailing umask).
[233] Fix | Delete
[234] Fix | Delete
"""
[235] Fix | Delete
# flag argument is currently ignored
[236] Fix | Delete
[237] Fix | Delete
# Modify mode depending on the umask
[238] Fix | Delete
try:
[239] Fix | Delete
um = _os.umask(0)
[240] Fix | Delete
_os.umask(um)
[241] Fix | Delete
except AttributeError:
[242] Fix | Delete
pass
[243] Fix | Delete
else:
[244] Fix | Delete
# Turn off any bits that are set in the umask
[245] Fix | Delete
mode = mode & (~um)
[246] Fix | Delete
[247] Fix | Delete
return _Database(file, mode)
[248] Fix | Delete
[249] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function