Completed
Pull Request — master (#2895)
by Anthony
05:34
created

match_command_to_alias()   A

Complexity

Conditions 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
dl 0
loc 18
rs 9.2
c 1
b 0
f 0
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
22
def list_format_strings_from_aliases(aliases):
23
    '''
24
    List patterns from a collection of alias objects
25
26
    :param aliases: The list of aliases
27
    :type  aliases: ``list`` of :class:`st2common.persistence.actionalias.ActionAlias`
28
29
    :return: A description of potential execution patterns in a list of aliases.
30
    :rtype: ``list`` of ``dict``
31
    '''
32
    patterns = []
33
    for alias in aliases:
34
        for _format in alias.formats:
35
            display, representations = normalise_alias_format(_format)
0 ignored issues
show
Bug introduced by
It seems like a value for argument alias_format is missing in the function call.
Loading history...
36
            patterns.append([(display, representation) for representation in representations])
37
    return patterns
38
39
40
def normalise_alias_format(self, alias_format):
41
    '''
42
    StackStorm action aliases can have two types;
43
        1. A simple string holding the format
44
        2. A dictionary which hold numerous alias format "representation(s)"
45
           With a single "display" for help about the action alias.
46
    This function processes both forms and returns a standardized form.
47
48
    :param alias_format: The alias format
49
    :type  alias_format: ``str`` or ``dict``
50
51
    :return: The representation of the alias
52
    :rtype: ``tuple`` of (``str``, ``str``)
53
    '''
54
    display = None
55
    representation = []
56
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
57
    if isinstance(alias_format, six.string_types):
58
        display = alias_format
59
        representation.append(alias_format)
60
    elif isinstance(alias_format, dict):
61
        display = alias_format['display']
62
        representation = alias_format['representation']
63
    else:
64
        raise TypeError("alias_format is neither a dictionary or string type.")
65
    return (display, representation)
66
67
68
def match_command_to_alias(command, aliases):
69
    """
70
    Match the text against an action and return the action reference.
71
    """
72
    results = []
73
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
74
    for alias in aliases:
75
        # this is lazy, fix up. -ANT
76
        format_strings = list_format_strings_from_aliases([aliases])
77
        for format_string in format_strings:
78
            try:
79
                extract_parameters(format_str=format_string,
80
                                   param_stream=command)
81
            except ParseException:
82
                continue
83
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
84
            results.append((alias, format_string))
85
    return results
86