Skip to content

Commit c08545c

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Set up logger of each command by metaclass"
2 parents eb36df1 + 0e6b86a commit c08545c

3 files changed

Lines changed: 80 additions & 22 deletions

File tree

openstackclient/common/command.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2016 NEC Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
import abc
16+
import logging
17+
18+
from cliff import command
19+
from cliff import lister
20+
from cliff import show
21+
import six
22+
23+
24+
class CommandMeta(abc.ABCMeta):
25+
def __new__(mcs, name, bases, cls_dict):
26+
if 'log' not in cls_dict:
27+
cls_dict['log'] = logging.getLogger(
28+
cls_dict['__module__'] + '.' + name)
29+
return super(CommandMeta, mcs).__new__(mcs, name, bases, cls_dict)
30+
31+
32+
@six.add_metaclass(CommandMeta)
33+
class Command(command.Command):
34+
pass
35+
36+
37+
class Lister(Command, lister.Lister):
38+
pass
39+
40+
41+
class ShowOne(Command, show.ShowOne):
42+
pass

openstackclient/compute/v2/flavor.py

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,16 @@
1515

1616
"""Flavor action implementations"""
1717

18-
import logging
1918
import six
2019

21-
from cliff import command
22-
from cliff import lister
23-
from cliff import show
24-
20+
from openstackclient.common import command
2521
from openstackclient.common import parseractions
2622
from openstackclient.common import utils
2723

2824

29-
class CreateFlavor(show.ShowOne):
25+
class CreateFlavor(command.ShowOne):
3026
"""Create new flavor"""
3127

32-
log = logging.getLogger(__name__ + ".CreateFlavor")
33-
3428
def get_parser(self, prog_name):
3529
parser = super(CreateFlavor, self).get_parser(prog_name)
3630
parser.add_argument(
@@ -128,8 +122,6 @@ def take_action(self, parsed_args):
128122
class DeleteFlavor(command.Command):
129123
"""Delete flavor"""
130124

131-
log = logging.getLogger(__name__ + ".DeleteFlavor")
132-
133125
def get_parser(self, prog_name):
134126
parser = super(DeleteFlavor, self).get_parser(prog_name)
135127
parser.add_argument(
@@ -147,11 +139,9 @@ def take_action(self, parsed_args):
147139
compute_client.flavors.delete(flavor.id)
148140

149141

150-
class ListFlavor(lister.Lister):
142+
class ListFlavor(command.Lister):
151143
"""List flavors"""
152144

153-
log = logging.getLogger(__name__ + ".ListFlavor")
154-
155145
def get_parser(self, prog_name):
156146
parser = super(ListFlavor, self).get_parser(prog_name)
157147
public_group = parser.add_mutually_exclusive_group()
@@ -231,11 +221,9 @@ def take_action(self, parsed_args):
231221
) for s in data))
232222

233223

234-
class ShowFlavor(show.ShowOne):
224+
class ShowFlavor(command.ShowOne):
235225
"""Display flavor details"""
236226

237-
log = logging.getLogger(__name__ + ".ShowFlavor")
238-
239227
def get_parser(self, prog_name):
240228
parser = super(ShowFlavor, self).get_parser(prog_name)
241229
parser.add_argument(
@@ -258,11 +246,9 @@ def take_action(self, parsed_args):
258246
return zip(*sorted(six.iteritems(flavor)))
259247

260248

261-
class SetFlavor(show.ShowOne):
249+
class SetFlavor(command.ShowOne):
262250
"""Set flavor properties"""
263251

264-
log = logging.getLogger(__name__ + ".SetFlavor")
265-
266252
def get_parser(self, prog_name):
267253
parser = super(SetFlavor, self).get_parser(prog_name)
268254
parser.add_argument(
@@ -292,11 +278,9 @@ def take_action(self, parsed_args):
292278
return zip(*sorted(six.iteritems(flavor)))
293279

294280

295-
class UnsetFlavor(show.ShowOne):
281+
class UnsetFlavor(command.ShowOne):
296282
"""Unset flavor properties"""
297283

298-
log = logging.getLogger(__name__ + ".UnsetFlavor")
299-
300284
def get_parser(self, prog_name):
301285
parser = super(UnsetFlavor, self).get_parser(prog_name)
302286
parser.add_argument(
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2016 NEC Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
import mock
16+
17+
from openstackclient.common import command
18+
from openstackclient.tests import utils as test_utils
19+
20+
21+
class FakeCommand(command.Command):
22+
def take_action(self, parsed_args):
23+
pass
24+
25+
26+
class TestCommand(test_utils.TestCase):
27+
28+
def test_command_has_logger(self):
29+
cmd = FakeCommand(mock.Mock(), mock.Mock())
30+
self.assertTrue(hasattr(cmd, 'log'))
31+
self.assertEqual('openstackclient.tests.common.test_command.'
32+
'FakeCommand', cmd.log.name)

0 commit comments

Comments
 (0)