Commit 2536399f authored by Raphaël Hertzog's avatar Raphaël Hertzog

core: new unfold_header() helper function

parent 6e551635
......@@ -48,6 +48,7 @@ from distro_tracker.core.utils.email_messages import decode_header
from distro_tracker.core.utils.email_messages import (
name_and_address_from_string,
names_and_addresses_from_string)
from distro_tracker.core.utils.email_messages import unfold_header
from distro_tracker.core.utils.linkify import linkify
from distro_tracker.core.utils.linkify import LinkifyDebianBugLinks
from distro_tracker.core.utils.linkify import LinkifyUbuntuBugLinks
......@@ -248,6 +249,17 @@ class EmailUtilsTest(SimpleTestCase):
self.assertSequenceEqual(names_and_addresses_from_string(''), [])
def test_unfold_header(self):
test_values = {
'a\n b': 'a b',
'a\r\n b': 'a b',
'a\n\tb': 'a\tb',
'a\r\n\tb\n c\n\td': 'a\tb c\td',
'a\n\t bc\n d': 'a\t bc d',
}
for folded, unfolded in test_values.items():
self.assertEqual(unfold_header(folded), unfolded)
class CustomEmailMessageTest(TestCase):
"""
......
......@@ -237,3 +237,21 @@ def decode_header(header, default_encoding='utf-8'):
else:
result += part
return result
def unfold_header(header):
"""
Unfolding is the process to remove the line wrapping added by mail agents.
An header is a single logical line and they are not allowed to be multi-line
values.
We need to unfold their values in particular when we want to reuse the
values to compose a reply message as Python's email API chokes on those
newline characters.
:param:header: the header value to unfold
:type param: str
:returns: the unfolded version of the header.
:rtype: str
"""
return re.sub(r'\r?\n(\s)', r'\1', header, 0, re.MULTILINE)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment