diff options
-rw-r--r-- | cliff/formatters/shell.py | 7 | ||||
-rw-r--r-- | cliff/formatters/table.py | 13 | ||||
-rw-r--r-- | cliff/tests/test_formatters_shell.py | 16 | ||||
-rw-r--r-- | docs/requirements.txt | 1 | ||||
-rw-r--r-- | test-requirements.txt | 2 | ||||
-rw-r--r-- | tox.ini | 1 |
6 files changed, 34 insertions, 6 deletions
diff --git a/cliff/formatters/shell.py b/cliff/formatters/shell.py index 385d48c..fd4f29e 100644 --- a/cliff/formatters/shell.py +++ b/cliff/formatters/shell.py @@ -3,6 +3,8 @@ from .base import SingleFormatter +import six + class ShellFormatter(SingleFormatter): @@ -34,6 +36,7 @@ class ShellFormatter(SingleFormatter): desired_columns = parsed_args.variables for name, value in zip(variable_names, data): if name in desired_columns or not desired_columns: - stdout.write('%s%s="%s"\n' % (parsed_args.prefix, name, - value.replace('"', '\\"'))) + if isinstance(value, six.string_types): + value = value.replace('"', '\\"') + stdout.write('%s%s="%s"\n' % (parsed_args.prefix, name, value)) return diff --git a/cliff/formatters/table.py b/cliff/formatters/table.py index e625a25..12e1ac6 100644 --- a/cliff/formatters/table.py +++ b/cliff/formatters/table.py @@ -19,7 +19,14 @@ class TableFormatter(ListFormatter, SingleFormatter): pass def add_argument_group(self, parser): - pass + group = parser.add_argument_group('table formatter') + group.add_argument( + '--max-width', + metavar='<integer>', + default=0, + type=int, + help='Maximum display width, 0 to disable', + ) def emit_list(self, column_names, data, stdout, parsed_args): x = prettytable.PrettyTable( @@ -27,6 +34,8 @@ class TableFormatter(ListFormatter, SingleFormatter): print_empty=False, ) x.padding_width = 1 + if parsed_args.max_width > 0: + x.max_width = int(parsed_args.max_width) # Figure out the types of the columns in the # first row and set the alignment of the # output accordingly. @@ -52,6 +61,8 @@ class TableFormatter(ListFormatter, SingleFormatter): x = prettytable.PrettyTable(field_names=('Field', 'Value'), print_empty=False) x.padding_width = 1 + if parsed_args.max_width > 0: + x.max_width = int(parsed_args.max_width) # Align all columns left because the values are # not all the same type. x.align['Field'] = 'l' diff --git a/cliff/tests/test_formatters_shell.py b/cliff/tests/test_formatters_shell.py index 93452ba..e6813e5 100644 --- a/cliff/tests/test_formatters_shell.py +++ b/cliff/tests/test_formatters_shell.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -from six import StringIO +from six import StringIO, text_type from cliff.formatters import shell @@ -18,3 +18,17 @@ def test_shell_formatter(): sf.emit_one(c, d, output, args) actual = output.getvalue() assert expected == actual + + +def test_shell_formatter_with_non_string_values(): + sf = shell.ShellFormatter() + c = ('a', 'b', 'c', 'd', 'e') + d = (True, False, 100, '"esc"', text_type('"esc"')) + expected = 'a="True"\nb="False"\nc="100"\nd="\\"esc\\""\ne="\\"esc\\""\n' + output = StringIO() + args = mock.Mock() + args.variables = ['a', 'b', 'c', 'd', 'e'] + args.prefix = '' + sf.emit_one(c, d, output, args) + actual = output.getvalue() + assert expected == actual diff --git a/docs/requirements.txt b/docs/requirements.txt index 6f01983..40286a6 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,2 +1,3 @@ httplib2==0.7.4 prettytable==0.5 +Sphinx diff --git a/test-requirements.txt b/test-requirements.txt index 619f5d9..c5b4f08 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,5 +1,3 @@ nose mock>=1.0 coverage>=3.6 -cmd2 -PrettyTable>=0.7,<0.8 @@ -17,6 +17,7 @@ commands = flake8 cliff docs/source/conf.py setup.py basepython=python2.6 [testenv:venv] +deps = -r{toxinidir}/docs/requirements.txt commands = {posargs} [testenv:neutronclient-stable] |