本文整理匯總了Python中jinja2.nodes方法的典型用法代碼示例。如果您正苦於以下問題:Python jinja2.nodes方法的具體用法?Python jinja2.nodes怎麽用?Python jinja2.nodes使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類jinja2
的用法示例。
在下文中一共展示了jinja2.nodes方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: parse_include_block
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import nodes [as 別名]
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)
示例2: template_config_variables
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import nodes [as 別名]
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)
示例3: grok_filter_name
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import nodes [as 別名]
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
示例4: grok_for_node
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import nodes [as 別名]
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
示例5: grok_if_node
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import nodes [as 別名]
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)
示例6: grok_vars
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import nodes [as 別名]
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
示例7: render_jinja_node
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import nodes [as 別名]
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()