[Python-checkins] python/dist/src/Lib/email Message.py,1.24,1.25

barry@users.sourceforge.net barry@users.sourceforge.net
Sat, 28 Sep 2002 13:40:27 -0700


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

Modified Files:
	Message.py 
Log Message:
Docstring and code cleanups, e.g. use True/False everywhere.


Index: Message.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/email/Message.py,v
retrieving revision 1.24
retrieving revision 1.25
diff -C2 -d -r1.24 -r1.25
*** Message.py	26 Sep 2002 17:19:34 -0000	1.24
--- Message.py	28 Sep 2002 20:40:25 -0000	1.25
***************
*** 17,20 ****
--- 17,26 ----
  SEMISPACE = '; '
  
+ try:
+     True, False
+ except NameError:
+     True = 1
+     False = 0
+ 
  # Regular expression used to split header parameters.  BAW: this may be too
  # simple.  It isn't strictly RFC 2045 (section 5.1) compliant, but it catches
***************
*** 29,33 ****
  
  # Helper functions
! def _formatparam(param, value=None, quote=1):
      """Convenience function to format and return a key=value pair.
  
--- 35,39 ----
  
  # Helper functions
! def _formatparam(param, value=None, quote=True):
      """Convenience function to format and return a key=value pair.
  
***************
*** 69,76 ****
      These objects implement part of the `mapping' interface, which assumes
      there is exactly one occurrance of the header per message.  Some headers
!     do in fact appear multiple times (e.g. Received:) and for those headers,
      you must use the explicit API to set or get all the headers.  Not all of
      the mapping methods are implemented.
- 
      """
      def __init__(self):
--- 75,81 ----
      These objects implement part of the `mapping' interface, which assumes
      there is exactly one occurrance of the header per message.  Some headers
!     do in fact appear multiple times (e.g. Received) and for those headers,
      you must use the explicit API to set or get all the headers.  Not all of
      the mapping methods are implemented.
      """
      def __init__(self):
***************
*** 88,96 ****
          This includes the headers, body, and `unixfrom' line.
          """
!         return self.as_string(unixfrom=1)
  
!     def as_string(self, unixfrom=0):
          """Return the entire formatted message as a string.
!         Optional `unixfrom' when true, means include the Unix From_ envelope
          header.
          """
--- 93,101 ----
          This includes the headers, body, and `unixfrom' line.
          """
!         return self.as_string(unixfrom=True)
  
!     def as_string(self, unixfrom=False):
          """Return the entire formatted message as a string.
!         Optional `unixfrom' when True, means include the Unix From_ envelope
          header.
          """
***************
*** 102,109 ****
  
      def is_multipart(self):
!         """Return true if the message consists of multiple parts."""
          if type(self._payload) is ListType:
!             return 1
!         return 0
  
      #
--- 107,114 ----
  
      def is_multipart(self):
!         """Return True if the message consists of multiple parts."""
          if type(self._payload) is ListType:
!             return True
!         return False
  
      #
***************
*** 129,137 ****
          if self._payload is None:
              self._payload = payload
!         elif type(self._payload) is ListType:
              self._payload.append(payload)
          elif self.get_main_type() not in (None, 'multipart'):
              raise Errors.MultipartConversionError(
!                 'Message main Content-Type: must be "multipart" or missing')
          else:
              self._payload = [self._payload, payload]
--- 134,142 ----
          if self._payload is None:
              self._payload = payload
!         elif isinstance(self._payload, ListType):
              self._payload.append(payload)
          elif self.get_main_type() not in (None, 'multipart'):
              raise Errors.MultipartConversionError(
!                 'Message main content type must be "multipart" or missing')
          else:
              self._payload = [self._payload, payload]
***************
*** 150,154 ****
              self._payload.append(payload)
  
!     def get_payload(self, i=None, decode=0):
          """Return a reference to the payload.
  
--- 155,159 ----
              self._payload.append(payload)
  
