Completed
Pull Request — master (#2284)
by Manas
06:16
created

st2client.commands.RuleListCommand.run()   A

Complexity

Conditions 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 12
rs 9.2
cc 4
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 st2client import models
17
from st2client.commands import resource
18
19
20
class RuleBranch(resource.ResourceBranch):
21
22
    def __init__(self, description, app, subparsers, parent_parser=None):
23
        super(RuleBranch, self).__init__(
24
            models.Rule, description, app, subparsers,
25
            parent_parser=parent_parser,
26
            commands={
27
                'list': RuleListCommand,
28
                'get': RuleGetCommand,
29
                'update': RuleUpdateCommand,
30
                'delete': RuleDeleteCommand
31
            })
32
33
        self.commands['enable'] = RuleEnableCommand(self.resource, self.app, self.subparsers)
34
        self.commands['disable'] = RuleDisableCommand(self.resource, self.app, self.subparsers)
35
36
37
class RuleListCommand(resource.ContentPackResourceListCommand):
38
    display_attributes = ['ref', 'pack', 'description', 'enabled']
39
    display_attributes_iftt = ['ref', 'trigger.ref', 'action.ref', 'enabled']
40
41
42
    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 17).

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...
43
        super(RuleListCommand, self).__init__(resource, *args, **kwargs)
44
45
        self.group = self.parser.add_argument_group()
46
        self.parser.add_argument('-n', '--last', type=int, dest='last',
47
                                 default=50,
48
                                 help=('List N most recent %s; '
49
                                       'list all if 0.' %
50
                                       resource.get_plural_display_name().lower()))
51
        self.parser.add_argument('--iftt', action='store_true',
52
                                 help='Show trigger and action in display list.')
53
        self.group.add_argument('-c', '--action',
54
                                help='Action reference to filter the list.')
55
        self.group.add_argument('-g', '--trigger',
56
                                help='Trigger type reference to filter the list.')
57
58
    @resource.add_auth_token_to_kwargs_from_cli
59
    def run(self, args, **kwargs):
60
        # Filtering options
61
        if args.action:
62
            kwargs['action'] = args.action
63
        if args.trigger:
64
            kwargs['trigger'] = args.trigger
65
        if args.iftt:
66
            # switch attr to display the trigger and action
67
            args.attr = self.display_attributes_iftt
68
69
        return self.manager.query(limit=args.last, **kwargs)
70
71
72
class RuleGetCommand(resource.ContentPackResourceGetCommand):
73
    display_attributes = ['all']
74
    attribute_display_order = ['id', 'uid', 'ref', 'pack', 'name', 'description',
75
                               'enabled']
76
77
78
class RuleUpdateCommand(resource.ContentPackResourceUpdateCommand):
79
    pass
80
81
82
class RuleEnableCommand(resource.ContentPackResourceEnableCommand):
83
    display_attributes = ['all']
84
    attribute_display_order = ['id', 'ref', 'pack', 'name', 'enabled', 'description',
85
                               'enabled']
86
87
88
class RuleDisableCommand(resource.ContentPackResourceDisableCommand):
89
    display_attributes = ['all']
90
    attribute_display_order = ['id', 'ref', 'pack', 'name', 'enabled', 'description',
91
                               'enabled']
92
93
94
class RuleDeleteCommand(resource.ContentPackResourceDeleteCommand):
95
    pass
96