| Conditions | 11 |
| Total Lines | 58 |
| Lines | 0 |
| Ratio | 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:
Complex classes like st2common.util.workflow._transform_action() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
| 131 | def _transform_action(name, spec): |
||
| 132 | |||
| 133 | action_key, input_key = None, None |
||
| 134 | |||
| 135 | for spec_type, spec_meta in six.iteritems(SPEC_TYPES): |
||
| 136 | if spec_meta['action_key'] in spec: |
||
| 137 | action_key = spec_meta['action_key'] |
||
| 138 | input_key = spec_meta['input_key'] |
||
| 139 | break |
||
| 140 | |||
| 141 | if not action_key: |
||
| 142 | return |
||
| 143 | |||
| 144 | if spec[action_key] == 'st2.callback': |
||
| 145 | raise WorkflowDefinitionException('st2.callback is deprecated.') |
||
| 146 | |||
| 147 | # Convert parameters that are inline (i.e. action: some_action var1={$.value1} var2={$.value2}) |
||
| 148 | # and split it to action name and input dict as illustrated below. |
||
| 149 | # |
||
| 150 | # action: some_action |
||
| 151 | # input: |
||
| 152 | # var1: <% $.value1 %> |
||
| 153 | # var2: <% $.value2 %> |
||
| 154 | # |
||
| 155 | # This step to separate the action name and the input parameters is required |
||
| 156 | # to wrap them with the st2.action proxy. |
||
| 157 | # |
||
| 158 | # action: st2.action |
||
| 159 | # input: |
||
| 160 | # ref: some_action |
||
| 161 | # parameters: |
||
| 162 | # var1: <% $.value1 %> |
||
| 163 | # var2: <% $.value2 %> |
||
| 164 | _eval_inline_params(spec, action_key, input_key) |
||
| 165 | |||
| 166 | transformed = (spec[action_key] == 'st2.action') |
||
| 167 | |||
| 168 | action_ref = spec[input_key]['ref'] if transformed else spec[action_key] |
||
| 169 | |||
| 170 | action = None |
||
| 171 | |||
| 172 | # Identify if action is a registered StackStorm action. |
||
| 173 | if action_ref and ResourceReference.is_resource_reference(action_ref): |
||
| 174 | action = action_utils.get_action_by_ref(ref=action_ref) |
||
| 175 | |||
| 176 | # If action is a registered StackStorm action, then wrap the |
||
| 177 | # action with the st2 proxy and validate the action input. |
||
| 178 | if action: |
||
| 179 | if not transformed: |
||
| 180 | spec[action_key] = 'st2.action' |
||
| 181 | action_input = spec.get(input_key) |
||
| 182 | spec[input_key] = {'ref': action_ref} |
||
| 183 | if action_input: |
||
| 184 | spec[input_key]['parameters'] = action_input |
||
| 185 | |||
| 186 | action_input = spec.get(input_key, {}) |
||
| 187 | action_params = action_input.get('parameters', {}) |
||
| 188 | _validate_action_parameters(name, action, action_params) |
||
| 189 | |||
| 231 |
Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with
rorRare they interpreted as regular expressions.The escape sequence that was used indicates that you might have intended to write a regular expression.
Learn more about the available escape sequences. in the Python documentation.