From 7ea592b69a9d020594a44fe9150b7a9cb267ffdc Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 13 May 2020 22:03:42 -0400 Subject: [PATCH] Switch testing to use jinja internals --- .gitignore | 4 +- test_tatsu.py | 31 ++----------- test_template.jinja | 108 -------------------------------------------- 3 files changed, 8 insertions(+), 135 deletions(-) delete mode 100644 test_template.jinja diff --git a/.gitignore b/.gitignore index 914d146d..4ce04422 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,8 @@ venv-*/ htmlcov .pytest_cache/ /.vscode/ + tatsu_jinja.json tatsu_jinja.py -parsed_jinja.py \ No newline at end of file +parsed_jinja.py +test_tatsu.jinja \ No newline at end of file diff --git a/test_tatsu.py b/test_tatsu.py index 3a054aa0..3c40eb1c 100644 --- a/test_tatsu.py +++ b/test_tatsu.py @@ -1,54 +1,33 @@ from datetime import datetime -from tatsu.exceptions import FailedSemantics -from tatsu.util import asjson -import json import pprint -import tatsu -import sys -from new_parser import parse_template from jinja2.environment import Environment +from jinja2.parser import Parser with open('grammar.ebnf', 'r') as tatsu_grammar: with open('test_template.jinja', 'r') as test_template: template_string = test_template.read() - grammar_start = datetime.now() - - grammar = tatsu.compile(tatsu_grammar.read()) - - grammar_end = datetime.now() - - parse_start = datetime.now() - - ast = grammar.parse(template_string, whitespace='', parseinfo=True, semantics=JinjaSemantics()) - - parse_end = datetime.now() - - with open('tatsu_jinja.json', 'w') as tatsu_ast_file: - json.dump(asjson(ast), tatsu_ast_file, indent=2) + env = Environment(line_statement_prefix='#', line_comment_prefix='##') + parser = Parser(env, template_string) new_parse_start = datetime.now() - new_ast = parse_template(ast) + new_ast = parser.parse() new_parse_end = datetime.now() with open('tatsu_jinja.py', 'w') as new_ast_file: pprint.pprint(new_ast, indent=2, stream=new_ast_file) - env = Environment(line_statement_prefix='#', line_comment_prefix='##') - jinja_parse_start = datetime.now() - jinja_ast = env.parse(template_string) + jinja_ast = parser.parse_old() jinja_parse_end = datetime.now() with open('parsed_jinja.py', 'w') as jinja_ast_file: pprint.pprint(jinja_ast, indent=2, stream=jinja_ast_file) - print("Grammar", grammar_end - grammar_start, file=sys.stderr) - print("Parser", parse_end - parse_start, file=sys.stderr) print("New Parser", new_parse_end - new_parse_start, file=sys.stderr) print("Jinja Parser", jinja_parse_end - jinja_parse_start, file=sys.stderr) diff --git a/test_template.jinja b/test_template.jinja deleted file mode 100644 index b8b21c53..00000000 --- a/test_template.jinja +++ /dev/null @@ -1,108 +0,0 @@ -{% from 'forms.html' import input as input_field, textarea %} -{{ dict_var['single']["double"].dot |test("first" ,2_000, named=3.14)|filter | lastFilter}} -{% with a='', b=a.attribute %}...{% endwith %} -{% for item in dict_var.values() %} -
  • {% block loop_item %}{{ item }}{% endblock %}
  • -{% endfor %} -{# comment contents -across lines #} -{% raw %} - {% block %}fake content{{ fake vars }} -{% endraw %} -{% if False %} -{{ '{{' }} -{% endif %} -{% for href, caption in [('index.html', 'Index'), ('about.html', 'About'), - ('downloads.html', 'Downloads')] %} -
  • {{ caption }}
  • -{% endfor %} -{% set navigation = [('index.html', 'Index'), ('about.html', 'About')] %} -{% set key, value = call_something() %} -{% for value in values %} - {% if loop.previtem is defined and value > loop.previtem %} - The value just increased! - {% endif %} - {{ value }} - {% if loop.nextitem is defined and loop.nextitem > value %} - The value will increase even more! - {% endif %} -{% endfor %} -{%- macro textarea(name, value='', rows=10, cols=40) -%} - -{%- endmacro -%} -{# comment contents -across lines #} - -{% set reply | wordwrap %} - You wrote: - {{ message }} -{% endset %} - -{% autoescape true %} - Autoescaping is active within this block -{% endautoescape %} - -{% autoescape false %} - Autoescaping is inactive within this block -{% endautoescape %} -{% if foo.attribute is sameas false %} - the foo attribute really is the `False` singleton -{% endif %} - -... - -{{ "[{}]".format(page.title) if page.title }} -{% extends layout_template if layout_template is defined else 'master.html' %} -{{ "Hello " ~ name ~ "!" }} -{{ 1 in [1, 2, 3] }} -{{ foo is not bar }} -{{ not (foo and bar) }} -{{ foo not in bar }} -{% from 'forms.html' import input with context %} -{% include 'header.html' without context %} -{% import 'forms.html' as forms %} -{% include ['special_sidebar.html', 'sidebar.html'] ignore missing %} -{% include "sidebar.html" ignore missing %} -{% include "sidebar.html" ignore missing with context %} -{% include "sidebar.html" ignore missing without context %} -{% for item in seq %} - {{ item }} -{% else %} - did not iterate -{% endfor %} -{% call(user) dump_users(list_of_user) %} -
    -
    Realname
    -
    {{ user.realname|e }}
    -
    Description
    -
    {{ user.description }}
    -
    -{% endcall %} -{% if kenny.sick %} - Kenny is sick. -{% elif kenny.dead %} - You killed Kenny! You bastard!!! -{% else %} - Kenny looks okay --- so far -{% endif %} -{% for user in users if not user.hidden %} -
  • {{ user.username|e }}
  • -{% endfor %} -{% for item in seq %} -
  • {% block loop_item scoped %}{{ item }}{% endblock %}
  • -{% endfor %} -{% if loop.index is divisibleby 3 %}{% endif %} -{% if loop.index is divisibleby(3) %}{% endif %} \ No newline at end of file -- 2.47.3