Skip to content

Commit b744f3a

Browse files
committed
#21083: add get_content_disposition method to email.message.
Patch by Abhilash Raj.
1 parent b9cec6a commit b744f3a

5 files changed

Lines changed: 41 additions & 0 deletions

File tree

Doc/library/email.message.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,15 @@ Here are the methods of the :class:`Message` class:
578578
will be *failobj*.
579579

580580

581+
.. method:: get_content_disposition()
582+
583+
Return the lowercased value (without parameters) of the message's
584+
:mailheader:`Content-Disposition` header if it has one, or ``None``. The
585+
possible values for this method are *inline*, *attachment* or ``None``
586+
if the message follows :rfc:`2183`.
587+
588+
.. versionadded:: 3.5
589+
581590
.. method:: walk()
582591

583592
The :meth:`walk` method is an all-purpose generator which can be used to

Doc/whatsnew/3.5.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,14 @@ doctest
348348
*module* contains no docstrings instead of raising :exc:`ValueError`.
349349
(Contributed by Glenn Jones in :issue:`15916`.)
350350

351+
email
352+
-----
353+
354+
* A new method :meth:`~email.message.Message.get_content_disposition` provides
355+
easy access to a canonical value for the :mailheader:`Content-Disposition`
356+
header (``None`` if there is no such header). (Contributed by Abhilash Raj
357+
in :issue:`21083`.)
358+
351359
glob
352360
----
353361

Lib/email/message.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,18 @@ def get_charsets(self, failobj=None):
927927
"""
928928
return [part.get_content_charset(failobj) for part in self.walk()]
929929

930+
def get_content_disposition(self):
931+
"""Return the message's content-disposition if it exists, or None.
932+
933+
The return values can be either 'inline', 'attachment' or None
934+
according to the rfc2183.
935+
"""
936+
value = self.get('content-disposition')
937+
if value is None:
938+
return None
939+
c_d = _splitparam(value)[0].lower()
940+
return c_d
941+
930942
# I.e. def walk(self): ...
931943
from email.iterators import walk
932944

Lib/test/test_email/test_email.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,17 @@ def test_replace_header(self):
586586
eq(msg.values(), ['One Hundred', 'Twenty', 'Three', 'Eleven'])
587587
self.assertRaises(KeyError, msg.replace_header, 'Fourth', 'Missing')
588588

589+
def test_get_content_disposition(self):
590+
msg = Message()
591+
self.assertIsNone(msg.get_content_disposition())
592+
msg.add_header('Content-Disposition', 'attachment',
593+
filename='random.avi')
594+
self.assertEqual(msg.get_content_disposition(), 'attachment')
595+
msg.replace_header('Content-Disposition', 'inline')
596+
self.assertEqual(msg.get_content_disposition(), 'inline')
597+
msg.replace_header('Content-Disposition', 'InlinE')
598+
self.assertEqual(msg.get_content_disposition(), 'inline')
599+
589600
# test_defect_handling:test_invalid_chars_in_base64_payload
590601
def test_broken_base64_payload(self):
591602
x = 'AwDp0P7//y6LwKEAcPa/6Q=9'

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,7 @@ Thomas Rachel
11341134
Ram Rachum
11351135
Jérôme Radix
11361136
Burton Radons
1137+
Abhilash Raj
11371138
Shorya Raj
11381139
Jeff Ramnani
11391140
Brodie Rao

0 commit comments

Comments
 (0)