anydbm.py 2.61 KB
Newer Older
Guido van Rossum's avatar
Guido van Rossum committed
1
"""Generic interface to all dbm clones.
2 3 4

Instead of

Tim Peters's avatar
Tim Peters committed
5 6
        import dbm
        d = dbm.open(file, 'w', 0666)
7 8 9

use

Tim Peters's avatar
Tim Peters committed
10 11
        import anydbm
        d = anydbm.open(file, 'w')
12

Guido van Rossum's avatar
Guido van Rossum committed
13
The returned object is a dbhash, gdbm, dbm or dumbdbm object,
14 15 16 17 18
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).
19 20 21

It has the following interface (key and data are strings):

Tim Peters's avatar
Tim Peters committed
22 23 24 25 26 27 28 29
        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!)
30 31

Future versions may change the order in which implementations are
Guido van Rossum's avatar
Guido van Rossum committed
32
tested for existence, add interfaces to other dbm-like
33
implementations.
34

35 36
The open function has an optional second argument.  This can be 'r',
for read-only access, 'w', for read-write access of an existing
37 38
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'.
39

40 41
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.
Guido van Rossum's avatar
Guido van Rossum committed
42

43
"""
44

45
try:
Tim Peters's avatar
Tim Peters committed
46 47
    class error(Exception):
        pass
48
except:
Tim Peters's avatar
Tim Peters committed
49
    error = "anydbm.error"
50

Guido van Rossum's avatar
Guido van Rossum committed
51
_names = ['dbhash', 'gdbm', 'dbm', 'dumbdbm']
52 53
_errors = [error]
_defaultmod = None
Guido van Rossum's avatar
Guido van Rossum committed
54 55

for _name in _names:
Tim Peters's avatar
Tim Peters committed
56 57 58 59 60 61 62
    try:
        _mod = __import__(_name)
    except ImportError:
        continue
    if not _defaultmod:
        _defaultmod = _mod
    _errors.append(_mod.error)
63 64

if not _defaultmod:
Tim Peters's avatar
Tim Peters committed
65
    raise ImportError, "no dbm clone found; tried %s" % _names
66

67
error = tuple(_errors)
68

69
def open(file, flag = 'r', mode = 0666):
Tim Peters's avatar
Tim Peters committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    # 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)