Exemple #1
0
def parse(template, context, _locals, timed, cache, repeat):
    # use of extensions to inject _locals
    _sandbox.extensions = _locals
    try:
        if timed and cache:
            t = timeit.Timer('tpl.render()', 'from __main__ import Template\ntpl = Template("{0}")\ncontext={1}'.format(template, str(context)))
            print '%.2f ms %s' % (1000 * t.timeit(100)/100, template)
        elif timed:
            t = timeit.Timer('Template("{0}").render()'.format(template), 'from __main__ import Template')
            print '%.2f ms %s' % (1000 * t.timeit(repeat)/repeat, template)
        else:
            t = Template(template)
            print '<!DOCTYPE html>\n'+etree.tostring(etree.fromstring(t.render(**context)), pretty_print=True)
    except Exception as e:
        print 'Exception rendering ', template
        print e
Exemple #2
0
def parse(template, context, _locals, timed, cache, repeat):
    # use of extensions to inject _locals
    _sandbox.extensions = _locals
    try:
        if timed and cache:
            t = timeit.Timer(
                'tpl.render()',
                'from __main__ import Template\ntpl = Template("{0}")\ncontext={1}'
                .format(template, str(context)))
            print '%.2f ms %s' % (1000 * t.timeit(100) / 100, template)
        elif timed:
            t = timeit.Timer('Template("{0}").render()'.format(template),
                             'from __main__ import Template')
            print '%.2f ms %s' % (1000 * t.timeit(repeat) / repeat, template)
        else:
            t = Template(template)
            print(t.render(**context))
            #print '<!DOCTYPE html>\n'+etree.tostring(etree.fromstring(t.render(**context)), pretty_print=True)
    except Exception as e:
        print 'Exception rendering ', template
        print e
Exemple #3
0
 def test_py_formatter(self):
     parsed, expected = self.t['py_formatter']
     parsed = Template(parsed).render()
     self.assertEqual(parsed.strip(), expected.strip())
Exemple #4
0
 def test_basic_multilinetext(self):
     parsed, expected = self.t['basic_multilinetext']
     parsed = Template(parsed).render()
     self.assertEqual(parsed.strip(), expected.strip())
Exemple #5
0
 def test_py_mixed_content(self):
     parsed, expected = self.t['py_mixed_content']
     parsed = Template(parsed).render()
     self.assertEqual(parsed.strip(), expected.strip())
Exemple #6
0
 def test_py_include(self):
     parsed, expected = self.t['py_include']
     parsed = Template(parsed).render()
     self.assertEqual(parsed.strip(), expected.strip())
Exemple #7
0
 def test_py_block_default(self):
     parsed, expected = self.t['py_block_default']
     parsed = Template(parsed).render()
     self.assertEqual(parsed.strip(), expected.strip())
Exemple #8
0
 def test_basic_tag_hashes(self):
     parsed, expected = self.t['basic_tag_hashes']
     parsed = Template(parsed).render()
     self.assertEqual(parsed.strip(), expected.strip())
Exemple #9
0
 def test_basic_ending_colon(self):
     parsed, expected = self.t['basic_ending_colon']
     parsed = Template(parsed).render()
     self.assertEqual(parsed.strip(), expected.strip())
Exemple #10
0
 def test_py_looping(self):
     parsed, expected = self.t['py_looping']
     parsed = Template(parsed).render()
     self.assertEqual(parsed.strip(), expected.strip())
Exemple #11
0
        if r is None:
            return ''

        py_id = id(self.py_q)
        py_parse = py_locals['__py_parse__']
        doc_py(r, py_id, py_parse)
        return r.tostring()


if __name__ == '__main__':
    import sys
    import codecs
    from time import time
    _f = sys.argv[1]
    #print parse(_f)
    t = Template(_f)
    if '-p' in sys.argv:
        def run():
            for x in range(2000):
                t.render()
        import cProfile, pstats
        prof = cProfile.Profile()
        prof.run('run()')
        stats = pstats.Stats(prof)
        stats.strip_dirs()
        stats.sort_stats('cumulative')
        stats.print_stats(25)
    else:
        print t.render()

