cpython/Lib/anydbm.py

87 lines
2.6 KiB
Python
Raw Normal View History

1995-08-10 19:24:30 +00:00
"""Generic interface to all dbm clones.
1995-02-09 17:18:10 +00:00
Instead of
2001-01-14 18:09:23 +00:00
import dbm
d = dbm.open(file, 'w', 0666)
1995-02-09 17:18:10 +00:00
use
2001-01-14 18:09:23 +00:00
import anydbm
d = anydbm.open(file, 'w')
1995-02-09 17:18:10 +00:00
1995-08-10 19:24:30 +00:00
The returned object is a dbhash, gdbm, dbm or dumbdbm object,
dependent on the type of database being opened (determined by whichdb
module) in the case of an existing dbm. If the dbm does not exist and
the create or new flag ('c' or 'n') was specified, the dbm type will
be determined by the availability of the modules (tested in the above
order).
1995-02-09 17:18:10 +00:00
It has the following interface (key and data are strings):
2001-01-14 18:09:23 +00:00
d[key] = data # store data at key (may override data at
# existing key)
data = d[key] # retrieve data at key (raise KeyError if no
# such key)
del d[key] # delete data stored at key (raises KeyError
# if no such key)
flag = d.has_key(key) # true if the key exists
list = d.keys() # return a list of all existing keys (slow!)
1995-02-09 17:18:10 +00:00
Future versions may change the order in which implementations are
1995-08-10 19:24:30 +00:00
tested for existence, add interfaces to other dbm-like
implementations.
1995-02-09 17:18:10 +00:00
The open function has an optional second argument. This can be 'r',
for read-only access, 'w', for read-write access of an existing
database, 'c' for read-write access to a new or existing database, and
'n' for read-write access to a new database. The default is 'r'.
Note: 'r' and 'w' fail if the database doesn't exist; 'c' creates it
only if it doesn't exist; and 'n' always creates a new database.
1995-08-10 19:24:30 +00:00
1995-02-09 17:18:10 +00:00
"""
try:
2001-01-14 18:09:23 +00:00
class error(Exception):
pass
except (NameError, TypeError):
2001-01-14 18:09:23 +00:00
error = "anydbm.error"
1995-08-10 19:24:30 +00:00
_names = ['dbhash', 'gdbm', 'dbm', 'dumbdbm']
_errors = [error]
_defaultmod = None
1995-08-10 19:24:30 +00:00
for _name in _names:
2001-01-14 18:09:23 +00:00
try:
_mod = __import__(_name)
except ImportError:
continue
if not _defaultmod:
_defaultmod = _mod
_errors.append(_mod.error)
if not _defaultmod:
2001-01-14 18:09:23 +00:00
raise ImportError, "no dbm clone found; tried %s" % _names
1996-01-25 18:26:57 +00:00
error = tuple(_errors)
1996-01-25 18:26:57 +00:00
1995-08-11 14:18:27 +00:00
def open(file, flag = 'r', mode = 0666):
2001-01-14 18:09:23 +00:00
# guess the type of an existing database
from whichdb import whichdb
result=whichdb(file)
if result is None:
# db doesn't exist
if 'c' in flag or 'n' in flag:
# file doesn't exist and the new
# flag was used so use default type
mod = _defaultmod
else:
raise error, "need 'c' or 'n' flag to open new db"
elif result == "":
# db type cannot be determined
raise error, "db type could not be determined"
else:
mod = __import__(result)
return mod.open(file, flag, mode)