# -*- coding: utf-8 -*- # Copyright (C) 2011-2012 Patrick Totzke # Copyright © 2017-2018 Dylan Baker # This file is released under the GNU GPL, version 3 or a later revision. # For further details see the COPYING file from collections import deque import logging import os import shlex import email def split_commandline(s): """ splits semi-colon separated commandlines """ # shlex seems to remove unescaped quotes and backslashes s = s.replace('\\', '\\\\') s = s.replace('\'', '\\\'') s = s.replace('\"', '\\\"') lex = shlex.shlex(s, posix = True) lex.whitespace_split = True lex.whitespace = ';' lex.commenters = '' return list(lex) def 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. """ assert isinstance(cmdstring, str) return shlex.split(cmdstring) def shorten(string, maxlen): """shortens string if longer than maxlen, appending ellipsis""" if 1 < maxlen < len(string): string = string[:maxlen - 1] + '…' return string[:maxlen] def 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. """ # I will create a list of authors by parsing author_string. I use # deque to do popleft without performance penalties authors = deque() # If author list is too long, it uses only the first part of each # name (gmail style) short_names = len(authors_string) > maxlength for au in authors_string.split(", "): if short_names: author_as_list = au.split() if len(author_as_list) > 0: authors.append(author_as_list[0]) else: authors.append(au) # Author chain will contain the list of author strings to be # concatenated using commas for the final formatted author_string. authors_chain = deque() if len(authors) == 0: return '' # reserve space for first author first_au = shorten(authors.popleft(), maxlength) remaining_length = maxlength - len(first_au) # Tries to add an ellipsis if no space to show more than 1 author if authors and maxlength > 3 and remaining_length < 3: first_au = shorten(first_au, maxlength - 3) remaining_length += 3 # Tries to add as more authors as possible. It takes into account # that if any author will be hidden, and ellipsis should be added while authors and remaining_length >= 3: au = authors.pop() if len(au) > 1 and (remaining_length == 3 or (authors and remaining_length < 7)): authors_chain.appendleft('…') break else: if authors: # 5= ellipsis + 2 x comma and space used as separators au_string = shorten(au, remaining_length - 5) else: # 2 = comma and space used as separator au_string = shorten(au, remaining_length - 2) remaining_length -= len(au_string) + 2 authors_chain.appendleft(au_string) # Add the first author to the list and concatenate list authors_chain.appendleft(first_au) authorsstring = ', '.join(authors_chain) return authorsstring def shell_quote(text): """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. :param text: the text to quote :type text: str :returns: the quoted text :rtype: str """ return "'%s'" % text.replace("'", """'"'"'""") def get_xdg_env(env_name, fallback): """ Used for XDG_* env variables to return fallback if unset *or* empty """ env = os.environ.get(env_name) return env if env else fallback def formataddr(pair): """ this is the inverse of email.utils.parseaddr: other than email.utils.formataddr, this - *will not* re-encode unicode strings, and - *will* re-introduce quotes around real names containing commas """ name, address = pair if not name: return address elif ',' in name: name = "\"" + name + "\"" return "{0} <{1}>".format(name, address)