| Conditions | 9 |
| Total Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
| 97 | def render_values(mapping=None, context=None, allow_undefined=False): |
||
| 98 | """ |
||
| 99 | Render an incoming mapping using context provided in context using Jinja2. Returns a dict |
||
| 100 | containing rendered mapping. |
||
| 101 | |||
| 102 | :param mapping: Input as a dictionary of key value pairs. |
||
| 103 | :type mapping: ``dict`` |
||
| 104 | |||
| 105 | :param context: Context to be used for dictionary. |
||
| 106 | :type context: ``dict`` |
||
| 107 | |||
| 108 | :rtype: ``dict`` |
||
| 109 | """ |
||
| 110 | |||
| 111 | if not context or not mapping: |
||
| 112 | return mapping |
||
| 113 | |||
| 114 | # Add in special __context variable that provides an easy way to get access to entire context. |
||
| 115 | # This mean __context is a reserve key word although backwards compat is preserved by making |
||
| 116 | # sure that real context is updated later and therefore will override the __context value. |
||
| 117 | super_context = {} |
||
| 118 | super_context['__context'] = context |
||
| 119 | super_context.update(context) |
||
| 120 | |||
| 121 | env = get_jinja_environment(allow_undefined=allow_undefined) |
||
| 122 | rendered_mapping = {} |
||
| 123 | for k, v in six.iteritems(mapping): |
||
| 124 | # jinja2 works with string so transform list and dict to strings. |
||
| 125 | reverse_json_dumps = False |
||
| 126 | if isinstance(v, dict) or isinstance(v, list): |
||
| 127 | v = json.dumps(v) |
||
| 128 | reverse_json_dumps = True |
||
| 129 | else: |
||
| 130 | v = str(v) |
||
| 131 | |||
| 132 | try: |
||
| 133 | LOG.info('Rendering string %s. Super context=%s', v, super_context) |
||
| 134 | rendered_v = env.from_string(v).render(super_context) |
||
| 135 | except Exception as e: |
||
| 136 | # Attach key and value which failed the rendering |
||
| 137 | e.key = k |
||
| 138 | e.value = v |
||
| 139 | raise e |
||
| 140 | |||
| 141 | # no change therefore no templatization so pick params from original to retain |
||
| 142 | # original type |
||
| 143 | if rendered_v == v: |
||
| 144 | rendered_mapping[k] = mapping[k] |
||
| 145 | continue |
||
| 146 | if reverse_json_dumps: |
||
| 147 | rendered_v = json.loads(rendered_v) |
||
| 148 | rendered_mapping[k] = rendered_v |
||
| 149 | LOG.info('Mapping: %s, rendered_mapping: %s, context: %s', mapping, rendered_mapping, context) |
||
| 150 | return rendered_mapping |
||
| 151 | |||
| 166 |