Guido van Rossum | 4ac0050 | 1995-08-10 19:24:30 +0000 | [diff] [blame] | 1 | """Generic interface to all dbm clones. |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 2 | |
| 3 | Instead of |
| 4 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 5 | import dbm |
| 6 | d = dbm.open(file, 'w', 0666) |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 7 | |
| 8 | use |
| 9 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 10 | import anydbm |
| 11 | d = anydbm.open(file, 'w') |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 4ac0050 | 1995-08-10 19:24:30 +0000 | [diff] [blame] | 13 | The returned object is a dbhash, gdbm, dbm or dumbdbm object, |
Guido van Rossum | a5c0998 | 1998-04-28 15:19:34 +0000 | [diff] [blame] | 14 | dependent on the type of database being opened (determined by whichdb |
| 15 | module) in the case of an existing dbm. If the dbm does not exist and |
| 16 | the create or new flag ('c' or 'n') was specified, the dbm type will |
| 17 | be determined by the availability of the modules (tested in the above |
| 18 | order). |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 19 | |
| 20 | It has the following interface (key and data are strings): |
| 21 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 22 | 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 Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 30 | |
| 31 | Future versions may change the order in which implementations are |
Guido van Rossum | 4ac0050 | 1995-08-10 19:24:30 +0000 | [diff] [blame] | 32 | tested for existence, add interfaces to other dbm-like |
Guido van Rossum | a5c0998 | 1998-04-28 15:19:34 +0000 | [diff] [blame] | 33 | implementations. |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | a5c0998 | 1998-04-28 15:19:34 +0000 | [diff] [blame] | 35 | The open function has an optional second argument. This can be 'r', |
| 36 | for read-only access, 'w', for read-write access of an existing |
Guido van Rossum | fc1f64d | 1998-04-28 15:23:09 +0000 | [diff] [blame] | 37 | database, '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 Rossum | a5c0998 | 1998-04-28 15:19:34 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | fc1f64d | 1998-04-28 15:23:09 +0000 | [diff] [blame] | 40 | Note: 'r' and 'w' fail if the database doesn't exist; 'c' creates it |
| 41 | only if it doesn't exist; and 'n' always creates a new database. |
Guido van Rossum | 4ac0050 | 1995-08-10 19:24:30 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 43 | """ |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | a5c0998 | 1998-04-28 15:19:34 +0000 | [diff] [blame] | 45 | try: |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 46 | class error(Exception): |
| 47 | pass |
Fred Drake | c79f3d0 | 2001-05-11 18:27:00 +0000 | [diff] [blame] | 48 | except (NameError, TypeError): |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 49 | error = "anydbm.error" |
Guido van Rossum | a5c0998 | 1998-04-28 15:19:34 +0000 | [diff] [blame] | 50 | |
Guido van Rossum | 4ac0050 | 1995-08-10 19:24:30 +0000 | [diff] [blame] | 51 | _names = ['dbhash', 'gdbm', 'dbm', 'dumbdbm'] |
Guido van Rossum | a5c0998 | 1998-04-28 15:19:34 +0000 | [diff] [blame] | 52 | _errors = [error] |
| 53 | _defaultmod = None |
Guido van Rossum | 4ac0050 | 1995-08-10 19:24:30 +0000 | [diff] [blame] | 54 | |
| 55 | for _name in _names: |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 56 | try: |
| 57 | _mod = __import__(_name) |
| 58 | except ImportError: |
| 59 | continue |
| 60 | if not _defaultmod: |
| 61 | _defaultmod = _mod |
| 62 | _errors.append(_mod.error) |
Guido van Rossum | a5c0998 | 1998-04-28 15:19:34 +0000 | [diff] [blame] | 63 | |
| 64 | if not _defaultmod: |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 65 | raise ImportError, "no dbm clone found; tried %s" % _names |
Guido van Rossum | 0182c06 | 1996-01-25 18:26:57 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | a5c0998 | 1998-04-28 15:19:34 +0000 | [diff] [blame] | 67 | error = tuple(_errors) |
Guido van Rossum | 0182c06 | 1996-01-25 18:26:57 +0000 | [diff] [blame] | 68 | |
Guido van Rossum | e03a86c | 1995-08-11 14:18:27 +0000 | [diff] [blame] | 69 | def open(file, flag = 'r', mode = 0666): |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 70 | # 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) |