Python jinja2.nodes() Examples

The following are 7 code examples of jinja2.nodes(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module jinja2 , or try the search function .
Example #1
Source File: jinja2tags.py    From wagtail with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def parse_include_block(self, parser):
        lineno = next(parser.stream).lineno

        args = [parser.parse_expression()]

        with_context = True
        if parser.stream.current.test_any('name:with', 'name:without') and parser.stream.look().test('name:context'):
            with_context = next(parser.stream).value == 'with'
            parser.stream.skip()

        if with_context:
            args.append(jinja2.nodes.ContextReference())
        else:
            # Actually we can just skip else branch because context arg default to None
            args.append(jinja2.nodes.Const(None))

        node = self.call_method('_include_block', args, lineno=lineno)
        return jinja2.nodes.Output([node], lineno=lineno) 
Example #2
Source File: jinja.py    From qutebrowser with GNU General Public License v3.0 5 votes vote down vote up
def template_config_variables(template: str) -> typing.FrozenSet[str]:
    """Return the config variables used in the template."""
    unvisted_nodes = [environment.parse(template)]
    result = set()  # type: typing.Set[str]
    while unvisted_nodes:
        node = unvisted_nodes.pop()
        if not isinstance(node, jinja2.nodes.Getattr):
            unvisted_nodes.extend(node.iter_child_nodes())
            continue

        # List of attribute names in reverse order.
        # For example it's ['ab', 'c', 'd'] for 'conf.d.c.ab'.
        attrlist = []  # type: typing.List[str]
        while isinstance(node, jinja2.nodes.Getattr):
            attrlist.append(node.attr)  # type: ignore[attr-defined]
            node = node.node  # type: ignore[attr-defined]

        if isinstance(node, jinja2.nodes.Name):
            if node.name == 'conf':  # type: ignore[attr-defined]
                result.add('.'.join(reversed(attrlist)))
            # otherwise, the node is a Name node so it doesn't have any
            # child nodes
        else:
            unvisted_nodes.append(node)

    from qutebrowser.config import config
    for option in result:
        config.instance.ensure_has_opt(option)

    return frozenset(result) 
Example #3
Source File: template.py    From aomi with MIT License 5 votes vote down vote up
def grok_filter_name(element):
    """Extracts the name, which may be embedded, for a Jinja2
    filter node"""
    e_name = None
    if element.name == 'default':
        if isinstance(element.node, jinja2.nodes.Getattr):
            e_name = element.node.node.name
        else:
            e_name = element.node.name

    return e_name 
Example #4
Source File: template.py    From aomi with MIT License 5 votes vote down vote up
def grok_for_node(element, default_vars):
    """Properly parses a For loop element"""
    if isinstance(element.iter, jinja2.nodes.Filter):
        if element.iter.name == 'default' \
           and element.iter.node.name not in default_vars:
            default_vars.append(element.iter.node.name)

        default_vars = default_vars + grok_vars(element)

    return default_vars 
Example #5
Source File: template.py    From aomi with MIT License 5 votes vote down vote up
def grok_if_node(element, default_vars):
    """Properly parses a If element"""
    if isinstance(element.test, jinja2.nodes.Filter) and \
       element.test.name == 'default':
        default_vars.append(element.test.node.name)

    return default_vars + grok_vars(element) 
Example #6
Source File: template.py    From aomi with MIT License 5 votes vote down vote up
def grok_vars(elements):
    """Returns a list of vars for which the value is being appropriately set
    This currently includes the default filter, for-based iterators,
    and the explicit use of set"""
    default_vars = []
    iterbody = None
    if hasattr(elements, 'body'):
        iterbody = elements.body
    elif hasattr(elements, 'nodes'):
        iterbody = elements.nodes

    for element in iterbody:
        if isinstance(element, jinja2.nodes.Output):
            default_vars = default_vars + grok_vars(element)
        elif isinstance(element, jinja2.nodes.Filter):
            e_name = grok_filter_name(element)
            if e_name not in default_vars:
                default_vars.append(e_name)
        elif isinstance(element, jinja2.nodes.For):
            default_vars = grok_for_node(element, default_vars)
        elif isinstance(element, jinja2.nodes.If):
            default_vars = grok_if_node(element, default_vars)
        elif isinstance(element, jinja2.nodes.Assign):
            default_vars.append(element.target.name)
        elif isinstance(element, jinja2.nodes.FromImport):
            for from_var in element.names:
                default_vars.append(from_var)

    return default_vars 
Example #7
Source File: compress.py    From canvas with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def render_jinja_node(node, context, env):
    template_node = jinja2.nodes.Template([node])
    code = env.compile(template_node)
    template = jinja2.environment.Template.from_code(env, code, context)
    return template.render()