summaryrefslogtreecommitdiff
path: root/src/leap/mail/utils.py
diff options
context:
space:
mode:
authorIvan Alejandro <ivanalejandro0@gmail.com>2014-01-22 11:01:05 -0300
committerIvan Alejandro <ivanalejandro0@gmail.com>2014-01-22 11:01:05 -0300
commitc2e052a08789057d550a0442caa28b27ebc4b416 (patch)
tree31355e0b85c058b759fb3e261cd095ed9632d79c /src/leap/mail/utils.py
parent92c161b7dc970530252662af4636c0ec3cdb8595 (diff)
Add find_charset helper and use where is needed.
Diffstat (limited to 'src/leap/mail/utils.py')
-rw-r--r--src/leap/mail/utils.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/leap/mail/utils.py b/src/leap/mail/utils.py
index 93388d3..6c79227 100644
--- a/src/leap/mail/utils.py
+++ b/src/leap/mail/utils.py
@@ -18,9 +18,14 @@
Mail utilities.
"""
import json
+import re
import traceback
+CHARSET_PATTERN = r"""charset=([\w-]+)"""
+CHARSET_RE = re.compile(CHARSET_PATTERN, re.IGNORECASE)
+
+
def first(things):
"""
Return the head of a collection.
@@ -31,6 +36,26 @@ def first(things):
return None
+def find_charset(thing, default=None):
+ """
+ Looks into the object 'thing' for a charset specification.
+ It searchs into the object's `repr`.
+
+ :param thing: the object to look into.
+ :type thing: object
+ :param default: the dafault charset to return if no charset is found.
+ :type default: str
+
+ :returns: the charset or 'default'
+ :rtype: str or None
+ """
+ charset = first(CHARSET_RE.findall(repr(thing)))
+ if charset is None:
+ charset = default
+
+ return charset
+
+
class CustomJsonScanner(object):
"""
This class is a context manager definition used to monkey patch the default