Fix PrettyPrinter, output verbosity is now slightly more discreete.

This commit is contained in:
Romain Dorgueil
2017-12-01 07:42:23 +01:00
parent d8c0dfe11a
commit db54ff41ef
8 changed files with 36 additions and 19 deletions

View File

@ -19,6 +19,13 @@ class ConvertCommand(BaseCommand):
help=
'Choose the writer factory if it cannot be detected from extension, or if detection is wrong (use - for console pretty print).'
)
parser.add_argument(
'--limit',
'-l',
type=int,
help='Adds a Limit() after the reader instance.',
default=None,
)
parser.add_argument(
'--transformation',
'-t',
@ -57,24 +64,32 @@ class ConvertCommand(BaseCommand):
writer=None,
writer_option=None,
option=None,
transformation=None
limit=None,
transformation=None,
):
reader_factory = default_registry.get_reader_factory_for(input_filename, format=reader)
reader_options = _resolve_options((option or []) + (reader_option or []))
reader_kwargs = _resolve_options((option or []) + (reader_option or []))
if output_filename == '-':
writer_factory = bonobo.PrettyPrinter
writer_args = ()
else:
writer_factory = default_registry.get_writer_factory_for(output_filename, format=writer)
writer_options = _resolve_options((option or []) + (writer_option or []))
writer_args = (output_filename, )
writer_kwargs = _resolve_options((option or []) + (writer_option or []))
transformations = _resolve_transformations(transformation)
transformations = ()
if limit:
transformations += (bonobo.Limit(limit), )
transformations += _resolve_transformations(transformation)
graph = bonobo.Graph()
graph.add_chain(
reader_factory(input_filename, **reader_options),
reader_factory(input_filename, **reader_kwargs),
*transformations,
writer_factory(output_filename, **writer_options),
writer_factory(*writer_args, **writer_kwargs),
)
return bonobo.run(

View File

@ -119,21 +119,21 @@ class PrettyPrinter(Configurable):
return ' '.join(((' ' if index else '-'), str(key), ':', str(value).strip()))
def print_console(self, context, *args, **kwargs):
print('\u250e' + '\u2500' * (self.max_width - 1))
print('\u250c')
for index, (key, value) in enumerate(itertools.chain(enumerate(args), kwargs.items())):
if self.filter(index, key, value):
print(self.format_console(index, key, value, fields=context.get_input_fields()))
print('\u2516' + '\u2500' * (self.max_width - 1))
print('\u2514')
def format_console(self, index, key, value, *, fields=None):
fields = fields or []
if not isinstance(key, str):
if len(fields) >= key and str(key) != str(fields[key]):
if len(fields) > key and str(key) != str(fields[key]):
key = '{}{}'.format(fields[key], term.lightblack('[{}]'.format(key)))
else:
key = str(index)
prefix = '\u2503 {} = '.format(key)
prefix = '\u2502 {} = '.format(key)
prefix_length = len(prefix)
def indent(text, prefix):
@ -141,7 +141,7 @@ class PrettyPrinter(Configurable):
yield (prefix if i else '') + line + CLEAR_EOL + '\n'
repr_of_value = ''.join(
indent(pprint.pformat(value, width=self.max_width - prefix_length), '\u2503' + ' ' * (len(prefix) - 1))
indent(pprint.pformat(value, width=self.max_width - prefix_length), '\u2502' + ' ' * (len(prefix) - 1))
).strip()
return '{}{}{}'.format(prefix, repr_of_value.replace('\n', CLEAR_EOL + '\n'), CLEAR_EOL)