920cb327c054b4c0a2aa4511f24cf0fcb5c38ac1
[sdc/sdc-distribution-client.git] /
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 """Base classes for formatters.
14 """
15
16 import abc
17
18 import six
19
20
21 @six.add_metaclass(abc.ABCMeta)
22 class Formatter(object):
23
24     @abc.abstractmethod
25     def add_argument_group(self, parser):
26         """Add any options to the argument parser.
27
28         Should use our own argument group.
29         """
30
31
32 @six.add_metaclass(abc.ABCMeta)
33 class ListFormatter(Formatter):
34     """Base class for formatters that know how to deal with multiple objects.
35     """
36
37     @abc.abstractmethod
38     def emit_list(self, column_names, data, stdout, parsed_args):
39         """Format and print the list from the iterable data source.
40
41         Data values can be primitive types like ints and strings, or
42         can be an instance of a :class:`FormattableColumn` for
43         situations where the value is complex, and may need to be
44         handled differently for human readable output vs. machine
45         readable output.
46
47         :param column_names: names of the columns
48         :param data: iterable data source, one tuple per object
49                      with values in order of column names
50         :param stdout: output stream where data should be written
51         :param parsed_args: argparse namespace from our local options
52
53         """
54
55
56 @six.add_metaclass(abc.ABCMeta)
57 class SingleFormatter(Formatter):
58     """Base class for formatters that work with single objects.
59     """
60
61     @abc.abstractmethod
62     def emit_one(self, column_names, data, stdout, parsed_args):
63         """Format and print the values associated with the single object.
64
65         Data values can be primitive types like ints and strings, or
66         can be an instance of a :class:`FormattableColumn` for
67         situations where the value is complex, and may need to be
68         handled differently for human readable output vs. machine
69         readable output.
70
71         :param column_names: names of the columns
72         :param data: iterable data source with values in order of column names
73         :param stdout: output stream where data should be written
74         :param parsed_args: argparse namespace from our local options
75         """