blob: f051200548f393fad5bc176f67d55f4d3ec5a37b [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
Tim Peters146965a2001-01-14 18:09:23 +00005 import dbm
6 d = dbm.open(file, 'w', 0666)
Guido van Rossumcc6764c1995-02-09 17:18:10 +00007
8use
9
Tim Peters146965a2001-01-14 18:09:23 +000010 import anydbm
11 d = anydbm.open(file, 'w')
Guido van Rossumcc6764c1995-02-09 17:18:10 +000012
Guido van Rossum4ac00501995-08-10 19:24:30 +000013The returned object is a dbhash, gdbm, dbm or dumbdbm object,
Guido van Rossuma5c09981998-04-28 15:19:34 +000014dependent on the type of database being opened (determined by whichdb
15module) in the case of an existing dbm. If the dbm does not exist and
16the create or new flag ('c' or 'n') was specified, the dbm type will
17be determined by the availability of the modules (tested in the above
18order).
Guido van Rossumcc6764c1995-02-09 17:18:10 +000019
20It has the following interface (key and data are strings):
21
Tim Peters146965a2001-01-14 18:09:23 +000022 d[key] = data # store data at key (may override data at
23 # existing key)
24 data = d[key] # retrieve data at key (raise KeyError if no
25 # such key)
26 del d[key] # delete data stored at key (raises KeyError
27 # if no such key)
28 flag = d.has_key(key) # true if the key exists
29 list = d.keys() # return a list of all existing keys (slow!)
Guido van Rossumcc6764c1995-02-09 17:18:10 +000030
31Future versions may change the order in which implementations are
Guido van Rossum4ac00501995-08-10 19:24:30 +000032tested for existence, add interfaces to other dbm-like
Guido van Rossuma5c09981998-04-28 15:19:34 +000033implementations.
Guido van Rossumcc6764c1995-02-09 17:18:10 +000034
Guido van Rossuma5c09981998-04-28 15:19:34 +000035The open function has an optional second argument. This can be 'r',
36for read-only access, 'w', for read-write access of an existing
Guido van Rossumfc1f64d1998-04-28 15:23:09 +000037database, 'c' for read-write access to a new or existing database, and
38'n' for read-write access to a new database. The default is 'r'.
Guido van Rossuma5c09981998-04-28 15:19:34 +000039
Guido van Rossumfc1f64d1998-04-28 15:23:09 +000040Note: 'r' and 'w' fail if the database doesn't exist; 'c' creates it
41only if it doesn't exist; and 'n' always creates a new database.
Guido van Rossum4ac00501995-08-10 19:24:30 +000042
Guido van Rossumcc6764c1995-02-09 17:18:10 +000043"""
Guido van Rossuma48061a1995-01-10 00:31:14 +000044
Guido van Rossuma5c09981998-04-28 15:19:34 +000045try:
Tim Peters146965a2001-01-14 18:09:23 +000046 class error(Exception):
47 pass
Fred Drakec79f3d02001-05-11 18:27:00 +000048except (NameError, TypeError):
Tim Peters146965a2001-01-14 18:09:23 +000049 error = "anydbm.error"
Guido van Rossuma5c09981998-04-28 15:19:34 +000050
Guido van Rossum4ac00501995-08-10 19:24:30 +000051_names = ['dbhash', 'gdbm', 'dbm', 'dumbdbm']
Guido van Rossuma5c09981998-04-28 15:19:34 +000052_errors = [error]
53_defaultmod = None
Guido van Rossum4ac00501995-08-10 19:24:30 +000054
55for _name in _names:
Tim Peters146965a2001-01-14 18:09:23 +000056 try:
57 _mod = __import__(_name)
58 except ImportError:
59 continue
60 if not _defaultmod:
61 _defaultmod = _mod
62 _errors.append(_mod.error)
Guido van Rossuma5c09981998-04-28 15:19:34 +000063
64if not _defaultmod:
Tim Peters146965a2001-01-14 18:09:23 +000065 raise ImportError, "no dbm clone found; tried %s" % _names
Guido van Rossum0182c061996-01-25 18:26:57 +000066
Guido van Rossuma5c09981998-04-28 15:19:34 +000067error = tuple(_errors)
Guido van Rossum0182c061996-01-25 18:26:57 +000068
Guido van Rossume03a86c1995-08-11 14:18:27 +000069def open(file, flag = 'r', mode = 0666):
Tim Peters146965a2001-01-14 18:09:23 +000070 # guess the type of an existing database
71 from whichdb import whichdb
72 result=whichdb(file)
73 if result is None:
74 # db doesn't exist
75 if 'c' in flag or 'n' in flag:
76 # file doesn't exist and the new
77 # flag was used so use default type
78 mod = _defaultmod
79 else:
80 raise error, "need 'c' or 'n' flag to open new db"
81 elif result == "":
82 # db type cannot be determined
83 raise error, "db type could not be determined"
84 else:
85 mod = __import__(result)
86 return mod.open(file, flag, mode)