Python jinja2.nodes.Output() Examples

The following are 30 code examples of jinja2.nodes.Output(). 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.nodes , or try the search function .
Example #1
Source File: jinja_extensions.py    From daf-recipes with GNU General Public License v3.0 6 votes vote down vote up
def parse(self, parser):
        stream = parser.stream
        tag = stream.next()
        # get arguments
        args = []
        kwargs = []
        while not stream.current.test_any('block_end'):
            if args or kwargs:
                stream.expect('comma')
            if stream.current.test('name') and stream.look().test('assign'):
                key = nodes.Const(stream.next().value)
                stream.skip()
                value = parser.parse_expression()
                kwargs.append(nodes.Pair(key, value, lineno=key.lineno))
            else:
                args.append(parser.parse_expression())

        def make_call_node(*kw):
            return self.call_method('_call', args=[
                nodes.List(args),
                nodes.Dict(kwargs),
            ], kwargs=kw)

        return nodes.Output([make_call_node()]).set_lineno(tag.lineno) 
Example #2
Source File: extension.py    From FTDAnsible with GNU General Public License v3.0 6 votes vote down vote up
def parse(self, parser):
        def parse_arguments():
            args = [parser.parse_expression()]
            # append task filters if any
            if parser.stream.skip_if('comma'):
                args.append(parser.parse_expression())
            return args

        lineno = next(parser.stream).lineno
        tag_args = parse_arguments()
        call = self.call_method(self._include_tasks.__name__, tag_args)
        return Output([call], lineno=lineno)


# By default, `yaml` package does not preserve field order, and
# playbook tasks do not look the same as in playbooks and in docs.
# These functions create custom Loader and Dumper to preserve field order.
# Source: https://stackoverflow.com/a/21912744 
Example #3
Source File: jinja2ext.py    From statik with MIT License 6 votes vote down vote up
def parse(self, parser):
        lineno = next(parser.stream).lineno

        # get the context
        context = nodes.ContextReference()

        # get the arguments
        args = [context]
        try:
            while True:
                args.append(parser.parse_expression())
        except TemplateSyntaxError:
            pass  # no more arguments

        # get the tag_name for use in looking up callable
        self.active_tag = parser._tag_stack[-1]
        args.insert(0, nodes.Const(self.active_tag))

        # create the node
        node = self.call_method('_invoke_tag', args=args, lineno=lineno)

        return nodes.Output(
            [node],
            lineno=lineno
        ) 
Example #4
Source File: jinja2ext.py    From statik with MIT License 6 votes vote down vote up
def parse(self, parser):
        lineno = next(parser.stream).lineno

        next_token = parser.stream.look()
        # if there are parameters
        if next_token.type == "comma":
            args = [parser.parse_expression()]
            if parser.stream.skip_if('comma'):
                args.append(parser.parse_expression())
            else:
                raise TemplateSyntaxError("Missing Lorem Ipsum generator parameter: kind", lineno)

            if args[1].value not in self.GENERATORS:
                raise TemplateSyntaxError(
                    "Supported Lorem Ipsum generator kinds are: %s" % ", ".join(self.GENERATORS.keys()),
                    lineno
                )
        else:
            # if no parameters were supplied
            args = [nodes.Const(1), nodes.Const("paragraphs")]

        return nodes.Output(
            [self.call_method("_lipsum", args)],
            lineno=lineno
        ) 
Example #5
Source File: jinja2ext.py    From statik with MIT License 6 votes vote down vote up
def parse(self, parser):
        lineno = next(parser.stream).lineno

        # get the first parameter: the view name
        args = [parser.parse_expression()]

        # if there's a comma, we've also got an instance variable here
        if parser.stream.skip_if('comma'):
            args.append(parser.parse_expression())
        else:
            # no instance supplied for URL tag
            args.append(nodes.Const(None))

        return nodes.Output(
            [self.call_method('_url', args)],
            lineno=lineno
        ) 
