Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3..../dbm
File: dumb.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 io as _io
[24] Fix | Delete
import os as _os
[25] Fix | Delete
import collections.abc
[26] Fix | Delete
[27] Fix | Delete
__all__ = ["error", "open"]
[28] Fix | Delete
[29] Fix | Delete
_BLOCKSIZE = 512
[30] Fix | Delete
[31] Fix | Delete
error = OSError
[32] Fix | Delete
[33] Fix | Delete
class _Database(collections.abc.MutableMapping):
[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
_io = _io # for _commit()
[45] Fix | Delete
[46] Fix | Delete
def __init__(self, filebasename, mode, flag='c'):
[47] Fix | Delete
self._mode = mode
[48] Fix | Delete
self._readonly = (flag == 'r')
[49] Fix | Delete
[50] Fix | Delete
# The directory file is a text file. Each line looks like
[51] Fix | Delete
# "%r, (%d, %d)\n" % (key, pos, siz)
[52] Fix | Delete
# where key is the string key, pos is the offset into the dat
[53] Fix | Delete
# file of the associated value's first byte, and siz is the number
[54] Fix | Delete
# of bytes in the associated value.
[55] Fix | Delete
self._dirfile = filebasename + '.dir'
[56] Fix | Delete
[57] Fix | Delete
# The data file is a binary file pointed into by the directory
[58] Fix | Delete
# file, and holds the values associated with keys. Each value
[59] Fix | Delete
# begins at a _BLOCKSIZE-aligned byte offset, and is a raw
[60] Fix | Delete
# binary 8-bit string value.
[61] Fix | Delete
self._datfile = filebasename + '.dat'
[62] Fix | Delete
self._bakfile = filebasename + '.bak'
[63] Fix | Delete
[64] Fix | Delete
# The index is an in-memory dict, mirroring the directory file.
[65] Fix | Delete
self._index = None # maps keys to (pos, siz) pairs
[66] Fix | Delete
[67] Fix | Delete
# Handle the creation
[68] Fix | Delete
self._create(flag)
[69] Fix | Delete
self._update(flag)
[70] Fix | Delete
[71] Fix | Delete
def _create(self, flag):
[72] Fix | Delete
if flag == 'n':
[73] Fix | Delete
for filename in (self._datfile, self._bakfile, self._dirfile):
[74] Fix | Delete
try:
[75] Fix | Delete
_os.remove(filename)
[76] Fix | Delete
except OSError:
[77] Fix | Delete
pass
[78] Fix | Delete
# Mod by Jack: create data file if needed
[79] Fix | Delete
try:
[80] Fix | Delete
f = _io.open(self._datfile, 'r', encoding="Latin-1")
[81] Fix | Delete
except OSError:
[82] Fix | Delete
if flag not in ('c', 'n'):
[83] Fix | Delete
raise
[84] Fix | Delete
with _io.open(self._datfile, 'w', encoding="Latin-1") as f:
[85] Fix | Delete
self._chmod(self._datfile)
[86] Fix | Delete
else:
[87] Fix | Delete
f.close()
[88] Fix | Delete
[89] Fix | Delete
# Read directory file into the in-memory index dict.
[90] Fix | Delete
def _update(self, flag):
[91] Fix | Delete
self._modified = False
[92] Fix | Delete
self._index = {}
[93] Fix | Delete
try:
[94] Fix | Delete
f = _io.open(self._dirfile, 'r', encoding="Latin-1")
[95] Fix | Delete
except OSError:
[96] Fix | Delete
if flag not in ('c', 'n'):
[97] Fix | Delete
raise
[98] Fix | Delete
self._modified = True
[99] Fix | Delete
else:
[100] Fix | Delete
with f:
[101] Fix | Delete
for line in f:
[102] Fix | Delete
line = line.rstrip()
[103] Fix | Delete
key, pos_and_siz_pair = _ast.literal_eval(line)
[104] Fix | Delete
key = key.encode('Latin-1')
[105] Fix | Delete
self._index[key] = pos_and_siz_pair
[106] Fix | Delete
[107] Fix | Delete
# Write the index dict to the directory file. The original directory
[108] Fix | Delete
# file (if any) is renamed with a .bak extension first. If a .bak
[109] Fix | Delete
# file currently exists, it's deleted.
[110] Fix | Delete
def _commit(self):
[111] Fix | Delete
# CAUTION: It's vital that _commit() succeed, and _commit() can
[112] Fix | Delete
# be called from __del__(). Therefore we must never reference a
[113] Fix | Delete
# global in this routine.
[114] Fix | Delete
if self._index is None or not self._modified:
[115] Fix | Delete
return # nothing to do
[116] Fix | Delete
[117] Fix | Delete
try:
[118] Fix | Delete
self._os.unlink(self._bakfile)
[119] Fix | Delete
except OSError:
[120] Fix | Delete
pass
[121] Fix | Delete
[122] Fix | Delete
try:
[123] Fix | Delete
self._os.rename(self._dirfile, self._bakfile)
[124] Fix | Delete
except OSError:
[125] Fix | Delete
pass
[126] Fix | Delete
[127] Fix | Delete
with self._io.open(self._dirfile, 'w', encoding="Latin-1") as f:
[128] Fix | Delete
self._chmod(self._dirfile)
[129] Fix | Delete
for key, pos_and_siz_pair in self._index.items():
[130] Fix | Delete
# Use Latin-1 since it has no qualms with any value in any
[131] Fix | Delete
# position; UTF-8, though, does care sometimes.
[132] Fix | Delete
entry = "%r, %r\n" % (key.decode('Latin-1'), pos_and_siz_pair)
[133] Fix | Delete
f.write(entry)
[134] Fix | Delete
[135] Fix | Delete
sync = _commit
[136] Fix | Delete
[137] Fix | Delete
def _verify_open(self):
[138] Fix | Delete
if self._index is None:
[139] Fix | Delete
raise error('DBM object has already been closed')
[140] Fix | Delete
[141] Fix | Delete
def __getitem__(self, key):
[142] Fix | Delete
if isinstance(key, str):
[143] Fix | Delete
key = key.encode('utf-8')
[144] Fix | Delete
self._verify_open()
[145] Fix | Delete
pos, siz = self._index[key] # may raise KeyError
[146] Fix | Delete
with _io.open(self._datfile, 'rb') as f:
[147] Fix | Delete
f.seek(pos)
[148] Fix | Delete
dat = f.read(siz)
[149] Fix | Delete
return dat
[150] Fix | Delete
[151] Fix | Delete
# Append val to the data file, starting at a _BLOCKSIZE-aligned
[152] Fix | Delete
# offset. The data file is first padded with NUL bytes (if needed)
[153] Fix | Delete
# to get to an aligned offset. Return pair
[154] Fix | Delete
# (starting offset of val, len(val))
[155] Fix | Delete
def _addval(self, val):
[156] Fix | Delete
with _io.open(self._datfile, 'rb+') as f:
[157] Fix | Delete
f.seek(0, 2)
[158] Fix | Delete
pos = int(f.tell())
[159] Fix | Delete
npos = ((pos + _BLOCKSIZE - 1) // _BLOCKSIZE) * _BLOCKSIZE
[160] Fix | Delete
f.write(b'\0'*(npos-pos))
[161] Fix | Delete
pos = npos
[162] Fix | Delete
f.write(val)
[163] Fix | Delete
return (pos, len(val))
[164] Fix | Delete
[165] Fix | Delete
# Write val to the data file, starting at offset pos. The caller
[166] Fix | Delete
# is responsible for ensuring that there's enough room starting at
[167] Fix | Delete
# pos to hold val, without overwriting some other value. Return
[168] Fix | Delete
# pair (pos, len(val)).
[169] Fix | Delete
def _setval(self, pos, val):
[170] Fix | Delete
with _io.open(self._datfile, 'rb+') as f:
[171] Fix | Delete
f.seek(pos)
[172] Fix | Delete
f.write(val)
[173] Fix | Delete
return (pos, len(val))
[174] Fix | Delete
[175] Fix | Delete
# key is a new key whose associated value starts in the data file
[176] Fix | Delete
# at offset pos and with length siz. Add an index record to
[177] Fix | Delete
# the in-memory index dict, and append one to the directory file.
[178] Fix | Delete
def _addkey(self, key, pos_and_siz_pair):
[179] Fix | Delete
self._index[key] = pos_and_siz_pair
[180] Fix | Delete
with _io.open(self._dirfile, 'a', encoding="Latin-1") as f:
[181] Fix | Delete
self._chmod(self._dirfile)
[182] Fix | Delete
f.write("%r, %r\n" % (key.decode("Latin-1"), pos_and_siz_pair))
[183] Fix | Delete
[184] Fix | Delete
def __setitem__(self, key, val):
[185] Fix | Delete
if self._readonly:
[186] Fix | Delete
raise error('The database is opened for reading only')
[187] Fix | Delete
if isinstance(key, str):
[188] Fix | Delete
key = key.encode('utf-8')
[189] Fix | Delete
elif not isinstance(key, (bytes, bytearray)):
[190] Fix | Delete
raise TypeError("keys must be bytes or strings")
[191] Fix | Delete
if isinstance(val, str):
[192] Fix | Delete
val = val.encode('utf-8')
[193] Fix | Delete
elif not isinstance(val, (bytes, bytearray)):
[194] Fix | Delete
raise TypeError("values must be bytes or strings")
[195] Fix | Delete
self._verify_open()
[196] Fix | Delete
self._modified = True
[197] Fix | Delete
if key not in self._index:
[198] Fix | Delete
self._addkey(key, self._addval(val))
[199] Fix | Delete
else:
[200] Fix | Delete
# See whether the new value is small enough to fit in the
[201] Fix | Delete
# (padded) space currently occupied by the old value.
[202] Fix | Delete
pos, siz = self._index[key]
[203] Fix | Delete
oldblocks = (siz + _BLOCKSIZE - 1) // _BLOCKSIZE
[204] Fix | Delete
newblocks = (len(val) + _BLOCKSIZE - 1) // _BLOCKSIZE
[205] Fix | Delete
if newblocks <= oldblocks:
[206] Fix | Delete
self._index[key] = self._setval(pos, val)
[207] Fix | Delete
else:
[208] Fix | Delete
# The new value doesn't fit in the (padded) space used
[209] Fix | Delete
# by the old value. The blocks used by the old value are
[210] Fix | Delete
# forever lost.
[211] Fix | Delete
self._index[key] = self._addval(val)
[212] Fix | Delete
[213] Fix | Delete
# Note that _index may be out of synch with the directory
[214] Fix | Delete
# file now: _setval() and _addval() don't update the directory
[215] Fix | Delete
# file. This also means that the on-disk directory and data
[216] Fix | Delete
# files are in a mutually inconsistent state, and they'll
[217] Fix | Delete
# remain that way until _commit() is called. Note that this
[218] Fix | Delete
# is a disaster (for the database) if the program crashes
[219] Fix | Delete
# (so that _commit() never gets called).
[220] Fix | Delete
[221] Fix | Delete
def __delitem__(self, key):
[222] Fix | Delete
if self._readonly:
[223] Fix | Delete
raise error('The database is opened for reading only')
[224] Fix | Delete
if isinstance(key, str):
[225] Fix | Delete
key = key.encode('utf-8')
[226] Fix | Delete
self._verify_open()
[227] Fix | Delete
self._modified = True
[228] Fix | Delete
# The blocks used by the associated value are lost.
[229] Fix | Delete
del self._index[key]
[230] Fix | Delete
# XXX It's unclear why we do a _commit() here (the code always
[231] Fix | Delete
# XXX has, so I'm not changing it). __setitem__ doesn't try to
[232] Fix | Delete
# XXX keep the directory file in synch. Why should we? Or
[233] Fix | Delete
# XXX why shouldn't __setitem__?
[234] Fix | Delete
self._commit()
[235] Fix | Delete
[236] Fix | Delete
def keys(self):
[237] Fix | Delete
try:
[238] Fix | Delete
return list(self._index)
[239] Fix | Delete
except TypeError:
[240] Fix | Delete
raise error('DBM object has already been closed') from None
[241] Fix | Delete
[242] Fix | Delete
def items(self):
[243] Fix | Delete
self._verify_open()
[244] Fix | Delete
return [(key, self[key]) for key in self._index.keys()]
[245] Fix | Delete
[246] Fix | Delete
def __contains__(self, key):
[247] Fix | Delete
if isinstance(key, str):
[248] Fix | Delete
key = key.encode('utf-8')
[249] Fix | Delete
try:
[250] Fix | Delete
return key in self._index
[251] Fix | Delete
except TypeError:
[252] Fix | Delete
if self._index is None:
[253] Fix | Delete
raise error('DBM object has already been closed') from None
[254] Fix | Delete
else:
[255] Fix | Delete
raise
[256] Fix | Delete
[257] Fix | Delete
def iterkeys(self):
[258] Fix | Delete
try:
[259] Fix | Delete
return iter(self._index)
[260] Fix | Delete
except TypeError:
[261] Fix | Delete
raise error('DBM object has already been closed') from None
[262] Fix | Delete
__iter__ = iterkeys
[263] Fix | Delete
[264] Fix | Delete
def __len__(self):
[265] Fix | Delete
try:
[266] Fix | Delete
return len(self._index)
[267] Fix | Delete
except TypeError:
[268] Fix | Delete
raise error('DBM object has already been closed') from None
[269] Fix | Delete
[270] Fix | Delete
def close(self):
[271] Fix | Delete
try:
[272] Fix | Delete
self._commit()
[273] Fix | Delete
finally:
[274] Fix | Delete
self._index = self._datfile = self._dirfile = self._bakfile = None
[275] Fix | Delete
[276] Fix | Delete
__del__ = close
[277] Fix | Delete
[278] Fix | Delete
def _chmod(self, file):
[279] Fix | Delete
self._os.chmod(file, self._mode)
[280] Fix | Delete
[281] Fix | Delete
def __enter__(self):
[282] Fix | Delete
return self
[283] Fix | Delete
[284] Fix | Delete
def __exit__(self, *args):
[285] Fix | Delete
self.close()
[286] Fix | Delete
[287] Fix | Delete
[288] Fix | Delete
def open(file, flag='c', mode=0o666):
[289] Fix | Delete
"""Open the database file, filename, and return corresponding object.
[290] Fix | Delete
[291] Fix | Delete
The flag argument, used to control how the database is opened in the
[292] Fix | Delete
other DBM implementations, supports only the semantics of 'c' and 'n'
[293] Fix | Delete
values. Other values will default to the semantics of 'c' value:
[294] Fix | Delete
the database will always opened for update and will be created if it
[295] Fix | Delete
does not exist.
[296] Fix | Delete
[297] Fix | Delete
The optional mode argument is the UNIX mode of the file, used only when
[298] Fix | Delete
the database has to be created. It defaults to octal code 0o666 (and
[299] Fix | Delete
will be modified by the prevailing umask).
[300] Fix | Delete
[301] Fix | Delete
"""
[302] Fix | Delete
[303] Fix | Delete
# Modify mode depending on the umask
[304] Fix | Delete
try:
[305] Fix | Delete
um = _os.umask(0)
[306] Fix | Delete
_os.umask(um)
[307] Fix | Delete
except AttributeError:
[308] Fix | Delete
pass
[309] Fix | Delete
else:
[310] Fix | Delete
# Turn off any bits that are set in the umask
[311] Fix | Delete
mode = mode & (~um)
[312] Fix | Delete
if flag not in ('r', 'w', 'c', 'n'):
[313] Fix | Delete
raise ValueError("Flag must be one of 'r', 'w', 'c', or 'n'")
[314] Fix | Delete
return _Database(file, mode, flag=flag)
[315] Fix | Delete
[316] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function