def extract_body(mail, types=None): """ returns a body text string for given mail. If types is `None`, 'text/*' is used: In case mail has a 'text/html' part, it is prefered over 'text/plain' parts. :param mail: the mail to use :type mail: :class:`email.Message` :param types: mime content types to use for body string :type types: list of str """ html = list(typed_subpart_iterator(mail, 'text', 'html')) # if no specific types are given, we favor text/html over text/plain drop_plaintext = False if html and not types: drop_plaintext = True body_parts = [] for part in mail.walk(): ctype = part.get_content_type() if types is not None: if ctype not in types: continue enc = part.get_content_charset() or 'ascii' raw_payload = part.get_payload(decode=True) if part.get_content_maintype() == 'text': raw_payload = string_decode(raw_payload, enc) if ctype == 'text/plain' and not drop_plaintext: body_parts.append(string_sanitize(raw_payload)) else: #get mime handler handler = get_mime_handler(ctype, key='view', interactive=False) if handler: #open tempfile. Not all handlers accept stuff from stdin tmpfile = tempfile.NamedTemporaryFile(delete=False, suffix='.html') #write payload to tmpfile if part.get_content_maintype() == 'text': tmpfile.write(raw_payload.encode('utf8')) else: tmpfile.write(raw_payload) tmpfile.close() #create and call external command cmd = handler % tmpfile.name cmdlist = shlex.split(cmd.encode('utf-8', errors='ignore')) rendered_payload, errmsg, retval = helper.call_cmd(cmdlist) #remove tempfile os.unlink(tmpfile.name) if rendered_payload: # handler had output body_parts.append(string_sanitize(rendered_payload)) elif part.get_content_maintype() == 'text': body_parts.append(string_sanitize(raw_payload)) # else drop return '\n\n'.join(body_parts)
def __init__(self, completer, on_exit, edit_text=u'', history=None, **kwargs): self.completer = completer self.on_exit = on_exit self.history = list(history) # we temporarily add stuff here self.historypos = None if not isinstance(edit_text, unicode): edit_text = string_decode(edit_text) self.start_completion_pos = len(edit_text) self.completions = None urwid.Edit.__init__(self, edit_text=edit_text, **kwargs)
def decode_header(header, normalize=False): """ decode a header value to a unicode string values are usually a mixture of different substrings encoded in quoted printable using diffetrent encodings. This turns it into a single unicode string :param header: the header value :type header: str in us-ascii :param normalize: replace trailing spaces after newlines :type normalize: bool :rtype: unicode """ valuelist = email.header.decode_header(header) decoded_list = [] for v, enc in valuelist: v = string_decode(v, enc) decoded_list.append(string_sanitize(v)) value = u' '.join(decoded_list) if normalize: value = re.sub(r'\n\s+', r' ', value) return value
def __str__(self): desc = '%s:%s (%s)' % (self.get_content_type(), self.get_filename(), helper.humanize_size(self.get_size())) return string_decode(desc)