Example #6
Source File: parser.py    From Financial-Portfolio-Flask with MIT License 5 votes vote down vote up
def parse_print(self):
        node = nodes.Output(lineno=next(self.stream).lineno)
        node.nodes = []
        while self.stream.current.type != 'block_end':
            if node.nodes:
                self.stream.expect('comma')
            node.nodes.append(self.parse_expression())
        return node 
Example #7
Source File: parser.py    From Flask-P2P with MIT License 5 votes vote down vote up
def parse_print(self):
        node = nodes.Output(lineno=next(self.stream).lineno)
        node.nodes = []
        while self.stream.current.type != 'block_end':
            if node.nodes:
                self.stream.expect('comma')
            node.nodes.append(self.parse_expression())
        return node 
Example #8
Source File: jinja2_time.py    From jinja2-time with MIT License 5 votes vote down vote up
def parse(self, parser):
        lineno = next(parser.stream).lineno

        node = parser.parse_expression()

        if parser.stream.skip_if('comma'):
            datetime_format = parser.parse_expression()
        else:
            datetime_format = nodes.Const(None)

        if isinstance(node, nodes.Add):
            call_method = self.call_method(
                '_datetime',
                [node.left, nodes.Const('+'), node.right, datetime_format],
                lineno=lineno,
            )
        elif isinstance(node, nodes.Sub):
            call_method = self.call_method(
                '_datetime',
                [node.left, nodes.Const('-'), node.right, datetime_format],
                lineno=lineno,
            )
        else:
            call_method = self.call_method(
                '_now',
                [node, datetime_format],
                lineno=lineno,
            )
        return nodes.Output([call_method], lineno=lineno) 
Example #9
Source File: parser.py    From scylla with Apache License 2.0 5 votes vote down vote up
def parse_print(self):
        node = nodes.Output(lineno=next(self.stream).lineno)
        node.nodes = []
        while self.stream.current.type != 'block_end':
            if node.nodes:
                self.stream.expect('comma')
            node.nodes.append(self.parse_expression())
        return node 
Example #10
Source File: extensions.py    From django-tex with MIT License 5 votes vote down vote up
def parse(self, parser):
        list_of_paths = getattr(settings, 'LATEX_GRAPHICSPATH', [settings.BASE_DIR])
        value = '\graphicspath{ ' + ' '.join(map(format_path_for_latex, list_of_paths)) + ' }'
        node = nodes.Output(lineno=next(parser.stream).lineno)
        node.nodes = [nodes.MarkSafe(nodes.Const(value))]
        return node 
Example #11
Source File: parser.py    From Building-Recommendation-Systems-with-Python with MIT License 5 votes vote down vote up
def parse_print(self):
        node = nodes.Output(lineno=next(self.stream).lineno)
        node.nodes = []
        while self.stream.current.type != 'block_end':
            if node.nodes:
                self.stream.expect('comma')
            node.nodes.append(self.parse_expression())
        return node 
Example #12
Source File: parser.py    From Building-Recommendation-Systems-with-Python with MIT License 5 votes vote down vote up
def parse_print(self):
        node = nodes.Output(lineno=next(self.stream).lineno)
        node.nodes = []
        while self.stream.current.type != 'block_end':
            if node.nodes:
                self.stream.expect('comma')
            node.nodes.append(self.parse_expression())
        return node 
Example #13
Source File: parser.py    From pySINDy with MIT License 5 votes vote down vote up
def parse_print(self):
        node = nodes.Output(lineno=next(self.stream).lineno)
        node.nodes = []
        while self.stream.current.type != 'block_end':
            if node.nodes:
                self.stream.expect('comma')
            node.nodes.append(self.parse_expression())
        return node 
