kajiki 0.2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,24 @@
1
+ Metadata-Version: 1.0
2
+ Name: Kajiki
3
+ Version: 0.2.2
4
+ Summary: Really fast well-formed xml templates
5
+ Home-page: http://bitbucket.org/rick446/fastpt
6
+ Author: Rick Copeland
7
+ Author-email: rick446@usa.net
8
+ License: MIT
9
+ Description: Are you tired of the slow performance of Genshi? But
10
+ you still long for the assurance that your output is well-formed that you
11
+ miss from all those other templating engines? Do you wish you had Jinja's
12
+ blocks with Genshi's syntax? Then look no further, Kajiki is for you!
13
+ Kajiki quickly compiles Genshi-like syntax to *real python bytecode*
14
+ that renders with blazing-fast speed! Don't delay! Pick up your
15
+ copy of Kajiki today!
16
+ Keywords: template xml
17
+ Platform: UNKNOWN
18
+ Classifier: Development Status :: 4 - Beta
19
+ Classifier: Environment :: Web Environment
20
+ Classifier: Intended Audience :: Developers
21
+ Classifier: License :: OSI Approved :: MIT License
22
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
23
+ Classifier: Topic :: Text Processing :: Markup :: HTML
24
+ Classifier: Topic :: Text Processing :: Markup :: XML
@@ -0,0 +1,24 @@
1
+ setup.cfg
2
+ setup.py
3
+ Kajiki.egg-info/PKG-INFO
4
+ Kajiki.egg-info/SOURCES.txt
5
+ Kajiki.egg-info/dependency_links.txt
6
+ Kajiki.egg-info/entry_points.txt
7
+ Kajiki.egg-info/not-zip-safe
8
+ Kajiki.egg-info/top_level.txt
9
+ kajiki/__init__.py
10
+ kajiki/html_utils.py
11
+ kajiki/ir.py
12
+ kajiki/lnotab.py
13
+ kajiki/loader.py
14
+ kajiki/markup_template.py
15
+ kajiki/template.py
16
+ kajiki/text.py
17
+ kajiki/util.py
18
+ kajiki/xml_template.py
19
+ kajiki/tests/__init__.py
20
+ kajiki/tests/test_ir.py
21
+ kajiki/tests/test_runtime.py
22
+ kajiki/tests/test_text.py
23
+ kajiki/tests/test_xml.py
24
+ kajiki/tests/data/__init__.py
@@ -0,0 +1,3 @@
1
+
2
+ # -*- Entry points: -*-
3
+
@@ -0,0 +1 @@
1
+
@@ -0,0 +1 @@
1
+ kajiki
Kajiki-0.2.2/PKG-INFO ADDED
@@ -0,0 +1,24 @@
1
+ Metadata-Version: 1.0
2
+ Name: Kajiki
3
+ Version: 0.2.2
4
+ Summary: Really fast well-formed xml templates
5
+ Home-page: http://bitbucket.org/rick446/fastpt
6
+ Author: Rick Copeland
7
+ Author-email: rick446@usa.net
8
+ License: MIT
9
+ Description: Are you tired of the slow performance of Genshi? But
10
+ you still long for the assurance that your output is well-formed that you
11
+ miss from all those other templating engines? Do you wish you had Jinja's
12
+ blocks with Genshi's syntax? Then look no further, Kajiki is for you!
13
+ Kajiki quickly compiles Genshi-like syntax to *real python bytecode*
14
+ that renders with blazing-fast speed! Don't delay! Pick up your
15
+ copy of Kajiki today!
16
+ Keywords: template xml
17
+ Platform: UNKNOWN
18
+ Classifier: Development Status :: 4 - Beta
19
+ Classifier: Environment :: Web Environment
20
+ Classifier: Intended Audience :: Developers
21
+ Classifier: License :: OSI Approved :: MIT License
22
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
23
+ Classifier: Topic :: Text Processing :: Markup :: HTML
24
+ Classifier: Topic :: Text Processing :: Markup :: XML
@@ -0,0 +1,5 @@
1
+ from .util import expose, flattener
2
+ from .template import Template
3
+ from .loader import MockLoader, FileLoader, PackageLoader
4
+ from .text import TextTemplate
5
+ from .xml_template import XMLTemplate
@@ -0,0 +1,38 @@
1
+ HTML_EMPTY_ATTRS=set([
2
+ 'checked',
3
+ 'disabled',
4
+ 'readonly',
5
+ 'multiple',
6
+ 'selected',
7
+ 'nohref',
8
+ 'ismap',
9
+ 'declare',
10
+ 'defer',
11
+ ])
12
+ HTML_OPTIONAL_END_TAGS=set([
13
+ 'area',
14
+ 'base',
15
+ 'body',
16
+ 'br',
17
+ 'col',
18
+ 'colgroup',
19
+ 'dd',
20
+ 'dt',
21
+ 'head',
22
+ 'hr',
23
+ 'html',
24
+ 'img',
25
+ 'input',
26
+ 'li',
27
+ 'link',
28
+ 'meta',
29
+ 'option',
30
+ 'p',
31
+ 'param',
32
+ 'tbody',
33
+ 'td',
34
+ 'tfoot',
35
+ 'th',
36
+ 'thead',
37
+ 'tr',
38
+ ])
@@ -0,0 +1,308 @@
1
+ from .util import gen_name
2
+
3
+ class Node(object):
4
+
5
+ def __init__(self):
6
+ self.filename = '<string>'
7
+ self.lineno = 0
8
+
9
+ def py(self): # pragma no cover
10
+ return []
11
+
12
+ def line(self, text):
13
+ return PyLine(self.filename, self.lineno, text)
14
+
15
+ class TemplateNode(Node):
16
+
17
+ def __init__(self, mod_py=None, defs=None):
18
+ super(TemplateNode, self).__init__()
19
+ if mod_py is None: mod_py = []
20
+ if defs is None: defs = []
21
+ self.mod_py = [ x for x in mod_py if x is not None ]
22
+ self.defs = [ x for x in defs if x is not None ]
23
+
24
+ def py(self):
25
+ for block in self.mod_py:
26
+ for line in block.py():
27
+ yield line
28
+ yield self.line('@kajiki.Template')
29
+ yield self.line('class template:')
30
+ for child in self.defs:
31
+ for line in child.py():
32
+ yield line.indent()
33
+
34
+ class ImportNode(Node):
35
+
36
+ def __init__(self, tpl_name, alias=None):
37
+ super(ImportNode, self).__init__()
38
+ self.tpl_name = tpl_name
39
+ self.alias = alias
40
+
41
+ def py(self):
42
+ yield self.line(
43
+ 'local.__kj__.import_(%r, %r, globals())' % (
44
+ self.tpl_name, self.alias))
45
+
46
+ class IncludeNode(Node):
47
+
48
+ def __init__(self, tpl_name):
49
+ super(IncludeNode, self).__init__()
50
+ self.tpl_name = tpl_name
51
+
52
+ def py(self):
53
+ yield self.line(
54
+ 'yield local.__kj__.import_(%r, None, {}).__main__()' % (
55
+ self.tpl_name))
56
+
57
+ class ExtendNode(Node):
58
+
59
+ def __init__(self, tpl_name):
60
+ super(ExtendNode, self).__init__()
61
+ self.tpl_name = tpl_name
62
+
63
+ def py(self):
64
+ yield self.line(
65
+ 'yield local.__kj__.extend(%r).__main__()' % (
66
+ self.tpl_name))
67
+
68
+ class DefNode(Node):
69
+ prefix = '@kajiki.expose'
70
+
71
+ def __init__(self, decl, *body):
72
+ super(DefNode, self).__init__()
73
+ self.decl = decl
74
+ self.body = tuple(x for x in body if x is not None)
75
+
76
+ def py(self):
77
+ yield self.line(self.prefix)
78
+ yield self.line('def %s:' % (self.decl))
79
+ for child in optimize(self.body):
80
+ for line in child.py():
81
+ yield line.indent()
82
+
83
+ class InnerDefNode(DefNode):
84
+ prefix='@__kj__.flattener.decorate'
85
+
86
+ class CallNode(Node):
87
+
88
+ def __init__(self, caller, callee, *body):
89
+ super(CallNode, self).__init__()
90
+ fname = gen_name()
91
+ self.decl = caller.replace('$caller', fname)
92
+ self.call = callee.replace('$caller', fname)
93
+ self.body = tuple(x for x in body if x is not None)
94
+
95
+ def py(self):
96
+ yield self.line('@__kj__.flattener.decorate')
97
+ yield self.line('def %s:' % (self.decl))
98
+ for child in optimize(self.body):
99
+ for line in child.py():
100
+ yield line.indent()
101
+ yield self.line('yield ' + self.call)
102
+
103
+ class ForNode(Node):
104
+
105
+ def __init__(self, decl, *body):
106
+ super(ForNode, self).__init__()
107
+ self.decl = decl
108
+ self.body = tuple(x for x in body if x is not None)
109
+
110
+ def py(self):
111
+ yield self.line('for %s:' % (self.decl))
112
+ for child in optimize(self.body):
113
+ for line in child.py():
114
+ yield line.indent()
115
+
116
+ class SwitchNode(Node):
117
+
118
+ def __init__(self, decl, *body):
119
+ super(SwitchNode, self).__init__()
120
+ self.decl = decl
121
+ self.body = tuple(x for x in body if x is not None)
122
+
123
+ def py(self):
124
+ yield self.line('local.__kj__.push_switch(%s)' % self.decl)
125
+ for child in optimize(self.body):
126
+ for line in child.py():
127
+ yield line
128
+ yield self.line('local.__kj__.pop_switch()')
129
+
130
+ class CaseNode(Node):
131
+
132
+ def __init__(self, decl, *body):
133
+ super(CaseNode, self).__init__()
134
+ self.decl = decl
135
+ self.body = tuple(x for x in body if x is not None)
136
+
137
+ def py(self):
138
+ yield self.line('if local.__kj__.case(%s):' % self.decl)
139
+ for child in optimize(self.body):
140
+ for line in child.py():
141
+ yield line.indent()
142
+
143
+ class IfNode(Node):
144
+
145
+ def __init__(self, decl, *body):
146
+ super(IfNode, self).__init__()
147
+ self.decl = decl
148
+ self.body = tuple(x for x in body if x is not None)
149
+
150
+ def py(self):
151
+ yield self.line('if %s:' % self.decl)
152
+ for child in optimize(self.body):
153
+ for line in child.py():
154
+ yield line.indent()
155
+
156
+ class ElseNode(Node):
157
+
158
+ def __init__(self, *body):
159
+ super(ElseNode, self).__init__()
160
+ self.body = tuple(x for x in body if x is not None)
161
+
162
+ def py(self):
163
+ yield self.line('else:')
164
+ for child in optimize(self.body):
165
+ for line in child.py():
166
+ yield line.indent()
167
+
168
+ class TextNode(Node):
169
+
170
+ def __init__(self, text, guard=None):
171
+ super(TextNode, self).__init__()
172
+ self.text = text
173
+ self.guard = guard
174
+
175
+ def py(self):
176
+ s = 'yield %r' % self.text
177
+ if self.guard:
178
+ yield self.line('if %s: %s' % (self.guard, s))
179
+ else:
180
+ yield self.line(s)
181
+
182
+ class ExprNode(Node):
183
+
184
+ def __init__(self, text):
185
+ super(ExprNode, self).__init__()
186
+ self.text = text
187
+
188
+ def py(self):
189
+ yield self.line('yield self.__kj__.escape(%s)' % self.text)
190
+
191
+ class PassNode(Node):
192
+
193
+ def py(self):
194
+ yield self.line('pass')
195
+
196
+ class AttrNode(Node):
197
+
198
+ def __init__(self, attr, value, guard=None, mode='xml'):
199
+ super(AttrNode, self).__init__()
200
+ self.attr = attr
201
+ self.value = value
202
+ self.guard = guard
203
+ self.mode = mode
204
+
205
+ def py(self):
206
+ x,gen = gen_name(), gen_name()
207
+ def _body():
208
+ yield self.line('def %s():' % gen)
209
+ for part in self.value:
210
+ for line in part.py():
211
+ yield line.indent()
212
+ yield self.line("%s = ''.join(%s())" % (gen,gen))
213
+ yield self.line(
214
+ 'for %s in self.__kj__.render_attrs({%r:%s}, %r):'
215
+ % (x, self.attr, gen, self.mode))
216
+ yield self.line(' yield %s' % x)
217
+ if self.guard:
218
+ yield self.line('if %s:' % self.guard)
219
+ for l in _body():
220
+ yield l.indent()
221
+ else:
222
+ for l in _body(): yield l
223
+
224
+ class AttrsNode(Node):
225
+
226
+ def __init__(self, attrs, guard=None, mode='xml'):
227
+ super(AttrsNode, self).__init__()
228
+ self.attrs = attrs
229
+ self.guard = guard
230
+ self.mode = mode
231
+
232
+ def py(self):
233
+ x = gen_name()
234
+ def _body():
235
+ yield self.line(
236
+ 'for %s in self.__kj__.render_attrs(%s, %r):' % (x, self.attrs, self.mode))
237
+ yield self.line(' yield %s' % x)
238
+ if self.guard:
239
+ yield self.line('if %s:' % self.guard)
240
+ for l in _body():
241
+ yield l.indent()
242
+ else:
243
+ for l in _body(): yield l
244
+
245
+ class PythonNode(Node):
246
+
247
+ def __init__(self, *body):
248
+ super(PythonNode, self).__init__()
249
+ self.module_level = False
250
+ blocks = []
251
+ for b in body:
252
+ assert isinstance(b, TextNode)
253
+ blocks.append(b.text)
254
+ text = ''.join(blocks)
255
+ if text[0] == '%':
256
+ self.module_level = True
257
+ text = text[1:]
258
+ self.lines = list(self._normalize(text))
259
+
260
+ def py(self):
261
+ for line in self.lines:
262
+ yield self.line(line)
263
+
264
+ def _normalize(self, text):
265
+ if text.startswith('#\n'):
266
+ text = text[2:]
267
+ prefix = None
268
+ for line in text.splitlines():
269
+ if prefix is None:
270
+ rest = line.lstrip()
271
+ prefix = line[:len(line)-len(rest)]
272
+ assert line.startswith(prefix)
273
+ yield line[len(prefix):]
274
+
275
+ def optimize(iter_node):
276
+ last_node = None
277
+ for node in iter_node:
278
+ if type(node) == TextNode:
279
+ if (type(last_node) == TextNode
280
+ and last_node.guard == node.guard):
281
+ last_node.text += node.text
282
+ else:
283
+ if last_node is not None: yield last_node
284
+ last_node = node
285
+ else:
286
+ if last_node is not None: yield last_node
287
+ last_node = node
288
+ if last_node is not None:
289
+ yield last_node
290
+
291
+ class PyLine(object):
292
+
293
+ def __init__(self, filename, lineno, text, indent=0):
294
+ self._filename = filename
295
+ self._lineno = lineno
296
+ self._text = text
297
+ self._indent = indent
298
+
299
+ def indent(self, sz=4):
300
+ return PyLine(self._filename, self._lineno, self._text, self._indent + sz)
301
+
302
+ def __str__(self):
303
+ return (' ' * self._indent) + self._text
304
+ if self._lineno:
305
+ return (' ' * self._indent) + self._text + '\t# %s:%d' % (self._filename, self._lineno)
306
+ else:
307
+ return (' ' * self._indent) + self._text
308
+
@@ -0,0 +1,91 @@
1
+ # Comment copied from Python/compile.c:
2
+ #
3
+ # All about a_lnotab.
4
+ #
5
+ # c_lnotab is an array of unsigned bytes disguised as a Python string.
6
+ # It is used to map bytecode offsets to source code line #s (when needed
7
+ # for tracebacks).
8
+ #
9
+ # The array is conceptually a list of
10
+ # (bytecode offset increment, line number increment)
11
+ # pairs. The details are important and delicate, best illustrated by example:
12
+ #
13
+ # byte code offset source code line number
14
+ # 0 1
15
+ # 6 2
16
+ # 50 7
17
+ # 350 307
18
+ # 361 308
19
+ #
20
+ # The first trick is that these numbers aren't stored, only the increments
21
+ # from one row to the next (this doesn't really work, but it's a start):
22
+ #
23
+ # 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
24
+ #
25
+ # The second trick is that an unsigned byte can't hold negative values, or
26
+ # values larger than 255, so (a) there's a deep assumption that byte code
27
+ # offsets and their corresponding line #s both increase monotonically, and (b)
28
+ # if at least one column jumps by more than 255 from one row to the next, more
29
+ # than one pair is written to the table. In case #b, there's no way to know
30
+ # from looking at the table later how many were written. That's the delicate
31
+ # part. A user of c_lnotab desiring to find the source line number
32
+ # corresponding to a bytecode address A should do something like this
33
+ #
34
+ # lineno = addr = 0
35
+ # for addr_incr, line_incr in c_lnotab:
36
+ # addr += addr_incr
37
+ # if addr > A:
38
+ # return lineno
39
+ # lineno += line_incr
40
+ #
41
+ # In order for this to work, when the addr field increments by more than 255,
42
+ # the line # increment in each pair generated must be 0 until the remaining addr
43
+ # increment is < 256. So, in the example above, assemble_lnotab (it used
44
+ # to be called com_set_lineno) should not (as was actually done until 2.2)
45
+ # expand 300, 300 to 255, 255, 45, 45,
46
+ # but to 255, 0, 45, 255, 0, 45.
47
+ #
48
+
49
+ def lnotab(pairs, first_lineno=0):
50
+ """Yields byte integers representing the pairs of integers passed in."""
51
+ assert first_lineno <= pairs[0][1]
52
+ cur_byte, cur_line = 0, first_lineno
53
+ for byte_off, line_off in pairs:
54
+ byte_delta = byte_off - cur_byte
55
+ line_delta = line_off - cur_line
56
+ assert byte_delta >= 0
57
+ assert line_delta >= 0
58
+ while byte_delta > 255:
59
+ yield 255 # byte
60
+ yield 0 # line
61
+ byte_delta -= 255
62
+ yield byte_delta
63
+ while line_delta > 255:
64
+ yield 255 # line
65
+ yield 0 # byte
66
+ line_delta -= 255
67
+ yield line_delta
68
+ cur_byte, cur_line = byte_off, line_off
69
+
70
+ def lnotab_string(pairs, first_lineno=0):
71
+ return "".join(chr(b) for b in lnotab(pairs, first_lineno))
72
+
73
+ def byte_pairs(lnotab):
74
+ """Yield pairs of integers from a string."""
75
+ for i in range(0, len(lnotab), 2):
76
+ yield ord(lnotab[i]), ord(lnotab[i+1])
77
+
78
+ def lnotab_numbers(lnotab, first_lineno=0):
79
+ """Yields the byte, line offset pairs from a packed lnotab string."""
80
+
81
+ last_line = None
82
+ cur_byte, cur_line = 0, first_lineno
83
+ for byte_delta, line_delta in byte_pairs(lnotab):
84
+ if byte_delta:
85
+ if cur_line != last_line:
86
+ yield cur_byte, cur_line
87
+ last_line = cur_line
88
+ cur_byte += byte_delta
89
+ cur_line += line_delta
90
+ if cur_line != last_line:
91
+ yield cur_byte, cur_line
@@ -0,0 +1,78 @@
1
+ import os
2
+ import pkg_resources
3
+
4
+ class Loader(object):
5
+
6
+ def __init__(self):
7
+ self.modules = {}
8
+
9
+ def import_(self, name):
10
+ mod = self.modules.get(name)
11
+ if mod: return mod
12
+ mod = self._load(name)
13
+ mod.loader = self
14
+ self.modules[name] = mod
15
+ return mod
16
+
17
+ def default_alias_for(self, name):
18
+ return os.path.splitext(os.path.basename(name))[0]
19
+
20
+ load=import_
21
+
22
+ class MockLoader(Loader):
23
+
24
+ def __init__(self, modules):
25
+ super(MockLoader, self).__init__()
26
+ self.modules.update(modules)
27
+ for v in self.modules.itervalues():
28
+ v.loader = self
29
+
30
+ class FileLoader(Loader):
31
+
32
+ def __init__(self, base, reload=True):
33
+ super(FileLoader, self).__init__()
34
+ from kajiki import XMLTemplate, TextTemplate
35
+ self.base = base
36
+ self.extension_map = dict(
37
+ txt=TextTemplate,
38
+ xml=XMLTemplate,
39
+ html=lambda *a,**kw:XMLTemplate(*a, mode='html', **kw),
40
+ html5=lambda *a,**kw:XMLTemplate(*a, mode='html5', **kw))
41
+ self._timestamps = {}
42
+ self._reload = reload
43
+
44
+ def _filename(self, name):
45
+ return os.path.join(self.base, name)
46
+
47
+ def import_(self, name):
48
+ filename = self._filename(name)
49
+ if self._reload and name in self.modules and os.stat(filename).st_mtime > self._timestamps.get(name, 0):
50
+ del self.modules[name]
51
+ return super(FileLoader, self).import_(name)
52
+
53
+ def _load(self, name):
54
+ filename = self._filename(name)
55
+ ext = os.path.splitext(filename)[1][1:]
56
+ self._timestamps[name] = os.stat(filename).st_mtime
57
+ source = open(filename, 'rb').read()
58
+ return self.extension_map[ext](source=source, filename=filename)
59
+
60
+ class PackageLoader(FileLoader):
61
+
62
+ def __init__(self, reload=True):
63
+ super(PackageLoader, self).__init__(None, reload)
64
+
65
+ def _filename(self, name):
66
+ package, module = name.rsplit('.', 1)
67
+ found = dict()
68
+ for fn in pkg_resources.resource_listdir(package, '.'):
69
+ if fn == name: return pkg_resources.resource_filename(package, fn)
70
+ root, ext = os.path.splitext(fn)
71
+ if root == module:
72
+ found[ext] = fn
73
+ for ext in ('.xml', '.html', '.html5', '.txt'):
74
+ if ext in found:
75
+ return pkg_resources.resource_filename(package, found[ext])
76
+ else:
77
+ raise IOError, 'Unknown template %r' % name
78
+
@@ -0,0 +1,17 @@
1
+ DIRECTIVES=[
2
+ ('def','function'),
3
+ ('call','function'),
4
+ ('case', 'value'),
5
+ ('else',''),
6
+ ('for','each'),
7
+ ('if','test'),
8
+ ('switch', 'test'),
9
+ ('with', 'vars'),
10
+ ('replace', 'value'),
11
+ ('block', 'name'),
12
+ ('extends', 'href'),]
13
+ QDIRECTIVES = [
14
+ ('py:%s' % (k,), v)
15
+ for k,v in DIRECTIVES ]
16
+ QDIRECTIVES_DICT = dict(QDIRECTIVES)
17
+