[Spambayes-checkins] spambayes/spambayes storage.py,1.44,1.45

Tony Meyer anadelonbrin at users.sourceforge.net
Wed Dec 8 03:06:28 CET 2004


Update of /cvsroot/spambayes/spambayes/spambayes
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11398/spambayes

Modified Files:
	storage.py 
Log Message:
Fix an import.

Let ZODB handle nham and nspam for us.

Avoid endless recursive AttributeErrors.

Print out (to stderr) the same messages that the other storage types do.

Ensure that we don't try to load a closed ZODB database.

Do a store before closing to avoid problems.

The unittests for ZODBClassifier now pass, and the database lasts more than one Outlook
 session, so I think it is somewhat usable :)

Index: storage.py
===================================================================
RCS file: /cvsroot/spambayes/spambayes/spambayes/storage.py,v
retrieving revision 1.44
retrieving revision 1.45
diff -C2 -d -r1.44 -r1.45
*** storage.py	22 Nov 2004 00:26:44 -0000	1.44
--- storage.py	8 Dec 2004 02:06:25 -0000	1.45
***************
*** 662,667 ****
  # is ok.
  try:
!     Persistent
! except NameError:
      Persistent = object
  class _PersistentClassifier(classifier.Classifier, Persistent):
--- 662,667 ----
  # is ok.
  try:
!     from persistent import Persistent
! except ImportError:
      Persistent = object
  class _PersistentClassifier(classifier.Classifier, Persistent):
***************
*** 675,685 ****
  class ZODBClassifier(object):
      def __init__(self, db_name):
-         self.statekey = STATE_KEY
          self.db_name = db_name
          self.load()
  
      def __getattr__(self, att):
          # We pretend that we are a classifier subclass.
!         if hasattr(self.classifier, att):
              return getattr(self.classifier, att)
          raise AttributeError("ZODBClassifier object has no attribute '%s'"
--- 675,685 ----
  class ZODBClassifier(object):
      def __init__(self, db_name):
          self.db_name = db_name
+         self.closed = True
          self.load()
  
      def __getattr__(self, att):
          # We pretend that we are a classifier subclass.
!         if hasattr(self, "classifier") and hasattr(self.classifier, att):
              return getattr(self.classifier, att)
          raise AttributeError("ZODBClassifier object has no attribute '%s'"
***************
*** 699,706 ****
  
      def load(self):
          import ZODB
          self.create_storage()
          self.db = ZODB.DB(self.storage)
!         root = self.db.open().root()
          self.classifier = root.get(self.db_name)
          if self.classifier is None:
--- 699,717 ----
  
      def load(self):
+         '''Load state from database'''
          import ZODB
+ 
+         if options["globals", "verbose"]:
+             print >> sys.stderr, 'Loading state from', self.db_name, 'database'
+ 
+         # If we are not closed, then we need to close first before we
+         # reload.
+         if not self.closed:
+             self.close()
+ 
          self.create_storage()
          self.db = ZODB.DB(self.storage)
!         self.conn = self.db.open()
!         root = self.conn.root()
          self.classifier = root.get(self.db_name)
          if self.classifier is None:
***************
*** 709,735 ****
                  print >> sys.stderr, self.db_name, 'is a new ZODB'
              self.classifier = root[self.db_name] = _PersistentClassifier()
-             get_transaction().commit()
          else:
-             # It seems to me that the persistent classifier should store
-             # the nham and nspam values, but that doesn't appear to be the
-             # case, so work around that.  This can be removed once I figure
-             # out the problem.
-             self.nham, self.nspam = self.classifier.wordinfo[self.statekey]
              if options["globals", "verbose"]:
                  print >> sys.stderr, '%s is an existing ZODB, with %d ' \
                        'ham and %d spam' % (self.db_name, self.nham,
                                             self.nspam)
          
      def store(self):
!         # It seems to me that the persistent classifier should store
!         # the nham and nspam values, but that doesn't appear to be the
!         # case, so work around that.  This can be removed once I figure
!         # out the problem.
!         self.classifier.wordinfo[self.statekey] = (self.nham, self.nspam)
!         get_transaction().commit()
  
      def close(self):
          self.db.close()
          self.storage.close()
  
  
--- 720,756 ----
                  print >> sys.stderr, self.db_name, 'is a new ZODB'
              self.classifier = root[self.db_name] = _PersistentClassifier()
          else:
              if options["globals", "verbose"]:
                  print >> sys.stderr, '%s is an existing ZODB, with %d ' \
                        'ham and %d spam' % (self.db_name, self.nham,
                                             self.nspam)
+         self.closed = False
          
      def store(self):
!         '''Place state into persistent store'''
!         import ZODB
!         import transaction
! 
!         assert self.closed == False, "Can't store a closed database"
! 
!         if options["globals", "verbose"]:
!             print >> sys.stderr, 'Persisting', self.db_name, 'state in database'
! 
!         transaction.commit()
  
      def close(self):
+         # Ensure that the db is saved before closing.  Alternatively, we
+         # could abort any waiting transaction.  We need to do *something*
+         # with it, though, or it will be still around after the db is
+         # closed and cause problems.  For now, saving seems to make sense
+         # (and we can always add abort methods if they are ever needed).
+         self.store()
+ 
+         # Do the closing.        
          self.db.close()
          self.storage.close()
+         self.closed = True
+         if options["globals", "verbose"]:
+             print >> sys.stderr, 'Closed', self.db_name, 'database'
  
  



More information about the Spambayes-checkins mailing list