This file is indexed.

/usr/lib/python3/dist-packages/kajiki/ir.py is in python3-kajiki 0.5.3-1.

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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
from itertools import chain
import re

from .util import gen_name, flattener, window
from nine import nine


def generate_python(ir):
    cur_indent = 0
    for node in flattener(ir):
        if isinstance(node, IndentNode):
            cur_indent += 4
        elif isinstance(node, DedentNode):
            cur_indent -= 4
        for line in node.py():
            yield line.indent(cur_indent)


class Node(object):
    def __init__(self):
        self.filename = '<string>'
        self.lineno = 0

    def py(self):  # pragma no cover
        return []

    def __iter__(self):
        yield self

    def line(self, text):
        return PyLine(self.filename, self.lineno, text)


class PassNode(Node):
    def py(self):
        # 'pass' would result in: TypeError: 'NoneType' object is not iterable
        yield self.line('yield ""')


class HierNode(Node):
    '''Base for nodes that contain an indented Python block (def, for, if etc.)
    '''
    def __init__(self, body):
        super(HierNode, self).__init__()
        self.body = tuple(x for x in body if x is not None)

    def body_iter(self):
        for x in optimize(flattener(map(flattener, self.body))):
            yield x

    def __iter__(self):
        yield self
        yield IndentNode()
        for x in self.body_iter():
            yield x
        yield DedentNode()


class IndentNode(Node):
    pass


class DedentNode(Node):
    pass


class TemplateNode(HierNode):
    class TemplateTail(Node):
        def py(self):
            yield self.line('template = kajiki.Template(template)')

    def __init__(self, mod_py=None, defs=None):
        super(TemplateNode, self).__init__(defs)
        if mod_py is None:
            mod_py = []
        if defs is None:
            defs = []
        self.mod_py = [x for x in mod_py if x is not None]

    def py(self):
        yield self.line('class template:')

    def __iter__(self):
        for x in flattener(self.mod_py):
            yield x
        for x in super(TemplateNode, self).__iter__():
            yield x
        yield self.TemplateTail()


class ImportNode(Node):
    def __init__(self, tpl_name, alias=None):
        super(ImportNode, self).__init__()
        self.tpl_name = tpl_name
        self.alias = alias

    def py(self):
        yield self.line(
            'local.__kj__.import_(%r, %r, self.__globals__)' % (
                self.tpl_name, self.alias))


class IncludeNode(Node):
    def __init__(self, tpl_name):
        super(IncludeNode, self).__init__()
        self.tpl_name = tpl_name

    def py(self):
        yield self.line(
            'yield local.__kj__.import_(%r, None, self.__globals__).__main__()' % (
                self.tpl_name))


class ExtendNode(Node):
    def __init__(self, tpl_name):
        super(ExtendNode, self).__init__()
        self.tpl_name = tpl_name

    def py(self):
        yield self.line(
            'yield local.__kj__.extend(%r).__main__()' % (
                self.tpl_name))


class DefNode(HierNode):
    prefix = '@kajiki.expose'

    def __init__(self, decl, *body):
        super(DefNode, self).__init__(body)
        self.decl = decl

    def py(self):
        yield self.line(self.prefix)
        yield self.line('def %s:' % (self.decl))

    def __iter__(self):
        yield self
        yield IndentNode()
        is_empty = True
        for x in self.body_iter():
            yield x
            is_empty = False
        if is_empty:  # In Python, a function without a body is a SyntaxError.
            yield PassNode()  # Prevent creation of a function without a body
        yield DedentNode()


class InnerDefNode(DefNode):
    prefix = '@__kj__.flattener.decorate'


class CallNode(HierNode):
    class CallTail(Node):
        def __init__(self, call):
            super(CallNode.CallTail, self).__init__()
            self.call = call

        def py(self):
            yield self.line('yield ' + self.call)

    def __init__(self, caller, callee, *body):
        super(CallNode, self).__init__(body)
        fname = gen_name()
        self.decl = caller.replace('$caller', fname)
        self.call = callee.replace('$caller', fname)

    def py(self):
        yield self.line('@__kj__.flattener.decorate')
        yield self.line('def %s:' % (self.decl))

    def __iter__(self):
        yield self
        yield IndentNode()
        for x in self.body_iter():
            yield x
        yield DedentNode()
        yield self.CallTail(self.call)


class ForNode(HierNode):
    def __init__(self, decl, *body):
        super(ForNode, self).__init__(body)
        self.decl = decl

    def py(self):
        yield self.line('for %s:' % (self.decl))


class WithNode(HierNode):

    assignment_pattern = re.compile(r'(?:^|;)\s*([^;=]+)=(?!=)', re.M)

    class WithTail(Node):
        def __init__(self, var_names):
            super(WithNode.WithTail, self).__init__()
            self.var_names = var_names

        def py(self):
            yield self.line('(%s,) = local.__kj__.pop_with()' %
                            (','.join(self.var_names),))
            # yield self.line('if %s == (): del %s' % (v, v))

    def __init__(self, vars, *body):
        super(WithNode, self).__init__(body)
        assignments = []
        matches = self.assignment_pattern.finditer(vars)
        for m1, m2 in window(chain(matches, [None]), 2):
            lhs = m1.group(1).strip()
            rhs = vars[m1.end():(m2.start() if m2 else len(vars))]
            assignments.append((lhs, rhs))

        self.vars = assignments
        self.var_names = [l for l, _ in assignments]

    def py(self):
        yield self.line(
            'local.__kj__.push_with(locals(), [%s])' % (
                ','.join('"%s"' % k for k in self.var_names),))
        for k, v in self.vars:
            yield self.line('%s = %s' % (k, v))

    def __iter__(self):
        yield self
        for x in self.body_iter():
            yield x
        yield self.WithTail(self.var_names)


