-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
305 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
"""Tabulate extension module.""" | ||
|
||
import sys | ||
from tabulate import tabulate | ||
from ..core import output, exc, handler | ||
from ..utils.misc import minimal_logger | ||
|
||
LOG = minimal_logger(__name__) | ||
|
||
|
||
class TabulateOutputHandler(output.CementOutputHandler): | ||
|
||
""" | ||
This class implements the :ref:`IOutput <cement.core.output>` | ||
interface. It provides tabularized text output using the | ||
`Tabulate <https://pypi.python.org/pypi/tabulate>`_ module. Please | ||
see the developer documentation on | ||
:ref:`Output Handling <dev_output_handling>`. | ||
**Note** This extension has an external dependency on ``tabulate``. You | ||
must include ``tabulate`` in your applications dependencies as Cement | ||
explicitly does **not** include external dependencies for optional | ||
extensions. | ||
Usage: | ||
.. code-block:: python | ||
from cement.core import foundation | ||
class MyApp(foundation.CementApp): | ||
class Meta: | ||
label = 'myapp' | ||
extensions = ['tabulate'] | ||
output_handler = 'tabulate' | ||
# ... | ||
Usage: | ||
.. code-block:: python | ||
# create a dataset | ||
headers = ['NAME', 'AGE', 'ADDRESS'] | ||
data = [ | ||
["Krystin Bartoletti", 47, "PSC 7591, Box 425, APO AP 68379"], | ||
["Cris Hegan", 54, "322 Reubin Islands, Leylabury, NC 34388"], | ||
["George Champlin", 25, "Unit 6559, Box 124, DPO AA 25518"], | ||
] | ||
# via the app object | ||
myapp.render(data, headers=headers) | ||
# or from within a controller or handler | ||
self.app.render(data, headers=headers) | ||
Looks like: | ||
.. code-block:: console | ||
| NAME | AGE | ADDRESS | | ||
|--------------------+-----+-----------------------------------------| | ||
| Krystin Bartoletti | 47 | PSC 7591, Box 425, APO AP 68379 | | ||
| Cris Hegan | 54 | 322 Reubin Islands, Leylabury, NC 34388 | | ||
| George Champlin | 25 | Unit 6559, Box 124, DPO AA 25518 | | ||
Configuration: | ||
This extension does not support any configuration file settings. | ||
""" | ||
|
||
class Meta: | ||
|
||
"""Handler meta-data.""" | ||
|
||
interface = output.IOutput | ||
label = 'tabulate' | ||
|
||
#: Whether or not to pad the output with an extra pre/post '\n' | ||
padding = True | ||
|
||
#: Default template format. See the ``tabulate`` documentation for | ||
#: all supported template formats. | ||
format = 'orgtbl' | ||
|
||
#: Default headers to use. | ||
headers = [] | ||
|
||
#: Default alignment for string columns. See the ``tabulate`` | ||
#: documentation for all supported ``stralign`` options. | ||
string_alignment = 'left' | ||
|
||
#: Default alignment for numeric columns. See the ``tabulate`` | ||
#: documentation for all supported ``numalign`` options. | ||
numeric_alignment = 'decimal' | ||
|
||
#: String format to use for float values. | ||
float_format = 'g' | ||
|
||
#: Default replacement for missing value. | ||
missing_value = '' | ||
|
||
#: Whether or not to include ``tabulate`` as an available to choice | ||
#: to override the ``output_handler`` via command line options. | ||
overridable = False | ||
|
||
|
||
|
||
def render(self, data, **kw): | ||
""" | ||
Take a data dictionary and render it into a table. Additional | ||
keyword arguments are passed directly to ``tabulate.tabulate``. | ||
Required Arguments: | ||
:param data_dict: The data dictionary to render. | ||
:returns: str (the rendered template text) | ||
""" | ||
headers = kw.get('headers', self._meta.headers) | ||
|
||
out = tabulate(data, headers, | ||
tablefmt=kw.get('tablefmt', self._meta.format), | ||
stralign=kw.get('stralign', self._meta.string_alignment), | ||
numalign=kw.get('numalign', self._meta.numeric_alignment), | ||
missingval=kw.get('missingval', self._meta.missing_value), | ||
floatfmt=kw.get('floatfmt', self._meta.float_format), | ||
) | ||
out = out + '\n' | ||
|
||
if self._meta.padding is True: | ||
out = '\n' + out + '\n' | ||
|
||
return out | ||
|
||
|
||
def load(app): | ||
handler.register(TabulateOutputHandler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.. _cement.ext.ext_tabulate: | ||
|
||
:mod:`cement.ext.ext_tabulate` | ||
------------------------------ | ||
|
||
.. automodule:: cement.ext.ext_tabulate | ||
:members: | ||
:private-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ with Cement. | |
bash_auto_completion | ||
arbitrary_extra_arguments | ||
reload_config | ||
tabularized_output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
Tabularized Output | ||
================== | ||
|
||
Users familiar with MySQL, PGSQL, etc find comfort in table-based output | ||
patterns. For one it adds structure, and two generally makes things much | ||
more readable. The folloiwing is an example of a simple app using the | ||
:ref:`Tabulate <cement.ext.ext_tabulate>` extension: | ||
|
||
.. code-block:: python | ||
from cement.core.foundation import CementApp | ||
from cement.core.controller import expose, CementBaseController | ||
class MyController(CementBaseController): | ||
class Meta: | ||
label = 'base' | ||
@expose(hide=True) | ||
def default(self): | ||
headers=['NAME', 'AGE', 'ADDRESS'] | ||
data=[ | ||
["Krystin Bartoletti", 47, "PSC 7591, Box 425, APO AP 68379"], | ||
["Cris Hegan", 54, "322 Reubin Islands, Leylabury, NC 34388"], | ||
["George Champlin", 25, "Unit 6559, Box 124, DPO AA 25518"], | ||
] | ||
self.app.render(data, headers=headers) | ||
class MyApp(CementApp): | ||
class Meta: | ||
label = 'myapp' | ||
extensions = ['tabulate'] | ||
output_handler = 'tabulate' | ||
handlers = [MyController] | ||
with MyApp() as app: | ||
app.run() | ||
The output looks like: | ||
|
||
.. code-block:: console | ||
$ python myapp.py | ||
| NAME | AGE | ADDRESS | | ||
|--------------------+-------+-----------------------------------------| | ||
| Krystin Bartoletti | 47 | PSC 7591, Box 425, APO AP 68379 | | ||
| Cris Hegan | 54 | 322 Reubin Islands, Leylabury, NC 34388 | | ||
| George Champlin | 25 | Unit 6559, Box 124, DPO AA 25518 | |
Oops, something went wrong.