[Spambayes-checkins] spambayes/scripts sb_imapfilter.py,1.45,1.46

Tony Meyer anadelonbrin at users.sourceforge.net
Thu Dec 16 06:31:22 CET 2004


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

Modified Files:
	sb_imapfilter.py 
Log Message:
Compile re's and do this only once.  This should speed things up (previously we regenerated
 the re each time (eg) we retrieved a message.

Safeguard getting the RFC822.Header data, as this is causing problems at the moment.
  Print out additional debugging material for the moment, and possibly handle this
 better later, when we know more about what is going wrong.

Index: sb_imapfilter.py
===================================================================
RCS file: /cvsroot/spambayes/spambayes/scripts/sb_imapfilter.py,v
retrieving revision 1.45
retrieving revision 1.46
diff -C2 -d -r1.45 -r1.46
*** sb_imapfilter.py	30 Nov 2004 23:49:23 -0000	1.45
--- sb_imapfilter.py	16 Dec 2004 05:31:02 -0000	1.46
***************
*** 131,135 ****
          # There's a tricky situation where if use_ssl is False, but we
          # try to connect to a IMAP over SSL server, we will just hang
!         # forever, waiting for a repsonse that will never come.  To
          # get past this, just for the welcome message, we install a
          # timeout on the connection.  Normal service is then returned.
--- 131,135 ----
          # There's a tricky situation where if use_ssl is False, but we
          # try to connect to a IMAP over SSL server, we will just hang
!         # forever, waiting for a response that will never come.  To
          # get past this, just for the welcome message, we install a
          # timeout on the connection.  Normal service is then returned.
***************
*** 256,259 ****
--- 256,261 ----
              return data
  
+     number_re = re.compile(r"{\d+}")
+     folder_re = re.compile(r"\(([\w\\ ]*)\) ")
      def folder_list(self):
          """Return a alphabetical list of all folders available on the
***************
*** 272,282 ****
              # literal, so we need to crunch this out.
              if isinstance(fol, types.TupleType):
!                 m = re.search(r"{\d+}", fol[0])
                  if not m:
                      # Something is wrong here!  Skip this folder.
                      continue
                  fol = '%s"%s"' % (fol[0][:m.start()], fol[1])
!             r = re.compile(r"\(([\w\\ ]*)\) ")
!             m = r.search(fol)
              if not m:
                  # Something is not good with this folder, so skip it.
--- 274,283 ----
              # literal, so we need to crunch this out.
              if isinstance(fol, types.TupleType):
!                 m = self.number_re.search(fol[0])
                  if not m:
                      # Something is wrong here!  Skip this folder.
                      continue
                  fol = '%s"%s"' % (fol[0][:m.start()], fol[1])
!             m = self.folder_re.search(fol)
              if not m:
                  # Something is not good with this folder, so skip it.
***************
*** 511,514 ****
--- 512,516 ----
              return message.SBHeaderMessage.as_string(self, unixfrom)
  
+     recent_re = re.compile(r"\\Recent ?| ?\\Recent")
      def Save(self):
          """Save message to IMAP server.
***************
*** 537,541 ****
              # The \Recent flag can be fetched, but cannot be stored
              # We must remove it from the list if it is there.
!             flags = re.sub(r"\\Recent ?| ?\\Recent", "", flags)
          else:
              flags = None
--- 539,543 ----
              # The \Recent flag can be fetched, but cannot be stored
              # We must remove it from the list if it is there.
!             flags = self.recent_re.sub("", flags)
          else:
              flags = None
***************
*** 676,679 ****
--- 678,686 ----
              return []
  
+     custom_header_id_re = re.compile(re.escape(\
+         options["Headers", "mailid_header_name"]) + "\:\s*(\d+(?:\-\d)?)",
+                                      re.IGNORECASE)
+     message_id_re = re.compile("Message-ID\: ?\<([^\n\>]+)\>",
+                                re.IGNORECASE)
      def __getitem__(self, key):
          """Return message matching the given *uid*.
***************
*** 688,694 ****
          # that not all servers accept it, even though it is in the RFC
          response = self.imap_server.uid("FETCH", key, "RFC822.HEADER")
!         data = self.imap_server.check_response("fetch %s rfc822.header" \
!                                                % (key,), response)
!         data = self.imap_server.extract_fetch_data(data[0])
  
          # Create a new IMAPMessage object, which will be the return value.
--- 695,701 ----
          # that not all servers accept it, even though it is in the RFC
          response = self.imap_server.uid("FETCH", key, "RFC822.HEADER")
!         response_data = self.imap_server.check_response(\
!             "fetch %s rfc822.header" % (key,), response)
!         data = self.imap_server.extract_fetch_data(response_data[0])
  
          # Create a new IMAPMessage object, which will be the return value.
***************
*** 700,709 ****
          # We use the MessageID header as the ID for the message, as long
          # as it is available, and if not, we add our own.
!         custom_header_id = re.escape(options["Headers",
!                                              "mailid_header_name"]) + \
!                            "\:\s*(\d+(?:\-\d)?)"
          # Search for our custom id first, for backwards compatibility.
!         for id_header in [custom_header_id, "Message-ID\: ?\<([^\n\>]+)\>"]:
!             mo = re.search(id_header, data["RFC822.HEADER"], re.IGNORECASE)
              if mo:
                  msg.setId(mo.group(1))
--- 707,728 ----
          # We use the MessageID header as the ID for the message, as long
          # as it is available, and if not, we add our own.
!         try:
!             headers = data["RFC822.HEADER"]
!         except KeyError:
!             # This is bad!  We asked for this in the fetch, so either
!             # our parsing is wrong or the response from the server is
!             # wrong.  For the moment, print out some debugging info
!             # and don't do anything else (which means we will keep
!             # coming back to this message).
!             print >> sys.stderr, "Trouble parsing response:", \
!                   response_data, data
!             print >> sys.stderr, "Please report this to spambayes at python.org"
!             if options["globals", "verbose"]:
!                 sys.stdout.write("?")
!             return msg
!         
          # Search for our custom id first, for backwards compatibility.
!         for id_header_re in [self.custom_header_id_re, self.message_id_re]:
!             mo = id_header_re.search(headers)
              if mo:
                  msg.setId(mo.group(1))



More information about the Spambayes-checkins mailing list