File: tsv_output_adapter.py

package info (click to toggle)
cli-helpers 2.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 356 kB
  • sloc: python: 2,208; makefile: 16
file content (23 lines) | stat: -rw-r--r-- 909 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# -*- coding: utf-8 -*-
"""A tsv data output adapter"""

from __future__ import unicode_literals

from .preprocessors import bytes_to_string, override_missing_value, convert_to_string
from itertools import chain
from cli_helpers.utils import replace

supported_formats = ("tsv", "tsv_noheader")
preprocessors = (override_missing_value, bytes_to_string, convert_to_string)


def adapter(data, headers, table_format="tsv", **kwargs):
    """Wrap the formatting inside a function for TabularOutputFormatter."""
    if table_format == "tsv":
        for row in chain((headers,), data):
            yield "\t".join((replace(r, (("\n", r"\n"), ("\t", r"\t"))) for r in row))
    elif table_format == "tsv_noheader":
        for row in data:
            yield "\t".join((replace(r, (("\n", r"\n"), ("\t", r"\t"))) for r in row))
    else:
        raise ValueError(f"Invalid table_format specified: {table_format}.")