Skip to content

Commit 8571344

Browse files
committed
osc: Use FormattableColumn for formatter
... because usage of legacy format function was deprecated 8 years ago and was recently removed from osc-lib[1]. [1] 0e7dada02b825b2f97ac3eb68544bdd438a3a099 Change-Id: I62521846b4aaf8036cd83e3f5a38d9eed6c520a7
1 parent 28464d9 commit 8571344

File tree

7 files changed

+76
-25
lines changed

7 files changed

+76
-25
lines changed

heatclient/osc/v1/build_info.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from osc_lib.command import command
1919
from osc_lib import utils
2020

21-
from heatclient.common import utils as heat_utils
21+
from heatclient.osc.v1 import common
2222

2323

2424
class BuildInfo(command.ShowOne):
@@ -37,8 +37,8 @@ def take_action(self, parsed_args):
3737
result = heat_client.build_info.build_info()
3838

3939
formatters = {
40-
'api': heat_utils.json_formatter,
41-
'engine': heat_utils.json_formatter,
40+
'api': common.JsonColumn,
41+
'engine': common.JsonColumn,
4242
}
4343
columns = sorted(list(result.keys()))
4444
return columns, utils.get_dict_properties(result, columns,

heatclient/osc/v1/common.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#
2+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
3+
# not use this file except in compliance with the License. You may obtain
4+
# a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
# License for the specific language governing permissions and limitations
12+
# under the License.
13+
#
14+
15+
from cliff import columns
16+
17+
from heatclient.common import utils as heat_utils
18+
19+
20+
class LinkColumn(columns.FormattableColumn):
21+
def human_readable(self):
22+
return heat_utils.link_formatter(self._value)
23+
24+
25+
class JsonColumn(columns.FormattableColumn):
26+
def human_readable(self):
27+
return heat_utils.json_formatter(self._value)
28+
29+
30+
class YamlColumn(columns.FormattableColumn):
31+
def human_readable(self):
32+
return heat_utils.yaml_formatter(self._value)
33+
34+
35+
class TextWrapColumn(columns.FormattableColumn):
36+
def human_readable(self):
37+
return heat_utils.text_wrap_formatter(self._value)
38+
39+
40+
class NewlineListColumn(columns.FormattableColumn):
41+
def human_readable(self):
42+
return heat_utils.newline_list_formatter(self._value)

heatclient/osc/v1/event.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from heatclient.common import event_utils
2424
from heatclient.common import utils as heat_utils
2525
from heatclient import exc
26+
from heatclient.osc.v1 import common
2627

2728

2829
class ShowEvent(command.ShowOne):
@@ -69,8 +70,8 @@ def take_action(self, parsed_args):
6970
raise exc.CommandError(str(ex))
7071

7172
formatters = {
72-
'links': heat_utils.link_formatter,
73-
'resource_properties': heat_utils.json_formatter
73+
'links': common.LinkColumn,
74+
'resource_properties': common.JsonColumn
7475
}
7576

7677
columns = []

heatclient/osc/v1/stack.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from heatclient.common import template_utils
3232
from heatclient.common import utils as heat_utils
3333
from heatclient import exc as heat_exc
34+
from heatclient.osc.v1 import common
3435

3536

3637
class CreateStack(command.ShowOne):
@@ -180,13 +181,13 @@ def take_action(self, parsed_args):
180181
stack = client.stacks.preview(**fields)
181182

182183
formatters = {
183-
'description': heat_utils.text_wrap_formatter,
184-
'template_description': heat_utils.text_wrap_formatter,
185-
'stack_status_reason': heat_utils.text_wrap_formatter,
186-
'parameters': heat_utils.json_formatter,
187-
'outputs': heat_utils.json_formatter,
188-
'resources': heat_utils.json_formatter,
189-
'links': heat_utils.link_formatter,
184+
'description': common.TextWrapColumn,
185+
'template_description': common.TextWrapColumn,
186+
'stack_status_reason': common.TextWrapColumn,
187+
'parameters': common.JsonColumn,
188+
'outputs': common.JsonColumn,
189+
'resources': common.JsonColumn,
190+
'links': common.LinkColumn,
190191
}
191192

192193
columns = []
@@ -386,7 +387,7 @@ def take_action(self, parsed_args):
386387
'resource_identity']
387388

