blob: 2dc312d5fd3a25dc4ebd2862bbcc2ca0a078f6ab [file] [log] [blame]
Guido van Rossum4ac00501995-08-10 19:24:30 +00001"""Generic interface to all dbm clones.
Guido van Rossumcc6764c1995-02-09 17:18:10 +00002
3Instead of
4
5 import dbm
Guido van Rossum4ac00501995-08-10 19:24:30 +00006 d = dbm.open(file, 'w', 0666)
Guido van Rossumcc6764c1995-02-09 17:18:10 +00007
8use
9
10 import anydbm
11 d = anydbm.open(file)
12
Guido van Rossum4ac00501995-08-10 19:24:30 +000013The returned object is a dbhash, gdbm, dbm or dumbdbm object,
Guido van Rossumcc6764c1995-02-09 17:18:10 +000014dependent on availability of the modules (tested in this order).
15
16It has the following interface (key and data are strings):
17
18 d[key] = data # store data at key (may override data at
19 # existing key)
20 data = d[key] # retrieve data at key (raise KeyError if no
21 # such key)
22 del d[key] # delete data stored at key (raises KeyError
23 # if no such key)
24 flag = d.has_key(key) # true if the key exists
25 list = d.keys() # return a list of all existing keys (slow!)
26
27Future versions may change the order in which implementations are
Guido van Rossum4ac00501995-08-10 19:24:30 +000028tested for existence, add interfaces to other dbm-like
29implementations, and (in the presence of multiple implementations)
Guido van Rossumcc6764c1995-02-09 17:18:10 +000030decide which module to use based upon the extension or contents of an
31existing database file.
32
33The open function has an optional second argument. This can be set to
Guido van Rossume03a86c1995-08-11 14:18:27 +000034'r' to open the database for reading only. The default is 'r', like
35the dbm default.
Guido van Rossum4ac00501995-08-10 19:24:30 +000036
Guido van Rossumcc6764c1995-02-09 17:18:10 +000037"""
Guido van Rossuma48061a1995-01-10 00:31:14 +000038
Guido van Rossum4ac00501995-08-10 19:24:30 +000039_names = ['dbhash', 'gdbm', 'dbm', 'dumbdbm']
40
41for _name in _names:
Guido van Rossumcc6764c1995-02-09 17:18:10 +000042 try:
Guido van Rossum4ac00501995-08-10 19:24:30 +000043 exec "import %s; _mod = %s" % (_name, _name)
Guido van Rossumcc6764c1995-02-09 17:18:10 +000044 except ImportError:
Guido van Rossum4ac00501995-08-10 19:24:30 +000045 continue
46 else:
47 break
48else:
49 raise ImportError, "no dbm clone found; tried %s" % _names
Guido van Rossum0182c061996-01-25 18:26:57 +000050
51error = _mod.error
52
Guido van Rossume03a86c1995-08-11 14:18:27 +000053def open(file, flag = 'r', mode = 0666):
Guido van Rossum4ac00501995-08-10 19:24:30 +000054 return _mod.open(file, flag, mode)