!     def get_payload(self, i=None, decode=False):
          """Return a reference to the payload.
  
***************
*** 158,171 ****
  
          Optional decode is a flag indicating whether the payload should be
!         decoded or not, according to the Content-Transfer-Encoding: header.
!         When true and the message is not a multipart, the payload will be
          decoded if this header's value is `quoted-printable' or `base64'.  If
          some other encoding is used, or the header is missing, the payload is
          returned as-is (undecoded).  If the message is a multipart and the
!         decode flag is true, then None is returned.
          """
          if i is None:
              payload = self._payload
!         elif type(self._payload) is not ListType:
              raise TypeError, i
          else:
--- 163,176 ----
  
          Optional decode is a flag indicating whether the payload should be
!         decoded or not, according to the Content-Transfer-Encoding header.
!         When True and the message is not a multipart, the payload will be
          decoded if this header's value is `quoted-printable' or `base64'.  If
          some other encoding is used, or the header is missing, the payload is
          returned as-is (undecoded).  If the message is a multipart and the
!         decode flag is True, then None is returned.
          """
          if i is None:
              payload = self._payload
!         elif not isinstance(self._payload, ListType):
              raise TypeError, i
          else:
***************
*** 197,201 ****
          will be converted to a Charset object by calling Charset's
          constructor.  If charset is None, the charset parameter will be
!         removed from the Content-Type: field.  Anything else will generate a
          TypeError.
  
--- 202,206 ----
          will be converted to a Charset object by calling Charset's
          constructor.  If charset is None, the charset parameter will be
!         removed from the Content-Type field.  Anything else will generate a
          TypeError.
  
***************
*** 388,392 ****
  
          The returned string is coerced to lowercase and returned as a single
!         string of the form `maintype/subtype'.  If there was no Content-Type:
          header in the message, failobj is returned (defaults to None).
          """
--- 393,397 ----
  
          The returned string is coerced to lowercase and returned as a single
!         string of the form `maintype/subtype'.  If there was no Content-Type
          header in the message, failobj is returned (defaults to None).
          """
***************
*** 425,429 ****
  
          The returned string is coerced to lowercase and returned as a ingle
!         string of the form `maintype/subtype'.  If there was no Content-Type:
          header in the message, the default type as give by get_default_type()
          will be returned.  Since messages always have a default type this will
--- 430,434 ----
  
          The returned string is coerced to lowercase and returned as a ingle
!         string of the form `maintype/subtype'.  If there was no Content-Type
          header in the message, the default type as give by get_default_type()
          will be returned.  Since messages always have a default type this will
***************
*** 479,483 ****
          ctype should be either "text/plain" or "message/rfc822", although this
          is not enforced.  The default content type is not stored in the
!         Content-Type: header.
          """
          self._default_type = ctype
--- 484,488 ----
          ctype should be either "text/plain" or "message/rfc822", although this
          is not enforced.  The default content type is not stored in the
!         Content-Type header.
          """
          self._default_type = ctype
***************
*** 504,509 ****
          return params
  
!     def get_params(self, failobj=None, header='content-type', unquote=1):
!         """Return the message's Content-Type: parameters, as a list.
  
          The elements of the returned list are 2-tuples of key/value pairs, as
--- 509,514 ----
          return params
  
!     def get_params(self, failobj=None, header='content-type', unquote=True):
!         """Return the message's Content-Type parameters, as a list.
  
          The elements of the returned list are 2-tuples of key/value pairs, as
***************
*** 513,519 ****
          described in the get_param() method.
  
!         Optional failobj is the object to return if there is no Content-Type:
          header.  Optional header is the header to search instead of
!         Content-Type:.
          """
          missing = []
--- 518,524 ----
          described in the get_param() method.
  
!         Optional failobj is the object to return if there is no Content-Type
          header.  Optional header is the header to search instead of
!         Content-Type.  If unquote is True, the value is unquoted.
          """
          missing = []
***************
*** 526,535 ****
              return params
  
!     def get_param(self, param, failobj=None, header='content-type', unquote=1):
!         """Return the parameter value if found in the Content-Type: header.
  
!         Optional failobj is the object to return if there is no Content-Type:
          header, or the Content-Type header has no such parameter.  Optional
!         header is the header to search instead of Content-Type:
  
          Parameter keys are always compared case insensitively.  The return
--- 531,541 ----
              return params
  
!     def get_param(self, param, failobj=None, header='content-type',
!                   unquote=True):
!         """Return the parameter value if found in the Content-Type header.
  
