| 1 |
"""A dumb and slow but simple dbm clone. |
| 2 |
|
| 3 |
For database spam, spam.dir contains the index (a text file), |
| 4 |
spam.bak *may* contain a backup of the index (also a text file), |
| 5 |
while spam.dat contains the data (a binary file). |
| 6 |
|
| 7 |
XXX TO DO: |
| 8 |
|
| 9 |
- seems to contain a bug when updating... |
| 10 |
|
| 11 |
- reclaim free space (currently, space once occupied by deleted or expanded |
| 12 |
items is never reused) |
| 13 |
|
| 14 |
- support concurrent access (currently, if two processes take turns making |
| 15 |
updates, they can mess up the index) |
| 16 |
|
| 17 |
- support efficient access to large databases (currently, the whole index |
| 18 |
is read when the database is opened, and some updates rewrite the whole index) |
| 19 |
|
| 20 |
- support opening for read-only (flag = 'm') |
| 21 |
|
| 22 |
""" |
| 23 |
|
| 24 |
import ast as _ast |
| 25 |
import io as _io |
| 26 |
import os as _os |
| 27 |
import collections.abc |
| 28 |
|
| 29 |
__all__ = ["error", "open"] |
| 30 |
|
| 31 |
_BLOCKSIZE = 512 |
| 32 |
|
| 33 |
error = OSError |
| 34 |
|
| 35 |
class _Database(collections.abc.MutableMapping): |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
_os = _os |
| 46 |
_io = _io |
| 47 |
|
| 48 |
def __init__(self, filebasename, mode, flag='c'): |
| 49 |
filebasename = self._os.fsencode(filebasename) |
| 50 |
self._mode = mode |
| 51 |
self._readonly = (flag == 'r') |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
self._dirfile = filebasename + b'.dir' |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
self._datfile = filebasename + b'.dat' |
| 65 |
self._bakfile = filebasename + b'.bak' |
| 66 |
|
| 67 |
|
| 68 |
self._index = None |
| 69 |
|
| 70 |
|
| 71 |
self._create(flag) |
| 72 |
self._update(flag) |
| 73 |
|
| 74 |
def _create(self, flag): |
| 75 |
if flag == 'n': |
| 76 |
for filename in (self._datfile, self._bakfile, self._dirfile): |
| 77 |
try: |
| 78 |
_os.remove(filename) |
| 79 |
except OSError: |
| 80 |
pass |
| 81 |
|
| 82 |
try: |
| 83 |
f = _io.open(self._datfile, 'r', encoding="Latin-1") |
| 84 |
except OSError: |
| 85 |
if flag not in ('c', 'n'): |
| 86 |
raise |
| 87 |
with _io.open(self._datfile, 'w', encoding="Latin-1") as f: |
| 88 |
self._chmod(self._datfile) |
| 89 |
else: |
| 90 |
f.close() |
| 91 |
|
| 92 |
|
| 93 |
def _update(self, flag): |
| 94 |
self._modified = False |
| 95 |
self._index = {} |
| 96 |
try: |
| 97 |
f = _io.open(self._dirfile, 'r', encoding="Latin-1") |
| 98 |
except OSError: |
| 99 |
if flag not in ('c', 'n'): |
| 100 |
raise |
| 101 |
self._modified = True |
| 102 |
else: |
| 103 |
with f: |
| 104 |
for line in f: |
| 105 |
line = line.rstrip() |
| 106 |
key, pos_and_siz_pair = _ast.literal_eval(line) |
| 107 |
key = key.encode('Latin-1') |
| 108 |
self._index[key] = pos_and_siz_pair |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
def _commit(self): |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
if self._index is None or not self._modified: |
| 118 |
return |
| 119 |
|
| 120 |
try: |
| 121 |
self._os.unlink(self._bakfile) |
| 122 |
except OSError: |
| 123 |
pass |
| 124 |
|
| 125 |
try: |
| 126 |
self._os.rename(self._dirfile, self._bakfile) |
| 127 |
except OSError: |
| 128 |
pass |
| 129 |
|
| 130 |
with self._io.open(self._dirfile, 'w', encoding="Latin-1") as f: |
| 131 |
self._chmod(self._dirfile) |
| 132 |
for key, pos_and_siz_pair in self._index.items(): |
| 133 |
|
| 134 |
|
| 135 |
entry = "%r, %r\n" % (key.decode('Latin-1'), pos_and_siz_pair) |
| 136 |
f.write(entry) |
| 137 |
|
| 138 |
sync = _commit |
| 139 |
|
| 140 |
def _verify_open(self): |
| 141 |
if self._index is None: |
| 142 |
raise error('DBM object has already been closed') |
| 143 |
|
| 144 |
def __getitem__(self, key): |
| 145 |
if isinstance(key, str): |
| 146 |
key = key.encode('utf-8') |
| 147 |
self._verify_open() |
| 148 |
pos, siz = self._index[key] |
| 149 |
with _io.open(self._datfile, 'rb') as f: |
| 150 |
f.seek(pos) |
| 151 |
dat = f.read(siz) |
| 152 |
return dat |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
def _addval(self, val): |
| 159 |
with _io.open(self._datfile, 'rb+') as f: |
| 160 |
f.seek(0, 2) |
| 161 |
pos = int(f.tell()) |
| 162 |
npos = ((pos + _BLOCKSIZE - 1) // _BLOCKSIZE) * _BLOCKSIZE |
| 163 |
f.write(b'\0'*(npos-pos)) |
| 164 |
pos = npos |
| 165 |
f.write(val) |
| 166 |
return (pos, len(val)) |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
def _setval(self, pos, val): |
| 173 |
with _io.open(self._datfile, 'rb+') as f: |
| 174 |
f.seek(pos) |
| 175 |
f.write(val) |
| 176 |
return (pos, len(val)) |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
def _addkey(self, key, pos_and_siz_pair): |
| 182 |
self._index[key] = pos_and_siz_pair |
| 183 |
with _io.open(self._dirfile, 'a', encoding="Latin-1") as f: |
| 184 |
self._chmod(self._dirfile) |
| 185 |
f.write("%r, %r\n" % (key.decode("Latin-1"), pos_and_siz_pair)) |
| 186 |
|
| 187 |
def __setitem__(self, key, val): |
| 188 |
if self._readonly: |
| 189 |
raise error('The database is opened for reading only') |
| 190 |
if isinstance(key, str): |
| 191 |
key = key.encode('utf-8') |
| 192 |
elif not isinstance(key, (bytes, bytearray)): |
| 193 |
raise TypeError("keys must be bytes or strings") |
| 194 |
if isinstance(val, str): |
| 195 |
val = val.encode('utf-8') |
| 196 |
elif not isinstance(val, (bytes, bytearray)): |
| 197 |
raise TypeError("values must be bytes or strings") |
| 198 |
self._verify_open() |
| 199 |
self._modified = True |
| 200 |
if key not in self._index: |
| 201 |
self._addkey(key, self._addval(val)) |
| 202 |
else: |
| 203 |
|
| 204 |
|
| 205 |
pos, siz = self._index[key] |
| 206 |
oldblocks = (siz + _BLOCKSIZE - 1) // _BLOCKSIZE |
| 207 |
newblocks = (len(val) + _BLOCKSIZE - 1) // _BLOCKSIZE |
| 208 |
if newblocks <= oldblocks: |
| 209 |
self._index[key] = self._setval(pos, val) |
| 210 |
else: |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
self._index[key] = self._addval(val) |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
def __delitem__(self, key): |
| 225 |
if self._readonly: |
| 226 |
raise error('The database is opened for reading only') |
| 227 |
if isinstance(key, str): |
| 228 |
key = key.encode('utf-8') |
| 229 |
self._verify_open() |
| 230 |
self._modified = True |
| 231 |
|
| 232 |
del self._index[key] |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
self._commit() |
| 238 |
|
| 239 |
def keys(self): |
| 240 |
try: |
| 241 |
return list(self._index) |
| 242 |
except TypeError: |
| 243 |
raise error('DBM object has already been closed') from None |
| 244 |
|
| 245 |
def items(self): |
| 246 |
self._verify_open() |
| 247 |
return [(key, self[key]) for key in self._index.keys()] |
| 248 |
|
| 249 |
def __contains__(self, key): |
| 250 |
if isinstance(key, str): |
| 251 |
key = key.encode('utf-8') |
| 252 |
try: |
| 253 |
return key in self._index |
| 254 |
except TypeError: |
| 255 |
if self._index is None: |
| 256 |
raise error('DBM object has already been closed') from None |
| 257 |
else: |
| 258 |
raise |
| 259 |
|
| 260 |
def iterkeys(self): |
| 261 |
try: |
| 262 |
return iter(self._index) |
| 263 |
except TypeError: |
| 264 |
raise error('DBM object has already been closed') from None |
| 265 |
__iter__ = iterkeys |
| 266 |
|
| 267 |
def __len__(self): |
| 268 |
try: |
| 269 |
return len(self._index) |
| 270 |
except TypeError: |
| 271 |
raise error('DBM object has already been closed') from None |
| 272 |
|
| 273 |
def close(self): |
| 274 |
try: |
| 275 |
self._commit() |
| 276 |
finally: |
| 277 |
self._index = self._datfile = self._dirfile = self._bakfile = None |
| 278 |
|
| 279 |
__del__ = close |
| 280 |
|
| 281 |
def _chmod(self, file): |
| 282 |
self._os.chmod(file, self._mode) |
| 283 |
|
| 284 |
def __enter__(self): |
| 285 |
return self |
| 286 |
|
| 287 |
def __exit__(self, *args): |
| 288 |
self.close() |
| 289 |
|
| 290 |
|
| 291 |
def open(file, flag='c', mode=0o666): |
| 292 |
"""Open the database file, filename, and return corresponding object. |
| 293 |
|
| 294 |
The flag argument, used to control how the database is opened in the |
| 295 |
other DBM implementations, supports only the semantics of 'c' and 'n' |
| 296 |
values. Other values will default to the semantics of 'c' value: |
| 297 |
the database will always opened for update and will be created if it |
| 298 |
does not exist. |
| 299 |
|
| 300 |
The optional mode argument is the UNIX mode of the file, used only when |
| 301 |
the database has to be created. It defaults to octal code 0o666 (and |
| 302 |
will be modified by the prevailing umask). |
| 303 |
|
| 304 |
""" |
| 305 |
|
| 306 |
|
| 307 |
try: |
| 308 |
um = _os.umask(0) |
| 309 |
_os.umask(um) |
| 310 |
except AttributeError: |
| 311 |
pass |
| 312 |
else: |
| 313 |
|
| 314 |
mode = mode & (~um) |
| 315 |
if flag not in ('r', 'w', 'c', 'n'): |
| 316 |
raise ValueError("Flag must be one of 'r', 'w', 'c', or 'n'") |
| 317 |
return _Database(file, mode, flag=flag) |