class SwitchNode(HierNode):
    class SwitchTail(Node):
        def py(self):
            yield self.line('local.__kj__.pop_switch()')

    def __init__(self, decl, *body):
        super(SwitchNode, self).__init__(body)
        self.decl = decl

    def py(self):
        yield self.line('local.__kj__.push_switch(%s)' % self.decl)

    def __iter__(self):
        yield self
        for x in self.body_iter():
            yield x
        yield self.SwitchTail()


class CaseNode(HierNode):
    def __init__(self, decl, *body):
        super(CaseNode, self).__init__(body)
        self.decl = decl

    def py(self):
        yield self.line('if local.__kj__.case(%s):' % self.decl)


class IfNode(HierNode):
    def __init__(self, decl, *body):
        super(IfNode, self).__init__(body)
        self.decl = decl

    def py(self):
        yield self.line('if %s:' % self.decl)


class ElseNode(HierNode):
    def __init__(self, *body):
        super(ElseNode, self).__init__(body)

    def py(self):
        yield self.line('else:')


class TextNode(Node):
    '''Node that outputs Python literals.'''
    def __init__(self, text, guard=None):
        super(TextNode, self).__init__()
        self.text = text
        self.guard = guard

    def py(self):
        s = 'yield %r' % self.text
        if self.guard:
            yield self.line('if %s: %s' % (self.guard, s))
        else:
            yield self.line(s)


class TranslatableTextNode(TextNode):
    def py(self):
        text = self.text.strip()
        if text:
            s = 'yield local.__kj__.gettext(%r)' % self.text
        else:
            s = 'yield %r' % self.text
        if self.guard:
            yield self.line('if %s: %s' % (self.guard, s))
        else:
            yield self.line(s)


class ExprNode(Node):
    '''Node that contains a Python expression to be evaluated when the template
    is executed.
    '''
    def __init__(self, text, safe=False):
        super(ExprNode, self).__init__()
        self.text = text
        self.safe = safe

    def py(self):
        if self.safe:
            yield self.line('yield %s' % self.text)
        else:
            yield self.line('yield self.__kj__.escape(%s)' % self.text)


class AttrNode(HierNode):
    '''Node that renders HTML/XML attributes.'''
    class AttrTail(Node):
        def __init__(self, parent):
            super(AttrNode.AttrTail, self).__init__()
            self.p = parent

        def py(self):
            gen = self.p.genname
            x = gen_name()
            yield self.line("%s = self.__kj__.collect(%s())" % (gen, gen))
            yield self.line(
                'for %s in self.__kj__.render_attrs({%r:%s}, %r):'
                % (x, self.p.attr, gen, self.p.mode))
            yield self.line('    yield %s' % x)

    def __init__(self, attr, value, guard=None, mode='xml'):
        super(AttrNode, self).__init__(value)
        self.attr = attr
        self.guard = guard
        self.mode = mode
        self.genname = gen_name()

    def py(self):
        yield self.line('def %s():' % self.genname)

    def __iter__(self):
        if self.guard:
            new_body = IfNode(
                self.guard,
                AttrNode(self.attr, value=self.body, mode=self.mode))
            for x in new_body:
                yield x
        else:
            yield self
            yield IndentNode()
            if self.body:
                for part in self.body_iter():
                    yield part
            else:
                yield TextNode('')
            yield DedentNode()
            yield self.AttrTail(self)


class AttrsNode(Node):
    def __init__(self, attrs, guard=None, mode='xml'):
        super(AttrsNode, self).__init__()
        self.attrs = attrs
        self.guard = guard
        self.mode = mode

    def py(self):
        x = gen_name()

        def _body():
            yield self.line(
                'for %s in self.__kj__.render_attrs(%s, %r):' % (
                    x, self.attrs, self.mode))
            yield self.line('    yield %s' % x)
        if self.guard:
            yield self.line('if %s:' % self.guard)
            for l in _body():
                yield l.indent()
        else:
            for l in _body():
                yield l


class PythonNode(Node):
    def __init__(self, *body):
        super(PythonNode, self).__init__()
        self.module_level = False
        blocks = []
        for b in body:
            assert isinstance(b, TextNode)
            blocks.append(b.text)
        text = ''.join(blocks)
        if text[0] == '%':
            self.module_level = True
            text = text[1:]
        self.lines = list(self._normalize(text))

    def py(self):
        for line in self.lines:
            yield self.line(line)

    def _normalize(self, text):
        if text.startswith('#\n'):
            text = text[2:]
        prefix = None
        for line in text.splitlines():
            if prefix is None:
                rest = line.lstrip()
                prefix = line[:len(line) - len(rest)]
            assert line.startswith(prefix)
            yield line[len(prefix):]


def optimize(iter_node):
    last_node = None
    for node in iter_node:
        if (type(node) == TextNode and type(last_node) == TextNode
                and last_node.guard == node.guard):
            last_node.text += node.text
            # Erase this node by not yielding it.
            continue
        if last_node is not None:
            yield last_node
        last_node = node
    if last_node is not None:
        yield last_node


@nine
class PyLine(object):
    def __init__(self, filename, lineno, text, indent=0):
        self._filename = filename
        self._lineno = lineno
        self._text = text
        self._indent = indent

    def indent(self, sz=4):
        return PyLine(self._filename, self._lineno, self._text,
                      self._indent + sz)

    def __str__(self):
        return (' ' * self._indent) + self._text
        if self._lineno:
            return (' ' * self._indent) + self._text + '\t# %s:%d' % (
                self._filename, self._lineno)
        else:
            return (' ' * self._indent) + self._text

    def __repr__(self):
        return '%s:%s %s' % (self._filename, self._lineno, self)