[Python-checkins] python/dist/src/Lib/email Message.py,1.16,1.17

bwarsaw@users.sourceforge.net bwarsaw@users.sourceforge.net
Fri, 19 Jul 2002 15:24:57 -0700


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

Modified Files:
	Message.py 
Log Message:
To better support default content types, fix an API wart, and preserve
backwards compatibility, we're silently deprecating get_type(),
get_subtype() and get_main_type().  We may eventually noisily
deprecate these.  For now, we'll just fix a bug in the splitting of
the main and subtypes.

get_content_type(), get_content_maintype(), get_content_subtype(): New
methods which replace the above.  These /always/ return a content type
string and do not take a failobj, because an email message always at
least has a default content type.

set_default_type(): Someday there may be additional default content
types, so don't hard code an assertion about the value of the ctype
argument.


Index: Message.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/email/Message.py,v
retrieving revision 1.16
retrieving revision 1.17
diff -C2 -d -r1.16 -r1.17
*** Message.py	18 Jul 2002 23:09:09 -0000	1.16
--- Message.py	19 Jul 2002 22:24:55 -0000	1.17
***************
*** 363,366 ****
--- 363,371 ----
          self._headers.append((_name, SEMISPACE.join(parts)))
  
+     #
+     # These methods are silently deprecated in favor of get_content_type() and
+     # friends (see below).  They will be noisily deprecated in email 3.0.
+     #
+ 
      def get_type(self, failobj=None):
          """Returns the message's content type.
***************
*** 382,389 ****
          if ctype is missing:
              return failobj
!         parts = ctype.split('/')
!         if len(parts) > 0:
!             return ctype.split('/')[0]
!         return failobj
  
      def get_subtype(self, failobj=None):
--- 387,393 ----
          if ctype is missing:
              return failobj
!         if ctype.count('/') <> 1:
!             return failobj
!         return ctype.split('/')[0]
  
      def get_subtype(self, failobj=None):
***************
*** 393,400 ****
          if ctype is missing:
              return failobj
!         parts = ctype.split('/')
!         if len(parts) > 1:
!             return ctype.split('/')[1]
!         return failobj
  
      def get_default_type(self):
--- 397,451 ----
          if ctype is missing:
              return failobj
!         if ctype.count('/') <> 1:
!             return failobj
!         return ctype.split('/')[1]
! 
!     #
!     # Use these three methods instead of the three above.
!     #
! 
!     def get_content_type(self):
!         """Returns the message's content type.
! 
!         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
!         always return a value.
! 
!         The current state of RFC standards define a message's default type to
!         be text/plain unless it appears inside a multipart/digest container,
!         in which case it would be message/rfc822.
!         """
!         missing = []
!         value = self.get('content-type', missing)
!         if value is missing:
!             # This should have no parameters
!             return self.get_default_type()
!         return paramre.split(value)[0].lower().strip()
! 
!     def get_content_maintype(self):
!         """Returns the message's main content type.
! 
!         This is the `maintype' part of the string returned by
!         get_content_type().  If no slash is found in the full content type, a
!         ValueError is raised.
!         """
!         ctype = self.get_content_type()
!         if ctype.count('/') <> 1:
!             raise ValueError, 'No maintype found in: %s' % ctype
!         return ctype.split('/')[0]
! 
!     def get_content_subtype(self):
!         """Returns the message's sub content type.
! 
!         This is the `subtype' part of the string returned by
!         get_content_type().  If no slash is found in the full content type, a
!         ValueError is raised.
!         """
!         ctype = self.get_content_type()
!         if ctype.count('/') <> 1:
!             raise ValueError, 'No subtype found in: %s' % ctype
!         return ctype.split('/')[1]
  
      def get_default_type(self):
***************
*** 410,419 ****
          """Set the `default' content type.
  
!         ctype must be either "text/plain" or "message/rfc822".  The default
!         content type is not stored in the Content-Type: header.
          """
-         if ctype not in ('text/plain', 'message/rfc822'):
-             raise ValueError(
-                 'first arg must be either "text/plain" or "message/rfc822"')
          self._default_type = ctype
  
--- 461,468 ----
          """Set the `default' content type.
  
!         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