Add ability to sort data by columns for list commands
Very often there is a need to sort data by one or several columns. This patch adds ability to do this by specifying extra '-s/--sort-column' argument with respective columns: -s SORT_COLUMN, --sort-column SORT_COLUMN specify the column(s) to sort the data (columns specified first have a priority, non-existing columns are ignored), can be repeated e.g.: * without sorting: foo_command bar_subcommand list +------+------+ | Col1 | Col2 | +------+------+ | a | A | | b | B | | c | A | +------+------+ * with sorting: foo_command bar_subcommand list -s Col2 Col1 +------+------+ | Col1 | Col2 | +------+------+ | a | A | | c | A | | b | B | +------+------+ Change-Id: Ifc6d6746448ff6f42898efd78ed3fd084a598a91
This commit is contained in:
parent
55fa3defd3
commit
21914ed74b
|
@ -52,6 +52,7 @@ class DisplayCommandBase(command.Command):
|
|||
title='output formatters',
|
||||
description='output formatter options',
|
||||
)
|
||||
self._formatter_group = formatter_group
|
||||
formatter_choices = sorted(self._formatter_plugins.names())
|
||||
formatter_default = self.formatter_default
|
||||
if formatter_default not in formatter_choices:
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
"""Application base class for providing a list of data as output.
|
||||
"""
|
||||
import abc
|
||||
import operator
|
||||
import six
|
||||
|
||||
from . import display
|
||||
|
@ -31,13 +32,41 @@ class Lister(display.DisplayCommandBase):
|
|||
def formatter_default(self):
|
||||
return 'table'
|
||||
|
||||
@property
|
||||
def need_sort_by_cliff(self):
|
||||
"""Whether sort procedure is performed by cliff itself.
|
||||
|
||||
Should be overridden (return False) when there is a need to implement
|
||||
custom sorting procedure or data is already sorted."""
|
||||
return True
|
||||
|
||||
@abc.abstractmethod
|
||||
def take_action(self, parsed_args):
|
||||
"""Return a tuple containing the column names and an iterable
|
||||
containing the data to be listed.
|
||||
"""
|
||||
|
||||
def get_parser(self, prog_name):
|
||||
parser = super(Lister, self).get_parser(prog_name)
|
||||
group = self._formatter_group
|
||||
group.add_argument(
|
||||
'-s', '--sort-column',
|
||||
action='append',
|
||||
default=[],
|
||||
dest='sort_columns',
|
||||
metavar='SORT_COLUMN',
|
||||
help=("specify the column(s) to sort the data (columns specified "
|
||||
"first have a priority, non-existing columns are ignored), "
|
||||
"can be repeated")
|
||||
)
|
||||
return parser
|
||||
|
||||
def produce_output(self, parsed_args, column_names, data):
|
||||
if parsed_args.sort_columns and self.need_sort_by_cliff:
|
||||
indexes = [column_names.index(c) for c in parsed_args.sort_columns
|
||||
if c in column_names]
|
||||
if indexes:
|
||||
data = sorted(data, key=operator.itemgetter(*indexes))
|
||||
(columns_to_include, selector) = self._generate_columns_and_selector(
|
||||
parsed_args, column_names)
|
||||
if selector:
|
||||
|
|
|
@ -40,10 +40,15 @@ class ExerciseLister(lister.Lister):
|
|||
def take_action(self, parsed_args):
|
||||
return (
|
||||
parsed_args.columns,
|
||||
[('a', 'A'), ('b', 'B')],
|
||||
[('a', 'A'), ('b', 'B'), ('c', 'A')],
|
||||
)
|
||||
|
||||
|
||||
class ExerciseListerCustomSort(ExerciseLister):
|
||||
|
||||
need_sort_by_cliff = False
|
||||
|
||||
|
||||
class TestLister(base.TestBase):
|
||||
|
||||
def test_formatter_args(self):
|
||||
|
@ -53,20 +58,23 @@ class TestLister(base.TestBase):
|
|||
parsed_args = mock.Mock()
|
||||
parsed_args.columns = ('Col1', 'Col2')
|
||||
parsed_args.formatter = 'test'
|
||||
parsed_args.sort_columns = []
|
||||
|
||||
test_lister.run(parsed_args)
|
||||
|
||||
f = test_lister._formatter_plugins['test']
|
||||
self.assertEqual(1, len(f.args))
|
||||
args = f.args[0]
|
||||
self.assertEqual(list(parsed_args.columns), args[0])
|
||||
data = list(args[1])
|
||||
self.assertEqual([['a', 'A'], ['b', 'B']], data)
|
||||
self.assertEqual([['a', 'A'], ['b', 'B'], ['c', 'A']], data)
|
||||
|
||||
def test_no_exist_column(self):
|
||||
test_lister = ExerciseLister(mock.Mock(), [])
|
||||
parsed_args = mock.Mock()
|
||||
parsed_args.columns = ('no_exist_column',)
|
||||
parsed_args.formatter = 'test'
|
||||
parsed_args.sort_columns = []
|
||||
with mock.patch.object(test_lister, 'take_action') as mock_take_action:
|
||||
mock_take_action.return_value = (('Col1', 'Col2', 'Col3'), [])
|
||||
self.assertRaises(
|
||||
|
@ -74,3 +82,63 @@ class TestLister(base.TestBase):
|
|||
test_lister.run,
|
||||
parsed_args,
|
||||
)
|
||||
|
||||
def test_sort_by_column_cliff_side_procedure(self):
|
||||
test_lister = ExerciseLister(mock.Mock(), [])
|
||||
parsed_args = mock.Mock()
|
||||
parsed_args.columns = ('Col1', 'Col2')
|
||||
parsed_args.formatter = 'test'
|
||||
parsed_args.sort_columns = ['Col2', 'Col1']
|
||||
|
||||
test_lister.run(parsed_args)
|
||||
|
||||
f = test_lister._formatter_plugins['test']
|
||||
args = f.args[0]
|
||||
data = list(args[1])
|
||||
self.assertEqual([['a', 'A'], ['c', 'A'], ['b', 'B']], data)
|
||||
|
||||
def test_sort_by_column_data_already_sorted(self):
|
||||
test_lister = ExerciseListerCustomSort(mock.Mock(), [])
|
||||
parsed_args = mock.Mock()
|
||||
parsed_args.columns = ('Col1', 'Col2')
|
||||
parsed_args.formatter = 'test'
|
||||
parsed_args.sort_columns = ['Col2', 'Col1']
|
||||
|
||||
test_lister.run(parsed_args)
|
||||
|
||||
f = test_lister._formatter_plugins['test']
|
||||
args = f.args[0]
|
||||
data = list(args[1])
|
||||
self.assertEqual([['a', 'A'], ['b', 'B'], ['c', 'A']], data)
|
||||
|
||||
def test_sort_by_non_displayed_column(self):
|
||||
test_lister = ExerciseLister(mock.Mock(), [])
|
||||
parsed_args = mock.Mock()
|
||||
parsed_args.columns = ('Col1',)
|
||||
parsed_args.formatter = 'test'
|
||||
parsed_args.sort_columns = ['Col2']
|
||||
|
||||
with mock.patch.object(test_lister, 'take_action') as mock_take_action:
|
||||
mock_take_action.return_value = (
|
||||
('Col1', 'Col2'), [['a', 'A'], ['b', 'B'], ['c', 'A']]
|
||||
)
|
||||
test_lister.run(parsed_args)
|
||||
|
||||
f = test_lister._formatter_plugins['test']
|
||||
args = f.args[0]
|
||||
data = list(args[1])
|
||||
self.assertEqual([['a'], ['c'], ['b']], data)
|
||||
|
||||
def test_sort_by_non_existing_column(self):
|
||||
test_lister = ExerciseLister(mock.Mock(), [])
|
||||
parsed_args = mock.Mock()
|
||||
parsed_args.columns = ('Col1', 'Col2')
|
||||
parsed_args.formatter = 'test'
|
||||
parsed_args.sort_columns = ['no_exist_column']
|
||||
|
||||
test_lister.run(parsed_args)
|
||||
|
||||
f = test_lister._formatter_plugins['test']
|
||||
args = f.args[0]
|
||||
data = list(args[1])
|
||||
self.assertEqual([['a', 'A'], ['b', 'B'], ['c', 'A']], data)
|
||||
|
|
Loading…
Reference in New Issue