diff options
| author | Chris McDonough <chrism@agendaless.com> | 2008-09-26 06:42:53 +0000 |
|---|---|---|
| committer | Chris McDonough <chrism@agendaless.com> | 2008-09-26 06:42:53 +0000 |
| commit | 01a6e567a20096f6033cc603667f4e900d2a44c3 (patch) | |
| tree | a87431383a63dbafbb5cccdfa7679b9187bdfc29 /repoze/bfg/chameleon_genshi.py | |
| parent | 26216e5526ca56d886d2348f9e1f09b86622aa72 (diff) | |
| download | pyramid-01a6e567a20096f6033cc603667f4e900d2a44c3.tar.gz pyramid-01a6e567a20096f6033cc603667f4e900d2a44c3.tar.bz2 pyramid-01a6e567a20096f6033cc603667f4e900d2a44c3.zip | |
Move to Chameleon.
Diffstat (limited to 'repoze/bfg/chameleon_genshi.py')
| -rw-r--r-- | repoze/bfg/chameleon_genshi.py | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/repoze/bfg/chameleon_genshi.py b/repoze/bfg/chameleon_genshi.py new file mode 100644 index 000000000..fbcc519e6 --- /dev/null +++ b/repoze/bfg/chameleon_genshi.py @@ -0,0 +1,82 @@ +import os + +from webob import Response + +from zope.component import queryUtility +from zope.component.interfaces import ComponentLookupError +from zope.component import getSiteManager + +from zope.interface import classProvides +from zope.interface import implements + +from repoze.bfg.path import caller_path +from repoze.bfg.interfaces import ITemplateFactory +from repoze.bfg.interfaces import ITemplate +from repoze.bfg.interfaces import ISettings + +from chameleon.genshi.template import GenshiTemplateFile + +class GenshiTemplateFactory(object): + classProvides(ITemplateFactory) + implements(ITemplate) + + def __init__(self, path, auto_reload=False): + try: + self.template = GenshiTemplateFile(path, auto_reload=auto_reload) + except ImportError, why: + why = str(why) + if 'z3c.pt' in why: + # unpickling error due to move from z3c.pt -> chameleon + cachefile = '%s.cache' % path + if os.path.isfile(cachefile): + os.remove(cachefile) + self.template = GenshiTemplateFile(path, + auto_reload=auto_reload) + else: + raise + + def __call__(self, **kw): + result = self.template.render(**kw) + return result + +def _get_template(path, **kw): + # XXX use pkg_resources + template = queryUtility(ITemplate, path) + + if template is None: + if not os.path.exists(path): + raise ValueError('Missing template file: %s' % path) + settings = queryUtility(ISettings) + auto_reload = settings and settings.reload_templates + template = GenshiTemplateFactory(path, auto_reload) + try: + sm = getSiteManager() + except ComponentLookupError: + pass + else: + sm.registerUtility(template, ITemplate, name=path) + + return template + +def get_template(path): + """ Return a ``chameleon.genshi`` template object at the + package-relative path (may also be absolute)""" + path = caller_path(path) + return _get_template(path).template + +def render_template(path, **kw): + """ Render a ``chameleon.genshi`` template at the package-relative + path (may also be absolute) using the kwargs in ``*kw`` as + top-level names and return a string.""" + path = caller_path(path) + template = get_template(path) + return template(**kw) + +def render_template_to_response(path, **kw): + """ Render a ``chameleon.genshi`` template at the package-relative + path (may also be absolute) using the kwargs in ``*kw`` as + top-level names and return a Response object.""" + path = caller_path(path) + result = render_template(path, **kw) + return Response(result) + |
