Passed
Push — develop ( 4bac37...be04cf )
by Plexxi
07:48 queued 03:49
created

RuleListCommand.run()   B

Complexity

Conditions 5

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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