/usr/share/pyshared/nevow/loaders.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 | # Copyright (c) 2004 Divmod.
# See LICENSE for details.
"""Builtin template loaders that supply a Page or renderer with a
document to render.
Nevow provides the following DocFactory loaders by default:
- B{stan} - turn a stan tag tree into a DocFactory
- B{xmlfile} - load a well formed XML document from file
- B{htmlfile} - load a HTML file from disk
- B{xmlstr} - load a well formed XML document from a string
- B{htmlstr} - load a HTML document from a string
Unless absolutely necessary you should use either the stan loader or
one of the xml loaders. The html loaders should only be used for badly
formed HTML documents, i.e. if your HTML developer hasn't heard of
XHTML yet. Even then, you should probably try to educate the HTML
developer first ;-).
"""
import warnings
import os.path
from zope.interface import implements
from twisted.python.reflect import getClass
from twisted.web import microdom
from nevow import inevow
from nevow import flat
from nevow.flat import flatsax
from nevow.util import CachedFile
class stan(object):
"""A stan tags document factory"""
implements(inevow.IDocFactory)
stan = None
pattern = None
_cache = None
def __init__(self, stan=None, pattern=None):
if stan is not None:
self.stan = stan
if pattern is not None:
self.pattern = pattern
def load(self, ctx=None, preprocessors=()):
if self._cache is None:
stan = [self.stan]
for proc in preprocessors:
stan = proc(stan)
stan = flat.precompile(stan, ctx)
if self.pattern is not None:
stan = inevow.IQ(stan).onePattern(self.pattern)
self._cache = stan
return self._cache
class htmlstr(object):
"""A document factory for HTML contained in a string"""
implements(inevow.IDocFactory)
template = None
pattern = None
beExtremelyLenient = True
_cache = None
def __init__(self, template=None, pattern=None, beExtremelyLenient=None):
warnings.warn(
"[v0.8] htmlstr is deprecated because it's buggy. Please start using xmlfile and/or xmlstr.",
DeprecationWarning,
stacklevel=2)
if template is not None:
self.template = template
if pattern is not None:
self.pattern = pattern
if beExtremelyLenient is not None:
self.beExtremelyLenient = beExtremelyLenient
def load(self, ctx=None, preprocessors=()):
assert not preprocessors, "preprocessors not supported by htmlstr"
if self._cache is None:
doc = microdom.parseString(self.template, beExtremelyLenient=self.beExtremelyLenient)
doc = flat.precompile(doc, ctx)
if self.pattern is not None:
doc = inevow.IQ(doc).onePattern(self.pattern)
self._cache = doc
return self._cache
class htmlfile(object):
"""A document factory for an HTML disk template"""
implements(inevow.IDocFactory)
template = None
pattern = None
templateDir = ''
beExtremelyLenient = True
def __init__(self, template=None, pattern=None, templateDir=None, beExtremelyLenient=None):
warnings.warn(
"[v0.8] htmlfile is deprecated because it's buggy. Please start using xmlfile and/or xmlstr.",
DeprecationWarning,
stacklevel=2)
if template is not None:
self.template = template
if pattern is not None:
self.pattern = pattern
if templateDir is not None:
self.templateDir = templateDir
if beExtremelyLenient is not None:
self.beExtremelyLenient = beExtremelyLenient
_filename = os.path.join(self.templateDir, self.template)
self._cache = CachedFile(_filename, self._reallyLoad)
def _reallyLoad(self, path, ctx):
doc = microdom.parse(path, beExtremelyLenient=self.beExtremelyLenient)
doc = flat.precompile(doc, ctx)
if self.pattern is not None:
doc = inevow.IQ(doc).onePattern(self.pattern)
return doc
def load(self, ctx=None, preprocessors=()):
assert not preprocessors, "preprocessors not supported by htmlfile"
return self._cache.load(ctx)
class xmlstr(object):
implements(inevow.IDocFactory)
template = None
pattern = None
ignoreDocType = None
ignoreComment = None
_cache = None
def __init__(self, template=None, pattern=None, ignoreDocType=False, ignoreComment=False):
if template is not None:
self.template = template
if pattern is not None:
self.pattern = pattern
if ignoreDocType is not None:
self.ignoreDocType = ignoreDocType
if ignoreComment is not None:
self.ignoreComment = ignoreComment
def load(self, ctx=None, preprocessors=()):
"""
Get an instance, possibly cached from a previous call, of this document
"""
if self._cache is None:
doc = flatsax.parseString(self.template, self.ignoreDocType, self.ignoreComment)
for proc in preprocessors:
doc = proc(doc)
doc = flat.precompile(doc, ctx)
if self.pattern is not None:
doc = inevow.IQ(doc).onePattern(self.pattern)
self._cache = doc
return self._cache
class xmlfile(object):
implements(inevow.IDocFactory)
template = None
templateDir = None
pattern = None
ignoreDocType = False
ignoreComment = False
def __init__(self, template=None, pattern=None, templateDir=None, ignoreDocType=None, ignoreComment=None):
self._cache = {}
if template is not None:
self.template = template
if pattern is not None:
self.pattern = pattern
if templateDir is not None:
self.templateDir = templateDir
if ignoreDocType is not None:
self.ignoreDocType = ignoreDocType
if ignoreComment is not None:
self.ignoreComment = ignoreComment
if self.templateDir is not None:
self._filename = os.path.join(self.templateDir, self.template)
else:
self._filename = self.template
self._cache = {}
def load(self, ctx=None, preprocessors=()):
rendererFactoryClass = None
if ctx is not None:
r = inevow.IRendererFactory(ctx, None)
if r is not None:
rendererFactoryClass = getClass(r)
cacheKey = (self._filename, self.pattern, rendererFactoryClass)
cachedFile = self._cache.get(cacheKey)
if cachedFile is None:
cachedFile = self._cache[cacheKey] = CachedFile(self._filename, self._reallyLoad)
return cachedFile.load(ctx, preprocessors)
def _reallyLoad(self, path, ctx, preprocessors):
doc = flatsax.parse(open(self._filename), self.ignoreDocType, self.ignoreComment)
for proc in preprocessors:
doc = proc(doc)
doc = flat.precompile(doc, ctx)
if self.pattern is not None:
doc = inevow.IQ(doc).onePattern(self.pattern)
return doc
|