| 1 | n/a | """Generic interface to all dbm clones. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | Instead of |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | import dbm |
|---|
| 6 | n/a | d = dbm.open(file, 'w', 0666) |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | use |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | import anydbm |
|---|
| 11 | n/a | d = anydbm.open(file, 'w') |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | The returned object is a dbhash, gdbm, dbm or dumbdbm object, |
|---|
| 14 | n/a | dependent on the type of database being opened (determined by whichdb |
|---|
| 15 | n/a | module) in the case of an existing dbm. If the dbm does not exist and |
|---|
| 16 | n/a | the create or new flag ('c' or 'n') was specified, the dbm type will |
|---|
| 17 | n/a | be determined by the availability of the modules (tested in the above |
|---|
| 18 | n/a | order). |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | It has the following interface (key and data are strings): |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | d[key] = data # store data at key (may override data at |
|---|
| 23 | n/a | # existing key) |
|---|
| 24 | n/a | data = d[key] # retrieve data at key (raise KeyError if no |
|---|
| 25 | n/a | # such key) |
|---|
| 26 | n/a | del d[key] # delete data stored at key (raises KeyError |
|---|
| 27 | n/a | # if no such key) |
|---|
| 28 | n/a | flag = key in d # true if the key exists |
|---|
| 29 | n/a | list = d.keys() # return a list of all existing keys (slow!) |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | Future versions may change the order in which implementations are |
|---|
| 32 | n/a | tested for existence, add interfaces to other dbm-like |
|---|
| 33 | n/a | implementations. |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | The open function has an optional second argument. This can be 'r', |
|---|
| 36 | n/a | for read-only access, 'w', for read-write access of an existing |
|---|
| 37 | n/a | database, 'c' for read-write access to a new or existing database, and |
|---|
| 38 | n/a | 'n' for read-write access to a new database. The default is 'r'. |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | Note: 'r' and 'w' fail if the database doesn't exist; 'c' creates it |
|---|
| 41 | n/a | only if it doesn't exist; and 'n' always creates a new database. |
|---|
| 42 | n/a | |
|---|
| 43 | 1 | """ |
|---|
| 44 | n/a | |
|---|
| 45 | 2 | class error(Exception): |
|---|
| 46 | 1 | pass |
|---|
| 47 | n/a | |
|---|
| 48 | 1 | _names = ['dbhash', 'gdbm', 'dbm', 'dumbdbm'] |
|---|
| 49 | 1 | _errors = [error] |
|---|
| 50 | 1 | _defaultmod = None |
|---|
| 51 | n/a | |
|---|
| 52 | 5 | for _name in _names: |
|---|
| 53 | 4 | try: |
|---|
| 54 | 4 | _mod = __import__(_name) |
|---|
| 55 | 0 | except ImportError: |
|---|
| 56 | 0 | continue |
|---|
| 57 | 4 | if not _defaultmod: |
|---|
| 58 | 1 | _defaultmod = _mod |
|---|
| 59 | 4 | _errors.append(_mod.error) |
|---|
| 60 | n/a | |
|---|
| 61 | 1 | if not _defaultmod: |
|---|
| 62 | 0 | raise ImportError, "no dbm clone found; tried %s" % _names |
|---|
| 63 | n/a | |
|---|
| 64 | 1 | error = tuple(_errors) |
|---|
| 65 | n/a | |
|---|
| 66 | 1 | def open(file, flag = 'r', mode = 0666): |
|---|
| 67 | n/a | # guess the type of an existing database |
|---|
| 68 | 88 | from whichdb import whichdb |
|---|
| 69 | 88 | result=whichdb(file) |
|---|
| 70 | 88 | if result is None: |
|---|
| 71 | n/a | # db doesn't exist |
|---|
| 72 | 85 | if 'c' in flag or 'n' in flag: |
|---|
| 73 | n/a | # file doesn't exist and the new |
|---|
| 74 | n/a | # flag was used so use default type |
|---|
| 75 | 85 | mod = _defaultmod |
|---|
| 76 | n/a | else: |
|---|
| 77 | 0 | raise error, "need 'c' or 'n' flag to open new db" |
|---|
| 78 | 3 | elif result == "": |
|---|
| 79 | n/a | # db type cannot be determined |
|---|
| 80 | 0 | raise error, "db type could not be determined" |
|---|
| 81 | n/a | else: |
|---|
| 82 | 3 | mod = __import__(result) |
|---|
| 83 | 88 | return mod.open(file, flag, mode) |
|---|