]> granicus.if.org Git - python/commitdiff
#21083: add get_content_disposition method to email.message.
authorR David Murray <rdmurray@bitdance.com>
Sat, 16 May 2015 19:41:07 +0000 (15:41 -0400)
committerR David Murray <rdmurray@bitdance.com>
Sat, 16 May 2015 19:41:07 +0000 (15:41 -0400)
Patch by Abhilash Raj.

Doc/library/email.message.rst
Doc/whatsnew/3.5.rst
Lib/email/message.py
Lib/test/test_email/test_email.py
Misc/ACKS

index aeea94221d5cda92cbd53e698783e67b68af2dd9..b91f26d12045eb4a25a79f8139290052695de5cc 100644 (file)
@@ -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
index 4b36315805969c59181ec4e2a666e0dfe2e82b01..0360de494ee1c732a94ea98faf9e3c6e9dc0c657 100644 (file)
@@ -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
 ----
 
index 3d3138fe27e6776bd8881e6a4ddc3ed945b7ace5..a892012c3fcc015e665d249161c3e79bc4338ec3 100644 (file)
@@ -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
 
index 218ce0ce8134a55d7d57e56a7b54230e68c47aea..b627760b09ade75b3927898e91be7d6a9978d200 100644 (file)
@@ -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'
index 87a6c4aa12bb2930e8cc0c8c070be7030e0ce699..71fb0c54b4c191739649ab778f8b4e70c5ccf5dd 100644 (file)
--- 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