!         Optional failobj is the object to return if there is no Content-Type
          header, or the Content-Type header has no such parameter.  Optional
!         header is the header to search instead of Content-Type.
  
          Parameter keys are always compared case insensitively.  The return
***************
*** 546,550 ****
          In any case, the parameter value (either the returned string, or the
          VALUE item in the 3-tuple) is always unquoted, unless unquote is set
!         to a false value.
          """
          if not self.has_key(header):
--- 552,556 ----
          In any case, the parameter value (either the returned string, or the
          VALUE item in the 3-tuple) is always unquoted, unless unquote is set
!         to False.
          """
          if not self.has_key(header):
***************
*** 558,575 ****
          return failobj
  
!     def set_param(self, param, value, header='Content-Type', requote=1,
                    charset=None, language=''):
!         """Set a parameter in the Content-Type: header.
  
          If the parameter already exists in the header, its value will be
          replaced with the new value.
  
!         If header is Content-Type: and has not yet been defined in this
          message, it will be set to "text/plain" and the new parameter and
          value will be appended, as per RFC 2045.
  
!         An alternate header can specified in the header argument, and
!         all parameters will be quoted as appropriate unless requote is
!         set to a false value.
  
          If charset is specified the parameter will be encoded according to RFC
--- 564,580 ----
          return failobj
  
!     def set_param(self, param, value, header='Content-Type', requote=True,
                    charset=None, language=''):
!         """Set a parameter in the Content-Type header.
  
          If the parameter already exists in the header, its value will be
          replaced with the new value.
  
!         If header is Content-Type and has not yet been defined in this
          message, it will be set to "text/plain" and the new parameter and
          value will be appended, as per RFC 2045.
  
!         An alternate header can specified in the header argument, and all
!         parameters will be quoted as appropriate unless requote is False.
  
          If charset is specified the parameter will be encoded according to RFC
***************
*** 606,615 ****
              self[header] = ctype
  
!     def del_param(self, param, header='content-type', requote=1):
          """Remove the given parameter completely from the Content-Type header.
  
          The header will be re-written in place without param or its value.
!         All values will be quoted as appropriate unless requote is set to a
!         false value.
          """
          if not self.has_key(header):
--- 611,619 ----
              self[header] = ctype
  
!     def del_param(self, param, header='content-type', requote=True):
          """Remove the given parameter completely from the Content-Type header.
  
          The header will be re-written in place without param or its value.
!         All values will be quoted as appropriate unless requote is False.
          """
          if not self.has_key(header):
***************
*** 627,643 ****
              self[header] = new_ctype
  
!     def set_type(self, type, header='Content-Type', requote=1):
!         """Set the main type and subtype for the Content-Type: header.
  
          type must be a string in the form "maintype/subtype", otherwise a
          ValueError is raised.
  
!         This method replaces the Content-Type: header, keeping all the
!         parameters in place.  If requote is false, this leaves the existing
          header's quoting as is.  Otherwise, the parameters will be quoted (the
          default).
  
          An alternate header can be specified in the header argument.  When the
!         Content-Type: header is set, we'll always also add a MIME-Version:
          header.
          """
--- 631,647 ----
              self[header] = new_ctype
  
!     def set_type(self, type, header='Content-Type', requote=True):
!         """Set the main type and subtype for the Content-Type header.
  
          type must be a string in the form "maintype/subtype", otherwise a
          ValueError is raised.
  
!         This method replaces the Content-Type header, keeping all the
!         parameters in place.  If requote is False, this leaves the existing
          header's quoting as is.  Otherwise, the parameters will be quoted (the
          default).
  
          An alternate header can be specified in the header argument.  When the
!         Content-Type header is set, we'll always also add a MIME-Version
          header.
          """
***************
*** 645,649 ****
          if not type.count('/') == 1:
              raise ValueError
!         # Set the Content-Type: you get a MIME-Version:
          if header.lower() == 'content-type':
              del self['mime-version']
--- 649,653 ----
          if not type.count('/') == 1:
              raise ValueError
