Mercurial > p > roundup > code
diff roundup/backends/back_anydbm.py @ 443:a0c598702f17
I fixed the problems with anydbm using the dbm module at the backend.
It turns out the dbm module modifies the file name to append ".db"
and my check to determine if we're opening an existing or new db just
tested os.path.exists() on the filename. Well, no longer! We now perform a
much better check _and_ cope with the anydbm implementation module changing
too!
I also fixed the backends __init__ so only ImportError is squashed.
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Wed, 12 Dec 2001 02:30:51 +0000 |
| parents | de5bf4191f11 |
| children | 3fa2268041a8 |
line wrap: on
line diff
--- a/roundup/backends/back_anydbm.py Tue Dec 11 04:50:49 2001 +0000 +++ b/roundup/backends/back_anydbm.py Wed Dec 12 02:30:51 2001 +0000 @@ -15,7 +15,7 @@ # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. # -#$Id: back_anydbm.py,v 1.14 2001-12-10 22:20:01 richard Exp $ +#$Id: back_anydbm.py,v 1.15 2001-12-12 02:30:51 richard Exp $ ''' This module defines a backend that saves the hyperdatabase in a database chosen by anydbm. It is guaranteed to always be available in python @@ -23,7 +23,7 @@ serious bugs, and is not available) ''' -import anydbm, os, marshal +import whichdb, anydbm, os, marshal from roundup import hyperdb, date, password # @@ -88,22 +88,46 @@ # Class DBs # def clear(self): + '''Delete all database contents + ''' for cn in self.classes.keys(): - db = os.path.join(self.dir, 'nodes.%s'%cn) - anydbm.open(db, 'n') - db = os.path.join(self.dir, 'journals.%s'%cn) - anydbm.open(db, 'n') + for type in 'nodes', 'journals': + path = os.path.join(self.dir, 'journals.%s'%cn) + if os.path.exists(path): + os.remove(path) + elif os.path.exists(path+'.db'): # dbm appends .db + os.remove(path+'.db') def getclassdb(self, classname, mode='r'): ''' grab a connection to the class db that will be used for multiple actions ''' + # determine which DB wrote the class file path = os.path.join(os.getcwd(), self.dir, 'nodes.%s'%classname) - if os.path.exists(path): - return anydbm.open(path, mode) - else: + db_type = whichdb.whichdb(path) + if not db_type: + # dbm appends ".db" + db_type = whichdb.whichdb(path+'.db') + db_type = whichdb.whichdb(path) + + # if we can't identify it and it exists... + if not db_type and os.path.exists(path) or os.path.exists(path+'.db'): + raise hyperdb.DatabaseError, \ + "Couldn't identify the database type" + + # new database? let anydbm pick the best dbm + if not db_type: return anydbm.open(path, 'n') + # open the database with the correct module + try: + dbm = __import__(db_type) + except: + raise hyperdb.DatabaseError, \ + "Couldn't open database - the required module '%s'"\ + "is not available"%db_type + return dbm.open(path, mode) + # # Nodes # @@ -254,6 +278,13 @@ # #$Log: not supported by cvs2svn $ +#Revision 1.14 2001/12/10 22:20:01 richard +#Enabled transaction support in the bsddb backend. It uses the anydbm code +#where possible, only replacing methods where the db is opened (it uses the +#btree opener specifically.) +#Also cleaned up some change note generation. +#Made the backends package work with pydoc too. +# #Revision 1.13 2001/12/02 05:06:16 richard #. We now use weakrefs in the Classes to keep the database reference, so # the close() method on the database is no longer needed.
