From 19b3d875fddc4e9d4e94741dc9a5b98c76dc9087 Mon Sep 17 00:00:00 2001 From: Daniel Santos Date: Wed, 30 Oct 2019 12:50:33 +0100 Subject: Implement feature new format: Cli. Generate adapter for tabulate. This close issue #340 Write respective tests. --- src/tablib/core.py | 8 ++++++++ src/tablib/formats/__init__.py | 3 ++- src/tablib/formats/_cli.py | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/tablib/formats/_cli.py (limited to 'src') diff --git a/src/tablib/core.py b/src/tablib/core.py index 17d9621..e0c4327 100644 --- a/src/tablib/core.py +++ b/src/tablib/core.py @@ -621,6 +621,14 @@ class Dataset: """ pass + @property + def cli(): + """A CLI table representation of the :class:`Dataset` object. + + .. note:: This method can be used for export only. + """ + pass + @property def jira(): """A Jira table representation of the :class:`Dataset` object. diff --git a/src/tablib/formats/__init__.py b/src/tablib/formats/__init__.py index 12f2610..b0afeec 100644 --- a/src/tablib/formats/__init__.py +++ b/src/tablib/formats/__init__.py @@ -14,6 +14,7 @@ from . import _tsv as tsv from . import _xls as xls from . import _xlsx as xlsx from . import _yaml as yaml +from . import _cli as cli # xlsx before as xls (xlrd) can also read xlsx -available = (json, xlsx, xls, yaml, csv, dbf, tsv, html, jira, latex, ods, df, rst) +available = (json, xlsx, xls, yaml, csv, dbf, tsv, html, jira, latex, ods, df, rst, cli) diff --git a/src/tablib/formats/_cli.py b/src/tablib/formats/_cli.py new file mode 100644 index 0000000..e00d4db --- /dev/null +++ b/src/tablib/formats/_cli.py @@ -0,0 +1,16 @@ +""" Tablib - CLI Support +""" +from tabulate import tabulate + +title = 'cli' +extensions = ('txt',) + +DEFAULT_FORMAT = 'plain' + +def export_set(dataset, **kwargs): + """Returns CLI representation of Dataset.""" + if( dataset.headers is not None ): + kwargs.setdefault('headers', dataset.headers) + kwargs.setdefault('tablefmt', DEFAULT_FORMAT) + return tabulate( dataset, **kwargs) + -- cgit v1.2.1