def _mail2pdf(files=None, splitted=None, return_errors=True): for stream, mimetype, name in request_files(files): for row in to_pdf(mail.dump_email(mail.msg_from_file(stream), filter_fun=html.filter_and_pdf, skip_unknown=not return_errors), return_errors=return_errors, splitted=splitted): yield row
dst = os.path.join(dn, os.pardir, sha(text) + '.pdf') LOG.info('pdf dst: %r', dst) try: pdf_fn = html2pdf(fn, dst) assert pdf_fn and os.path.exists(pdf_fn), pdf_fn except: LOG.exception('error converting %s to %s', fn, dst) yield (False, MMFile(fn, delete_on_close=True)) else: yield (True, pdf_fn) finally: shutil.rmtree(dn) continue # yield (False, part) def sha(text): hsh = hashlib.new('sha1') hsh.update(text) return hsh.hexdigest() if '__main__' == __name__: import sys import mail logging.basicConfig(level=logging.DEBUG) msg = mail.msg_from_file(sys.argv[1]) for fn in filter_and_pdf(msg): print >> sys.stderr, fn