1#  Licensed under the Apache License, Version 2.0 (the "License"); you may
2#  not use this file except in compliance with the License. You may obtain
3#  a copy of the License at
4#
5#       http://www.apache.org/licenses/LICENSE-2.0
6#
7#  Unless required by applicable law or agreed to in writing, software
8#  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10#  License for the specific language governing permissions and limitations
11#  under the License.
12
13"""Application base class for displaying data about a single object.
14"""
15import abc
16
17import six
18
19from . import display
20
21
22@six.add_metaclass(abc.ABCMeta)
23class ShowOne(display.DisplayCommandBase):
24    """Command base class for displaying data about a single object.
25    """
26
27    @property
28    def formatter_namespace(self):
29        return 'cliff.formatter.show'
30
31    @property
32    def formatter_default(self):
33        return 'table'
34
35    @abc.abstractmethod
36    def take_action(self, parsed_args):
37        """Return a two-part tuple with a tuple of column names
38        and a tuple of values.
39        """
40
41    def produce_output(self, parsed_args, column_names, data):
42        (columns_to_include, selector) = self._generate_columns_and_selector(
43            parsed_args, column_names)
44        if selector:
45            data = list(self._compress_iterable(data, selector))
46        self.formatter.emit_one(columns_to_include,
47                                data,
48                                self.app.stdout,
49                                parsed_args)
50        return 0
51
52    def dict2columns(self, data):
53        """Implement the common task of converting a dict-based object
54        to the two-column output that ShowOne expects.
55        """
56        if not data:
57            return ({}, {})
58        else:
59            return zip(*sorted(data.items()))
60