Completed
Pull Request — master (#2836)
by Edward
32:11
created

PackCreateCommand.__init__()   A

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 8
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 st2client.models import Pack
17
from st2client.commands import resource
18
from st2client.commands.noop import NoopCommand
19
20
21
class PackBranch(resource.ResourceBranch):
22
    def __init__(self, description, app, subparsers, parent_parser=None):
23
        super(PackBranch, self).__init__(
24
            Pack, description, app, subparsers,
25
            parent_parser=parent_parser,
26
            read_only=True,
27
            commands={
28
                'list': PackListCommand,
29
                'get': NoopCommand
30
            })
31
32
        self.commands['register'] = PackRegisterCommand(self.resource, self.app, self.subparsers)
33
        self.commands['create'] = PackCreateCommand(self.resource, self.app, self.subparsers)
34
        self.commands['install'] = PackInstallCommand(self.resource, self.app, self.subparsers)
35
        self.commands['remove'] = PackRemoveCommand(self.resource, self.app, self.subparsers)
36
        self.commands['search'] = PackSearchCommand(self.resource, self.app, self.subparsers)
37
38
39
class PackResourceCommand(resource.ResourceCommand):
40
    def run_and_print(self, args, **kwargs):
41
        try:
42
            instance = self.run(args, **kwargs)
43
            if not instance:
44
                raise Exception('Server did not create instance.')
45
            self.print_output(instance, table.PropertyValueTable,
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'table'
Loading history...
46
                              attributes=['all'], json=args.json, yaml=args.yaml)
47
        except Exception as e:
48
            message = e.message or str(e)
49
            print('ERROR: %s' % (message))
50
            raise OperationFailureException(message)
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'OperationFailureException'
Loading history...
51
52
53
class PackListCommand(resource.ResourceListCommand):
54
    display_attributes = ['name', 'description', 'version', 'author']
55
    attribute_display_order = ['name', 'description', 'version', 'author']
56
57
58
class PackInstallCommand(PackResourceCommand):
59
    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...
60
        super(PackInstallCommand, self).__init__(resource, 'install',
61
            'Install a new %s.' % resource.get_display_name().lower(),
62
            *args, **kwargs)
63
64
        self.parser.add_argument('name',
65
                                 help='Name of the %s to install.' %
66
                                 resource.get_display_name().lower())
67
68
    @resource.add_auth_token_to_kwargs_from_cli
69
    def run(self, args, **kwargs):
70
        return self.manager.install(instance, **kwargs)
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'instance'
Loading history...
71
72
73
class PackRemoveCommand(PackResourceCommand):
74
    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...
75
        super(PackRemoveCommand, self).__init__(resource, 'remove',
76
            'Remove a %s.' % resource.get_display_name().lower(),
77
            *args, **kwargs)
78
79
        self.parser.add_argument('name',
80
                                 help='Name of the %s to remove.' %
81
                                 resource.get_display_name().lower())
82
83
    @resource.add_auth_token_to_kwargs_from_cli
84
    def run(self, args, **kwargs):
85
        return self.manager.remove(instance, **kwargs)
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'instance'
Loading history...
86
87
88
class PackCreateCommand(PackResourceCommand):
89
    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...
90
        super(PackCreateCommand, self).__init__(resource, 'create',
91
            'Create a template for a new %s.' % resource.get_display_name().lower(),
92
            *args, **kwargs)
93
94
        self.parser.add_argument('name',
95
                                 help='Name of the %s to create.' %
96
                                 resource.get_display_name().lower())
97
98
    @resource.add_auth_token_to_kwargs_from_cli
99
    def run(self, args, **kwargs):
100
        return self.manager.create(instance, **kwargs)
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'instance'
Loading history...
101
102
103
class PackRegisterCommand(PackResourceCommand):
104
    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...
105
        super(PackRegisterCommand, self).__init__(resource, 'register',
106
            'Register a %s: sync all file changes with DB.' % resource.get_display_name().lower(),
107
            *args, **kwargs)
108
109
        self.parser.add_argument('name',
110
                                 help='Name of the %s to register.' %
111
                                 resource.get_display_name().lower())
112
113
    @resource.add_auth_token_to_kwargs_from_cli
114
    def run(self, args, **kwargs):
115
        return self.manager.register(instance, **kwargs)
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'instance'
Loading history...
116
117
118
class PackSearchCommand(PackResourceCommand):
119
    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...
120
        super(PackSearchCommand, self).__init__(resource, 'search',
121
            'Search for a %s in the directory.' % resource.get_display_name().lower(),
122
            *args, **kwargs)
123
124
        self.parser.add_argument('query',
125
                                 help='Search query.')
126
127
    @resource.add_auth_token_to_kwargs_from_cli
128
    def run(self, args, **kwargs):
129
        return self.manager.search(instance, **kwargs)
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'instance'
Loading history...
130