388389
columns = sorted(changes.get("resource_changes", {}).keys())
389-
data = [heat_utils.json_formatter(changes["resource_changes"][key])
390+
data = [common.JsonColumn(changes["resource_changes"][key])
390391
for key in columns]
391392

392393
return columns, data
@@ -478,9 +479,9 @@ def _show_stack(heat_client, stack_id, format='', short=False,
478479
formatters = {}
479480
complex_formatter = None
480481
if format in 'table':
481-
complex_formatter = heat_utils.yaml_formatter
482+
complex_formatter = common.YamlColumn
482483
elif format in ('shell', 'value', 'html'):
483-
complex_formatter = heat_utils.json_formatter
484+
complex_formatter = common.JsonColumn
484485
if complex_formatter:
485486
formatters['parameters'] = complex_formatter
486487
formatters['outputs'] = complex_formatter
@@ -1019,7 +1020,7 @@ def take_action(self, parsed_args):
10191020
values = []
10201021
for output in outputs:
10211022
columns.append(output['output_key'])
1022-
values.append(heat_utils.json_formatter(output))
1023+
values.append(common.JsonColumn(output))
10231024

10241025
return columns, values
10251026

heatclient/osc/v1/template.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import logging
1616

17+
from cliff import columns
1718
from osc_lib.command import command
1819
from osc_lib import utils
1920

@@ -25,6 +26,11 @@
2526
from heatclient import exc
2627

2728

29+
class ListColumn(columns.FormattableColumn):
30+
def human_readable(self):
31+
return ','.join(self._value)
32+
33+
2834
class VersionList(command.Lister):
2935
"""List the available template versions."""
3036

@@ -39,11 +45,8 @@ def take_action(self, parsed_args):
3945
try:
4046
versions[1].aliases
4147

42-
def format_alias(aliases):
43-
return ','.join(aliases)
44-
4548
fields = ['Version', 'Type', 'Aliases']
46-
formatters = {'Aliases': format_alias}
49+
formatters = {'Aliases': ListColumn}
4750
except AttributeError:
4851
fields = ['Version', 'Type']
4952
formatters = None

heatclient/tests/unit/osc/v1/test_build_info.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from unittest import mock
1515

1616
from heatclient.osc.v1 import build_info as osc_build_info
17+
from heatclient.osc.v1 import common
1718
from heatclient.tests.unit.osc.v1 import fakes as orchestration_fakes
1819

1920

@@ -39,6 +40,7 @@ def test_build_info(self):
3940
columns, data = self.cmd.take_action(parsed_args)
4041
self.mock_client.build_info.build_info.assert_called_with()
4142
self.assertEqual(['api', 'engine'], columns)
42-
self.assertEqual(['{\n "revision": "{api_build_revision}"\n}',
43-
'{\n "revision": "{engine_build_revision}"\n}'],
44-
list(data))
43+
self.assertEqual([
44+
common.JsonColumn({"revision": "{api_build_revision}"}),
45+
common.JsonColumn({"revision": "{engine_build_revision}"})
46+
], list(data))

heatclient/tests/unit/osc/v1/test_template.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ def test_version_list_with_aliases(self):
5757
columns, data = self.cmd.take_action(parsed_args)
5858

5959
self.assertEqual(['Version', 'Type', 'Aliases'], columns)
60-
self.assertEqual([('HOT123', 'hot', 'releasex'),
61-
('CFN456', 'cfn', 'releasey')], list(data))
60+
self.assertEqual([
61+
('HOT123', 'hot', template.ListColumn(['releasex'])),
62+
('CFN456', 'cfn', template.ListColumn(['releasey']))
63+
], list(data))
6264

6365

6466
class TestTemplateFunctionList(TestTemplate):

0 commit comments

Comments
 (0)