def parse(self): body = self.__dict__['body'] # if the body is trivial, we don't try to compile it if None in (body, self.parser): return parse_method = getattr(self.parser, 'parse_%s' % self.format) try: tree = parse_method(body) except: utils.raise_template_exception(exc_info=sys.exc_info(), filename=self.filename) # it's not clear from the tree if an XML declaration was # present in the document source; the following is a # work-around to ensure that output matches input first_line = body.lstrip().split('\n', 1)[0] if first_line.startswith('<?xml '): xml_declaration = ("""<?xml version="%s" """ % tree.docinfo.xml_version) if tree.docinfo.encoding: xml_declaration += ("""encoding="%s" """ % tree.docinfo.encoding) if 'standalone="' in first_line: xml_declaration += ("""standalone="%s" """ % tree.docinfo.standalone) xml_declaration += """?>""" else: xml_declaration = None explicit_doctype = self.explicit_doctype if not explicit_doctype is doctypes.no_doctype and not explicit_doctype: explicit_doctype = self.parser.doctype compiler = translation.Compiler( tree, explicit_doctype=explicit_doctype, xml_declaration=xml_declaration, encoding=self.encoding, omit_default_prefix=self.omit_default_prefix) self.__dict__['compiler'] = compiler self.__dict__['slots'] = compiler.macros self.__dict__['macros'] = Macros(self.render_macro, *compiler.macros)
def parse(self): body = self.__dict__['body'] # if the body is trivial, we don't try to compile it if None in (body, self.parser): return parse_method = getattr(self.parser, 'parse_%s' % self.format) try: tree = parse_method(body) except: utils.raise_template_exception( exc_info=sys.exc_info(), filename=self.filename) # it's not clear from the tree if an XML declaration was # present in the document source; the following is a # work-around to ensure that output matches input first_line = body.lstrip().split('\n', 1)[0] if first_line.startswith('<?xml '): xml_declaration = ( """<?xml version="%s" """ % tree.docinfo.xml_version) if tree.docinfo.encoding: xml_declaration += ( """encoding="%s" """ % tree.docinfo.encoding) if 'standalone="' in first_line: xml_declaration += ( """standalone="%s" """ % tree.docinfo.standalone) xml_declaration += """?>""" else: xml_declaration = None explicit_doctype = self.explicit_doctype if not explicit_doctype is doctypes.no_doctype and not explicit_doctype: explicit_doctype = self.parser.doctype compiler = translation.Compiler( tree, explicit_doctype=explicit_doctype, xml_declaration=xml_declaration, encoding=self.encoding, omit_default_prefix=self.omit_default_prefix) self.__dict__['compiler'] = compiler self.__dict__['slots'] = compiler.macros self.__dict__['macros'] = Macros(self.render_macro, *compiler.macros)
def cook_and_render(self, kwargs, slots, macro, global_scope): """Cook and render template. This method finds a render-method from the template registry facility, cooking one if required (by invoking the compiler). """ if self.compiler is None: raise NotImplemented("Python compiler-support required.") key = macro, global_scope, self.signature if key not in self.registry: self.acquire() try: source = self.compiler(macro, global_scope) finally: self.release() self.registry.add(key, source, self.filename) if key not in self.registry: raise RuntimeError( "Unable to add template '%s' to registry (%s)." % (self.filename, type(self.registry).__name__)) func = self.registry[key] econtext = rcontext = kwargs.pop("econtext", self) if global_scope is False: econtext = econtext.copy() if econtext is self: econtext = utils.econtext(kwargs) else: econtext.update(kwargs) econtext[config.SYMBOLS.slots] = slots econtext.setdefault(config.SYMBOLS.translate, self.translate) __traceback_info__ = (self, ) if self.debug is False: return func(econtext, rcontext) try: return func(econtext, rcontext) except: utils.raise_template_exception(kwargs, sys.exc_info())
def cook_and_render(self, kwargs, slots, macro, global_scope): """Cook and render template. This method finds a render-method from the template registry facility, cooking one if required (by invoking the compiler). """ if self.compiler is None: raise NotImplemented("Python compiler-support required.") key = macro, global_scope, self.signature if key not in self.registry: self.acquire() try: source = self.compiler(macro, global_scope) finally: self.release() self.registry.add(key, source, self.filename) if key not in self.registry: raise RuntimeError( "Unable to add template '%s' to registry (%s)." % ( self.filename, type(self.registry).__name__)) func = self.registry[key] econtext = rcontext = kwargs.pop("econtext", self) if global_scope is False: econtext = econtext.copy() if econtext is self: econtext = utils.econtext(kwargs) else: econtext.update(kwargs) econtext[config.SYMBOLS.slots] = slots econtext.setdefault(config.SYMBOLS.translate, self.translate) __traceback_info__ = (self,) if self.debug is False: return func(econtext, rcontext) try: return func(econtext, rcontext) except: utils.raise_template_exception(kwargs, sys.exc_info())