changeset: 96103:29ba76f5c3dc user: R David Murray date: Sat May 16 15:41:07 2015 -0400 files: Doc/library/email.message.rst Doc/whatsnew/3.5.rst Lib/email/message.py Lib/test/test_email/test_email.py Misc/ACKS description: #21083: add get_content_disposition method to email.message. Patch by Abhilash Raj. diff -r 09bd552999bf -r 29ba76f5c3dc Doc/library/email.message.rst --- a/Doc/library/email.message.rst Sat May 16 22:13:27 2015 +0300 +++ b/Doc/library/email.message.rst Sat May 16 15:41:07 2015 -0400 @@ -578,6 +578,15 @@ will be *failobj*. + .. method:: get_content_disposition() + + Return the lowercased value (without parameters) of the message's + :mailheader:`Content-Disposition` header if it has one, or ``None``. The + possible values for this method are *inline*, *attachment* or ``None`` + if the message follows :rfc:`2183`. + + .. versionadded:: 3.5 + .. method:: walk() The :meth:`walk` method is an all-purpose generator which can be used to diff -r 09bd552999bf -r 29ba76f5c3dc Doc/whatsnew/3.5.rst --- a/Doc/whatsnew/3.5.rst Sat May 16 22:13:27 2015 +0300 +++ b/Doc/whatsnew/3.5.rst Sat May 16 15:41:07 2015 -0400 @@ -348,6 +348,14 @@ *module* contains no docstrings instead of raising :exc:`ValueError`. (Contributed by Glenn Jones in :issue:`15916`.) +email +----- + +* A new method :meth:`~email.message.Message.get_content_disposition` provides + easy access to a canonical value for the :mailheader:`Content-Disposition` + header (``None`` if there is no such header). (Contributed by Abhilash Raj + in :issue:`21083`.) + glob ---- diff -r 09bd552999bf -r 29ba76f5c3dc Lib/email/message.py --- a/Lib/email/message.py Sat May 16 22:13:27 2015 +0300 +++ b/Lib/email/message.py Sat May 16 15:41:07 2015 -0400 @@ -927,6 +927,18 @@ """ return [part.get_content_charset(failobj) for part in self.walk()] + def get_content_disposition(self): + """Return the message's content-disposition if it exists, or None. + + The return values can be either 'inline', 'attachment' or None + according to the rfc2183. + """ + value = self.get('content-disposition') + if value is None: + return None + c_d = _splitparam(value)[0].lower() + return c_d + # I.e. def walk(self): ... from email.iterators import walk diff -r 09bd552999bf -r 29ba76f5c3dc Lib/test/test_email/test_email.py --- a/Lib/test/test_email/test_email.py Sat May 16 22:13:27 2015 +0300 +++ b/Lib/test/test_email/test_email.py Sat May 16 15:41:07 2015 -0400 @@ -586,6 +586,17 @@ eq(msg.values(), ['One Hundred', 'Twenty', 'Three', 'Eleven']) self.assertRaises(KeyError, msg.replace_header, 'Fourth', 'Missing') + def test_get_content_disposition(self): + msg = Message() + self.assertIsNone(msg.get_content_disposition()) + msg.add_header('Content-Disposition', 'attachment', + filename='random.avi') + self.assertEqual(msg.get_content_disposition(), 'attachment') + msg.replace_header('Content-Disposition', 'inline') + self.assertEqual(msg.get_content_disposition(), 'inline') + msg.replace_header('Content-Disposition', 'InlinE') + self.assertEqual(msg.get_content_disposition(), 'inline') + # test_defect_handling:test_invalid_chars_in_base64_payload def test_broken_base64_payload(self): x = 'AwDp0P7//y6LwKEAcPa/6Q=9' diff -r 09bd552999bf -r 29ba76f5c3dc Misc/ACKS --- a/Misc/ACKS Sat May 16 22:13:27 2015 +0300 +++ b/Misc/ACKS Sat May 16 15:41:07 2015 -0400 @@ -1134,6 +1134,7 @@ Ram Rachum Jérôme Radix Burton Radons +Abhilash Raj Shorya Raj Jeff Ramnani Brodie Rao