/usr/share/pyshared/z3c/pt/pagetemplate.py is in python-z3c.pt 2.2.3-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | import os
import sys
from zope import i18n
from zope.security.proxy import ProxyFactory
from chameleon.i18n import fast_translate
from chameleon.zpt import template
from chameleon.tales import StringExpr
from chameleon.tales import NotExpr
from chameleon.compiler import ExpressionEvaluator
from z3c.pt import expressions
try:
from Missing import MV
MV # pyflakes
except ImportError:
MV = object()
_marker = object()
BOOLEAN_HTML_ATTRS = frozenset([
# List of Boolean attributes in HTML that should be rendered in
# minimized form (e.g. <img ismap> rather than <img ismap="">)
# From http://www.w3.org/TR/xhtml1/#guidelines (C.10)
# TODO: The problem with this is that this is not valid XML and
# can't be parsed back!
"compact", "nowrap", "ismap", "declare", "noshade", "checked",
"disabled", "readonly", "multiple", "selected", "noresize",
"defer"
])
class OpaqueDict(dict):
def __new__(cls, dictionary):
inst = dict.__new__(cls)
inst.dictionary = dictionary
return inst
@property
def __getitem__(self):
return self.dictionary.__getitem__
@property
def __len__(self):
return self.dictionary.__len__
def __repr__(self):
return "{...} (%d entries)" % len(self)
sys_modules = ProxyFactory(OpaqueDict(sys.modules))
class DummyRegistry(object):
"""This class is for B/W with Chameleon 1.x API."""
@staticmethod
def purge():
pass
class BaseTemplate(template.PageTemplate):
content_type = None
version = 2
registry = DummyRegistry()
expression_types = {
'python': expressions.PythonExpr,
'string': StringExpr,
'not': NotExpr,
'exists': expressions.ExistsExpr,
'path': expressions.PathExpr,
'provider': expressions.ProviderExpr,
'nocall': expressions.NocallExpr,
}
default_expression = "path"
literal_false = True
strict = False
trim_attribute_space = True
@property
def boolean_attributes(self):
if self.content_type == 'text/xml':
return set()
return BOOLEAN_HTML_ATTRS
@property
def builtins(self):
builtins = {
'nothing': None,
'modules': sys_modules,
}
tales = ExpressionEvaluator(self.engine, builtins)
builtins['tales'] = tales
return builtins
def bind(self, ob, request=None):
def render(request=request, **kwargs):
context = self._pt_get_context(ob, request, kwargs)
return self.render(**context)
return BoundPageTemplate(self, render)
def render(self, target_language=None, **context):
# We always include a ``request`` variable; it is (currently)
# depended on in various expression types and must be defined
request = context.setdefault('request', None)
if target_language is None:
try:
target_language = i18n.negotiate(request)
except:
target_language = None
context['target_language'] = target_language
# bind translation-method to request
def translate(
msgid, domain=None, mapping=None,
target_language=None, default=None,
context=None):
if msgid is MV:
# Special case handling of Zope2's Missing.MV
# (Missing.Value) used by the ZCatalog but is
# unhashable
return
return fast_translate(
msgid, domain, mapping, request, target_language, default)
context["translate"] = translate
if request is not None and not isinstance(request, basestring):
content_type = self.content_type or 'text/html'
response = request.response
if response and not response.getHeader("Content-Type"):
response.setHeader(
"Content-Type", content_type)
base_renderer = super(BaseTemplate, self).render
return base_renderer(**context)
def __call__(self, *args, **kwargs):
bound_pt = self.bind(self)
return bound_pt(*args, **kwargs)
def _pt_get_context(self, instance, request, kwargs):
return dict(
context=instance,
here=instance,
options=kwargs,
request=request,
template=self,
)
class BaseTemplateFile(BaseTemplate, template.PageTemplateFile):
"""If ``filename`` is a relative path, the module path of the
class where the instance is used to get an absolute path."""
cache = {}
def __init__(self, filename, path=None, content_type=None, **kwargs):
if path is not None:
filename = os.path.join(path, filename)
if not os.path.isabs(filename):
for depth in (1, 2):
frame = sys._getframe(depth)
package_name = frame.f_globals.get('__name__', None)
if package_name is not None and \
package_name != self.__module__:
module = sys.modules[package_name]
try:
path = module.__path__[0]
except AttributeError:
path = module.__file__
path = path[:path.rfind(os.sep)]
break
else:
package_path = frame.f_globals.get('__file__', None)
if package_path is not None:
path = os.path.dirname(package_path)
break
if path is not None:
filename = os.path.join(path, filename)
template.PageTemplateFile.__init__(
self, filename, **kwargs)
# Set content-type last, so that we can override whatever was
# magically sniffed from the source template.
self.content_type = content_type
class PageTemplate(BaseTemplate):
"""Page Templates using TAL, TALES, and METAL.
This class is suitable for standalone use or class
property. Keyword-arguments are passed into the template as-is.
Initialize with a template string."""
version = 1
def __get__(self, instance, type):
if instance is not None:
return self.bind(instance)
return self
class PageTemplateFile(BaseTemplateFile, PageTemplate):
"""Page Templates using TAL, TALES, and METAL.
This class is suitable for standalone use or class
property. Keyword-arguments are passed into the template as-is.
Initialize with a filename."""
cache = {}
class ViewPageTemplate(PageTemplate):
"""Template class suitable for use with a Zope browser view; the
variables ``view``, ``context`` and ``request`` variables are
brought in to the local scope of the template automatically, while
keyword arguments are passed in through the ``options``
dictionary. Note that the default expression type for this class
is 'path' (standard Zope traversal)."""
def _pt_get_context(self, view, request, kwargs):
context = kwargs.get('context')
if context is None:
context = view.context
request = request or kwargs.get('request') or view.request
return dict(
view=view,
context=context,
request=request,
options=kwargs,
template=self,
)
def __call__(self, _ob=None, context=None, request=None, **kwargs):
kwargs.setdefault('context', context)
kwargs.setdefault('request', request)
bound_pt = self.bind(_ob)
return bound_pt(**kwargs)
class ViewPageTemplateFile(ViewPageTemplate, PageTemplateFile):
"""If ``filename`` is a relative path, the module path of the
class where the instance is used to get an absolute path."""
cache = {}
class BoundPageTemplate(object):
"""When a page template class is used as a property, it's bound to
the class instance on access, which is implemented using this
helper class."""
def __init__(self, pt, render):
object.__setattr__(self, 'im_self', pt)
object.__setattr__(self, 'im_func', render)
macros = property(lambda self: self.im_self.macros)
filename = property(lambda self: self.im_self.filename)
def __call__(self, *args, **kw):
kw.setdefault('args', args)
return self.im_func(**kw)
def __setattr__(self, name, v):
raise AttributeError("Can't set attribute", name)
def __repr__(self):
return "<%s.Bound%s %r>" % (
type(self.im_self).__module__,
type(self.im_self).__name__, self.filename)
|