diff options
author | Kali Kaneko <kali@leap.se> | 2014-01-23 02:33:32 -0400 |
---|---|---|
committer | Kali Kaneko <kali@leap.se> | 2014-01-28 19:38:44 -0400 |
commit | 06c9b95a4e92a7f43f1e91ffcb718aebfe9c3c7d (patch) | |
tree | 95f83975bec4278e5d36603edfb2f7980d2e9036 | |
parent | 02a9cd4d80b76e1bb1001de12eb7af7ae56155ed (diff) |
move utilities
-rw-r--r-- | mail/src/leap/mail/utils.py | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/mail/src/leap/mail/utils.py b/mail/src/leap/mail/utils.py index 6c79227..64af04f 100644 --- a/mail/src/leap/mail/utils.py +++ b/mail/src/leap/mail/utils.py @@ -36,6 +36,14 @@ def first(things): return None +def maybe_call(thing): + """ + Return the same thing, or the result of its invocation if it is a + callable. + """ + return thing() if callable(thing) else thing + + def find_charset(thing, default=None): """ Looks into the object 'thing' for a charset specification. @@ -46,16 +54,28 @@ def find_charset(thing, default=None): :param default: the dafault charset to return if no charset is found. :type default: str - :returns: the charset or 'default' + :return: the charset or 'default' :rtype: str or None """ charset = first(CHARSET_RE.findall(repr(thing))) if charset is None: charset = default - return charset +def lowerdict(_dict): + """ + Return a dict with the keys in lowercase. + + :param _dict: the dict to convert + :rtype: dict + """ + # TODO should properly implement a CaseInsensitive dict. + # Look into requests code. + return dict((key.lower(), value) + for key, value in _dict.items()) + + class CustomJsonScanner(object): """ This class is a context manager definition used to monkey patch the default |