Completed
Pull Request — master (#2895)
by Anthony
15:01 queued 09:05
created

ActionAliasMatchCommand.run()   A

Complexity

Conditions 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
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 st2common.util.actionalias_matching import match_command_to_alias
17
18
from st2client.models.action_alias import ActionAlias
19
from st2client.commands import resource
20
from st2client.formatters import table
21
22
__all__ = [
23
    'ActionAliasBranch'
24
]
25
26
27
class ActionAliasBranch(resource.ResourceBranch):
28
    def __init__(self, description, app, subparsers, parent_parser=None):
29
        super(ActionAliasBranch, self).__init__(
30
            ActionAlias, description, app, subparsers,
31
            parent_parser=parent_parser, read_only=False,
32
            commands={
33
                'list': ActionAliasListCommand,
34
                'get': ActionAliasGetCommand
35
            })
36
37
        self.commands['match'] = ActionAliasMatchCommand(
38
            self.resource, self.app, self.subparsers,
39
            add_help=False)
40
41
42
class ActionAliasListCommand(resource.ContentPackResourceListCommand):
43
    display_attributes = ['ref', 'pack', 'description', 'enabled']
44
45
46
class ActionAliasGetCommand(resource.ContentPackResourceGetCommand):
47
    display_attributes = ['all']
48
    attribute_display_order = ['id', 'ref', 'pack', 'name', 'description',
49
                               'enabled', 'action_ref', 'formats']
50
51
52
class ActionAliasMatchCommand(resource.ResourceCommand):
53
    display_attributes = ['id', 'name', 'description']
54
55
    def __init__(self, resource, *args, **kwargs):
0 ignored issues
show
Comprehensibility Bug introduced by
resource is re-defining a name which is already available in the outer-scope (previously defined on line 19).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
56
        super(ActionAliasMatchCommand, self).__init__(
57
            resource, 'match',
58
            'Get the list of %s that match the command text.' %
59
            resource.get_plural_display_name().lower(),
60
            *args, **kwargs)
61
62
        self.parser.add_argument('match_text',
63
                                 metavar='command',
64
                                 help=help)
65
66
        self.parser.add_argument('-a', '--attr', nargs='+',
67
                                 default=self.display_attributes,
68
                                 help=('List of attributes to include in the '
69
                                       'output. "all" will return all '
70
                                       'attributes.'))
71
        self.parser.add_argument('-w', '--width', nargs='+', type=int,
72
                                 default=None,
73
                                 help=('Set the width of columns in output.'))
74
75
    @resource.add_auth_token_to_kwargs_from_cli
76
    def run(self, args, **kwargs):
77
        aliases = self.manager.get_all(**kwargs)
78
        matches = match_command_to_alias(args.match_text, aliases)
79
        return [match[0] for match in matches]  # show only alias objects
80
81
    def run_and_print(self, args, **kwargs):
82
        instances = self.run(args, **kwargs)
83
        self.print_output(instances, table.MultiColumnTable,
84
                          attributes=args.attr, widths=args.width,
85
                          json=args.json, yaml=args.yaml)
86