Test Failed
Push — master ( e380d0...f5671d )
by W
02:58
created

st2common/st2common/util/actionalias_helpstring.py (1 issue)

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
from __future__ import absolute_import
17
import re
18
from st2common.util.actionalias_matching import normalise_alias_format_string
19
20
21
__all__ = [
22
    'generate_helpstring_result'
23
]
24
25
26
def generate_helpstring_result(aliases, filter=None, pack=None, limit=0, offset=0):
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in filter.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
27
    """
28
    List help strings from a collection of alias objects.
29
30
    :param aliases: The list of aliases
31
    :type  aliases: ``list`` of :class:`st2common.models.api.action.ActionAliasAPI`
32
    :param filter_: A search pattern.
33
    :type  filter_: ``string``
34
    :param pack: Name of a pack
35
    :type  pack: ``string``
36
    :param limit: The number of help strings to return in the list.
37
    :type  limit: ``integer``
38
    :param offset: The offset in the list to start returning help strings.
39
    :type  limit: ``integer``
40
41
    :return: A list of aliases help strings.
42
    :rtype: ``list`` of ``list``
43
    """
44
    matches = []
45
    count = 0
46
    if not (isinstance(limit, int) and isinstance(offset, int)):
47
        raise TypeError('limit or offset argument is not an integer')
48
    for alias in aliases:
49
        # Skip disable aliases.
50
        if not alias.enabled:
51
            continue
52
        # Skip packs which don't explicitly match the requested pack.
53
        if pack and pack != alias.pack:
54
            continue
55
        for format_ in alias.formats:
56
            display, _, _ = normalise_alias_format_string(format_)
57
            if display:
58
                # Skip help strings not containing keyword.
59
                if not re.search(filter or '', display, flags=re.IGNORECASE):
60
                    continue
61
                # Skip over help strings not within the requested offset/limit range.
62
                if (offset == 0 and limit > 0) and count >= limit:
63
                        count += 1
64
                        continue
65
                elif (offset > 0 and limit == 0) and count < offset:
66
                        count += 1
67
                        continue
68
                elif (offset > 0 and limit > 0) and (count < offset or count >= offset + limit):
69
                        count += 1
70
                        continue
71
72
                matches.append({
73
                    "pack": alias.pack,
74
                    "display": display,
75
                    "description": alias.description
76
                })
77
                count += 1
78
    return {"available": count, "helpstrings": matches}
79