Skip to content
This repository has been archived by the owner on Jul 22, 2023. It is now read-only.

Commit

Permalink
better error message
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed May 23, 2018
1 parent cf1dfe5 commit bcfafce
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/pymmails/render/email_message_renderer.py
Expand Up @@ -5,6 +5,8 @@
"""
import os
import re
import pprint
from jinja2.exceptions import UndefinedError
from pyquickhelper.loghelper import noLOG
from ..grabber.email_message import EmailMessage
from ..helpers.buffer_files_writing import BufferFilesWriting
Expand All @@ -23,8 +25,8 @@ def __init__(self, tmpl=None, css=None,
buffer_write=None,
fLOG=noLOG):
"""
constructor, defines a template based
on `Jinja2 <http://jinja.pocoo.org/docs/dev/>`_
Defines a template based
on `Jinja2 <http://jinja.pocoo.org/docs/dev/>`_.
@param tmpl template (string or file)
@param css style
Expand Down Expand Up @@ -184,7 +186,7 @@ def __init__(self, tmpl=None, css=None,
def render(self, location, mail, attachments, file_css="mail_style.css",
prev_mail=None, next_mail=None, **addition):
"""
render a mail
Renders a mail.
@paramp location location where this mail should be saved
@param mail instance of @see cl EmailMessage
Expand All @@ -206,17 +208,29 @@ def render(self, location, mail, attachments, file_css="mail_style.css",
"""
file_css = os.path.relpath(file_css, location)
css = self._css.render(message=mail)
html = self._template.render(message=mail, css=file_css, render=self,
location=location, EmailMessage=EmailMessage,
attachments=attachments, prev_mail=prev_mail,
next_mail=next_mail, **addition)
try:
html = self._template.render(message=mail, css=file_css, render=self,
location=location, EmailMessage=EmailMessage,
attachments=attachments, prev_mail=prev_mail,
next_mail=next_mail, **addition)
except UndefinedError as e:
empty = []
if 'groups' in addition:
for gr in addition['groups']:
if 'emails' in gr and len(gr['emails']) == 0:
empty.append(gr)
tmpl = self._raw_template
disp1 = pprint.pformat(empty)
mes = "This usually happens when the project was sent with a mail not retained in a the final list."
raise RuntimeError("Unable to apply pattern\n----\n{0}\n----\n{1}\n----\n{2}".format(
mes, tmpl, disp1)) from e
return html, css

def write(self, location, mail, filename, attachments=None,
overwrite=False, file_css="mail_style.css", encoding="utf8",
prev_mail=None, next_mail=None, **addition):
"""
writes a mail, the function assumes the attachments were already dumped
Writes a mail, the function assumes the attachments were already dumped.
@param location location
@param mail instance of @see cl EmailMessage
Expand Down
1 change: 1 addition & 0 deletions src/pymmails/render/renderer.py
Expand Up @@ -32,6 +32,7 @@ def __init__(self, tmpl, css,
self._style_highlight = style_highlight
self._session = None
self._buffer_write = buffer_write
self._raw_template = tmpl
self.fLOG = fLOG

def flush(self):
Expand Down

0 comments on commit bcfafce

Please sign in to comment.