summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2015-05-16 19:41:07 (GMT)
committerR David Murray <rdmurray@bitdance.com>2015-05-16 19:41:07 (GMT)
commitb744f3a45e3228ea2b344c8cf9424e2592dc93b7 (patch)
treed642d35aa8a2fb02ce9ef8ba8354126d366ff6f0
parentb9cec6a30f77c4c0ab208053ffafc02fddd01962 (diff)
downloadcpython-b744f3a45e3228ea2b344c8cf9424e2592dc93b7.zip
cpython-b744f3a45e3228ea2b344c8cf9424e2592dc93b7.tar.gz
cpython-b744f3a45e3228ea2b344c8cf9424e2592dc93b7.tar.bz2
#21083: add get_content_disposition method to email.message.
Patch by Abhilash Raj.
-rw-r--r--Doc/library/email.message.rst9
-rw-r--r--Doc/whatsnew/3.5.rst8
-rw-r--r--Lib/email/message.py12
-rw-r--r--Lib/test/test_email/test_email.py11
-rw-r--r--Misc/ACKS1
5 files changed, 41 insertions, 0 deletions
diff --git a/Doc/library/email.message.rst b/Doc/library/email.message.rst
index aeea942..b91f26d 100644
--- a/Doc/library/email.message.rst
+++ b/Doc/library/email.message.rst
@@ -578,6 +578,15 @@ Here are the methods of the :class:`Message` class:
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 --git a/Doc/whatsnew/3.5.rst b/Doc/whatsnew/3.5.rst
index 4b36315..0360de4 100644
--- a/Doc/whatsnew/3.5.rst
+++ b/Doc/whatsnew/3.5.rst
@@ -348,6 +348,14 @@ doctest
*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 --git a/Lib/email/message.py b/Lib/email/message.py
index 3d3138f..a892012 100644
--- a/Lib/email/message.py
+++ b/Lib/email/message.py
@@ -927,6 +927,18 @@ class Message:
"""
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 --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py
index 218ce0c..b627760 100644
--- a/Lib/test/test_email/test_email.py
+++ b/Lib/test/test_email/test_email.py
@@ -586,6 +586,17 @@ class TestMessageAPI(TestEmailBase):
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 --git a/Misc/ACKS b/Misc/ACKS
index 87a6c4a..71fb0c5 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1134,6 +1134,7 @@ Thomas Rachel
Ram Rachum
Jérôme Radix
Burton Radons
+Abhilash Raj
Shorya Raj
Jeff Ramnani
Brodie Rao