diff options
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) |