st2common/st2common/util/jinja.py
def render_values(mapping=None, context=None, allow_undefined=False):
    """
    Render an incoming mapping using context provided in context using Jinja2. Returns a dict
    containing rendered mapping.

    :param mapping: Input as a dictionary of key value pairs.
    :type mapping: ``dict``

    :param context: Context to be used for dictionary.
    :type context: ``dict``

    :rtype: ``dict``
    """

    if not context or not mapping:
        return mapping

    # Add in special __context variable that provides an easy way to get access to entire context.
    # This mean __context is a reserve key word although backwards compat is preserved by making
    # sure that real context is updated later and therefore will override the __context value.
    super_context = {}
    super_context['__context'] = context
    super_context.update(context)

    env = get_jinja_environment(allow_undefined=allow_undefined)
    rendered_mapping = {}
    for k, v in six.iteritems(mapping):
        # jinja2 works with string so transform list and dict to strings.
        reverse_json_dumps = False
        if isinstance(v, dict) or isinstance(v, list):
            v = json.dumps(v)
            reverse_json_dumps = True
        else:
            # Special case for text type to handle unicode
            if isinstance(v, six.string_types):
                v = to_unicode(v)
            else:
                # Other types (e.g. boolean, etc.)
                v = str(v)

        try:
            LOG.info('Rendering string %s. Super context=%s', v, super_context)
            rendered_v = env.from_string(v).render(super_context)
        except Exception as e:
            # Attach key and value which failed the rendering
            e.key = k
            e.value = v
            raise e

        # no change therefore no templatization so pick params from original to retain
        # original type
        if rendered_v == v:
            rendered_mapping[k] = mapping[k]
            continue
        if reverse_json_dumps:
            rendered_v = json.loads(rendered_v)
        rendered_mapping[k] = rendered_v
    LOG.info('Mapping: %s, rendered_mapping: %s, context: %s', mapping, rendered_mapping, context)
    return rendered_mapping

    Render an incoming mapping using context provided in context using Jinja2. Returns a dict
    containing rendered mapping.

    :param mapping: Input as a dictionary of key value pairs.
    :type mapping: ``dict``

    :param context: Context to be used for dictionary.
    :type context: ``dict``

    :rtype: ``dict``
    
Render an incoming mapping using context provided in context using Jinja2. Returns a dict
containing rendered mapping.
Render an incoming mapping using context provided in context using Jinja2 . Returns a dict containing rendered mapping .
def render_values(mapping=None, context=None, allow_undefined=False):
    if not context or not mapping:
        return mapping
    super_context = {}
    super_context['__context'] = context
    super_context.update(context)
    env = get_jinja_environment(allow_undefined=allow_undefined)
    rendered_mapping = {}
    for k, v in six.iteritems(mapping):
        reverse_json_dumps = False
        if isinstance(v, dict) or isinstance(v, list):
            v = json.dumps(v)
            reverse_json_dumps = True
        else:
            if isinstance(v, six.string_types):
                v = to_unicode(v)
            else:
                v = str(v)
        try:
            LOG.info('Rendering string %s. Super context=%s', v, super_context)
            rendered_v = env.from_string(v).render(super_context)
        except Exception as e:
            e.key = k
            e.value = v
            raise e
        if rendered_v == v:
            rendered_mapping[k] = mapping[k]
            continue
        if reverse_json_dumps:
            rendered_v = json.loads(rendered_v)
        rendered_mapping[k] = rendered_v
    LOG.info('Mapping: %s, rendered_mapping: %s, context: %s', mapping, rendered_mapping, context)
    return rendered_mapping
def render_values ( mapping = None , context = None , allow_undefined = False ) : if not context or not mapping : return mapping super_context = { } super_context [ '__context' ] = context super_context . update ( context ) env = get_jinja_environment ( allow_undefined = allow_undefined ) rendered_mapping = { } for k , v in six . iteritems ( mapping ) : reverse_json_dumps = False if isinstance ( v , dict ) or isinstance ( v , list ) : v = json . dumps ( v ) reverse_json_dumps = True else : if isinstance ( v , six . string_types ) : v = to_unicode ( v ) else : v = str ( v ) try : LOG . info ( 'Rendering string %s. Super context=%s' , v , super_context ) rendered_v = env . from_string ( v ) . render ( super_context ) except Exception as e : e . key = k e . value = v raise e if rendered_v == v : rendered_mapping [ k ] = mapping [ k ] continue if reverse_json_dumps : rendered_v = json . loads ( rendered_v ) rendered_mapping [ k ] = rendered_v LOG . info ( 'Mapping: %s, rendered_mapping: %s, context: %s' , mapping , rendered_mapping , context ) return rendered_mapping
Render an incoming mapping using context provided in context using Jinja2.
Render an incoming mapping using context provided in context using Jinja2 .
"""
    Render an incoming mapping using context provided in context using Jinja2. Returns a dict
    containing rendered mapping.

    :param mapping: Input as a dictionary of key value pairs.
    :type mapping: ``dict``

    :param context: Context to be used for dictionary.
    :type context: ``dict``

    :rtype: ``dict``
    """ # Add in special __context variable that provides an easy way to get access to entire context. # This mean __context is a reserve key word although backwards compat is preserved by making # sure that real context is updated later and therefore will override the __context value. # jinja2 works with string so transform list and dict to strings. # Special case for text type to handle unicode # Other types (e.g. boolean, etc.) # Attach key and value which failed the rendering # no change therefore no templatization so pick params from original to retain # original type
None
``dict``
mapping
Input as a dictionary of key value pairs.
Input as a dictionary of key value pairs .
context
Context to be used for dictionary.
Context to be used for dictionary .
allow_undefined