summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Santos <dfsantosbu@unal.edu.co>2019-12-10 00:04:03 +0100
committerHugo van Kemenade <hugovk@users.noreply.github.com>2019-12-10 01:04:03 +0200
commitfa30ea858d0ac41f2f542e0c50d9f9a2e343deb2 (patch)
tree43b8ec6362e4989a9b6669f82d1e15426d3f5f40 /src
parent4de2e17984165400bc8aa610ea61c9f441ccbedb (diff)
downloadtablib-fa30ea858d0ac41f2f542e0c50d9f9a2e343deb2.tar.gz
Implement feature that allows to export tabular data suited to a… (#437)
Diffstat (limited to 'src')
-rw-r--r--src/tablib/formats/__init__.py8
-rw-r--r--src/tablib/formats/_cli.py20
2 files changed, 27 insertions, 1 deletions
diff --git a/src/tablib/formats/__init__.py b/src/tablib/formats/__init__.py
index 5c46008..543f682 100644
--- a/src/tablib/formats/__init__.py
+++ b/src/tablib/formats/__init__.py
@@ -12,6 +12,10 @@ from ._json import JSONFormat
from ._tsv import TSVFormat
uninstalled_format_messages = {
+ 'cli': (
+ "The 'cli' format is not available. You may want to install the tabulate "
+ "package (or `pip install tablib[cli]`)."
+ ),
'df': (
"The 'df' format is not available. You may want to install the pandas "
"package (or `pip install tablib[pandas]`)."
@@ -88,7 +92,7 @@ class Registry:
def register(self, key, format_or_path):
from tablib.core import Databook, Dataset
- # Create Databook.<format> read or read/write properties
+ # Create Databook.<format> read or read/write properties
setattr(Databook, key, ImportExportBookDescriptor(key, format_or_path))
# Create Dataset.<format> read or read/write properties,
@@ -124,6 +128,8 @@ class Registry:
if find_spec('pandas'):
self.register('df', 'tablib.formats._df.DataFrameFormat')
self.register('rst', 'tablib.formats._rst.ReSTFormat')
+ if find_spec('tabulate'):
+ self.register('cli', 'tablib.formats._cli.CLIFormat')
def formats(self):
for key, frm in self._formats.items():
diff --git a/src/tablib/formats/_cli.py b/src/tablib/formats/_cli.py
new file mode 100644
index 0000000..0297ae8
--- /dev/null
+++ b/src/tablib/formats/_cli.py
@@ -0,0 +1,20 @@
+"""Tablib - Command-line Interface table export support.
+
+ Generates a representation for CLI from the dataset.
+ Wrapper for tabulate library.
+"""
+from tabulate import tabulate as Tabulate
+
+
+class CLIFormat:
+ """ Class responsible to export to CLI Format """
+ title = 'cli'
+ DEFAULT_FMT = 'plain'
+
+ @classmethod
+ def export_set(cls, dataset, **kwargs):
+ """Returns CLI representation of a Dataset."""
+ if dataset.headers:
+ kwargs.setdefault('headers', dataset.headers)
+ kwargs.setdefault('tablefmt', cls.DEFAULT_FMT)
+ return Tabulate(dataset, **kwargs)