4.6. Utils


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.


This returns with the first screen content for interactive commands.

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

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

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

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

Parameters:blob (data) – file content as read by file.read()
Return type:str

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

Create a nice human readable representation of the given number (understood as bytes) using the “KiB” and “MiB” suffixes to indicate kibibytes and mebibytes. A kibibyte is defined as 1024 bytes (as opposed to a kilobyte which is 1000 bytes) and a mibibyte is 1024**2 bytes (as opposed to a megabyte which is 1000**2 bytes).

Parameters:size (int) – the number to convert
Returns:the human readable representation of size
Return type:str

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

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

alot.helper.mimewrap(path, filename=None, ctype=None)

Take the contents of the given path and wrap them into an email MIME part according to the content type. The content type is auto detected from the actual file contents and the file name if it is not given.

  • path (str) – the path to the file contents
  • filename (str or None) – the file name to use in the generated MIME part
  • ctype (str or None) – the content type of the file contents in path

the message MIME part storing the data from path

Return type:

subclasses of email.mime.base.MIMEBase


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


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)

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))
>>> 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'

Escape the given text for passing it to the shell for interpretation. The resulting string will be parsed into one “word” (in the sense used in the shell documentation, see sh(1)) by the shell.

Parameters:text (str) – the text to quote
Returns:the quoted text
Return type:str
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.
alot.helper.split_commandline(s, comments=False, posix=True)

splits semi-colon separated commandlines


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'