| 1 |
|
| 2 |
"""A tsv data output adapter""" |
| 3 |
|
| 4 |
from __future__ import unicode_literals |
| 5 |
|
| 6 |
from .preprocessors import bytes_to_string, override_missing_value, convert_to_string |
| 7 |
from itertools import chain |
| 8 |
from cli_helpers.utils import replace |
| 9 |
|
| 10 |
supported_formats = ("tsv", "tsv_noheader") |
| 11 |
preprocessors = (override_missing_value, bytes_to_string, convert_to_string) |
| 12 |
|
| 13 |
|
| 14 |
def adapter(data, headers, table_format="tsv", **kwargs): |
| 15 |
"""Wrap the formatting inside a function for TabularOutputFormatter.""" |
| 16 |
if table_format == "tsv": |
| 17 |
for row in chain((headers,), data): |
| 18 |
yield "\t".join((replace(r, (("\n", r"\n"), ("\t", r"\t"))) for r in row)) |
| 19 |
elif table_format == "tsv_noheader": |
| 20 |
for row in data: |
| 21 |
yield "\t".join((replace(r, (("\n", r"\n"), ("\t", r"\t"))) for r in row)) |
| 22 |
else: |
| 23 |
raise ValueError(f"Invalid table_format specified: {table_format}.") |