anydbm.py 2.64 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 46
__all__ = ["error","open"]

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

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

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

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

69
error = tuple(_errors)
70

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