Python jinja2.meta.find_undeclared_variables() Examples

The following are 14 code examples of jinja2.meta.find_undeclared_variables(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module jinja2.meta , or try the search function .
Example #1
Source File: controller.py    From ansible-api with GNU General Public License v3.0 6 votes vote down vote up
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 
Example #2
Source File: test.py    From cloudformation-cli with Apache License 2.0 6 votes vote down vote up
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 
Example #3
Source File: parser.py    From jinja2-live-parser with MIT License 5 votes vote down vote up
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 />') 
Example #4
Source File: utils.py    From intake with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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 
Example #5
Source File: utils.py    From intake with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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 
Example #6
Source File: helpers.py    From appetite with Apache License 2.0 5 votes vote down vote up
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) 
Example #7
Source File: settings.py    From burp-ui with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #8
Source File: loader.py    From rally with Apache License 2.0 5 votes vote down vote up
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) 
Example #9
Source File: cf.py    From stacks with MIT License 5 votes vote down vote up
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) 
Example #10
Source File: undercloud_config.py    From python-tripleoclient with Apache License 2.0 5 votes vote down vote up
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 
Example #11
Source File: module_logic.py    From govready-q with GNU General Public License v3.0 5 votes vote down vote up
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)) 
Example #12
Source File: param.py    From st2 with Apache License 2.0 5 votes vote down vote up
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) 
Example #13
Source File: __init__.py    From python-docx-template with GNU Lesser General Public License v2.1 5 votes vote down vote up
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) 
Example #14
Source File: template.py    From aomi with MIT License 4 votes vote down vote up
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))