本文整理汇总了Python中mako.exceptions.text_error_template方法的典型用法代码示例。如果您正苦于以下问题:Python exceptions.text_error_template方法的具体用法?Python exceptions.text_error_template怎么用?Python exceptions.text_error_template使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mako.exceptions
的用法示例。
在下文中一共展示了exceptions.text_error_template方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: template_to_file
# 需要导入模块: from mako import exceptions [as 别名]
# 或者: from mako.exceptions import text_error_template [as 别名]
def template_to_file(template_file, dest, output_encoding, **kw):
template = Template(filename=template_file)
try:
output = template.render_unicode(**kw).encode(output_encoding)
except:
with tempfile.NamedTemporaryFile(suffix='.txt', delete=False) as ntf:
ntf.write(
exceptions.text_error_template().
render_unicode().encode(output_encoding))
fname = ntf.name
raise CommandError(
"Template rendering failed; see %s for a "
"template-oriented traceback." % fname)
else:
with open(dest, 'wb') as f:
f.write(output)
示例2: template_to_file
# 需要导入模块: from mako import exceptions [as 别名]
# 或者: from mako.exceptions import text_error_template [as 别名]
def template_to_file(template_file, dest, output_encoding, **kw):
template = Template(filename=template_file)
try:
output = template.render_unicode(**kw).encode(output_encoding)
except:
with tempfile.NamedTemporaryFile(suffix=".txt", delete=False) as ntf:
ntf.write(
exceptions.text_error_template()
.render_unicode()
.encode(output_encoding)
)
fname = ntf.name
raise CommandError(
"Template rendering failed; see %s for a "
"template-oriented traceback." % fname
)
else:
with open(dest, "wb") as f:
f.write(output)
示例3: test_code_block_line_number
# 需要导入模块: from mako import exceptions [as 别名]
# 或者: from mako.exceptions import text_error_template [as 别名]
def test_code_block_line_number(self):
l = TemplateLookup()
l.put_string(
"foo.html",
"""
<%
msg = "Something went wrong."
raise RuntimeError(msg) # This is the line.
%>
""",
)
t = l.get_template("foo.html")
try:
t.render()
except:
text_error = exceptions.text_error_template().render_unicode()
assert 'File "foo.html", line 4, in render_body' in text_error
assert "raise RuntimeError(msg) # This is the line." in text_error
else:
assert False
示例4: test_module_block_line_number
# 需要导入模块: from mako import exceptions [as 别名]
# 或者: from mako.exceptions import text_error_template [as 别名]
def test_module_block_line_number(self):
l = TemplateLookup()
l.put_string(
"foo.html",
"""
<%!
def foo():
msg = "Something went wrong."
raise RuntimeError(msg) # This is the line.
%>
${foo()}
""",
)
t = l.get_template("foo.html")
try:
t.render()
except:
text_error = exceptions.text_error_template().render_unicode()
assert 'File "foo.html", line 7, in render_body' in text_error
assert 'File "foo.html", line 5, in foo' in text_error
assert "raise RuntimeError(msg) # This is the line." in text_error
else:
assert False
示例5: _exit
# 需要导入模块: from mako import exceptions [as 别名]
# 或者: from mako.exceptions import text_error_template [as 别名]
def _exit():
sys.stderr.write(exceptions.text_error_template().render())
sys.exit(1)
示例6: render_string
# 需要导入模块: from mako import exceptions [as 别名]
# 或者: from mako.exceptions import text_error_template [as 别名]
def render_string(template_name, request, context, *, app_key):
lookup = request.app.get(app_key)
if lookup is None:
raise web.HTTPInternalServerError(
text=("Template engine is not initialized, "
"call aiohttp_mako.setup(app_key={}) first"
"".format(app_key)))
try:
template = lookup.get_template(template_name)
except TemplateLookupException as e:
raise web.HTTPInternalServerError(
text="Template '{}' not found".format(template_name)) from e
if not isinstance(context, Mapping):
raise web.HTTPInternalServerError(
text="context should be mapping, not {}".format(type(context)))
if request.get(REQUEST_CONTEXT_KEY):
context = dict(request[REQUEST_CONTEXT_KEY], **context)
try:
text = template.render_unicode(**context)
except Exception: # pragma: no cover
exc_info = sys.exc_info()
errtext = text_error_template().render(
error=exc_info[1],
traceback=exc_info[2])
exc = MakoRenderingException(errtext).with_traceback(exc_info[2])
raise exc
return text
示例7: test_text_error_template
# 需要导入模块: from mako import exceptions [as 别名]
# 或者: from mako.exceptions import text_error_template [as 别名]
def test_text_error_template(self):
code = """
% i = 0
"""
try:
template = Template(code)
template.render_unicode()
assert False
except exceptions.CompileException:
text_error = exceptions.text_error_template().render_unicode()
assert "Traceback (most recent call last):" in text_error
assert (
"CompileException: Fragment 'i = 0' is not a partial "
"control statement"
) in text_error
示例8: test_alternating_file_names
# 需要导入模块: from mako import exceptions [as 别名]
# 或者: from mako.exceptions import text_error_template [as 别名]
def test_alternating_file_names(self):
l = TemplateLookup()
l.put_string(
"base.html",
"""
<%!
def broken():
raise RuntimeError("Something went wrong.")
%> body starts here
<%block name="foo">
${broken()}
</%block>
""",
)
l.put_string(
"foo.html",
"""
<%inherit file="base.html"/>
<%block name="foo">
${parent.foo()}
</%block>
""",
)
t = l.get_template("foo.html")
try:
t.render()
except:
text_error = exceptions.text_error_template().render_unicode()
assert """
File "base.html", line 5, in render_body
%> body starts here
File "foo.html", line 4, in render_foo
${parent.foo()}
File "base.html", line 7, in render_foo
${broken()}
File "base.html", line 4, in broken
raise RuntimeError("Something went wrong.")
""" in text_error
else:
assert False
示例9: doTemplate
# 需要导入模块: from mako import exceptions [as 别名]
# 或者: from mako.exceptions import text_error_template [as 别名]
def doTemplate(tmpl, namespace = {}, leftMargin = 0, rightMargin = 80, formatExceptions = True, encoding = 'utf-8'):
buf = StringIO()
ctx = Context(buf, **namespace)
try:
tobj = Template(filename = tmpl, output_encoding = encoding, format_exceptions = formatExceptions) #, imports ='re' # TODO: imports parameter.
tobj.render_context(ctx)
except:
print(exceptions.text_error_template().render())
return None
##return strings.reformat(buf.getvalue(), leftMargin, rightMargin)
return buf.getvalue()
示例10: doTemplateFromText
# 需要导入模块: from mako import exceptions [as 别名]
# 或者: from mako.exceptions import text_error_template [as 别名]
def doTemplateFromText(tmpl, namespace = {}, leftMargin = 0, rightMargin = 80, formatExceptions = True, encoding = 'utf-8'):
buf = StringIO()
ctx = Context(buf, **namespace)
try:
tobj = Template(text = tmpl, output_encoding = encoding, format_exceptions = formatExceptions) #, imports ='re'
tobj.render_context(ctx)
except:
print(exceptions.text_error_template().render())
return None
return indentText(buf.getvalue(), leftMargin) #, rightMargin)
示例11: render
# 需要导入模块: from mako import exceptions [as 别名]
# 或者: from mako.exceptions import text_error_template [as 别名]
def render(bv):
try:
show_markdown_report("{} Writeup".format(name), template.render(bv=bv, images=images))
except:
show_plain_text_report("{}: Error in Rendering".format(name), exceptions.text_error_template().render())
示例12: render
# 需要导入模块: from mako import exceptions [as 别名]
# 或者: from mako.exceptions import text_error_template [as 别名]
def render(bv):
try:
show_markdown_report("{} Writeup".format(name), template.render(bv=bv))
except:
show_plain_text_report("{}: Error in Rendering".format(name), exceptions.text_error_template().render())
示例13: render_with_template
# 需要导入模块: from mako import exceptions [as 别名]
# 或者: from mako.exceptions import text_error_template [as 别名]
def render_with_template(text=None, filename=None, preprocessor=None, template_kwargs={}):
from mako.template import Template
from mako import exceptions
tmpl = Template(text=text, filename=filename, preprocessor=preprocessor)
try:
return tmpl.render(**template_kwargs)
except Exception as e:
import sys
print('-' * 78, file=sys.stderr)
print('Template exceptions', file=sys.stderr)
print('-' * 78, file=sys.stderr)
print(exceptions.text_error_template().render(), file=sys.stderr)
print('-' * 78, file=sys.stderr)
raise e
示例14: render
# 需要导入模块: from mako import exceptions [as 别名]
# 或者: from mako.exceptions import text_error_template [as 别名]
def render(template, dest, **kwargs):
"""
Render a Mako template passing it optional arguments
"""
log.debug('Generating %s\n' % dest)
try:
text = Template(filename=template).render(**kwargs)
with closing(open(dest, 'w')) as f:
f.write(text)
except:
# Display template errors in a less cryptic way
log.error('Couldn''t render a config file (%s)',
os.path.basename(template))
log.error(exceptions.text_error_template().render())
示例15: mkorendertpl
# 需要导入模块: from mako import exceptions [as 别名]
# 或者: from mako.exceptions import text_error_template [as 别名]
def mkorendertpl(tpl,outfile,*args,**kwargs):
with open(outfile,"w") as f:
try:
f.write(tpl.render(*args,**kwargs));
except:
print((exceptions.text_error_template().render()))
raise;