Published in: Python
Seemed like a good idea - lets you used a.b.c to access uncompiled Cheetah templates via directory structure, and the same syntax auto-imports compiled templates.
class TemplateMapper: """Attribute-style access to non-compiled Cheetah templates.. This hides the distinction between importing a pre-compiled template from a module versus parsing a raw file template in a subdirectory. Seemed like a good idea at the time.""" def __init__(self, root=path("templates"), ext=".tmpl", module=None, gvars={}, template_class=Cheetah.Template.Template, kwargs={}): """Initialise template mapper. @param root: Path to root template directory @param ext: Template extension @param module: Root modulue for templates (root is then not used) @param gvars: Global variables dictionary @param template_class: Defaults to Cheetah.Template.Template @param kwargs: Keyword args for tmpl_class.__init__() """ self.root = path(root) self.gvars = gvars self.gvars["_inc"] = lambda x: str(self.root / x) self.ext = ext self.module = module self.template_class = template_class self.kwargs = kwargs def __getattr__(self, name): """Attribute access to mapper creates a callable template object""" # Instantiate using self.root / name if self.module is None: return self.CallableTemplate( template = self.root / name + self.ext, default_search = self.gvars, template_class = self.template_class) # Instantiate using self.module.name.name else: return self.CallableTemplate( template = getattr(getattr(self.module, name), name), default_search = self.gvars, template_class = self.template_class) class CallableTemplate: """Proxy for a Cheetah template. Calling this proxy template fills it, with correct constructor invocation for file, string or compiled templates.""" def __init__(self, template, default_search={}, template_class=Cheetah.Template.Template, kwargs={}): """Initialise callable template with set of global variables @param template: path, string, or Template subclass @param default_search: Default search dictionary @param template_class: What to instantiate if template is path/string @param kwargs: Keyword arguments for Template constructor """ self.template = template self.default_search = default_search self.template_class = template_class self.kwargs = kwargs def __call__(self, *pvars, **kvars): """Call template with self.gvars for globals. @param pvars: Dictionaries to add to the searchlist @param kvars: Keywords to add to search list.""" slist = list(pvars) + [kvars, self.default_search] if isinstance(self.template, path): return self.template_class( file=str(self.template), searchList=slist, **self.kwargs) elif isinstance(self.template, basestring): return self.template_class( source=self.template, searchList=slist, **self.kwargs) elif issubclass(self.template, Template): return self.template( searchList=slist, **self.kwargs) else: raise ValueError("Unrecognised template")
You need to login to post a comment.