Example #14
Source File: extensions.py    From jinja2-django-tags with MIT License 5 votes vote down vote up
def parse(self, parser):
        lineno = next(parser.stream).lineno
        token = parser.stream.expect(lexer.TOKEN_STRING)
        format_string = nodes.Const(token.value)
        call = self.call_method('_now', [format_string], lineno=lineno)

        token = parser.stream.current
        if token.test('name:as'):
            next(parser.stream)
            as_var = parser.stream.expect(lexer.TOKEN_NAME)
            as_var = nodes.Name(as_var.value, 'store', lineno=as_var.lineno)
            return nodes.Assign(as_var, call, lineno=lineno)
        else:
            return nodes.Output([call], lineno=lineno) 
Example #15
Source File: extensions.py    From jinja2-django-tags with MIT License 5 votes vote down vote up
def _parse_trans(self, parser, lineno):
        string = parser.stream.expect(lexer.TOKEN_STRING)
        string = nodes.Const(string.value, lineno=string.lineno)
        is_noop = False
        context = None
        as_var = None
        for token in iter(lambda: parser.stream.next_if(lexer.TOKEN_NAME), None):
            if token.value == 'noop' and not is_noop:
                if context is not None:
                    parser.fail("noop translation can't have context", lineno=token.lineno)
                is_noop = True
            elif token.value == 'context' and context is None:
                if is_noop:
                    parser.fail("noop translation can't have context", lineno=token.lineno)
                context = parser.stream.expect(lexer.TOKEN_STRING)
                context = nodes.Const(context.value, lineno=context.lineno)
            elif token.value == 'as' and as_var is None:
                as_var = parser.stream.expect(lexer.TOKEN_NAME)
                as_var = nodes.Name(as_var.value, 'store', lineno=as_var.lineno)
            else:
                parser.fail("expected 'noop', 'context' or 'as'", lineno=token.lineno)
        if is_noop:
            output = string
        elif context is not None:
            func = nodes.Name('pgettext', 'load', lineno=lineno)
            output = nodes.Call(func, [context, string], [], None, None, lineno=lineno)
        else:
            func = nodes.Name('gettext', 'load')
            output = nodes.Call(func, [string], [], None, None, lineno=lineno)

        if as_var is None:
            return nodes.Output([output], lineno=lineno)
        else:
            return nodes.Assign(as_var, output, lineno=lineno) 
Example #16
Source File: extensions.py    From jinja2-django-tags with MIT License 5 votes vote down vote up
def parse(self, parser):
        lineno = next(parser.stream).lineno
        token = parser.stream.expect(lexer.TOKEN_STRING)
        path = nodes.Const(token.value)
        call = self.call_method('_static', [path], lineno=lineno)

        token = parser.stream.current
        if token.test('name:as'):
            next(parser.stream)
            as_var = parser.stream.expect(lexer.TOKEN_NAME)
            as_var = nodes.Name(as_var.value, 'store', lineno=as_var.lineno)
            return nodes.Assign(as_var, call, lineno=lineno)
        else:
            return nodes.Output([call], lineno=lineno) 
Example #17
Source File: parser.py    From jbox with MIT License 5 votes vote down vote up
def parse_print(self):
        node = nodes.Output(lineno=next(self.stream).lineno)
        node.nodes = []
        while self.stream.current.type != 'block_end':
            if node.nodes:
                self.stream.expect('comma')
            node.nodes.append(self.parse_expression())
        return node 
Example #18
Source File: ext.py    From Flask-P2P with MIT License 5 votes vote down vote up
def parse(self, parser):
        return nodes.Output([self.call_method('_dump', [
            nodes.EnvironmentAttribute('sandboxed'),
            self.attr('ext_attr'),
            nodes.ImportedName(__name__ + '.importable_object'),
            nodes.ContextReference()
        ])]).set_lineno(next(parser.stream).lineno) 
Example #19
Source File: parser.py    From cadquery-freecad-module with GNU Lesser General Public License v3.0 5 votes vote down vote up
def parse_print(self):
        node = nodes.Output(lineno=next(self.stream).lineno)
        node.nodes = []
        while self.stream.current.type != 'block_end':
            if node.nodes:
                self.stream.expect('comma')
            node.nodes.append(self.parse_expression())
        return node 
