summaryrefslogtreecommitdiff
path: root/alot/helper.py
diff options
context:
space:
mode:
authorDylan Baker <dylan@pnwbakers.com>2018-08-01 11:14:52 -0700
committerDylan Baker <dylan@pnwbakers.com>2018-08-02 10:51:09 -0700
commit293bf7ad89c837d6a2ee9e08443c00703172d953 (patch)
tree8927a34a5b8aca0287e385f834457258cadfc8c4 /alot/helper.py
parent834a658dfbe25707eebed34d5f5fdd10e1fddd60 (diff)
helper: replace email_as_* with email builtins
Python 3.3 added a new feature to the email module, policies (https://docs.python.org/3.5/library/email.policy.html). Policy objects allow precise control over how numerous features work when converting to and from str or bytes. With the `email.policy.SMTP` the behavior of email_as_bytes and email_as_string can be achieved using the builtin `.as_string()` and `.as_bytes()` methods, without custom code or the need to test it. Additionally these methods handle corner cases that we don't currently handle, such as multi-part messages with different encodings. Fixes #1257
Diffstat (limited to 'alot/helper.py')
-rw-r--r--alot/helper.py43
1 files changed, 0 insertions, 43 deletions
diff --git a/alot/helper.py b/alot/helper.py
index b7b7d25d..2b076964 100644
--- a/alot/helper.py
+++ b/alot/helper.py
@@ -595,49 +595,6 @@ def RFC3156_canonicalize(text):
return text
-def email_as_string(mail):
- """
- Converts the given message to a string, without mangling "From" lines
- (like as_string() does).
-
- :param mail: email to convert to string
- :rtype: str
- """
- fp = StringIO()
- g = Generator(fp, mangle_from_=False, maxheaderlen=78)
- g.flatten(mail)
- as_string = RFC3156_canonicalize(fp.getvalue())
- return as_string
-
-
-def email_as_bytes(mail):
- string = email_as_string(mail)
- charset = mail.get_charset()
- if charset:
- charset = str(charset)
- else:
- charsets = set(mail.get_charsets())
- if None in charsets:
- # None is equal to US-ASCII
- charsets.discard(None)
- charsets.add('ascii')
-
- if len(charsets) == 1:
- charset = list(charsets)[0]
- elif 'ascii' in charsets:
- # If we get here and the assert triggers it means that different
- # parts of the email are encoded differently. I don't think we're
- # likely to see that, but it's possible
- if not {'utf-8', 'ascii', 'us-ascii'}.issuperset(charsets):
- raise RuntimeError(
- "different encodings detected: {}".format(charsets))
- charset = 'utf-8' # It's a strict super-set
- else:
- charset = 'utf-8'
-
- return string.encode(charset)
-
-
def get_xdg_env(env_name, fallback):
""" Used for XDG_* env variables to return fallback if unset *or* empty """
env = os.environ.get(env_name)