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())
def render(target_language=None, **kwargs): d = tempfile.mkdtemp() f = tempfile.NamedTemporaryFile(suffix=".py", dir=d) try: # compile source f.write(source) f.flush() name, ext = os.path.splitext(os.path.basename(f.name)) args = imp.find_module(name, [os.path.dirname(f.name)]) module = imp.load_module(name, *args) func = module.bind() # render setup kwargs.setdefault(config.SYMBOLS.slots, utils.emptydict) kwargs.setdefault(config.SYMBOLS.translate, lambda msg, *args, **kwargs: msg) rcontext = utils.econtext(kwargs) econtext = rcontext.copy() result = func(econtext, rcontext) finally: f.close() shutil.rmtree(d) return result