diff options
author | Anton Khirnov <anton@khirnov.net> | 2021-01-21 16:17:41 +0100 |
---|---|---|
committer | Anton Khirnov <anton@khirnov.net> | 2021-01-21 16:17:41 +0100 |
commit | 56f41be8d02fa3424ba9888765f83a223393003d (patch) | |
tree | c210fb65d380a64a78ae83e6a067350113a0b355 | |
parent | ed8b9267425d319c6449092bc537503d5e1f42a3 (diff) |
db/message: split _render_part()
Rename it to _render_part_external() and make it only handle external
rendering. Move the other code - dealing with text parts without an
external handler - to _MimeTree.render_str(). This takes into account
overridden content types, and also processes text/plain parts if that is
configured.
-rw-r--r-- | alot/db/message.py | 102 |
1 files changed, 51 insertions, 51 deletions
diff --git a/alot/db/message.py b/alot/db/message.py index 0430025c..33b0f880 100644 --- a/alot/db/message.py +++ b/alot/db/message.py @@ -23,62 +23,56 @@ charset.add_charset('utf-8', charset.QP, charset.QP, 'utf-8') _APP_PGP_SIG = 'application/pgp-signature' _APP_PGP_ENC = 'application/pgp-encrypted' -_TEXT_PLAIN = 'text/plain' -def _render_part(part, ctype, field_key='copiousoutput'): +def _render_part_external(raw_payload, ctype, params, field_key='copiousoutput'): """ renders a non-multipart email part into displayable plaintext by piping its payload through an external script. The handler itself is determined by the mailcap entry for this part's ctype. """ - raw_payload = part.get_content() - rendered_payload = None # get mime handler _, entry = settings.mailcap_find_match(ctype, key=field_key) - if entry is not None: - if isinstance(raw_payload, str): - raw_payload = raw_payload.encode('utf-8') - - tempfile_name = None - stdin = None - handler_raw_commandstring = entry['view'] - # in case the mailcap defined command contains no '%s', - # we pipe the files content to the handling command via stdin - if '%s' in handler_raw_commandstring: - # open tempfile, respect mailcaps nametemplate - nametemplate = entry.get('nametemplate', '%s') - prefix, suffix = parse_mailcap_nametemplate(nametemplate) - with tempfile.NamedTemporaryFile( - delete=False, prefix=prefix, suffix=suffix) \ - as tmpfile: - tmpfile.write(raw_payload) - tempfile_name = tmpfile.name - else: - stdin = raw_payload - - # read parameter, create handler command - parms = tuple('='.join(p) for p in part.get_params()) - - # create and call external command - cmd = mailcap.subst(entry['view'], ctype, - filename=tempfile_name, plist=parms) - logging.debug('command: %s', cmd) - logging.debug('parms: %s', str(parms)) - cmdlist = split_commandstring(cmd) - # call handler - stdout, _, _ = helper.call_cmd(cmdlist, stdin=stdin) - if stdout: - rendered_payload = stdout - - # remove tempfile - if tempfile_name: - os.unlink(tempfile_name) - elif part.get_content_maintype() == 'text': - # return text parts without a handler as-is - rendered_payload = raw_payload - if not isinstance(rendered_payload, str): - rendered_payload = rendered_payload.decode('utf-8', errors = 'backslashreplace') + if entry is None: + return None + + if isinstance(raw_payload, str): + raw_payload = raw_payload.encode('utf-8') + + tempfile_name = None + stdin = None + handler_raw_commandstring = entry['view'] + # in case the mailcap defined command contains no '%s', + # we pipe the files content to the handling command via stdin + if '%s' in handler_raw_commandstring: + # open tempfile, respect mailcaps nametemplate + nametemplate = entry.get('nametemplate', '%s') + prefix, suffix = parse_mailcap_nametemplate(nametemplate) + with tempfile.NamedTemporaryFile( + delete=False, prefix=prefix, suffix=suffix) \ + as tmpfile: + tmpfile.write(raw_payload) + tempfile_name = tmpfile.name + else: + stdin = raw_payload + + # read parameter, create handler command + parms = tuple('='.join(p) for p in params) + + # create and call external command + cmd = mailcap.subst(entry['view'], ctype, + filename=tempfile_name, plist=parms) + logging.debug('command: %s', cmd) + logging.debug('parms: %s', str(parms)) + cmdlist = split_commandstring(cmd) + # call handler + stdout, _, _ = helper.call_cmd(cmdlist, stdin=stdin) + if stdout: + rendered_payload = stdout + + # remove tempfile + if tempfile_name: + os.unlink(tempfile_name) return rendered_payload @@ -177,12 +171,18 @@ class _MimeTree: content = self._part.get_content() - # no processing for plaintext - # XXX we may want to process plaintext as well - if self.content_type == _TEXT_PLAIN: + # try procesing content with an external program + rendered = _render_part_external(content, self.content_type, self._part.get_params()) + if rendered: + return rendered + + # return text parts without a handler as-is + if self.content_maintype == 'text': + if not isinstance(content, str): + content = content.decode('utf-8', errors = 'backslashreplace') return content - return _render_part(self._part, self.content_type) + return None @property def raw_data(self): |