blob: ba7e90510a0a523a53c81a0c9b168caf4a2da251 [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)
Raymond Hettinger54f02222002-06-01 14:18:47 +000028 flag = key in d # true if the key exists
Tim Peters146965a2001-01-14 18:09:23 +000029 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
Éric Araujod6dcf822011-05-01 02:02:30 +020032tested for existence, and 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 Rossuma48061a1995-01-10 00:31:14 +000035
Skip Montanaroc99475e2002-03-18 03:07:20 +000036class error(Exception):
37 pass
Guido van Rossuma5c09981998-04-28 15:19:34 +000038
Guido van Rossum4ac00501995-08-10 19:24:30 +000039_names = ['dbhash', 'gdbm', 'dbm', 'dumbdbm']
Guido van Rossuma5c09981998-04-28 15:19:34 +000040_errors = [error]
41_defaultmod = None
Guido van Rossum4ac00501995-08-10 19:24:30 +000042
43for _name in _names:
Tim Peters146965a2001-01-14 18:09:23 +000044 try:
45 _mod = __import__(_name)
46 except ImportError:
47 continue
48 if not _defaultmod:
49 _defaultmod = _mod
50 _errors.append(_mod.error)
Guido van Rossuma5c09981998-04-28 15:19:34 +000051
52if not _defaultmod:
Tim Peters146965a2001-01-14 18:09:23 +000053 raise ImportError, "no dbm clone found; tried %s" % _names
Guido van Rossum0182c061996-01-25 18:26:57 +000054
Guido van Rossuma5c09981998-04-28 15:19:34 +000055error = tuple(_errors)
Guido van Rossum0182c061996-01-25 18:26:57 +000056
Éric Araujod6dcf822011-05-01 02:02:30 +020057def open(file, flag='r', mode=0666):
58 """Open or create database at path given by *file*.
59
60 Optional argument *flag* can be 'r' (default) for read-only access, 'w'
61 for read-write access of an existing database, 'c' for read-write access
62 to a new or existing database, and 'n' for read-write access to a new
63 database.
64
65 Note: 'r' and 'w' fail if the database doesn't exist; 'c' creates it
66 only if it doesn't exist; and 'n' always creates a new database.
67 """
68
Tim Peters146965a2001-01-14 18:09:23 +000069 # guess the type of an existing database
70 from whichdb import whichdb
71 result=whichdb(file)
72 if result is None:
73 # db doesn't exist
74 if 'c' in flag or 'n' in flag:
75 # file doesn't exist and the new
76 # flag was used so use default type
77 mod = _defaultmod
78 else:
79 raise error, "need 'c' or 'n' flag to open new db"
80 elif result == "":
81 # db type cannot be determined
82 raise error, "db type could not be determined"
83 else:
84 mod = __import__(result)
85 return mod.open(file, flag, mode)