Completed
Pull Request — master (#2895)
by Anthony
10:42 queued 04:58
created

match_command_to_alias()   A

Complexity

Conditions 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
c 2
b 0
f 0
dl 0
loc 17
rs 9.2
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 six
17
18
from st2common.exceptions.content import ParseException
19
from st2common.models.utils.action_alias_utils import extract_parameters
20
21
__all__ = [
22
    'list_format_strings_from_aliases',
23
    'normalise_alias_format_string',
24
    'match_command_to_alias'
25
]
26
27
28
def list_format_strings_from_aliases(aliases):
29
    '''
30
    List patterns from a collection of alias objects
31
32
    :param aliases: The list of aliases
33
    :type  aliases: ``list`` of :class:`st2common.models.api.action.ActionAliasAPI`
34
35
    :return: A description of potential execution patterns in a list of aliases.
36
    :rtype: ``list`` of ``list``
37
    '''
38
    patterns = []
39
    for alias in aliases:
40
        for _format in alias.formats:
41
            display, representations = normalise_alias_format_string(_format)
42
            patterns.extend([(display, representation) for representation in representations])
43
    return patterns
44
45
46
def normalise_alias_format_string(alias_format):
47
    '''
48
    StackStorm action aliases can have two types;
49
        1. A simple string holding the format
50
        2. A dictionary which hold numerous alias format "representation(s)"
51
           With a single "display" for help about the action alias.
52
    This function processes both forms and returns a standardized form.
53
54
    :param alias_format: The alias format
55
    :type  alias_format: ``str`` or ``dict``
56
57
    :return: The representation of the alias
58
    :rtype: ``tuple`` of (``str``, ``str``)
59
    '''
60
    display = None
61
    representation = []
62
63
    if isinstance(alias_format, six.string_types):
64
        display = alias_format
65
        representation.append(alias_format)
66
    elif isinstance(alias_format, dict):
67
        display = alias_format['display']
68
        representation = alias_format['representation']
69
    else:
70
        raise TypeError("alias_format is neither a dictionary or string type.")
71
    return (display, representation)
72
73
74
def match_command_to_alias(command, aliases):
75
    """
76
    Match the text against an action and return the action reference.
77
    """
78
    results = []
79
80
    for alias in aliases:
81
        format_strings = list_format_strings_from_aliases([alias])
82
        for format_string in format_strings:
83
            try:
84
                extract_parameters(format_str=format_string[1],
85
                                   param_stream=command)
86
            except ParseException:
87
                continue
88
89
            results.append((alias, format_string[0], format_string[1]))
90
    return results
91