/usr/share/pyshared/nevow/flat/flatstan.py is in python-nevow 0.10.0-4build1.
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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | # Copyright (c) 2004 Divmod.
# See LICENSE for details.
from __future__ import generators
import urllib, warnings
from twisted.python import log, failure
from nevow import util
from nevow.stan import directive, Unset, invisible, _PrecompiledSlot
from nevow.inevow import ICanHandleException, IData, IMacroFactory, IRenderer, IRendererFactory
from nevow.flat import precompile, serialize
from nevow.accessors import convertToData
from nevow.context import WovenContext
allowSingleton = ('img', 'br', 'hr', 'base', 'meta', 'link', 'param', 'area',
'input', 'col', 'basefont', 'isindex', 'frame')
def ProtoSerializer(original, context):
return '<%s />' % original
def _datacallback(result, context):
context.remember(result, IData)
return ''
def TagSerializer(original, context, contextIsMine=False):
"""
Original is the tag.
Context is either:
- the context of someone up the chain (if contextIsMine is False)
- this tag's context (if contextIsMine is True)
"""
# print "TagSerializer:",original, "ContextIsMine",contextIsMine, "Context:",context
visible = bool(original.tagName)
if visible and context.isAttrib:
raise RuntimeError, "Tried to render tag '%s' in an tag attribute context." % (original.tagName)
if context.precompile and original.macro:
toBeRenderedBy = original.macro
## Special case for directive; perhaps this could be handled some other way with an interface?
if isinstance(toBeRenderedBy, directive):
toBeRenderedBy = IMacroFactory(context).macro(context, toBeRenderedBy.name)
original.macro = Unset
newContext = WovenContext(context, original)
yield serialize(toBeRenderedBy(newContext), newContext)
return
## TODO: Do we really need to bypass precompiling for *all* specials?
## Perhaps just render?
if context.precompile and (
[x for x in original._specials.values()
if x is not None and x is not Unset]
or original.slotData):
## The tags inside this one get a "fresh" parent chain, because
## when the context yielded here is serialized, the parent
## chain gets reconnected to the actual parents at that
## point, since the render function here could change
## the actual parentage hierarchy.
nestedcontext = WovenContext(precompile=context.precompile, isAttrib=context.isAttrib)
# If necessary, remember the MacroFactory onto the new context chain.
macroFactory = IMacroFactory(context, None)
if macroFactory is not None:
nestedcontext.remember(macroFactory, IMacroFactory)
original = original.clone(deep=False)
if not contextIsMine:
context = WovenContext(context, original)
context.tag.children = precompile(context.tag.children, nestedcontext)
yield context
return
## Don't render patterns
if original.pattern is not Unset and original.pattern is not None:
return
if not contextIsMine:
if original.render:
### We must clone our tag before passing to a render function
original = original.clone(deep=False)
context = WovenContext(context, original)
if original.data is not Unset:
newdata = convertToData(original.data, context)
if isinstance(newdata, util.Deferred):
yield newdata.addCallback(lambda newdata: _datacallback(newdata, context))
else:
_datacallback(newdata, context)
if original.render:
## If we have a render function we want to render what it returns,
## not our tag
toBeRenderedBy = original.render
# erase special attribs so if the renderer returns the tag,
# the specials won't be on the context twice.
original._clearSpecials()
yield serialize(toBeRenderedBy, context)
return
if not visible:
for child in original.children:
yield serialize(child, context)
return
yield '<%s' % original.tagName
if original.attributes:
attribContext = WovenContext(parent=context, precompile=context.precompile, isAttrib=True)
for (k, v) in original.attributes.iteritems():
if v is None:
continue
yield ' %s="' % k
yield serialize(v, attribContext)
yield '"'
if not original.children:
if original.tagName in allowSingleton:
yield ' />'
else:
yield '></%s>' % original.tagName
else:
yield '>'
for child in original.children:
yield serialize(child, context)
yield '</%s>' % original.tagName
def EntitySerializer(original, context):
if original.name in ['amp', 'gt', 'lt', 'quot']:
return '&%s;' % original.name
return '&#%s;' % original.num
def _jsSingleQuoteQuote(quotable):
return quotable.replace(
"\\", "\\\\").replace(
"'", r"\'").replace(
"\n", "\\n").replace(
"\r", "\\r")
def RawSerializer(original, context):
if context.inJSSingleQuoteString:
return _jsSingleQuoteQuote(original)
return original
def StringSerializer(original, context):
# Quote the string as necessary. URLs need special quoting - only
# alphanumeric and a few punctation characters are valid.
# Otherwise we use normal XML escaping rules but also replacing "
# in an attribute because Nevow always uses "..." for values.
if context.inURL:
# The magic string "-_.!*'()" also appears in url.py. Thinking about
# changing this? Change that, too.
return urllib.quote(original, safe="-_.!*'()")
## quote it
if context.inJS:
original = _jsSingleQuoteQuote(original)
if not context.inJSSingleQuoteString:
original = "'%s'" % (original, )
if context.isAttrib:
return original.replace("&", "&").replace("<", "<").replace(">", ">").replace('"', """)
elif context.inJS:
return original
else:
return original.replace("&", "&").replace("<", "<").replace(">", ">")
def NoneWarningSerializer(original, context):
if context.isAttrib:
## We don't want the big red None warning inside a html attribute. Just leave it blank.
return ''
elif context.inURL:
return ''
elif context.inJS:
return ''
return '<span style="font-size: xx-large; font-weight: bold; color: red; border: thick solid red;">None</span>'
def StringCastSerializer(original, context):
if context.inJS:
return str(original)
return StringSerializer(str(original), context)
def BooleanSerializer(original, context):
if context.inJS:
if original:
return 'true'
return 'false'
return str(original)
def ListSerializer(original, context):
for item in original:
yield serialize(item, context)
def XmlSerializer(original, context):
return original.content
PASS_SELF = object()
def FunctionSerializer_nocontext(original):
code = getattr(original, 'func_code', None)
if code is None:
return True
argcount = code.co_argcount
if argcount == 1:
return True
if argcount == 3:
return PASS_SELF
return False
def FunctionSerializer(original, context, nocontextfun=FunctionSerializer_nocontext):
if context.precompile:
return WovenContext(tag=invisible(render=original))
else:
data = convertToData(context.locate(IData), context)
try:
nocontext = nocontextfun(original)
if nocontext is True:
result = original(data)
else:
if nocontext is PASS_SELF:
renderer = context.locate(IRenderer)
result = original(renderer, context, data)
else:
result = original(context, data)
except StopIteration:
raise RuntimeError, "User function %r raised StopIteration." % original
return serialize(result, context)
def MethodSerializer(original, context):
def nocontext(original):
func = getattr(original, 'im_func', None)
code = getattr(func, 'func_code', None)
return code is None or code.co_argcount == 2
return FunctionSerializer(original, context, nocontext)
def RendererSerializer(original, context):
def nocontext(original):
func = getattr(original, 'im_func', None)
code = getattr(func, 'func_code', None)
return code is None or code.co_argcount == 2
return FunctionSerializer(original.rend, context, nocontext)
def DirectiveSerializer(original, context):
if context.precompile:
return original
rendererFactory = context.locate(IRendererFactory)
renderer = rendererFactory.renderer(context, original.name)
return serialize(renderer, context)
def SlotSerializer(original, context):
"""
Serialize a slot.
If the value is already available in the given context, serialize and
return it. Otherwise, if this is a precompilation pass, return a new
kind of slot which captures the current render context, so that any
necessary quoting may be performed. Otherwise, raise an exception
indicating that the slot cannot be serialized.
"""
if context.precompile:
try:
data = context.locateSlotData(original.name)
except KeyError:
return _PrecompiledSlot(
original.name,
precompile(original.children, context),
original.default,
context.isAttrib,
context.inURL,
context.inJS,
context.inJSSingleQuoteString,
original.filename,
original.lineNumber,
original.columnNumber)
else:
return serialize(data, context)
try:
data = context.locateSlotData(original.name)
except KeyError:
if original.default is None:
raise
data = original.default
return serialize(data, context)
def PrecompiledSlotSerializer(original, context):
"""
Serialize a pre-compiled slot.
Return the serialized value of the slot or raise a KeyError if it has no
value.
"""
# Precompilation should _not_ be happening at this point, but Nevow is very
# sloppy about precompiling multiple times, so sometimes we are in a
# precompilation context. In this case, there is nothing to do, just
# return the original object. The case which seems to exercise this most
# often is the use of a pattern as the stan document given to the stan
# loader. The pattern has already been precompiled, but the stan loader
# precompiles it again. This case should be eliminated by adding a loader
# for precompiled documents.
if context.precompile:
warnings.warn(
"[v0.9.9] Support for multiple precompilation passes is deprecated.",
PendingDeprecationWarning)
return original
try:
data = context.locateSlotData(original.name)
except KeyError:
if original.default is None:
raise
data = original.default
originalContext = context.clone(deep=False)
originalContext.isAttrib = original.isAttrib
originalContext.inURL = original.inURL
originalContext.inJS = original.inJS
originalContext.inJSSingleQuoteString = original.inJSSingleQuoteString
return serialize(data, originalContext)
def ContextSerializer(original, context):
"""
Serialize the given context's tag in that context.
"""
originalContext = original.clone(deep=False)
originalContext.precompile = context and context.precompile or False
if originalContext.parent is not None:
originalContext.parent = originalContext.parent.clone(cloneTags=False)
originalContext.chain(context)
try:
return TagSerializer(originalContext.tag, originalContext, contextIsMine=True)
except:
f = failure.Failure()
handler = context.locate(ICanHandleException)
if handler:
return handler.renderInlineError(context, f)
else:
log.err(f)
return """<div style="border: 1px dashed red; color: red; clear: both">[[ERROR]]</div>"""
def CommentSerializer(original, context):
yield "<!--"
for x in original.children:
yield serialize(x, context)
yield "-->"
def DocFactorySerializer(original, ctx):
"""Serializer for document factories.
"""
return serialize(original.load(ctx), ctx)
def FailureSerializer(original, ctx):
from nevow import failure
return serialize(failure.formatFailure(original), ctx)
def inlineJSSerializer(original, ctx):
from nevow import livepage
from nevow.tags import script, xml
theJS = livepage.js(original.children)
new = livepage.JavascriptContext(ctx, invisible[theJS])
return serialize(script(type="text/javascript")[
xml('\n//<![CDATA[\n'),
serialize(theJS, new),
xml('\n//]]>\n')], ctx)
|