GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — develop ( 35542f...45798c )
by Plexxi
12:38 queued 06:20
created

CustomFilters   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 94
rs 10
c 0
b 0
f 0
wmc 20

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get_jinja_environment() 0 17 3
A get_filters() 0 21 1
A use_none() 0 5 2
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
import json
17
import six
18
19
import jinja2
20
21
from st2common.jinja.filters import data
22
from st2common.jinja.filters import regex
23
from st2common.jinja.filters import time
24
from st2common.jinja.filters import version
25
26
27
__all__ = [
28
    'get_jinja_environment',
29
    'render_values',
30
    'is_jinja_expression'
31
]
32
33
# Magic string to which None type is serialized when using use_none filter
34
NONE_MAGIC_VALUE = '%*****__%NONE%__*****%'
35
36
JINJA_EXPRESSIONS_START_MARKERS = [
37
    '{{',
38
    '{%'
39
]
40
41
42
def use_none(value):
43
    if value is None:
44
        return NONE_MAGIC_VALUE
45
46
    return value
47
48
49
def get_filters():
50
    return {
51
        'to_json_string': data.to_json_string,
52
        'to_yaml_string': data.to_yaml_string,
53
54
        'regex_match': regex.regex_match,
55
        'regex_replace': regex.regex_replace,
56
        'regex_search': regex.regex_search,
57
58
        'to_human_time_from_seconds': time.to_human_time_from_seconds,
59
60
        'version_compare': version.version_compare,
61
        'version_more_than': version.version_more_than,
62
        'version_less_than': version.version_less_than,
63
        'version_equal': version.version_equal,
64
        'version_match': version.version_match,
65
        'version_bump_major': version.version_bump_major,
66
        'version_bump_minor': version.version_bump_minor,
67
        'version_bump_patch': version.version_bump_patch,
68
        'version_strip_patch': version.version_strip_patch,
69
        'use_none': use_none
70
    }
71
72
73
def get_jinja_environment(allow_undefined=False, trim_blocks=True, lstrip_blocks=True):
74
    '''
75
    jinja2.Environment object that is setup with right behaviors and custom filters.
76
77
    :param strict_undefined: If should allow undefined variables in templates
78
    :type strict_undefined: ``bool``
79
80
    '''
81
    undefined = jinja2.Undefined if allow_undefined else jinja2.StrictUndefined
82
    env = jinja2.Environment(  # nosec
83
        undefined=undefined,
84
        trim_blocks=trim_blocks,
85
        lstrip_blocks=lstrip_blocks
86
    )
87
    env.filters.update(get_filters())
88
    env.tests['in'] = lambda item, list: item in list
89
    return env
90
91
92
def render_values(mapping=None, context=None, allow_undefined=False):
93
    """
94
    Render an incoming mapping using context provided in context using Jinja2. Returns a dict
95
    containing rendered mapping.
96
97
    :param mapping: Input as a dictionary of key value pairs.
98
    :type mapping: ``dict``
99
100
    :param context: Context to be used for dictionary.
101
    :type context: ``dict``
102
103
    :rtype: ``dict``
104
    """
105
106
    if not context or not mapping:
107
        return mapping
108
109
    # Add in special __context variable that provides an easy way to get access to entire context.
110
    # This mean __context is a reserve key word although backwards compat is preserved by making
111
    # sure that real context is updated later and therefore will override the __context value.
112
    super_context = {}
113
    super_context['__context'] = context
114
    super_context.update(context)
115
116
    env = get_jinja_environment(allow_undefined=allow_undefined)
117
    rendered_mapping = {}
118
    for k, v in six.iteritems(mapping):
119
        # jinja2 works with string so transform list and dict to strings.
120
        reverse_json_dumps = False
121
        if isinstance(v, dict) or isinstance(v, list):
122
            v = json.dumps(v)
123
            reverse_json_dumps = True
124
        else:
125
            v = str(v)
126
127
        try:
128
            rendered_v = env.from_string(v).render(super_context)
129
        except Exception as e:
130
            # Attach key and value which failed the rendering
131
            e.key = k
132
            e.value = v
133
            raise e
134
135
        # no change therefore no templatization so pick params from original to retain
136
        # original type
137
        if rendered_v == v:
138
            rendered_mapping[k] = mapping[k]
139
            continue
140
        if reverse_json_dumps:
141
            rendered_v = json.loads(rendered_v)
142
        rendered_mapping[k] = rendered_v
143
    return rendered_mapping
144
145
146
def is_jinja_expression(value):
147
    """
148
    Function which very simplisticly detect if the provided value contains or is a Jinja
149
    expression.
150
    """
151
    if not value or not isinstance(value, six.string_types):
152
        return False
153
154
    for marker in JINJA_EXPRESSIONS_START_MARKERS:
155
        if marker in value:
156
            return True
157
158
    return False
159