Passed
Push — master ( 34f323...e86776 )
by Plexxi
02:43
created

generate_helpstring_result()   F

Complexity

Conditions 21

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 21
dl 0
loc 53
rs 3.3185
c 1
b 0
f 1

How to fix   Long Method    Complexity   

Long Method

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:

Complexity

Complex classes like generate_helpstring_result() 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 re
17
from st2common.util.actionalias_matching import normalise_alias_format_string
18
19
20
__all__ = [
21
    'generate_helpstring_result'
22
]
23
24
25
def generate_helpstring_result(aliases, filter_="", pack="", limit=0, offset=0):
26
    """
27
    List help strings from a collection of alias objects.
28
29
    :param aliases: The list of aliases
30
    :type  aliases: ``list`` of :class:`st2common.models.api.action.ActionAliasAPI`
31
    :param filter_: A search pattern.
32
    :type  filter_: ``string``
33
    :param pack: Name of a pack
34
    :type  pack: ``string``
35
    :param limit: The number of help strings to return in the list.
36
    :type  limit: ``integer``
37
    :param offset: The offset in the list to start returning help strings.
38
    :type  limit: ``integer``
39
40
    :return: A list of aliases help strings.
41
    :rtype: ``list`` of ``list``
42
    """
43
    matches = {}
44
    count = 0
45
    if not (isinstance(limit, int) and isinstance(offset, int)):
46
        raise TypeError
47
    for alias in aliases:
48
        # Skip disable aliases.
49
        if not alias.enabled:
50
            continue
51
        # Skip packs which don't explicitly match the requested pack.
52
        if pack != alias.pack and pack != "":
53
            continue
54
        for format_ in alias.formats:
55
            display, _ = normalise_alias_format_string(format_)
56
            if display:
57
                # Skip help strings not containing keyword.
58
                if not re.search(filter_, display, flags=re.IGNORECASE):
59
                    continue
60
                # Skip over help strings not within the requested offset/limit range.
61
                if (offset == 0 and limit > 0) and count >= limit:
62
                        count += 1
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 20 spaces were expected, but 24 were found.
Loading history...
63
                        continue
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 20 spaces were expected, but 24 were found.
Loading history...
64
                elif (offset > 0 and limit == 0) and count < offset:
65
                        count += 1
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 20 spaces were expected, but 24 were found.
Loading history...
66
                        continue
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 20 spaces were expected, but 24 were found.
Loading history...
67
                elif (offset > 0 and limit > 0) and (count < offset or count >= offset + limit):
68
                        count += 1
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 20 spaces were expected, but 24 were found.
Loading history...
69
                        continue
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 20 spaces were expected, but 24 were found.
Loading history...
70
                if alias.pack not in matches:
71
                    matches[alias.pack] = []
72
                matches[alias.pack].append({
73
                    "display": display,
74
                    "description": alias.description
75
                })
76
                count += 1
77
    return {"available": count, "helpstrings": matches}
78