MCPcopy Create free account
hub / github.com/webpy/webpy / compile_template

Method compile_template

web/template.py:1010–1035  ·  view source on GitHub ↗
(self, template_string, filename)

Source from the content-addressed store, hash-verified

1008 return p
1009
1010 def compile_template(self, template_string, filename):
1011 code = Template.generate_code(
1012 template_string, filename, parser=self.create_parser()
1013 )
1014
1015 def get_source_line(filename, lineno):
1016 try:
1017 lines = open(filename, encoding="utf-8").read().splitlines()
1018 return lines[lineno]
1019 except:
1020 return None
1021
1022 try:
1023 # compile the code first to report the errors, if any, with the filename
1024 compiled_code = compile(code, filename, "exec")
1025 except SyntaxError as err:
1026 # display template line that caused the error along with the traceback.
1027 err.msg += f"\n\nTemplate traceback:\n File {repr(err.filename)}, line {err.lineno}\n {get_source_line(err.filename, err.lineno - 1)}"
1028
1029 raise
1030
1031 # make sure code is safe
1032 ast_node = ast.parse(code, filename)
1033 SafeVisitor().walk(ast_node, filename)
1034
1035 return compiled_code
1036
1037
1038class CompiledTemplate(Template):

Callers 1

__init__Method · 0.95

Calls 5

create_parserMethod · 0.95
SafeVisitorClass · 0.85
generate_codeMethod · 0.80
walkMethod · 0.80
parseMethod · 0.45

Tested by

no test coverage detected