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
5 # http://www.apache.org/licenses/LICENSE-2.0
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
13 """Base classes for formatters.
21 @six.add_metaclass(abc.ABCMeta)
22 class Formatter(object):
25 def add_argument_group(self, parser):
26 """Add any options to the argument parser.
28 Should use our own argument group.
32 @six.add_metaclass(abc.ABCMeta)
33 class ListFormatter(Formatter):
34 """Base class for formatters that know how to deal with multiple objects.
38 def emit_list(self, column_names, data, stdout, parsed_args):
39 """Format and print the list from the iterable data source.
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
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
56 @six.add_metaclass(abc.ABCMeta)
57 class SingleFormatter(Formatter):
58 """Base class for formatters that work with single objects.
62 def emit_one(self, column_names, data, stdout, parsed_args):
63 """Format and print the values associated with the single object.
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
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