From 39ea687e3c09c9686f90d41108e478854cee2d1d Mon Sep 17 00:00:00 2001 From: Lennart Regebro Date: Thu, 11 Jun 2015 07:53:46 -0400 Subject: [PATCH] Fix output of "tuskar plan-list --verbose" Add pretty printing for most values, and re-wrapping for text. Change-Id: Iab8cc15c9407eaac20bf6fc5332f194e23930c24 Closes-bug: https://bugzilla.redhat.com/show_bug.cgi?id=1196191 --- tuskarclient/common/formatting.py | 30 ++++++++++++++++++++++- tuskarclient/tests/v2/test_plans_shell.py | 27 ++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/tuskarclient/common/formatting.py b/tuskarclient/common/formatting.py index 00248a8..807e81b 100644 --- a/tuskarclient/common/formatting.py +++ b/tuskarclient/common/formatting.py @@ -12,15 +12,43 @@ from __future__ import print_function +import pprint import sys +import textwrap import prettytable +import six + + +def value_formatter(value, width=70): + # Most values can be pretty printed for a reasonable output + if not isinstance(value, six.string_types): + return pprint.pformat(value, width=width) + + # pprint doesn't touch strings, so we do them manually + + # First join lines that are not indented: + joined = [] + parts = [] + for l in value.splitlines(): + if l[0] in u' \t\r\n': + # break here + joined.append(' '.join(parts)) + parts = [] + parts.append(l.rstrip()) + if parts: + joined.append(' '.join(parts)) + + result = [] + for line in joined: + result.append(textwrap.fill(line, width=width)) + return u"\n".join(result) def attributes_formatter(attributes): """Given a simple dict format the keyvalue pairs with one on each line. """ - return u"".join(u"{0}={1}\n".format(k, v) for k, v in + return u"".join(u"{0}={1}\n".format(k, value_formatter(v)) for k, v in sorted(attributes.items())) diff --git a/tuskarclient/tests/v2/test_plans_shell.py b/tuskarclient/tests/v2/test_plans_shell.py index acd7210..7f3467e 100644 --- a/tuskarclient/tests/v2/test_plans_shell.py +++ b/tuskarclient/tests/v2/test_plans_shell.py @@ -271,6 +271,33 @@ class PlansShellTest(BasePlansShellTest): self.shell.do_plan_show(self.tuskar, args, outfile=self.outfile) mock_find_resource.assert_called_with(self.tuskar.plans, '5') + @mock.patch('tuskarclient.common.utils.find_resource') + def test_print_plan_wrap(self, mock_find_resource): + mock_find_resource.return_value = mock_plan() + mock_find_resource.return_value.parameters.append( + {'name': 'foo', + 'value': 'This is a really long parameter value with ' + 'multiple lines to test the output wrapping.\n' + 'Indents is assumed to be code:\n' + ' {\n' + ' "like": "this"\n' + ' }\n'} + ) + + args = empty_args() + args.plan = '5' + args.verbose = True + + self.shell.do_plan_show(self.tuskar, args, outfile=self.outfile) + output = self.outfile.getvalue() + + # Lines should not be way to long: + self.assertTrue(all(len(line) < 100 for line in output.splitlines())) + # The lines are rewraped: + self.assertIn("wrapping. Indents", output) + # But not if the start with an indent: + self.assertIn(" {", output) + @mock.patch('tuskarclient.common.utils.find_resource') def test_print_plan_detail(self, mock_find_resource): mock_find_resource.return_value = mock_plan()