diff options
author | Doug Hellmann <doug.hellmann@dreamhost.com> | 2012-05-06 20:06:17 -0400 |
---|---|---|
committer | Doug Hellmann <doug.hellmann@dreamhost.com> | 2012-05-06 20:06:17 -0400 |
commit | 6efada5b6a084e5e22e3b7a225375ff8132be255 (patch) | |
tree | 12075126f5e2bd3708ece4b1bbf995027b452320 | |
parent | 2acfc3ce1e6d88a9acbd994360388927eb25c844 (diff) | |
download | cliff-tablib-6efada5b6a084e5e22e3b7a225375ff8132be255.tar.gz |
add yaml, json, and html formatters
-rw-r--r-- | cliff/formatters/tablibformatters.py | 47 | ||||
-rw-r--r-- | docs/source/list_commands.rst | 64 | ||||
-rw-r--r-- | setup.py | 4 |
3 files changed, 111 insertions, 4 deletions
diff --git a/cliff/formatters/tablibformatters.py b/cliff/formatters/tablibformatters.py new file mode 100644 index 0000000..5cc5489 --- /dev/null +++ b/cliff/formatters/tablibformatters.py @@ -0,0 +1,47 @@ +"""Output formatters using tablib. +""" + +from .base import ListFormatter, SingleFormatter + +import tablib + + +class TablibFormatterBase(ListFormatter, SingleFormatter): + + def add_argument_group(self, parser): + return + + def emit_list(self, column_names, data, stdout, parsed_args): + dataset = tablib.Dataset(headers=column_names) + for row in data: + dataset.append(row) + stdout.write(self._format_dataset(dataset)) + return + + def emit_one(self, column_names, data, stdout, parsed_args): + dataset = tablib.Dataset(headers=column_names) + for row in data: + dataset.append(row) + stdout.write(self._format_dataset(dataset)) + return + + +class YamlFormatter(TablibFormatterBase): + """YAML output""" + + def _format_dataset(self, dataset): + return dataset.yaml + + +class HtmlFormatter(TablibFormatterBase): + """HTML output""" + + def _format_dataset(self, dataset): + return dataset.html + + +class JsonFormatter(TablibFormatterBase): + """JSON output""" + + def _format_dataset(self, dataset): + return dataset.json diff --git a/docs/source/list_commands.rst b/docs/source/list_commands.rst index 2dbf748..fbca699 100644 --- a/docs/source/list_commands.rst +++ b/docs/source/list_commands.rst @@ -43,11 +43,23 @@ further manipulation. "Makefile",5569 "source",408 -PrettyTable ------------ +json +---- -The ``PrettyTable`` formatter uses PrettyTable_ to produce output -formatted for human consumption. +The ``json`` formatter uses tablib_ to produce JSON output. + +:: + + (.venv)$ cliffdemo files -f json + [{"Name": "build", "Size": 136}, {"Name": "cliffdemo.log", "Size": + 3461}, {"Name": "Makefile", "Size": 5569}, {"Name": + "requirements.txt", "Size": 33}, {"Name": "source", "Size": 782}] + +table +----- + +The ``table`` formatter uses PrettyTable_ to produce output formatted +for human consumption. .. _PrettyTable: http://code.google.com/p/prettytable/ @@ -63,6 +75,50 @@ formatted for human consumption. | source | 408 | +---------------+------+ +yaml +---- + +The ``yaml`` formatter uses tablib_ to produce YAML output as a +sequence of mappings. + +:: + + (.venv)$ cliffdemo files -f yaml + - {Name: build, Size: 136} + - {Name: cliffdemo.log, Size: 3043} + - {Name: Makefile, Size: 5569} + - {Name: requirements.txt, Size: 33} + - {Name: source, Size: 816} + +.. _tablib: https://github.com/kennethreitz/tablib + + +html +---- + +The ``html`` formatter uses tablib_ to produce HTML output as a +sequence of mappings. + +:: + + (.venv)$ cliffdemo files -f html + <table> + <thead> + <tr><th>Name</th> + <th>Size</th></tr> + </thead> + <tr><td>build</td> + <td>136</td></tr> + <tr><td>cliffdemo.log</td> + <td>3252</td></tr> + <tr><td>Makefile</td> + <td>5569</td></tr> + <tr><td>requirements.txt</td> + <td>33</td></tr> + <tr><td>source</td> + <td>782</td></tr> + </table> + Creating Your Own Formatter --------------------------- @@ -148,6 +148,7 @@ setup( install_requires=['distribute', 'PrettyTable>=0.6', 'cmd2', + 'tablib', ], namespace_packages=[], @@ -165,6 +166,9 @@ setup( 'cliff.formatter.list': [ 'table = cliff.formatters.table:TableFormatter', 'csv = cliff.formatters.commaseparated:CSVLister', + 'yaml = cliff.formatters.tablibformatters:YamlFormatter', + 'html = cliff.formatters.tablibformatters:HtmlFormatter', + 'json = cliff.formatters.tablibformatters:JsonFormatter', ], 'cliff.formatter.show': [ 'table = cliff.formatters.table:TableFormatter', |