[Python-checkins] CVS: python/dist/src/Lib/email Generator.py,1.1,1.2

Barry Warsaw bwarsaw@users.sourceforge.net
Tue, 25 Sep 2001 22:32:43 -0700


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

Modified Files:
	Generator.py 
Log Message:
In class Generator:

    _handle_text(): If the payload is None, then just return (i.e. don't
	write anything).  Subparts of message/delivery-status types
	will have this property since they are just blocks of headers.

	Also, when raising the TypeError, include the type of the
	payload in the error message.

    _handle_multipart(), _handle_message(): When creating a clone of self,
	pass in our _mangle_from_ and maxheaderlen flags so the clone
	has the same behavior.

    _handle_message_delivery_status(): New method to do the proper
	printing of message/delivery-status type messages.  These have
	to be handled differently than other message/* types because
	their payloads are subparts containing just blocks of headers.

In class DecodedGenerator:

    _dispatch(): Skip over multipart/* messages since we don't care
        about them, and don't want the non-text format to appear in
        the printed results.


Index: Generator.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/email/Generator.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** Generator.py	2001/09/23 03:17:28	1.1
--- Generator.py	2001/09/26 05:32:41	1.2
***************
*** 191,196 ****
      def _handle_text(self, msg):
          payload = msg.get_payload()
          if not isinstance(payload, StringType):
!             raise TypeError, 'string payload expected'
          if self._mangle_from_:
              payload = fcre.sub('>From ', payload)
--- 191,198 ----
      def _handle_text(self, msg):
          payload = msg.get_payload()
+         if payload is None:
+             return
          if not isinstance(payload, StringType):
!             raise TypeError, 'string payload expected: %s' % type(payload)
          if self._mangle_from_:
              payload = fcre.sub('>From ', payload)
***************
*** 207,211 ****
          for part in msg.get_payload():
              s = StringIO()
!             g = self.__class__(s)
              g(part, unixfrom=0)
              msgtexts.append(s.getvalue())
--- 209,213 ----
          for part in msg.get_payload():
              s = StringIO()
!             g = self.__class__(s, self._mangle_from_, self.__maxheaderlen)
              g(part, unixfrom=0)
              msgtexts.append(s.getvalue())
***************
*** 246,252 ****
          self._handle_multipart(msg, isdigest=1)
  
!     def _handle_message_rfc822(self, msg):
          s = StringIO()
!         g = self.__class__(s)
          # A message/rfc822 should contain a scalar payload which is another
          # Message object.  Extract that object, stringify it, and write that
--- 248,275 ----
          self._handle_multipart(msg, isdigest=1)
  
!     def _handle_message_delivery_status(self, msg):
!         # We can't just write the headers directly to self's file object
!         # because this will leave an extra newline between the last header
!         # block and the boundary.  Sigh.
!         blocks = []
!         for part in msg.get_payload():
!             s = StringIO()
!             g = self.__class__(s, self._mangle_from_, self.__maxheaderlen)
!             g(part, unixfrom=0)
!             text = s.getvalue()
!             lines = text.split('\n')
!             # Strip off the unnecessary trailing empty line
!             if lines and lines[-1] == '':
!                 blocks.append(NL.join(lines[:-1]))
!             else:
!                 blocks.append(text)
!         # Now join all the blocks with an empty line.  This has the lovely
!         # effect of separating each block with an empty line, but not adding
!         # an extra one after the last one.
!         self._fp.write(NL.join(blocks))
! 
!     def _handle_message(self, msg):
          s = StringIO()
!         g = self.__class__(s, self._mangle_from_, self.__maxheaderlen)
          # A message/rfc822 should contain a scalar payload which is another
          # Message object.  Extract that object, stringify it, and write that
***************
*** 293,298 ****
      def _dispatch(self, msg):
          for part in msg.walk():
!             if part.get_main_type('text') == 'text':
                  print >> self, part.get_payload(decode=1)
              else:
                  print >> self, self._fmt % {
--- 316,325 ----
      def _dispatch(self, msg):
          for part in msg.walk():
!             maintype = part.get_main_type('text')
!             if maintype == 'text':
                  print >> self, part.get_payload(decode=1)
+             elif maintype == 'multipart':
+                 # Just skip this
+                 pass
              else:
                  print >> self, self._fmt % {