Example #20
Source File: parser.py    From planespotter with MIT License 5 votes vote down vote up
def parse_print(self):
        node = nodes.Output(lineno=next(self.stream).lineno)
        node.nodes = []
        while self.stream.current.type != 'block_end':
            if node.nodes:
                self.stream.expect('comma')
            node.nodes.append(self.parse_expression())
        return node 
Example #21
Source File: parser.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def parse_print(self):
        node = nodes.Output(lineno=next(self.stream).lineno)
        node.nodes = []
        while self.stream.current.type != 'block_end':
            if node.nodes:
                self.stream.expect('comma')
            node.nodes.append(self.parse_expression())
        return node 
Example #22
Source File: parser.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def parse_print(self):
        node = nodes.Output(lineno=next(self.stream).lineno)
        node.nodes = []
        while self.stream.current.type != 'block_end':
            if node.nodes:
                self.stream.expect('comma')
            node.nodes.append(self.parse_expression())
        return node 
Example #23
Source File: parser.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def parse_print(self):
        node = nodes.Output(lineno=next(self.stream).lineno)
        node.nodes = []
        while self.stream.current.type != 'block_end':
            if node.nodes:
                self.stream.expect('comma')
            node.nodes.append(self.parse_expression())
        return node 
Example #24
Source File: parser.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def parse_print(self):
        node = nodes.Output(lineno=next(self.stream).lineno)
        node.nodes = []
        while self.stream.current.type != 'block_end':
            if node.nodes:
                self.stream.expect('comma')
            node.nodes.append(self.parse_expression())
        return node 
Example #25
Source File: parser.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def parse_print(self):
        node = nodes.Output(lineno=next(self.stream).lineno)
        node.nodes = []
        while self.stream.current.type != 'block_end':
            if node.nodes:
                self.stream.expect('comma')
            node.nodes.append(self.parse_expression())
        return node 
Example #26
Source File: jinja2ext.py    From statik with MIT License 5 votes vote down vote up
def parse(self, parser):
        lineno = next(parser.stream).lineno

        # get the first parameter: the relative URL of the asset file
        args = [parser.parse_expression()]

        return nodes.Output(
            [self.call_method('_asset', args)],
            lineno=lineno
        ) 
Example #27
Source File: parser.py    From PhonePi_SampleServer with MIT License 5 votes vote down vote up
def parse_print(self):
        node = nodes.Output(lineno=next(self.stream).lineno)
        node.nodes = []
        while self.stream.current.type != 'block_end':
            if node.nodes:
                self.stream.expect('comma')
            node.nodes.append(self.parse_expression())
        return node 
Example #28
Source File: parser.py    From syntheticmass with Apache License 2.0 5 votes vote down vote up
def parse_print(self):
        node = nodes.Output(lineno=next(self.stream).lineno)
        node.nodes = []
        while self.stream.current.type != 'block_end':
            if node.nodes:
                self.stream.expect('comma')
            node.nodes.append(self.parse_expression())
        return node 
Example #29
Source File: parser.py    From odoo13-x64 with GNU General Public License v3.0 5 votes vote down vote up
def parse_print(self):
        node = nodes.Output(lineno=next(self.stream).lineno)
        node.nodes = []
        while self.stream.current.type != 'block_end':
            if node.nodes:
                self.stream.expect('comma')
            node.nodes.append(self.parse_expression())
        return node 
Example #30
Source File: parser.py    From SA-ctf_scoreboard with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
def parse_print(self):
        node = nodes.Output(lineno=next(self.stream).lineno)
        node.nodes = []
        while self.stream.current.type != 'block_end':
            if node.nodes:
                self.stream.expect('comma')
            node.nodes.append(self.parse_expression())
        return node