Completed
Pull Request — master (#2895)
by Anthony
09:10
created

ActionAliasMatchCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 5 2
A __init__() 0 19 1
A run_and_print() 0 5 1
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
                'match': ActionAliasMatchCommand
36
            })
37
38
39
class ActionAliasListCommand(resource.ContentPackResourceListCommand):
40
    display_attributes = ['ref', 'pack', 'description', 'enabled']
41
42
43
class ActionAliasGetCommand(resource.ContentPackResourceGetCommand):
44
    display_attributes = ['all']
45
    attribute_display_order = ['id', 'ref', 'pack', 'name', 'description',
46
                               'enabled', 'action_ref', 'formats']
47
48
49
class ActionAliasMatchCommand(resource.ResourceCommand):
50
    display_attributes = ['id', 'name', 'description']
51
52
    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...
53
        super(ActionAliasMatchCommand, self).__init__(
54
            resource, 'list',
55
            'Get the list of %s that match the command text.' %
56
            resource.get_plural_display_name().lower(),
57
            *args, **kwargs)
58
59
        self.parser.add_argument('match_text',
60
                                 metavar='command',
61
                                 help=help)
62
63
        self.parser.add_argument('-a', '--attr', nargs='+',
64
                                 default=self.display_attributes,
65
                                 help=('List of attributes to include in the '
66
                                       'output. "all" will return all '
67
                                       'attributes.'))
68
        self.parser.add_argument('-w', '--width', nargs='+', type=int,
69
                                 default=None,
70
                                 help=('Set the width of columns in output.'))
71
72
    @resource.add_auth_token_to_kwargs_from_cli
73
    def run(self, args, **kwargs):
74
        aliases = self.manager.get_all(**kwargs)
75
        matches = match_command_to_alias(args.match_text, aliases)
76
        return [match[0] for match in matches]  # show only alias objects
77
78
    def run_and_print(self, args, **kwargs):
79
        instances = self.run(args, **kwargs)
80
        self.print_output(instances, table.MultiColumnTable,
81
                          attributes=args.attr, widths=args.width,
82
                          json=args.json, yaml=args.yaml)
0 ignored issues
show
Coding Style introduced by
Final newline missing
Loading history...