本文整理汇总了Python中jinja2.nodes.Name方法的典型用法代码示例。如果您正苦于以下问题:Python nodes.Name方法的具体用法?Python nodes.Name怎么用?Python nodes.Name使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jinja2.nodes
的用法示例。
在下文中一共展示了nodes.Name方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse_assign_target
# 需要导入模块: from jinja2 import nodes [as 别名]
# 或者: from jinja2.nodes import Name [as 别名]
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
示例2: _process_name
# 需要导入模块: from jinja2 import nodes [as 别名]
# 或者: from jinja2.nodes import Name [as 别名]
def _process_name(self, node, **kwargs):
"""
Processes a `Name` node. Some examples of `Name` nodes:
{{ foo }} -> 'foo' is a Name
{% if foo }} -> 'foo' is a Name
"""
with self._interpolation():
with self._python_bool_wrapper(**kwargs):
if node.name not in self.stored_names and node.ctx != 'store':
self.output.write(self.context_name)
self.output.write('.')
if node.ctx == 'store':
self.stored_names.add(node.name)
self.output.write(node.name)
示例3: test_is_method_call
# 需要导入模块: from jinja2 import nodes [as 别名]
# 或者: from jinja2.nodes import Name [as 别名]
def test_is_method_call(self):
node = self.env.parse('{{ foo() }}').find(nodes.Call)
assert is_method_call(node, 'foo') is True
assert is_method_call(node, 'bar') is False
node = self.env.parse('{{ foo }}').find(nodes.Name)
assert is_method_call(node, 'foo') is False
node = nodes.Call()
node.node = None
assert is_method_call(node, 'foo') is False
node = self.env.parse('{{ foo.bar.baz() }}').find(nodes.Call)
assert is_method_call(node, 'baz') is True
assert is_method_call(node, 'bar') is False
assert is_method_call(node, 'foo') is False
assert is_method_call(node, ('foo', 'bar', 'baz')) is True
node = self.env.parse('{{ foo["bar"]() }}').find(nodes.Call)
assert is_method_call(node, 'bar') is True
示例4: parse_assign_target
# 需要导入模块: from jinja2 import nodes [as 别名]
# 或者: from jinja2.nodes import Name [as 别名]
def parse_assign_target(self, with_tuple=True, name_only=False,
extra_end_rules=None, with_namespace=False):
"""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
`with_namespace` is enabled, a namespace assignment may be parsed.
"""
if with_namespace and self.stream.look().type == 'dot':
token = self.stream.expect('name')
next(self.stream) # dot
attr = self.stream.expect('name')
target = nodes.NSRef(token.value, attr.value, lineno=token.lineno)
elif 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
示例5: parse_primary
# 需要导入模块: from jinja2 import nodes [as 别名]
# 或者: from jinja2.nodes import Name [as 别名]
def parse_primary(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 == 'string':
next(self.stream)
buf = [token.value]
lineno = token.lineno
while self.stream.current.type == 'string':
buf.append(self.stream.current.value)
next(self.stream)
node = nodes.Const(''.join(buf), lineno=lineno)
elif token.type in ('integer', 'float'):
next(self.stream)
node = nodes.Const(token.value, lineno=token.lineno)
elif token.type == 'lparen':
next(self.stream)
node = self.parse_tuple(explicit_parentheses=True)
self.stream.expect('rparen')
elif token.type == 'lbracket':
node = self.parse_list()
elif token.type == 'lbrace':
node = self.parse_dict()
else:
self.fail("unexpected '%s'" % describe_token(token), token.lineno)
return node
示例6: make_assignment_frame
# 需要导入模块: from jinja2 import nodes [as 别名]
# 或者: from jinja2.nodes import Name [as 别名]
def make_assignment_frame(self, frame):
# toplevel assignments however go into the local namespace and
# the current template's context. We create a copy of the frame
# here and add a set so that the Name visitor can add the assigned
# names here.
if not frame.toplevel:
return frame
assignment_frame = frame.copy()
assignment_frame.toplevel_assignments = set()
return assignment_frame
示例7: parse
# 需要导入模块: from jinja2 import nodes [as 别名]
# 或者: from jinja2.nodes import Name [as 别名]
def parse(self, parser):
lineno = parser.stream.expect('name:csrf_token').lineno
call = self.call_method(
'_csrf_token',
[nodes.Name('csrf_token', 'load', lineno=lineno)],
lineno=lineno
)
return nodes.Output([nodes.MarkSafe(call)])
示例8: _parse_trans
# 需要导入模块: from jinja2 import nodes [as 别名]
# 或者: from jinja2.nodes import Name [as 别名]
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)