diff options
author | Ryan Petrello <lists@ryanpetrello.com> | 2015-05-06 10:17:30 -0400 |
---|---|---|
committer | Ryan Petrello <lists@ryanpetrello.com> | 2015-05-06 10:17:56 -0400 |
commit | ca2e90a94031fdbba31bf4bda87b51a87e97eb7c (patch) | |
tree | fb7475ce51ddc4d8807900553d52eff682fe4f9e /cliff/formatters | |
parent | 79a8791e7fefdcbe62f264bf905ac09a1958e753 (diff) | |
download | cliff-master.tar.gz |
Diffstat (limited to 'cliff/formatters')
-rw-r--r-- | cliff/formatters/__init__.py | 0 | ||||
-rw-r--r-- | cliff/formatters/base.py | 48 | ||||
-rw-r--r-- | cliff/formatters/commaseparated.py | 35 | ||||
-rw-r--r-- | cliff/formatters/shell.py | 38 | ||||
-rw-r--r-- | cliff/formatters/table.py | 64 |
5 files changed, 0 insertions, 185 deletions
diff --git a/cliff/formatters/__init__.py b/cliff/formatters/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/cliff/formatters/__init__.py +++ /dev/null diff --git a/cliff/formatters/base.py b/cliff/formatters/base.py deleted file mode 100644 index 43b8f17..0000000 --- a/cliff/formatters/base.py +++ /dev/null @@ -1,48 +0,0 @@ -"""Base classes for formatters. -""" - -import abc - - -class Formatter(object): - __metaclass__ = abc.ABCMeta - - @abc.abstractmethod - def add_argument_group(self, parser): - """Add any options to the argument parser. - - Should use our own argument group. - """ - - -class ListFormatter(Formatter): - """Base class for formatters that know how to deal with multiple objects. - """ - __metaclass__ = abc.ABCMeta - - @abc.abstractmethod - def emit_list(self, column_names, data, stdout, parsed_args): - """Format and print the list from the iterable data source. - - :param column_names: names of the columns - :param data: iterable data source, one tuple per object - with values in order of column names - :param stdout: output stream where data should be written - :param parsed_args: argparse namespace from our local options - """ - - -class SingleFormatter(Formatter): - """Base class for formatters that work with single objects. - """ - __metaclass__ = abc.ABCMeta - - @abc.abstractmethod - def emit_one(self, column_names, data, stdout, parsed_args): - """Format and print the values associated with the single object. - - :param column_names: names of the columns - :param data: iterable data source with values in order of column names - :param stdout: output stream where data should be written - :param parsed_args: argparse namespace from our local options - """ diff --git a/cliff/formatters/commaseparated.py b/cliff/formatters/commaseparated.py deleted file mode 100644 index 1e320f3..0000000 --- a/cliff/formatters/commaseparated.py +++ /dev/null @@ -1,35 +0,0 @@ -"""Output formatters using csv format. -""" - -import csv - -from .base import ListFormatter - - -class CSVLister(ListFormatter): - - QUOTE_MODES = { - 'all': csv.QUOTE_ALL, - 'minimal': csv.QUOTE_MINIMAL, - 'nonnumeric': csv.QUOTE_NONNUMERIC, - 'none': csv.QUOTE_NONE, - } - - def add_argument_group(self, parser): - group = parser.add_argument_group('CSV Formatter') - group.add_argument( - '--quote', - choices=sorted(self.QUOTE_MODES.keys()), - dest='quote_mode', - default='nonnumeric', - help='when to include quotes, defaults to nonnumeric', - ) - - def emit_list(self, column_names, data, stdout, parsed_args): - writer = csv.writer(stdout, - quoting=self.QUOTE_MODES[parsed_args.quote_mode], - ) - writer.writerow(column_names) - for row in data: - writer.writerow(row) - return diff --git a/cliff/formatters/shell.py b/cliff/formatters/shell.py deleted file mode 100644 index d1c392b..0000000 --- a/cliff/formatters/shell.py +++ /dev/null @@ -1,38 +0,0 @@ -"""Output formatters using shell syntax. -""" - -from .base import SingleFormatter - - -class ShellFormatter(SingleFormatter): - - def add_argument_group(self, parser): - group = parser.add_argument_group( - title='shell formatter', - description='a format a UNIX shell can parse (variable="value")', - ) - group.add_argument( - '--variable', - action='append', - default=[], - dest='variables', - metavar='VARIABLE', - help='specify the variable(s) to include, can be repeated', - ) - group.add_argument( - '--prefix', - action='store', - default='', - dest='prefix', - help='add a prefix to all variable names', - ) - - def emit_one(self, column_names, data, stdout, parsed_args): - variable_names = [c.lower().replace(' ', '_') - for c in column_names - ] - 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)) - return diff --git a/cliff/formatters/table.py b/cliff/formatters/table.py deleted file mode 100644 index e625a25..0000000 --- a/cliff/formatters/table.py +++ /dev/null @@ -1,64 +0,0 @@ -"""Output formatters using prettytable. -""" - -import prettytable - -from .base import ListFormatter, SingleFormatter - - -class TableFormatter(ListFormatter, SingleFormatter): - - ALIGNMENTS = { - int: 'r', - str: 'l', - float: 'r', - } - try: - ALIGNMENTS[unicode] = 'l' - except NameError: - pass - - def add_argument_group(self, parser): - pass - - def emit_list(self, column_names, data, stdout, parsed_args): - x = prettytable.PrettyTable( - column_names, - print_empty=False, - ) - x.padding_width = 1 - # Figure out the types of the columns in the - # first row and set the alignment of the - # output accordingly. - data_iter = iter(data) - try: - first_row = next(data_iter) - except StopIteration: - pass - else: - for value, name in zip(first_row, column_names): - alignment = self.ALIGNMENTS.get(type(value), 'l') - x.align[name] = alignment - # Now iterate over the data and add the rows. - x.add_row(first_row) - for row in data_iter: - x.add_row(row) - formatted = x.get_string(fields=column_names) - stdout.write(formatted) - stdout.write('\n') - return - - def emit_one(self, column_names, data, stdout, parsed_args): - x = prettytable.PrettyTable(field_names=('Field', 'Value'), - print_empty=False) - x.padding_width = 1 - # Align all columns left because the values are - # not all the same type. - x.align['Field'] = 'l' - x.align['Value'] = 'l' - for name, value in zip(column_names, data): - x.add_row((name, value)) - formatted = x.get_string(fields=('Field', 'Value')) - stdout.write(formatted) - stdout.write('\n') - return |