[Spambayes-checkins] spambayes/spambayes storage.py,1.51,1.52

Tony Meyer anadelonbrin at users.sourceforge.net
Tue Nov 15 01:22:19 CET 2005


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

Modified Files:
	storage.py 
Log Message:
Allow subclassing of ZODB storage.
Work with earlier versions of ZODB.
Add simple handling of ConflictErrors.
Pack database.
Have onAddMessage honour training flags.

Index: storage.py
===================================================================
RCS file: /cvsroot/spambayes/spambayes/spambayes/storage.py,v
retrieving revision 1.51
retrieving revision 1.52
diff -C2 -d -r1.51 -r1.52
*** storage.py	22 Apr 2005 04:08:25 -0000	1.51
--- storage.py	15 Nov 2005 00:22:15 -0000	1.52
***************
*** 65,68 ****
--- 65,69 ----
  import os
  import sys
+ import time
  import types
  from spambayes import classifier
***************
*** 677,681 ****
      from persistent import Persistent
  except ImportError:
!     Persistent = object
  class _PersistentClassifier(classifier.Classifier, Persistent):
      def __init__(self):
--- 678,685 ----
      from persistent import Persistent
  except ImportError:
!     try:
!         from ZODB import Persistent
!     except ImportError:
!         Persistent = object
  class _PersistentClassifier(classifier.Classifier, Persistent):
      def __init__(self):
***************
*** 687,690 ****
--- 691,697 ----
  
  class ZODBClassifier(object):
+     # Allow subclasses to override classifier class.
+     ClassifierClass = _PersistentClassifier
+ 
      def __init__(self, db_name):
          self.db_filename = db_name
***************
*** 702,706 ****
      def __setattr__(self, att, value):
          # For some attributes, we change the classifier instead.
!         if att in ["nham", "nspam"]:
              setattr(self.classifier, att, value)
          else:
--- 709,713 ----
      def __setattr__(self, att, value):
          # For some attributes, we change the classifier instead.
!         if att in ("nham", "nspam") and hasattr(self, "classifier"):
              setattr(self.classifier, att, value)
          else:
***************
*** 729,733 ****
          self.conn = self.db.open()
          root = self.conn.root()
!         
          self.classifier = root.get(self.db_name)
          if self.classifier is None:
--- 736,740 ----
          self.conn = self.db.open()
          root = self.conn.root()
! 
          self.classifier = root.get(self.db_name)
          if self.classifier is None:
***************
*** 735,739 ****
              if options["globals", "verbose"]:
                  print >> sys.stderr, self.db_name, 'is a new ZODB'
!             self.classifier = root[self.db_name] = _PersistentClassifier()
          else:
              if options["globals", "verbose"]:
--- 742,746 ----
              if options["globals", "verbose"]:
                  print >> sys.stderr, self.db_name, 'is a new ZODB'
!             self.classifier = root[self.db_name] = self.ClassifierClass()
          else:
              if options["globals", "verbose"]:
***************
*** 742,750 ****
                                             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"
--- 749,764 ----
                                             self.nspam)
          self.closed = False
! 
      def store(self):
          '''Place state into persistent store'''
!         try:
!             import ZODB
!             import ZODB.Transaction
!         except ImportError:
!             import transaction
!             commit = transaction.commit
!         else:
!             commit = ZODB.Transaction.get_transaction().commit
!         from ZODB.POSException import ConflictError            
  
          assert self.closed == False, "Can't store a closed database"
***************
*** 753,757 ****
              print >> sys.stderr, 'Persisting', self.db_name, 'state in database'
  
!         transaction.commit()
  
      def close(self):
--- 767,778 ----
              print >> sys.stderr, 'Persisting', self.db_name, 'state in database'
  
!         try:
!             commit()
!         except ConflictError:
!             # We'll save it next time, or on close.  It'll be lost if we
!             # hard-crash, but that's unlikely, and not a particularly big
!             # deal.
!             if options["globals", "verbose"]:
!                 print >> sys.stderr, "Conflict on commit", self.db_name
  
      def close(self):
***************
*** 765,769 ****
--- 786,803 ----
          # Do the closing.        
          self.db.close()
+ 
+         # We don't make any use of the 'undo' capabilities of the
+         # FileStorage at the moment, so might as well pack the database
+         # each time it is closed, to save as much disk space as possible.
+         # Pack it up to where it was 'yesterday'.
+         # XXX What is the 'referencesf' parameter for pack()?  It doesn't
+         # XXX seem to do anything according to the source.
+         if hasattr(self.storage, "pack"):
+             self.storage.pack(time.time()-60*60*24, None)
          self.storage.close()
+ 
+         # Ensure that we cannot continue to use this classifier.
+         delattr(self, "classifier")
+ 
          self.closed = True
          if options["globals", "verbose"]:
***************
*** 812,819 ****
      def onAddMessage(self, message, flags=0):
          '''A message is being added to an observed corpus.'''
!         # There are no flags that we currently care about, so
!         # get rid of the variable so that PyChecker doesn't bother us.
!         del flags
!         self.train(message)
  
      def train(self, message):
--- 846,851 ----
      def onAddMessage(self, message, flags=0):
          '''A message is being added to an observed corpus.'''
!         if not (flags & NO_TRAINING_FLAG):
!             self.train(message)
  
      def train(self, message):



More information about the Spambayes-checkins mailing list