4.6. Utils

alot.helper.RFC3156_canonicalize(text)

Canonicalizes plain text (MIME-encoded usually) according to RFC3156.

This function works as follows (in that order):

  1. Convert all line endings to \r\n (DOS line endings).
  2. Ensure the text ends with a newline (\r\n).
  3. Encode all occurences of “From ” at the beginning of a line to “From=20” in order to prevent other mail programs to replace this with “> From” (to avoid MBox conflicts) and thus invalidate the signature.
Parameters:text – text to canonicalize (already encoded as quoted-printable)
Return type:str
alot.helper.call_cmd(cmdlist, stdin=None)

get a shell commands output, error message and return value and immediately return.

Warning

This returns with the first screen content for interactive commands.

Parameters:
  • cmdlist (list of str) – shellcommand to call, already splitted into a list accepted by subprocess.Popen()
  • stdin (str) – string to pipe to the process
Returns:

triple of stdout, stderr, return value of the shell command

Return type:

str, str, int

alot.helper.call_cmd_async(cmdlist, stdin=None, env=None)

get a shell commands output, error message and return value as a deferred.

Parameters:stdin (str) – string to pipe to the process
Returns:deferred that calls back with triple of stdout, stderr and return value of the shell command
Return type:twisted.internet.defer.Deferred
alot.helper.email_as_string(mail)

Converts the given message to a string, without mangling “From” lines (like as_string() does).

Parameters:mail – email to convert to string
Return type:str
alot.helper.guess_encoding(blob)

uses file magic to determine the encoding of the given data blob.

Parameters:blob (data) – file content as read by file.read()
Returns:encoding
Return type:str
alot.helper.guess_mimetype(blob)

uses file magic to determine the mime-type of the given data blob.

Parameters:blob (data) – file content as read by file.read()
Returns:mime-type, falls back to ‘application/octet-stream’
Return type:str
alot.helper.humanize_size(size)
>>> humanize_size(1)
'1'
>>> humanize_size(123)
'123'
>>> humanize_size(1234)
'1K'
>>> humanize_size(1234 * 1024)
'1.2M'
>>> humanize_size(1234 * 1024 * 1024)
'1234.0M'
alot.helper.libmagic_version_at_least(version)

checks if the libmagic library installed is more recent than a given version.

Parameters:version – minimum version expected in the form XYY (i.e. 5.14 -> 514) with XYY >= 513
alot.helper.mailto_to_envelope(mailto_str)

Interpret mailto-string into a alot.db.envelope.Envelope

alot.helper.parse_mailcap_nametemplate(tmplate='%s')

this returns a prefix and suffix to be used in the tempfile module for a given mailcap nametemplate string

alot.helper.parse_mailto(mailto_str)

Interpret mailto-string

Parameters:mailto_str (str) – the string to interpret. Must conform to :rfc:2368.
Returns:the header fields and the body found in the mailto link as a tuple of length two
Return type:tuple(dict(str->list(str)), str)
alot.helper.pretty_datetime(d)

translates datetime d to a “sup-style” human readable string.

>>> now = datetime.now()
>>> now.strftime('%c')
'Sat 31 Mar 2012 14:47:26 '
>>> pretty_datetime(now)
u'just now'
>>> pretty_datetime(now - timedelta(minutes=1))
u'1min ago'
>>> pretty_datetime(now - timedelta(hours=5))
u'5h ago'
>>> pretty_datetime(now - timedelta(hours=12))
u'02:54am'
>>> pretty_datetime(now - timedelta(days=1))
u'yest 02pm'
>>> pretty_datetime(now - timedelta(days=2))
u'Thu 02pm'
>>> pretty_datetime(now - timedelta(days=7))
u'Mar 24'
>>> pretty_datetime(now - timedelta(days=356))
u'Apr 2011'
alot.helper.shell_quote(text)
>>> print(shell_quote("hello"))
'hello'
>>> print(shell_quote("hello'there"))
'hello'"'"'there'
alot.helper.shorten(string, maxlen)

shortens string if longer than maxlen, appending ellipsis

alot.helper.shorten_author_string(authors_string, maxlength)

Parse a list of authors concatenated as a text string (comma separated) and smartly adjust them to maxlength.

1) If the complete list of sender names does not fit in maxlength, it tries to shorten names by using only the first part of each.

2) If the list is still too long, hide authors according to the following priority:

  • First author is always shown (if too long is shorten with ellipsis)
  • If possible, last author is also shown (if too long, uses ellipsis)
  • If there are more than 2 authors in the thread, show the maximum of them. More recent senders have higher priority.
  • If it is finally necessary to hide any author, an ellipsis between first and next authors is added.
>>> authors = u'King Kong, Mucho Muchacho, Jaime Huerta, Flash Gordon'
>>> print shorten_author_string(authors, 60)
King Kong, Mucho Muchacho, Jaime Huerta, Flash Gordon
>>> print shorten_author_string(authors, 40)
King, Mucho, Jaime, Flash
>>> print shorten_author_string(authors, 20)
King, …, Jai…, Flash
>>> print shorten_author_string(authors, 10)
King, …
>>> print shorten_author_string(authors, 2)
K…
>>> print shorten_author_string(authors, 1)
K
alot.helper.split_commandline(s, comments=False, posix=True)

splits semi-colon separated commandlines

alot.helper.split_commandstring(cmdstring)

split command string into a list of strings to pass on to subprocess.Popen and the like. This simply calls shlex.split but works also with unicode bytestrings.

alot.helper.string_decode(string, enc='ascii')

safely decodes string to unicode bytestring, respecting enc as a hint.

alot.helper.string_sanitize(string, tab_width=8)

strips, and replaces non-printable characters

Parameters:tab_width (int or None) – number of spaces to replace tabs with. Read from globals.tabwidth setting if None
>>> string_sanitize(' foo\rbar ', 8)
' foobar '
>>> string_sanitize('foo\tbar', 8)
'foo     bar'
>>> string_sanitize('foo\t\tbar', 8)
'foo             bar'
alot.helper.tag_cmp(a, b)

Sorting tags using this function puts all tags of length 1 at the beginning. This groups all tags mapped to unicode characters.