summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDylan Baker <dylan@pnwbakers.com>2018-08-03 10:21:11 -0700
committerPatrick Totzke <patricktotzke@gmail.com>2018-08-04 16:21:31 +0100
commit1240eba9cd31e81ea364cdab038685fabb7eab2e (patch)
tree1fdef1fb9de1f8d30e9530506a8cf5df1cd112a7
parent273f4143626020321b6cf15f94ff0a5ec71d642d (diff)
db/utils: Replace encode_header with Message.add_header
Which appears to be capable of doing all the same things, but is in the stdlib instead of something we hand rolled.
-rw-r--r--alot/commands/thread.py4
-rw-r--r--alot/db/envelope.py3
-rw-r--r--alot/db/utils.py26
-rw-r--r--tests/db/utils_test.py66
4 files changed, 2 insertions, 97 deletions
diff --git a/alot/commands/thread.py b/alot/commands/thread.py
index 6513872c..02a801d9 100644
--- a/alot/commands/thread.py
+++ b/alot/commands/thread.py
@@ -27,7 +27,6 @@ from .common import RetagPromptCommand
from .envelope import SendCommand
from ..completion import ContactsCompleter, PathCompleter
from ..db.utils import decode_header
-from ..db.utils import encode_header
from ..db.utils import extract_headers
from ..db.utils import extract_body
from ..db.envelope import Envelope
@@ -746,8 +745,7 @@ class PipeCommand(Command):
for msg in to_print:
mail = msg.get_email()
if self.add_tags:
- mail['Tags'] = encode_header('Tags',
- ', '.join(msg.get_tags()))
+ mail.add_header('Tags', ', '.join(msg.get_tags()))
if self.output_format == 'raw':
pipestrings.append(mail.as_string())
elif self.output_format == 'decoded':
diff --git a/alot/db/envelope.py b/alot/db/envelope.py
index 4c2e5cd9..44da6f71 100644
--- a/alot/db/envelope.py
+++ b/alot/db/envelope.py
@@ -15,7 +15,6 @@ import email.charset as charset
import gpg
from .attachment import Attachment
-from .utils import encode_header
from .. import __version__
from .. import helper
from .. import crypto
@@ -282,7 +281,7 @@ class Envelope(object):
# copy headers from envelope to mail
for k, vlist in headers.items():
for v in vlist:
- outer_msg[k] = encode_header(k, v)
+ outer_msg.add_header(k, v)
return outer_msg
diff --git a/alot/db/utils.py b/alot/db/utils.py
index 33330f72..399b7df1 100644
--- a/alot/db/utils.py
+++ b/alot/db/utils.py
@@ -508,32 +508,6 @@ def decode_header(header, normalize=False):
return value
-def encode_header(key, value):
- """
- encodes a unicode string as a valid header value
-
- :param key: the header field this value will be stored in
- :type key: str
- :param value: the value to be encoded
- :type value: unicode
- """
- # handle list of "realname <email>" entries separately
- if key.lower() in ['from', 'to', 'cc', 'bcc']:
- rawentries = email.utils.getaddresses([value])
- encodedentries = []
- for name, address in rawentries:
- # try to encode as ascii, if that fails, revert to utf-8
- # name must be a unicode string here
- namepart = Header(name)
- # append address part encoded as ascii
- entry = email.utils.formataddr((namepart.encode(), address))
- encodedentries.append(entry)
- value = Header(', '.join(encodedentries))
- else:
- value = Header(value)
- return value.encode()
-
-
def is_subdir_of(subpath, superpath):
# make both absolute
superpath = os.path.realpath(superpath)
diff --git a/tests/db/utils_test.py b/tests/db/utils_test.py
index 7d50d888..6dd23a99 100644
--- a/tests/db/utils_test.py
+++ b/tests/db/utils_test.py
@@ -164,72 +164,6 @@ class TestExtractHeader(unittest.TestCase):
self.assertEqual(actual, expected)
-class TestEncodeHeader(unittest.TestCase):
-
- def test_only_value_is_used_in_output(self):
- actual = utils.encode_header('x-key', 'value')
- expected = email.header.Header('value')
- self.assertEqual(actual, expected)
-
- @unittest.expectedFailure
- def test_unicode_chars_are_encoded(self):
- actual = utils.encode_header('x-key', u'välüe')
- expected = email.header.Header('=?utf-8?b?dsOkbMO8ZQ==?=')
- self.assertEqual(actual, expected)
-
- def test_plain_email_addresses_are_accepted(self):
- address = 'user@example.com'
- actual = utils.encode_header('from', address)
- expected = email.header.Header(address)
- self.assertEqual(actual, expected)
-
- def test_email_addresses_with_realnames_are_accepted(self):
- address = 'someone <user@example.com>'
- actual = utils.encode_header('from', address)
- expected = email.header.Header(address)
- self.assertEqual(actual, expected)
-
- def test_email_addresses_with_empty_realnames_are_treated_like_plain(self):
- address = 'user@example.com'
- empty_realname = '<'+address+'>'
- actual = utils.encode_header('from', empty_realname)
- expected = email.header.Header(address)
- self.assertEqual(str(actual), str(expected))
-
- def test_space_around_email_address_is_striped(self):
- address = ' someone <user@example.com> '
- actual = utils.encode_header('from', address)
- expected = email.header.Header(address.strip())
- self.assertEqual(actual, expected)
-
- def test_spaces_in_user_names_are_accepted(self):
- address = 'some one <user@example.com>'
- actual = utils.encode_header('from', address)
- expected = email.header.Header(address)
- self.assertEqual(actual, expected)
-
- def test_multible_addresses_can_be_given(self):
- addresses = 'one <guy@example.com>, other <guy@example.com>, ' \
- 'last <guy@example.com>'
- actual = utils.encode_header('from', addresses)
- expected = email.header.Header(addresses)
- self.assertEqual(actual, expected)
-
- def test_comma_in_names_are_allowed(self):
- addresses = '"last, first" <guy@example.com>, ' \
- '"name, other" <guy@example.com>'
- actual = utils.encode_header('from', addresses)
- expected = email.header.Header(addresses)
- self.assertEqual(str(actual), str(expected))
-
- def test_utf_8_chars_in_realnames_are_accepted(self):
- address = u'Ümlaut <uemlaut@example.com>'
- actual = utils.encode_header('from', address)
- expected = email.header.Header(
- '=?utf-8?q?=C3=9Cmlaut?= <uemlaut@example.com>')
- self.assertEqual(actual, expected)
-
-
class TestDecodeHeader(unittest.TestCase):
@staticmethod