本文整理汇总了Python中jinja2.meta.find_undeclared_variables方法的典型用法代码示例。如果您正苦于以下问题:Python meta.find_undeclared_variables方法的具体用法?Python meta.find_undeclared_variables怎么用?Python meta.find_undeclared_variables使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jinja2.meta
的用法示例。
在下文中一共展示了meta.find_undeclared_variables方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse_vars
# 需要导入模块: from jinja2 import meta [as 别名]
# 或者: from jinja2.meta import find_undeclared_variables [as 别名]
def parse_vars(self, file_path):
contents = FileReadWrite.read_file(file_path)
env = Environment()
ignore_vars = []
yaml_stream = yaml.load(contents)
for yaml_item in yaml_stream:
if isinstance(yaml_item, dict) and yaml_item.get('vars_files', []) and len(yaml_item['vars_files']) > 0:
for vf in yaml_item['vars_files']:
tmp_file = Config.get('dir_playbook') + vf
if os.path.isfile(tmp_file):
with open(tmp_file, 'r') as fc:
tmp_vars = yaml.load(fc)
if isinstance(tmp_vars, dict):
ignore_vars += tmp_vars.keys()
if len(ignore_vars) > 0:
Tool.LOGGER.info("skip vars: " + ",".join(ignore_vars))
ast = env.parse(contents)
var = list(meta.find_undeclared_variables(ast))
var = list(set(var).difference(set(ignore_vars)))
return var
示例2: render_jinja
# 需要导入模块: from jinja2 import meta [as 别名]
# 或者: from jinja2.meta import find_undeclared_variables [as 别名]
def render_jinja(overrides_string, region_name, endpoint_url):
env = Environment(autoescape=True)
parsed_content = env.parse(overrides_string)
variables = meta.find_undeclared_variables(parsed_content)
if variables:
exports = get_cloudformation_exports(region_name, endpoint_url)
invalid_exports = variables - exports.keys()
if len(invalid_exports) > 0:
invalid_exports_message = (
"Override file invalid: %s are not valid cloudformation exports."
+ "No Overrides will be applied"
)
LOG.warning(invalid_exports_message, invalid_exports)
return empty_override()
overrides_template = Template(overrides_string)
to_return = json.loads(overrides_template.render(exports))
else:
to_return = json.loads(overrides_string)
return to_return
示例3: _expand
# 需要导入模块: from jinja2 import meta [as 别名]
# 或者: from jinja2.meta import find_undeclared_variables [as 别名]
def _expand(p, context, all_vars, client, getenv, getshell):
if isinstance(p, dict):
return {k: _expand(v, context, all_vars, client, getenv, getshell)
for k, v in p.items()}
elif isinstance(p, (list, tuple, set)):
return type(p)(_expand(v, context, all_vars, client, getenv, getshell)
for v in p)
elif isinstance(p, str):
jinja = Environment()
if getenv and not client:
jinja.globals['env'] = _j_getenv
else:
jinja.globals['env'] = lambda x: _j_passthrough(x, funcname='env')
if getenv and client:
jinja.globals['client_env'] = _j_getenv
else:
jinja.globals['client_env'] = lambda x: _j_passthrough(x, funcname='client_env')
if getshell and not client:
jinja.globals['shell'] = _j_getshell
else:
jinja.globals['shell'] = lambda x: _j_passthrough(x, funcname='shell')
if getshell and client:
jinja.globals['client_shell'] = _j_getshell
else:
jinja.globals['client_shell'] = lambda x: _j_passthrough(x, funcname='client_shell')
ast = jinja.parse(p)
all_vars -= meta.find_undeclared_variables(ast)
return jinja.from_string(p).render(context)
else:
# no expansion
return p
示例4: _has_catalog_dir
# 需要导入模块: from jinja2 import meta [as 别名]
# 或者: from jinja2.meta import find_undeclared_variables [as 别名]
def _has_catalog_dir(args):
"""Check is any value in args dict needs CATALOG_DIR variable"""
env = Environment()
for k, arg in args.items():
parsed_content = env.parse(arg)
vars = meta.find_undeclared_variables(parsed_content)
if 'CATALOG_DIR' in vars:
return True
return False
示例5: get_template_vars
# 需要导入模块: from jinja2 import meta [as 别名]
# 或者: from jinja2.meta import find_undeclared_variables [as 别名]
def get_template_vars(content):
"""Get all templated keys from jinja2 templated string"""
env = Environment(autoescape=True)
parsed_content = env.parse(content)
return meta.find_undeclared_variables(parsed_content)
示例6: get
# 需要导入模块: from jinja2 import meta [as 别名]
# 或者: from jinja2.meta import find_undeclared_variables [as 别名]
def get(self, server=None):
"""Returns a list of clients"""
parser = bui.client.get_parser(agent=server)
res = parser.list_static_templates()
env = Environment()
for obj in res:
ast = env.parse(obj['content'])
obj['variables'] = [x for x in meta.find_undeclared_variables(ast) if x not in TEMPLATE_EXCLUDES]
return jsonify(result=res)
示例7: register_all_params_in_track
# 需要导入模块: from jinja2 import meta [as 别名]
# 或者: from jinja2.meta import find_undeclared_variables [as 别名]
def register_all_params_in_track(assembled_source, complete_track_params=None):
j2env = jinja2.Environment()
# we don't need the following j2 filters/macros but we define them anyway to prevent parsing failures
internal_template_vars = default_internal_template_vars()
for macro_type in internal_template_vars:
for env_global_key, env_global_value in internal_template_vars[macro_type].items():
getattr(j2env, macro_type)[env_global_key] = env_global_value
ast = j2env.parse(assembled_source)
j2_variables = meta.find_undeclared_variables(ast)
if complete_track_params:
complete_track_params.populate_track_defined_params(j2_variables)
示例8: _check_missing_vars
# 需要导入模块: from jinja2 import meta [as 别名]
# 或者: from jinja2.meta import find_undeclared_variables [as 别名]
def _check_missing_vars(env, tpl_file, config):
"""Check for missing variables in a template string"""
tpl_str = tpl_file.read()
ast = env.parse(tpl_str)
required_properties = meta.find_undeclared_variables(ast)
missing_properties = required_properties - config.keys() - set(dir(builtins))
if len(missing_properties) > 0:
print('Required properties not set: {}'.format(','.join(missing_properties)))
sys.exit(1)
示例9: _get_unknown_instack_tags
# 需要导入模块: from jinja2 import meta [as 别名]
# 或者: from jinja2.meta import find_undeclared_variables [as 别名]
def _get_unknown_instack_tags(env, src):
found_tags = set(meta.find_undeclared_variables(env.parse(src)))
known_tags = set(INSTACK_NETCONF_MAPPING.keys())
if found_tags <= known_tags:
return (', ').join(found_tags - known_tags)
else:
return None
示例10: get_jinja2_template_vars
# 需要导入模块: from jinja2 import meta [as 别名]
# 或者: from jinja2.meta import find_undeclared_variables [as 别名]
def get_jinja2_template_vars(template):
from jinja2 import meta, TemplateSyntaxError
env = SandboxedEnvironment()
try:
expr = env.parse(template)
except TemplateSyntaxError as e:
raise Exception("expression {} is invalid: {}".format(template, e))
return set(meta.find_undeclared_variables(expr))
示例11: _process
# 需要导入模块: from jinja2 import meta [as 别名]
# 或者: from jinja2.meta import find_undeclared_variables [as 别名]
def _process(G, name, value):
'''
Determines whether parameter is a template or a value. Adds graph nodes and edges accordingly.
'''
# Jinja defaults to ascii parser in python 2.x unless you set utf-8 support on per module level
# Instead we're just assuming every string to be a unicode string
if isinstance(value, str):
value = to_unicode(value)
complex_value_str = None
if isinstance(value, list) or isinstance(value, dict):
complex_value_str = str(value)
is_jinja_expr = (
jinja_utils.is_jinja_expression(value) or jinja_utils.is_jinja_expression(
complex_value_str
)
)
if is_jinja_expr:
G.add_node(name, template=value)
template_ast = ENV.parse(value)
LOG.debug('Template ast: %s', template_ast)
# Dependencies of the node represent jinja variables used in the template
# We're connecting nodes with an edge for every depencency to traverse them
# in the right order and also make sure that we don't have missing or cyclic
# dependencies upfront.
dependencies = meta.find_undeclared_variables(template_ast)
LOG.debug('Dependencies: %s', dependencies)
if dependencies:
for dependency in dependencies:
G.add_edge(dependency, name)
else:
G.add_node(name, value=value)
示例12: get_undeclared_template_variables
# 需要导入模块: from jinja2 import meta [as 别名]
# 或者: from jinja2.meta import find_undeclared_variables [as 别名]
def get_undeclared_template_variables(self, jinja_env=None):
xml = self.get_xml()
xml = self.patch_xml(xml)
for uri in [self.HEADER_URI, self.FOOTER_URI]:
for relKey, _xml in self.get_headers_footers_xml(uri):
xml += self.patch_xml(_xml)
if jinja_env:
env = jinja_env
else:
env = Environment()
parse_content = env.parse(xml)
return meta.find_undeclared_variables(parse_content)
示例13: convert
# 需要导入模块: from jinja2 import meta [as 别名]
# 或者: from jinja2.meta import find_undeclared_variables [as 别名]
def convert():
jinja2_env = Environment()
# Load custom filters
custom_filters = get_custom_filters()
app.logger.debug('Add the following customer filters to Jinja environment: %s' % ', '.join(custom_filters.keys()))
jinja2_env.filters.update(custom_filters)
# Load the template
try:
jinja2_tpl = jinja2_env.from_string(request.form['template'])
except (exceptions.TemplateSyntaxError, exceptions.TemplateError) as e:
return "Syntax error in jinja2 template: {0}".format(e)
dummy_values = [ 'Lorem', 'Ipsum', 'Amet', 'Elit', 'Expositum',
'Dissimile', 'Superiori', 'Laboro', 'Torquate', 'sunt',
]
values = {}
if bool(int(request.form['dummyvalues'])):
# List template variables (introspection)
vars_to_fill = meta.find_undeclared_variables(jinja2_env.parse(request.form['template']))
for v in vars_to_fill:
values[v] = choice(dummy_values)
else:
# Check JSON for errors
if request.form['input_type'] == "json":
try:
values = json.loads(request.form['values'])
except ValueError as e:
return "Value error in JSON: {0}".format(e)
# Check YAML for errors
elif request.form['input_type'] == "yaml":
try:
values = yaml.load(request.form['values'])
except (ValueError, yaml.parser.ParserError, TypeError) as e:
return "Value error in YAML: {0}".format(e)
else:
return "Undefined input_type: {0}".format(request.form['input_type'])
# If ve have empty var array or other errors we need to catch it and show
try:
rendered_jinja2_tpl = jinja2_tpl.render(values)
except (exceptions.TemplateRuntimeError, ValueError, TypeError) as e:
return "Error in your values input filed: {0}".format(e)
if bool(int(request.form['showwhitespaces'])):
# Replace whitespaces with a visible character (will be grayed with javascript)
rendered_jinja2_tpl = rendered_jinja2_tpl.replace(' ', u'•')
return escape(rendered_jinja2_tpl).replace('\n', '<br />')
示例14: render
# 需要导入模块: from jinja2 import meta [as 别名]
# 或者: from jinja2.meta import find_undeclared_variables [as 别名]
def render(filename, obj):
"""Render a template, maybe mixing in extra variables"""
template_path = abspath(filename)
env = jinja_env(template_path)
template_base = os.path.basename(template_path)
try:
parsed_content = env.parse(env
.loader
.get_source(env, template_base))
template_vars = meta.find_undeclared_variables(parsed_content)
if template_vars:
missing_vars(template_vars, parsed_content, obj)
LOG.debug("rendering %s with %s vars",
template_path, len(template_vars))
return env \
.get_template(template_base) \
.render(**obj)
except jinja2.exceptions.TemplateSyntaxError as exception:
template_trace = traceback.format_tb(sys.exc_info()[2])
# Different error context depending on whether it is the
# pre-render variable scan or not
if exception.filename:
template_line = template_trace[len(template_trace) - 1]
raise aomi_excep.Validation("Bad template %s %s" %
(template_line,
str(exception)))
template_str = ''
if isinstance(exception.source, tuple):
# PyLint seems confused about whether or not this is a tuple
# pylint: disable=locally-disabled, unsubscriptable-object
template_str = "Embedded Template\n%s" % exception.source[0]
raise aomi_excep.Validation("Bad template %s" % str(exception),
source=template_str)
except jinja2.exceptions.UndefinedError as exception:
template_traces = [x.strip()
for x in traceback.format_tb(sys.exc_info()[2])
if 'template code' in x]
raise aomi_excep.Validation("Missing template variable %s" %
' '.join(template_traces))