diff options
author | Guido van Rossum <guido@python.org> | 1998-04-28 15:19:34 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1998-04-28 15:19:34 (GMT) |
commit | a5c09982421414b10753b3c698fcbc65efdff0eb (patch) | |
tree | 8fdc1a8f98d3ec82641c66c7dcc78bc96930962f /Lib/anydbm.py | |
parent | f8d50bfdb07ac8d5e5b7eb435e750af01857dc45 (diff) | |
download | cpython-a5c09982421414b10753b3c698fcbc65efdff0eb.zip cpython-a5c09982421414b10753b3c698fcbc65efdff0eb.tar.gz cpython-a5c09982421414b10753b3c698fcbc65efdff0eb.tar.bz2 |
Inspired by Ben Sayer, rewritten the code and some of the comments to
be more intelligent when the database already exists (use the module
for the existing file, according to whichdb). Noted in the doc
strings that there doesn't seem to be a different between 'c' and 'n'.
Diffstat (limited to 'Lib/anydbm.py')
-rw-r--r-- | Lib/anydbm.py | 61 |
1 files changed, 47 insertions, 14 deletions
diff --git a/Lib/anydbm.py b/Lib/anydbm.py index 2dc312d..ef913bb 100644 --- a/Lib/anydbm.py +++ b/Lib/anydbm.py @@ -8,10 +8,14 @@ Instead of use import anydbm - d = anydbm.open(file) + d = anydbm.open(file, 'w') The returned object is a dbhash, gdbm, dbm or dumbdbm object, -dependent on availability of the modules (tested in this order). +dependent on the type of database being opened (determined by whichdb +module) in the case of an existing dbm. If the dbm does not exist and +the create or new flag ('c' or 'n') was specified, the dbm type will +be determined by the availability of the modules (tested in the above +order). It has the following interface (key and data are strings): @@ -26,29 +30,58 @@ It has the following interface (key and data are strings): Future versions may change the order in which implementations are tested for existence, add interfaces to other dbm-like -implementations, and (in the presence of multiple implementations) -decide which module to use based upon the extension or contents of an -existing database file. +implementations. -The open function has an optional second argument. This can be set to -'r' to open the database for reading only. The default is 'r', like -the dbm default. +The open function has an optional second argument. This can be 'r', +for read-only access, 'w', for read-write access of an existing +database, 'n' or 'c' for read-write access to a new database. The +default is 'r'. + +Note: the difference between 'w' and 'n' is that 'w' fails if the +database doesn't already exist. There appears to be no difference +between 'n' and 'c'. """ +try: + class error(Exception): + pass +except: + error = "anydbm.error" + _names = ['dbhash', 'gdbm', 'dbm', 'dumbdbm'] +_errors = [error] +_defaultmod = None for _name in _names: try: - exec "import %s; _mod = %s" % (_name, _name) + _mod = __import__(_name) except ImportError: continue - else: - break -else: + if not _defaultmod: + _defaultmod = _mod + _errors.append(_mod.error) + +if not _defaultmod: raise ImportError, "no dbm clone found; tried %s" % _names -error = _mod.error +error = tuple(_errors) def open(file, flag = 'r', mode = 0666): - return _mod.open(file, flag, mode) + # guess the type of an existing database + from whichdb import whichdb + result=whichdb(file) + if result is None: + # db doesn't exist + if 'c' in flag or 'n' in flag: + # file doesn't exist and the new + # flag was used so use default type + mod = _defaultmod + else: + raise error, "need 'c' or 'n' flag to open new db" + elif result == "": + # db type cannot be determined + raise error, "db type could not be determined" + else: + mod = __import__(result) + return mod.open(file, flag, mode) |