Completed
Pull Request — master (#2334)
by Edward
06:02
created

st2client.formatters.CLIConfigParser.parse()   F

Complexity

Conditions 10

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 10
dl 0
loc 40
rs 3.1304

How to fix   Complexity   

Complexity

Complex classes like st2client.formatters.CLIConfigParser.parse() 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
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 ast
17
import logging
18
import sys
19
20
import yaml
21
22
from st2client import formatters
23
from st2client.utils import jsutil
24
from st2client.utils import strutil
25
from st2client.utils.color import DisplayColors
26
27
28
LOG = logging.getLogger(__name__)
29
30
31
class ExecutionResult(formatters.Formatter):
32
33
    @classmethod
34
    def format(cls, entry, *args, **kwargs):
35
        attrs = kwargs.get('attributes', [])
36
        key = kwargs.get('key', None)
37
        if key:
38
            output = jsutil.get_value(entry.result, key)
39
        else:
40
            # drop entry to the dict so that jsutil can operate
41
            entry = vars(entry)
42
            output = ''
43
            for attr in attrs:
44
                value = jsutil.get_value(entry, attr)
45
                if (isinstance(value, basestring) and len(value) > 0 and
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'basestring'
Loading history...
46
                        value[0] in ['{', '['] and value[len(value) - 1] in ['}', ']']):
47
                    new_value = ast.literal_eval(value)
48
                    if type(new_value) in [dict, list]:
49
                        value = new_value
50
                if type(value) in [dict, list]:
51
                    # 1. To get a nice overhang indent get safe_dump to generate output with
52
                    #    the attribute key and then remove the attribute key from the string.
53
                    # 2. Drop the trailing newline
54
                    # 3. Set width to maxint so pyyaml does not split text. Anything longer
55
                    #    and likely we will see other issues like storage :P.
56
                    formatted_value = yaml.safe_dump({attr: value},
57
                                                     default_flow_style=False,
58
                                                     width=sys.maxint,
59
                                                     indent=2)[len(attr) + 2:-1]
60
                    value = ('\n' if isinstance(value, dict) else '') + formatted_value
61
                output += ('\n' if output else '') + '%s: %s' % \
62
                    (DisplayColors.colorize(attr, DisplayColors.BLUE), value)
63
        return strutil.unescape(output)
64