Exemple #12
0
 def test_basic_variable_indent(self):
     parsed, expected = self.t['basic_variable_indent']
     parsed = Template(parsed).render()
     self.assertEqual(parsed.strip(), expected.strip())
Exemple #13
0
 def test_basic_tag_hashes(self):
     parsed, expected = self.t['basic_tag_hashes']
     parsed = Template(parsed).render()
     self.assertEqual(parsed.strip(), expected.strip())
Exemple #14
0
 def test_py_include(self):
     parsed, expected = self.t['py_include']
     parsed = Template(parsed).render()
     self.assertEqual(parsed.strip(), expected.strip())
Exemple #15
0
 def test_py_mixed_content(self):
     parsed, expected = self.t['py_mixed_content']
     parsed = Template(parsed).render()
     self.assertEqual(parsed.strip(), expected.strip())
Exemple #16
0
def parse(t, **kwargs):
    return Template(t).render(**kwargs)
Exemple #17
0
 def test_py_newline_var(self):
     parsed, expected = self.t['py_newline_var']
     parsed = Template(parsed).render()
     self.assertEqual(parsed.strip(), expected.strip())
Exemple #18
0
 def test_basic_multilinetext(self):
     parsed, expected = self.t['basic_multilinetext']
     parsed = Template(parsed).render()
     self.assertEqual(parsed.strip(), expected.strip())
Exemple #19
0
 def test_py_raise(self):
     parsed, expected = self.t['py_raise']
     #TODO fix this test for 2.6
     self.assertRaises(Exception, Template(parsed).render)
Exemple #20
0
 def test_basic_variable_indent(self):
     parsed, expected = self.t['basic_variable_indent']
     parsed = Template(parsed).render()
     self.assertEqual(parsed.strip(), expected.strip())
Exemple #21
0
 def test_py_block_default(self):
     parsed, expected = self.t['py_block_default']
     parsed = Template(parsed).render()
     self.assertEqual(parsed.strip(), expected.strip())
Exemple #22
0
 def test_py_complex3(self):
     parsed, expected = self.t['py_complex3']
     parsed = Template(parsed).render()
     self.assertEqual(parsed.strip(), expected.strip())
Exemple #23
0
 def test_py_complex3(self):
     parsed, expected = self.t['py_complex3']
     parsed = Template(parsed).render()
     self.assertEqual(parsed.strip(), expected.strip())
Exemple #24
0
 def test_py_looping(self):
     parsed, expected = self.t['py_looping']
     parsed = Template(parsed).render()
     self.assertEqual(parsed.strip(), expected.strip())
Exemple #25
0
 def test_py_ending_colon(self):
     parsed, expected = self.t['py_ending_colon']
     parsed = Template(parsed).render()
     self.assertEqual(parsed.strip(), expected.strip())
Exemple #26
0
 def test_py_nested_for(self):
     parsed, expected = self.t['py_nested_for']
     parsed = Template(parsed).render()
     self.assertEqual(parsed.strip(), expected.strip())
Exemple #27
0
        if r is None:
            return ''

        py_id = id(self.py_q)
        py_parse = py_locals['__py_parse__']
        doc_py(r, py_id, py_parse)
        return r.tostring()


if __name__ == '__main__':
    import sys
    import codecs
    from time import time
    _f = sys.argv[1]
    #print parse(_f)
    t = Template(_f)
    if '-p' in sys.argv:

        def run():
            for x in range(2000):
                t.render()

        import cProfile, pstats
        prof = cProfile.Profile()
        prof.run('run()')
        stats = pstats.Stats(prof)
        stats.strip_dirs()
        stats.sort_stats('cumulative')
        stats.print_stats(25)
    else:
        print t.render()