!         # Set the Content-Type, you get a MIME-Version
          if header.lower() == 'content-type':
              del self['mime-version']
***************
*** 662,666 ****
          """Return the filename associated with the payload if present.
  
!         The filename is extracted from the Content-Disposition: header's
          `filename' parameter, and it is unquoted.
          """
--- 666,670 ----
          """Return the filename associated with the payload if present.
  
!         The filename is extracted from the Content-Disposition header's
          `filename' parameter, and it is unquoted.
          """
***************
*** 680,684 ****
          """Return the boundary associated with the payload if present.
  
!         The boundary is extracted from the Content-Type: header's `boundary'
          parameter, and it is unquoted.
          """
--- 684,688 ----
          """Return the boundary associated with the payload if present.
  
!         The boundary is extracted from the Content-Type header's `boundary'
          parameter, and it is unquoted.
          """
***************
*** 693,725 ****
  
      def set_boundary(self, boundary):
!         """Set the boundary parameter in Content-Type: to 'boundary'.
  
!         This is subtly different than deleting the Content-Type: header and
          adding a new one with a new boundary parameter via add_header().  The
          main difference is that using the set_boundary() method preserves the
!         order of the Content-Type: header in the original message.
  
!         HeaderParseError is raised if the message has no Content-Type: header.
          """
          missing = []
          params = self._get_params_preserve(missing, 'content-type')
          if params is missing:
!             # There was no Content-Type: header, and we don't know what type
              # to set it to, so raise an exception.
!             raise Errors.HeaderParseError, 'No Content-Type: header found'
          newparams = []
!         foundp = 0
          for pk, pv in params:
              if pk.lower() == 'boundary':
                  newparams.append(('boundary', '"%s"' % boundary))
!                 foundp = 1
              else:
                  newparams.append((pk, pv))
          if not foundp:
!             # The original Content-Type: header had no boundary attribute.
              # Tack one one the end.  BAW: should we raise an exception
              # instead???
              newparams.append(('boundary', '"%s"' % boundary))
!         # Replace the existing Content-Type: header with the new value
          newheaders = []
          for h, v in self._headers:
--- 697,729 ----
  
      def set_boundary(self, boundary):
!         """Set the boundary parameter in Content-Type to 'boundary'.
  
!         This is subtly different than deleting the Content-Type header and
          adding a new one with a new boundary parameter via add_header().  The
          main difference is that using the set_boundary() method preserves the
!         order of the Content-Type header in the original message.
  
!         HeaderParseError is raised if the message has no Content-Type header.
          """
          missing = []
          params = self._get_params_preserve(missing, 'content-type')
          if params is missing:
!             # There was no Content-Type header, and we don't know what type
              # to set it to, so raise an exception.
!             raise Errors.HeaderParseError, 'No Content-Type header found'
          newparams = []
!         foundp = False
          for pk, pv in params:
              if pk.lower() == 'boundary':
                  newparams.append(('boundary', '"%s"' % boundary))
!                 foundp = True
              else:
                  newparams.append((pk, pv))
          if not foundp:
!             # The original Content-Type header had no boundary attribute.
              # Tack one one the end.  BAW: should we raise an exception
              # instead???
              newparams.append(('boundary', '"%s"' % boundary))
!         # Replace the existing Content-Type header with the new value
          newheaders = []
          for h, v in self._headers:
***************
*** 761,770 ****
          """Return a list containing the charset(s) used in this message.
  
!         The returned list of items describes the Content-Type: headers'
          charset parameter for this message and all the subparts in its
          payload.
  
          Each item will either be a string (the value of the charset parameter
!         in the Content-Type: header of that part) or the value of the
          'failobj' parameter (defaults to None), if the part does not have a
          main MIME type of "text", or the charset is not defined.
--- 765,774 ----
          """Return a list containing the charset(s) used in this message.
  
!         The returned list of items describes the Content-Type headers'
          charset parameter for this message and all the subparts in its
          payload.
  
          Each item will either be a string (the value of the charset parameter
!         in the Content-Type header of that part) or the value of the
          'failobj' parameter (defaults to None), if the part does not have a
          main MIME type of "text", or the charset is not defined.