我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用jinja2.nodes.Name()。
def parse_assign_target(self, with_tuple=True, name_only=False, extra_end_rules=None): """Parse an assignment target. As Jinja2 allows assignments to tuples, this function can parse all allowed assignment targets. Per default assignments to tuples are parsed, that can be disable however by setting `with_tuple` to `False`. If only assignments to names are wanted `name_only` can be set to `True`. The `extra_end_rules` parameter is forwarded to the tuple parsing function. """ if name_only: token = self.stream.expect('name') target = nodes.Name(token.value, 'store', lineno=token.lineno) else: if with_tuple: target = self.parse_tuple(simplified=True, extra_end_rules=extra_end_rules) else: target = self.parse_primary() target.set_ctx('store') if not target.can_assign(): self.fail('can\'t assign to %r' % target.__class__. __name__.lower(), target.lineno) return target
def parse(self, parser): # the first token is the token that started the tag. In our case # we only listen to ``'webpack'`` so this will be a name token with # `webpack` as value. We get the line number so that we can give # that line number to the nodes we create by hand. lineno = six.next(parser.stream).lineno ctx_ref = nodes.ContextReference() # Parse a single expression that is the 'bundle' or 'config:bundle' args = [ctx_ref, parser.parse_expression()] # if there is a comma, the user provided an 'extensions' arg if parser.stream.skip_if('comma'): args.append(parser.parse_expression()) else: args.append(nodes.Const(None)) # now we parse the body of the cache block up to `endwebpack` and # drop the needle (which would always be `endwebpack` in that case) body = parser.parse_statements(['name:endwebpack'], drop_needle=True) call_args = [nodes.Name('ASSET', 'param')] return nodes.CallBlock(self.call_method('_get_graph', args), call_args, [], body).set_lineno(lineno)
def parse_compare(self): token = self.stream.current if token.type == 'name': if token.value in ('true', 'false', 'True', 'False'): node = nodes.Const(token.value in ('true', 'True'), lineno=token.lineno) elif token.value in ('none', 'None'): node = nodes.Const(None, lineno=token.lineno) else: node = nodes.Name(token.value, 'load', lineno=token.lineno) next(self.stream) elif token.type == 'lparen': next(self.stream) node = self.parse_expression() self.stream.expect('rparen') else: self.fail("unexpected token '%s'" % (token,), token.lineno) return node
def eval_condition(self, condition): # exceptions are handled by the caller parser = BooleanParser(env, condition, state='variable') expr = parser.parse_expression() if not parser.stream.eos: raise ValueError('chunk after expression') def eval_node(node): if isinstance(node, nodes.CondExpr): if eval_node(node.test): return eval_node(node.expr1) else: return eval_node(node.expr2) elif isinstance(node, nodes.And): return eval_node(node.left) and eval_node(node.right) elif isinstance(node, nodes.Or): return eval_node(node.left) or eval_node(node.right) elif isinstance(node, nodes.Not): return not eval_node(node.node) elif isinstance(node, nodes.Name): return self.tags.get(node.name, False) else: raise ValueError('invalid node, check parsing') return eval_node(expr)
def parse(self, parser): lineno = next(parser.stream).lineno parts = [parser.stream.expect('name').value] while parser.stream.current.type != 'block_end': parser.stream.expect('dot') parts.append(parser.stream.expect('name').value) body = parser.parse_statements(['name:endeditable'], drop_needle=True) call = self.call_method( '_editable_loader', [nodes.Name(parts[-2], 'load'), nodes.Const(parts[-1])]) output_nodes = [nodes.Output([ nodes.MarkSafe(nodes.TemplateData('<div class="editable-container">')), nodes.MarkSafe(nodes.TemplateData('<div class="editable-content">'))]) ] output_nodes.extend(body) output_nodes.extend([ nodes.Output([nodes.MarkSafe(nodes.TemplateData('</div>'))]), nodes.Output([nodes.MarkSafe(call)]), nodes.Output([nodes.MarkSafe(nodes.TemplateData('</div>'))]) ]) block_name = '%s_%s_%d' %(parts[-2], parts[-1], random.randint(0, 500)) return nodes.Block(block_name, output_nodes, True)
def parse(self, parser): lineno = next(parser.stream).lineno parts = [parser.stream.expect('name').value] while parser.stream.current.type != 'block_end': parser.stream.expect('dot') parts.append(parser.stream.expect('name').value) body = parser.parse_statements(['name:endeditable'], drop_needle=True) call = self.call_method( '_editable_loader', [nodes.Name(parts[0], 'load'), nodes.Const(parts[1:]), nodes.Const(not body)]) output_nodes = [ nodes.Output([nodes.MarkSafe(call)]) ] output_nodes.extend(body) output_nodes.extend([ nodes.Output([nodes.MarkSafe(nodes.TemplateData('</span>'))]), ]) block_name = '%s_%s_%d' %(parts[-2], parts[-1], random.randint(0, 500)) return nodes.Block(block_name, output_nodes, True)