/usr/lib/python3/dist-packages/cement/ext/ext_tabulate.py is in python3-cement 2.10.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | """
The Tabulate Extension provides output handling based on the
`Tabulate <https://pypi.python.org/pypi/tabulate>`_ library. It's format is
familiar to users of MySQL, Postgres, etc.
Requirements
------------
* Tabulate (``pip install tabulate``)
Configuration
-------------
This extension does not support any configuration settings.
Usage
-----
.. code-block:: python
from cement.core import foundation
class MyApp(foundation.CementApp):
class Meta:
label = 'myapp'
extensions = ['tabulate']
output_handler = 'tabulate'
with MyApp() as app:
app.run()
# 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"],
]
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 |
"""
from tabulate import tabulate
from ..core import output
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.
"""
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):
app.handler.register(TabulateOutputHandler)
|