5.6. Utils

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 interctive 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, error msg, 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.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.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.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.safely_get(clb, E, on_error='')

returns result of clb() and falls back to on_error in case exception E is raised.

Parameters:
  • clb (callable) – function to evaluate
  • E (Exception) – exception to catch
  • on_error (str) – default string returned when exception is caught
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.

Previous topic

5.5. User Settings

Next topic

5.7. Commands

This Page