本文整理汇总了Python中jinja2.nodes.Const方法的典型用法代码示例。如果您正苦于以下问题:Python nodes.Const方法的具体用法?Python nodes.Const怎么用?Python nodes.Const使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jinja2.nodes
的用法示例。
在下文中一共展示了nodes.Const方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse_subscript
# 需要导入模块: from jinja2 import nodes [as 别名]
# 或者: from jinja2.nodes import Const [as 别名]
def parse_subscript(self, node):
token = next(self.stream)
if token.type == 'dot':
attr_token = self.stream.current
next(self.stream)
if attr_token.type == 'name':
return nodes.Getattr(node, attr_token.value, 'load',
lineno=token.lineno)
elif attr_token.type != 'integer':
self.fail('expected name or number', attr_token.lineno)
arg = nodes.Const(attr_token.value, lineno=attr_token.lineno)
return nodes.Getitem(node, arg, 'load', lineno=token.lineno)
if token.type == 'lbracket':
args = []
while self.stream.current.type != 'rbracket':
if args:
self.stream.expect('comma')
args.append(self.parse_subscribed())
self.stream.expect('rbracket')
if len(args) == 1:
arg = args[0]
else:
arg = nodes.Tuple(args, 'load', lineno=token.lineno)
return nodes.Getitem(node, arg, 'load', lineno=token.lineno)
self.fail('expected subscript expression', self.lineno)
示例2: parse
# 需要导入模块: from jinja2 import nodes [as 别名]
# 或者: from jinja2.nodes import Const [as 别名]
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)
示例3: parse_primary
# 需要导入模块: from jinja2 import nodes [as 别名]
# 或者: from jinja2.nodes import Const [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
示例4: parse
# 需要导入模块: from jinja2 import nodes [as 别名]
# 或者: from jinja2.nodes import Const [as 别名]
def parse(self, parser):
tag = parser.stream.current.value
lineno = next(parser.stream).lineno
args, kwargs = self.parse_args(parser)
default_cache_key = (None if parser.filename is None
else '%s:%d' % (parser.filename, lineno))
kwargs.append(Keyword('default_cache_key', Const(default_cache_key),
lineno=lineno))
body = parser.parse_statements(['name:end' + tag], drop_needle=True)
return CallBlock(self.call_method('cache', args, kwargs),
[], [], body).set_lineno(lineno)
示例5: _parse_trans
# 需要导入模块: from jinja2 import nodes [as 别名]
# 或者: from jinja2.nodes import Const [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)
示例6: parse
# 需要导入模块: from jinja2 import nodes [as 别名]
# 或者: from jinja2.nodes import Const [as 别名]
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)
示例7: parse_expression
# 需要导入模块: from jinja2 import nodes [as 别名]
# 或者: from jinja2.nodes import Const [as 别名]
def parse_expression(parser):
# Due to how the jinja2 parser works, it treats "foo" "bar" as a single
# string literal as it is the case in python.
# But the url tag in django supports multiple string arguments, e.g.
# "{% url 'my_view' 'arg1' 'arg2' %}".
# That's why we have to check if it's a string literal first.
token = parser.stream.current
if token.test(lexer.TOKEN_STRING):
expr = nodes.Const(force_text(token.value), lineno=token.lineno)
next(parser.stream)
else:
expr = parser.parse_expression(False)
return expr