Completed
Pull Request — master (#2282)
by Manas
06:40
created

st2client.commands.RuleListCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %
Metric Value
wmc 4
dl 0
loc 26
rs 10

2 Methods

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