1515# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717#
18- #$Id: back_anydbm.py,v 1.26 2002-01-22 05:18:38 rochecompaan Exp $
18+ #$Id: back_anydbm.py,v 1.27 2002-01-22 07:21:13 richard Exp $
1919'''
2020This module defines a backend that saves the hyperdatabase in a database
2121chosen by anydbm. It is guaranteed to always be available in python
2626import whichdb , anydbm , os , marshal
2727from roundup import hyperdb , date , password
2828
29- DEBUG = os .environ .get ('HYPERDBDEBUG' , '' )
30-
3129#
3230# Now the database
3331#
@@ -71,13 +69,13 @@ def __repr__(self):
7169 def __getattr__ (self , classname ):
7270 """A convenient way of calling self.getclass(classname)."""
7371 if self .classes .has_key (classname ):
74- if DEBUG :
72+ if hyperdb . DEBUG :
7573 print '__getattr__' , (self , classname )
7674 return self .classes [classname ]
7775 raise AttributeError , classname
7876
7977 def addclass (self , cl ):
80- if DEBUG :
78+ if hyperdb . DEBUG :
8179 print 'addclass' , (self , cl )
8280 cn = cl .classname
8381 if self .classes .has_key (cn ):
@@ -86,7 +84,7 @@ def addclass(self, cl):
8684
8785 def getclasses (self ):
8886 """Return a list of the names of all existing classes."""
89- if DEBUG :
87+ if hyperdb . DEBUG :
9088 print 'getclasses' , (self ,)
9189 l = self .classes .keys ()
9290 l .sort ()
@@ -97,7 +95,7 @@ def getclass(self, classname):
9795
9896 If 'classname' is not a valid class name, a KeyError is raised.
9997 """
100- if DEBUG :
98+ if hyperdb . DEBUG :
10199 print 'getclass' , (self , classname )
102100 return self .classes [classname ]
103101
@@ -107,7 +105,7 @@ def getclass(self, classname):
107105 def clear (self ):
108106 '''Delete all database contents
109107 '''
110- if DEBUG :
108+ if hyperdb . DEBUG :
111109 print 'clear' , (self ,)
112110 for cn in self .classes .keys ():
113111 for type in 'nodes' , 'journals' :
@@ -121,15 +119,15 @@ def getclassdb(self, classname, mode='r'):
121119 ''' grab a connection to the class db that will be used for
122120 multiple actions
123121 '''
124- if DEBUG :
122+ if hyperdb . DEBUG :
125123 print 'getclassdb' , (self , classname , mode )
126124 return self ._opendb ('nodes.%s' % classname , mode )
127125
128126 def _opendb (self , name , mode ):
129127 '''Low-level database opener that gets around anydbm/dbm
130128 eccentricities.
131129 '''
132- if DEBUG :
130+ if hyperdb . DEBUG :
133131 print '_opendb' , (self , name , mode )
134132 # determine which DB wrote the class file
135133 db_type = ''
@@ -145,7 +143,7 @@ def _opendb(self, name, mode):
145143
146144 # new database? let anydbm pick the best dbm
147145 if not db_type :
148- if DEBUG :
146+ if hyperdb . DEBUG :
149147 print "_opendb anydbm.open(%r, 'n')" % path
150148 return anydbm .open (path , 'n' )
151149
@@ -156,7 +154,7 @@ def _opendb(self, name, mode):
156154 raise hyperdb .DatabaseError , \
157155 "Couldn't open database - the required module '%s'" \
158156 "is not available" % db_type
159- if DEBUG :
157+ if hyperdb . DEBUG :
160158 print "_opendb %r.open(%r, %r)" % (db_type , path , mode )
161159 return dbm .open (path , mode )
162160
@@ -166,7 +164,7 @@ def _opendb(self, name, mode):
166164 def addnode (self , classname , nodeid , node ):
167165 ''' add the specified node to its class's db
168166 '''
169- if DEBUG :
167+ if hyperdb . DEBUG :
170168 print 'addnode' , (self , classname , nodeid , node )
171169 self .newnodes .setdefault (classname , {})[nodeid ] = 1
172170 self .cache .setdefault (classname , {})[nodeid ] = node
@@ -175,7 +173,7 @@ def addnode(self, classname, nodeid, node):
175173 def setnode (self , classname , nodeid , node ):
176174 ''' change the specified node
177175 '''
178- if DEBUG :
176+ if hyperdb . DEBUG :
179177 print 'setnode' , (self , classname , nodeid , node )
180178 self .dirtynodes .setdefault (classname , {})[nodeid ] = 1
181179 # can't set without having already loaded the node
@@ -185,15 +183,15 @@ def setnode(self, classname, nodeid, node):
185183 def savenode (self , classname , nodeid , node ):
186184 ''' perform the saving of data specified by the set/addnode
187185 '''
188- if DEBUG :
186+ if hyperdb . DEBUG :
189187 print 'savenode' , (self , classname , nodeid , node )
190188 self .transactions .append ((self ._doSaveNode , (classname , nodeid , node )))
191189
192190 def getnode (self , classname , nodeid , db = None , cache = 1 ):
193191 ''' get a node from the database
194192 '''
195- if DEBUG :
196- print 'getnode' , (self , classname , nodeid , cldb )
193+ if hyperdb . DEBUG :
194+ print 'getnode' , (self , classname , nodeid , db )
197195 if cache :
198196 # try the cache
199197 cache = self .cache .setdefault (classname , {})
@@ -213,8 +211,8 @@ def getnode(self, classname, nodeid, db=None, cache=1):
213211 def hasnode (self , classname , nodeid , db = None ):
214212 ''' determine if the database has a given node
215213 '''
216- if DEBUG :
217- print 'hasnode' , (self , classname , nodeid , cldb )
214+ if hyperdb . DEBUG :
215+ print 'hasnode' , (self , classname , nodeid , db )
218216 # try the cache
219217 cache = self .cache .setdefault (classname , {})
220218 if cache .has_key (nodeid ):
@@ -227,8 +225,8 @@ def hasnode(self, classname, nodeid, db=None):
227225 return res
228226
229227 def countnodes (self , classname , db = None ):
230- if DEBUG :
231- print 'countnodes' , (self , classname , cldb )
228+ if hyperdb . DEBUG :
229+ print 'countnodes' , (self , classname , db )
232230 # include the new nodes not saved to the DB yet
233231 count = len (self .newnodes .get (classname , {}))
234232
@@ -239,7 +237,7 @@ def countnodes(self, classname, db=None):
239237 return count
240238
241239 def getnodeids (self , classname , db = None ):
242- if DEBUG :
240+ if hyperdb . DEBUG :
243241 print 'getnodeids' , (self , classname , db )
244242 # start off with the new nodes
245243 res = self .newnodes .get (classname , {}).keys ()
@@ -294,15 +292,15 @@ def addjournal(self, classname, nodeid, action, params):
294292 'link' or 'unlink' -- 'params' is (classname, nodeid, propname)
295293 'retire' -- 'params' is None
296294 '''
297- if DEBUG :
295+ if hyperdb . DEBUG :
298296 print 'addjournal' , (self , classname , nodeid , action , params )
299297 self .transactions .append ((self ._doSaveJournal , (classname , nodeid ,
300298 action , params )))
301299
302300 def getjournal (self , classname , nodeid ):
303301 ''' get the journal for id
304302 '''
305- if DEBUG :
303+ if hyperdb . DEBUG :
306304 print 'getjournal' , (self , classname , nodeid )
307305 # attempt to open the journal - in some rare cases, the journal may
308306 # not exist
@@ -322,7 +320,7 @@ def getjournal(self, classname, nodeid):
322320
323321 def pack (self , pack_before ):
324322 ''' delete all journal entries before 'pack_before' '''
325- if DEBUG :
323+ if hyperdb . DEBUG :
326324 print 'packjournal' , (self , pack_before )
327325
328326 pack_before = pack_before .get_tuple ()
@@ -375,7 +373,7 @@ def pack(self, pack_before):
375373 def commit (self ):
376374 ''' Commit the current transactions.
377375 '''
378- if DEBUG :
376+ if hyperdb . DEBUG :
379377 print 'commit' , (self ,)
380378 # TODO: lock the DB
381379
@@ -399,7 +397,7 @@ def commit(self):
399397 self .transactions = []
400398
401399 def _doSaveNode (self , classname , nodeid , node ):
402- if DEBUG :
400+ if hyperdb . DEBUG :
403401 print '_doSaveNode' , (self , classname , nodeid , node )
404402
405403 # get the database handle
@@ -413,7 +411,7 @@ def _doSaveNode(self, classname, nodeid, node):
413411 db [nodeid ] = marshal .dumps (node )
414412
415413 def _doSaveJournal (self , classname , nodeid , action , params ):
416- if DEBUG :
414+ if hyperdb . DEBUG :
417415 print '_doSaveJournal' , (self , classname , nodeid , action , params )
418416 entry = (nodeid , date .Date ().get_tuple (), self .journaltag , action ,
419417 params )
@@ -441,7 +439,7 @@ def _doStoreFile(self, name, **databases):
441439 def rollback (self ):
442440 ''' Reverse all actions from the current transaction.
443441 '''
444- if DEBUG :
442+ if hyperdb . DEBUG :
445443 print 'rollback' , (self , )
446444 for method , args in self .transactions :
447445 # delete temporary files
@@ -455,6 +453,9 @@ def rollback(self):
455453
456454#
457455#$Log: not supported by cvs2svn $
456+ #Revision 1.26 2002/01/22 05:18:38 rochecompaan
457+ #last_set_entry was referenced before assignment
458+ #
458459#Revision 1.25 2002/01/22 05:06:08 rochecompaan
459460#We need to keep the last 'set' entry in the journal to preserve
460461#information on 'activity' for nodes.
0 commit comments