[Python-checkins] CVS: python/dist/src/Lib mailbox.py,1.28,1.29

Barry Warsaw bwarsaw@users.sourceforge.net
Wed, 31 Jan 2001 14:13:17 -0800


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv11828

Modified Files:
	mailbox.py 
Log Message:
Two changes:

- All constructors grow an optional argument `factory' which is a
  callable used when new message instances are created by the next()
  methods.  Defaults to the rfc822.Message class.

- A new subclass of UnixMailbox is added, called PortableUnixMailbox.
  It's identical to UnixMailbox, but uses a more portable test for
  From_ delimiter lines.  With PortableUnixMailbox, any line that
  starts with "From " is considered a delimiter (this should really
  check for two newlines before the F, but it doesn't.


Index: mailbox.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/mailbox.py,v
retrieving revision 1.28
retrieving revision 1.29
diff -C2 -r1.28 -r1.29
*** mailbox.py	2001/01/24 06:27:27	1.28
--- mailbox.py	2001/01/31 22:13:15	1.29
***************
*** 10,16 ****
  
  class _Mailbox:
!     def __init__(self, fp):
          self.fp = fp
          self.seekp = 0
  
      def seek(self, pos, whence=0):
--- 10,17 ----
  
  class _Mailbox:
!     def __init__(self, fp, factory=rfc822.Message):
          self.fp = fp
          self.seekp = 0
+         self.factory = factory
  
      def seek(self, pos, whence=0):
***************
*** 35,39 ****
              if start != stop:
                  break
!         return rfc822.Message(_Subfile(self.fp, start, stop))
  
  
--- 36,40 ----
              if start != stop:
                  break
!         return self.factory(_Subfile(self.fp, start, stop))
  
  
***************
*** 118,126 ****
                  return
  
!     # An overridable mechanism to test for From-line-ness.
!     # You can either specify a different regular expression
!     # or define a whole new _isrealfromline() method.
!     # Note that this only gets called for lines starting with
!     # the 5 characters "From ".
  
      _fromlinepattern = r"From \s*[^\s]+\s+\w\w\w\s+\w\w\w\s+\d?\d\s+" \
--- 119,146 ----
                  return
  
!     # An overridable mechanism to test for From-line-ness.  You can either
!     # specify a different regular expression or define a whole new
!     # _isrealfromline() method.  Note that this only gets called for lines
!     # starting with the 5 characters "From ".
!     #
!     # BAW: According to
!     #http://home.netscape.com/eng/mozilla/2.0/relnotes/demo/content-length.html
!     # the only portable, reliable way to find message delimiters in a BSD (i.e
!     # Unix mailbox) style folder is to search for "\n\nFrom .*\n", or at the
!     # beginning of the file, "^From .*\n".  While _fromlinepattern below seems
!     # like a good idea, in practice, there are too many variations for more
!     # strict parsing of the line to be completely accurate.
!     #
!     # _strict_isrealfromline() is the old version which tries to do stricter
!     # parsing of the From_ line.  _portable_isrealfromline() simply returns
!     # true, since it's never called if the line doesn't already start with
!     # "From ".
!     #
!     # This algorithm, and the way it interacts with _search_start() and
!     # _search_end() may not be completely correct, because it doesn't check
!     # that the two characters preceding "From " are \n\n or the beginning of
!     # the file.  Fixing this would require a more extensive rewrite than is
!     # necessary.  For convenience, we've added a StrictUnixMailbox class which
!     # uses the older, more strict _fromlinepattern regular expression.
  
      _fromlinepattern = r"From \s*[^\s]+\s+\w\w\w\s+\w\w\w\s+\d?\d\s+" \
***************
*** 128,132 ****
      _regexp = None
  
!     def _isrealfromline(self, line):
          if not self._regexp:
              import re
--- 148,152 ----
      _regexp = None
  
!     def _strict_isrealfromline(self, line):
          if not self._regexp:
              import re
***************
*** 134,138 ****
--- 154,167 ----
          return self._regexp.match(line)
  
+     def _portable_isrealfromline(self, line):
+         return 1
  
+     _isrealfromline = _strict_isrealfromline
+ 
+ 
+ class PortableUnixMailbox(UnixMailbox):
+     _isrealfromline = UnixMailbox._portable_isrealfromline
+ 
+ 
  class MmdfMailbox(_Mailbox):
      def _search_start(self):
***************
*** 156,160 ****
  
  class MHMailbox:
!     def __init__(self, dirname):
          import re
          pat = re.compile('^[1-9][0-9]*$')
--- 185,189 ----
  
  class MHMailbox:
!     def __init__(self, dirname, factory=rfc822.Message):
          import re
          pat = re.compile('^[1-9][0-9]*$')
***************
*** 169,172 ****
--- 198,202 ----
          # before that str() added 'L':
          self.boxes = map(str, list)
+         self.factory = factory
  
      def next(self):
***************
*** 176,180 ****
          del self.boxes[0]
          fp = open(os.path.join(self.dirname, fn))
!         return rfc822.Message(fp)
  
  
--- 206,210 ----
          del self.boxes[0]
          fp = open(os.path.join(self.dirname, fn))
!         return self.factory(fp)
  
  
***************
*** 182,187 ****
      # Qmail directory mailbox
  
!     def __init__(self, dirname):
          self.dirname = dirname
  
          # check for new mail
--- 212,218 ----
      # Qmail directory mailbox
  
!     def __init__(self, dirname, factory=rfc822.Message):
          self.dirname = dirname
+         self.factory = factory
  
          # check for new mail
***************
*** 203,207 ****
          del self.boxes[0]
          fp = open(fn)
!         return rfc822.Message(fp)
  
  
--- 234,238 ----
          del self.boxes[0]
          fp = open(fn)
!         return self.factory(fp)