[Spambayes-checkins] spambayes/Outlook2000 manager.py,1.88,1.89

Mark Hammond mhammond at users.sourceforge.net
Sun Dec 7 19:59:21 EST 2003


Update of /cvsroot/spambayes/spambayes/Outlook2000
In directory sc8-pr-cvs1:/tmp/cvs-serv29828

Modified Files:
	manager.py 
Log Message:
* Add/Fix a number of 'unicode file' related comments.
* Allow multiple manager objects to work in the same process (but not
  at the same time):
  - If the global manager is being closed, drop the global var holding it,
    so the next request for the manager will make a new one.
  - In this case, 'spambayes.Options' has already been imported, so our
    simple 'imported too early' check failed.  If spambayes.Options has
    been imported, we assert that the INI file it is using is our INI.
* Wrap a few long lines.


Index: manager.py
===================================================================
RCS file: /cvsroot/spambayes/spambayes/Outlook2000/manager.py,v
retrieving revision 1.88
retrieving revision 1.89
diff -C2 -d -r1.88 -r1.89
*** manager.py	29 Sep 2003 02:14:25 -0000	1.88
--- manager.py	8 Dec 2003 00:59:19 -0000	1.89
***************
*** 60,66 ****
                                        win32con.MB_ICONQUESTION) == win32con.IDYES
  
! # Notes on Unicode directory names
! # You will have much more success with extended characters in
! # directory names using Python 2.3.
  try:
      filesystem_encoding = sys.getfilesystemencoding()
--- 60,65 ----
                                        win32con.MB_ICONQUESTION) == win32con.IDYES
  
! # Non-ascii characters in file or directory names only fully work in
! # Python 2.3.3+, but latin-1 "compatible" filenames should work in 2.2
  try:
      filesystem_encoding = sys.getfilesystemencoding()
***************
*** 71,85 ****
  # the directory of our main .py/.dll/.exe file we
  # are running from.
! try:
!     if hasattr(sys, "frozen"):
!         if sys.frozen == "dll":
!             this_filename = win32api.GetModuleFileName(sys.frozendllhandle)
!         else:
!             # Don't think we will ever run as a .EXE, but...
!             this_filename = os.path.abspath(sys.argv[0])
!     else:
          this_filename = os.path.abspath(__file__)
! except NameError: # no __file__
!     this_filename = os.path.abspath(sys.argv[0])
  
  # See if we can use the new bsddb module. (The old one is unreliable
--- 70,81 ----
  # the directory of our main .py/.dll/.exe file we
  # are running from.
! if hasattr(sys, "frozen"):
!     assert sys.frozen == "dll", "outlook only supports inproc servers"
!     this_filename = win32api.GetModuleFileName(sys.frozendllhandle)
! else:
!     try:
          this_filename = os.path.abspath(__file__)
!     except NameError: # no __file__ - means Py2.2 and __name__=='__main__'
!         this_filename = os.path.abspath(sys.argv[0])
  
  # See if we can use the new bsddb module. (The old one is unreliable
***************
*** 96,99 ****
--- 92,97 ----
      except ImportError:
          # No DB library at all!
+         assert not hasattr(sys, "frozen"), \
+                "Don't build binary versions without bsbbd!"
          use_db = False
  
***************
*** 117,122 ****
  
  def import_core_spambayes_stuff(ini_filename):
!     assert "spambayes.Options" not in sys.modules, \
!         "'spambayes.Options' was imported too early"
      global bayes_classifier, bayes_tokenize, bayes_storage
      # ini_filename is Unicode, but environ not unicode aware
--- 115,129 ----
  
  def import_core_spambayes_stuff(ini_filename):
!     if "spambayes.Options" in sys.modules:
!         # Manager probably being re-initialized (via the Outlook 'addin' GUI
!         # Check that nothing has changed underneath us.
!         if __debug__:
!             import spambayes.Options
!             assert spambayes.Options.optionsPathname == \
!                    ini_filename.encode(filesystem_encoding), \
!                    "'spambayes.Options' was imported too early, with the " \
!                    "incorrect directory %r" \
!                    % (spambayes.Options.optionsPathname,)
!         return
      global bayes_classifier, bayes_tokenize, bayes_storage
      # ini_filename is Unicode, but environ not unicode aware
***************
*** 131,138 ****
          "Expected 'spambayes.Options' to be loaded here"
  
- class ManagerError(Exception):
-     pass
- 
- 
  # Function to "safely" save a pickle, only overwriting
  # the existing file after a successful write.
--- 138,141 ----
***************
*** 351,361 ****
                  if not os.path.isdir(value):
                      os.makedirs(value)
!                 if not os.path.isdir(value):
!                     raise os.error
                  value = os.path.abspath(value)
              except os.error:
!                 print "The configuration files have specified a data directory of"
!                 print repr(value)
!                 print "but it is not valid.  Using default"
                  value = None
          if value:
--- 354,363 ----
                  if not os.path.isdir(value):
                      os.makedirs(value)
!                 assert os.path.isdir(value), "just made the *ucker"
                  value = os.path.abspath(value)
              except os.error:
!                 print "The configuration files have specified a data " \
!                       "directory of", repr(value), "but it is not valid. " \
!                       "Using default."
                  value = None
          if value:
***************
*** 373,377 ****
          self.LoadConfig()
  
!         bayes_options_filename = os.path.join(self.data_directory, "default_bayes_customize.ini")
          import_core_spambayes_stuff(bayes_options_filename)
  
--- 375,380 ----
          self.LoadConfig()
  
!         bayes_options_filename = os.path.join(self.data_directory,
!                                               "default_bayes_customize.ini")
          import_core_spambayes_stuff(bayes_options_filename)
  
***************
*** 487,491 ****
          # A bit of a nod to save people doing a full retrain.
          # Try and locate our files in the old location, and move
!         # then to the new one.
          # Also used first time SpamBayes is run - this will cause
          # the ini file to be *copied* to the correct directory
--- 490,494 ----
          # A bit of a nod to save people doing a full retrain.
          # Try and locate our files in the old location, and move
!         # them to the new one.
          # Also used first time SpamBayes is run - this will cause
          # the ini file to be *copied* to the correct directory
***************
*** 753,762 ****
  
      def Close(self):
          self.classifier_data.Close()
!         self.config = None
          if self.message_store is not None:
              self.message_store.Close()
              self.message_store = None
          self.outlook = None
  
      def score(self, msg, evidence=False):
--- 756,770 ----
  
      def Close(self):
+         global _mgr
          self.classifier_data.Close()
!         self.config = self.options = None
          if self.message_store is not None:
              self.message_store.Close()
              self.message_store = None
          self.outlook = None
+         self.addin = None
+         # If we are the global manager, reset that
+         if _mgr is self:
+             _mgr = None
  
      def score(self, msg, evidence=False):
***************
*** 850,860 ****
  
  def main(verbose_level = 1):
!     try:
!         mgr = GetManager()
!         mgr.verbose = max(mgr.verbose, verbose_level)
!     except ManagerError, d:
!         print "Error initializing Bayes manager"
!         print d
!         return 1
      ShowManager(mgr)
      mgr.Save()
--- 858,863 ----
  
  def main(verbose_level = 1):
!     mgr = GetManager()
!     mgr.verbose = max(mgr.verbose, verbose_level)
      ShowManager(mgr)
      mgr.Save()





More information about the Spambayes-checkins mailing list