10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"""Generic interface to all dbm clones.
20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
30a8c90248264a8b26970b4473770bcc3df8515fJosh GaoInstead of
40a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
50a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        import dbm
60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        d = dbm.open(file, 'w', 0666)
70a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
80a8c90248264a8b26970b4473770bcc3df8515fJosh Gaouse
90a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        import anydbm
110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        d = anydbm.open(file, 'w')
120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
130a8c90248264a8b26970b4473770bcc3df8515fJosh GaoThe returned object is a dbhash, gdbm, dbm or dumbdbm object,
140a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodependent on the type of database being opened (determined by whichdb
150a8c90248264a8b26970b4473770bcc3df8515fJosh Gaomodule) in the case of an existing dbm. If the dbm does not exist and
160a8c90248264a8b26970b4473770bcc3df8515fJosh Gaothe create or new flag ('c' or 'n') was specified, the dbm type will
170a8c90248264a8b26970b4473770bcc3df8515fJosh Gaobe determined by the availability of the modules (tested in the above
180a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoorder).
190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
200a8c90248264a8b26970b4473770bcc3df8515fJosh GaoIt has the following interface (key and data are strings):
210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        d[key] = data   # store data at key (may override data at
230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        # existing key)
240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        data = d[key]   # retrieve data at key (raise KeyError if no
250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        # such key)
260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        del d[key]      # delete data stored at key (raises KeyError
270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        # if no such key)
280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        flag = key in d   # true if the key exists
290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        list = d.keys() # return a list of all existing keys (slow!)
300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
310a8c90248264a8b26970b4473770bcc3df8515fJosh GaoFuture versions may change the order in which implementations are
320a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotested for existence, and add interfaces to other dbm-like
330a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimplementations.
340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"""
350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
360a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass error(Exception):
370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    pass
380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_names = ['dbhash', 'gdbm', 'dbm', 'dumbdbm']
400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_errors = [error]
410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_defaultmod = None
420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
430a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofor _name in _names:
440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    try:
450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        _mod = __import__(_name)
460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    except ImportError:
470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        continue
480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if not _defaultmod:
490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        _defaultmod = _mod
500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    _errors.append(_mod.error)
510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
520a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif not _defaultmod:
530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    raise ImportError, "no dbm clone found; tried %s" % _names
540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
550a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoerror = tuple(_errors)
560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
570a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef open(file, flag='r', mode=0666):
580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Open or create database at path given by *file*.
590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Optional argument *flag* can be 'r' (default) for read-only access, 'w'
610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    for read-write access of an existing database, 'c' for read-write access
620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    to a new or existing database, and 'n' for read-write access to a new
630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    database.
640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Note: 'r' and 'w' fail if the database doesn't exist; 'c' creates it
660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    only if it doesn't exist; and 'n' always creates a new database.
670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # guess the type of an existing database
700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    from whichdb import whichdb
710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    result=whichdb(file)
720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if result is None:
730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # db doesn't exist
740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if 'c' in flag or 'n' in flag:
750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # file doesn't exist and the new
760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # flag was used so use default type
770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            mod = _defaultmod
780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise error, "need 'c' or 'n' flag to open new db"
800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    elif result == "":
810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # db type cannot be determined
820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        raise error, "db type could not be determined"
830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    else:
840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mod = __import__(result)
850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return mod.open(file, flag, mode)
86