/
chameleon_text.py
142 lines (122 loc) · 5.49 KB
/
chameleon_text.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import sys
from zope.interface import implements
try:
from chameleon.core.template import TemplateFile
TemplateFile # prevent pyflakes complaining about a redefinition below
except ImportError: # pragma: no cover
exc_class, exc, tb = sys.exc_info()
# Chameleon doesn't work on non-CPython platforms
class TemplateFile(object):
def __init__(self, *arg, **kw):
raise ImportError, exc, tb
try:
from chameleon.zpt.language import Parser
Parser # prevent pyflakes complaining about a redefinition below
except ImportError: # pragma: no cover
# Chameleon doesn't work on non-CPython platforms
class Parser(object):
pass
from pyramid.interfaces import ITemplateRenderer
from pyramid.interfaces import IChameleonTranslate
from pyramid.decorator import reify
from pyramid import renderers
from pyramid.path import caller_package
from pyramid.settings import get_settings
from pyramid.threadlocal import get_current_registry
class TextTemplateFile(TemplateFile):
default_parser = Parser()
def __init__(self, filename, parser=None, format='text', doctype=None,
**kwargs):
if parser is None:
parser = self.default_parser
super(TextTemplateFile, self).__init__(filename, parser, format,
doctype, **kwargs)
def renderer_factory(path):
return renderers.template_renderer_factory(path, TextTemplateRenderer)
class TextTemplateRenderer(object):
implements(ITemplateRenderer)
def __init__(self, path):
self.path = path
@reify # avoid looking up reload_templates before manager pushed
def template(self):
if sys.platform.startswith('java'): # pragma: no cover
raise RuntimeError(
'Chameleon templates are not compatible with Jython')
settings = get_settings()
debug = False
auto_reload = False
if settings:
# using .get here is a strategy to be kind to old *tests* rather
# than being kind to any existing production system
auto_reload = settings.get('reload_templates')
debug = settings.get('debug_templates')
reg = get_current_registry()
translate = None
if reg is not None:
translate = reg.queryUtility(IChameleonTranslate)
return TextTemplateFile(self.path,
auto_reload=auto_reload,
debug=debug,
translate=translate)
def implementation(self):
return self.template
def __call__(self, value, system):
try:
system.update(value)
except (TypeError, ValueError):
raise ValueError('renderer was passed non-dictionary as value')
result = self.template(**system)
return result
def get_renderer(path):
""" Return a callable object which can be used to render a
:term:`Chameleon` text template using the template implied by the
``path`` argument. The ``path`` argument may be a
package-relative path, an absolute path, or a :term:`resource
specification`.
.. warning:: This API is deprecated in :app:`Pyramid` 1.0. Use
:func:`pyramid.renderers.get_renderer` instead.
"""
package = caller_package()
factory = renderers.RendererHelper(path, package=package)
return factory.get_renderer()
def get_template(path):
""" Return the underyling object representing a :term:`Chameleon`
text template using the template implied by the ``path`` argument.
The ``path`` argument may be a package-relative path, an absolute
path, or a :term:`resource specification`.
.. warning:: This API is deprecated in :app:`Pyramid` 1.0. Use
the ``implementation()`` method of a template renderer retrieved via
:func:`pyramid.renderers.get_renderer` instead.
"""
package = caller_package()
factory = renderers.RendererHelper(path, package=package)
return factory.get_renderer().implementation()
def render_template(path, **kw):
""" Render a :term:`Chameleon` text template using the template
implied by the ``path`` argument. The ``path`` argument may be a
package-relative path, an absolute path, or a :term:`resource
specification`. The arguments in ``*kw`` are passed as top-level
names to the template, and so may be used within the template
itself. Returns a string.
.. warning:: This API is deprecated in :app:`Pyramid` 1.0. Use
:func:`pyramid.renderers.render` instead.
"""
package = caller_package()
request = kw.pop('request', None)
renderer = renderers.RendererHelper(path, package=package)
return renderer.render(kw, None, request=request)
def render_template_to_response(path, **kw):
""" Render a :term:`Chameleon` text template using the template
implied by the ``path`` argument. The ``path`` argument may be a
package-relative path, an absolute path, or a :term:`resource
specification`. The arguments in ``*kw`` are passed as top-level
names to the template, and so may be used within the template
itself. Returns a :term:`Response` object with the body as the
template result.
.. warning:: This API is deprecated in :app:`Pyramid` 1.0. Use
:func:`pyramid.renderers.render_to_response` instead.
"""
package = caller_package()
request = kw.pop('request', None)
renderer = renderers.RendererHelper(path, package=package)
return renderer.render_to_response(kw, None, request=request)