| 1 |
# -*- coding: utf-8 -*- |
| 2 |
"""Format adapter for the tabulate module.""" |
| 3 |
|
| 4 |
from __future__ import unicode_literals |
| 5 |
|
| 6 |
import os |
| 7 |
|
| 8 |
from cli_helpers.utils import filter_dict_by_key, version_as_tuple |
| 9 |
from cli_helpers.compat import ( |
| 10 |
Terminal256Formatter, |
| 11 |
TerminalTrueColorFormatter, |
| 12 |
Token, |
| 13 |
StringIO, |
| 14 |
) |
| 15 |
from .preprocessors import ( |
| 16 |
convert_to_string, |
| 17 |
truncate_string, |
| 18 |
override_missing_value, |
| 19 |
style_output, |
| 20 |
HAS_PYGMENTS, |
| 21 |
escape_newlines, |
| 22 |
) |
| 23 |
|
| 24 |
import tabulate |
| 25 |
|
| 26 |
|
| 27 |
tabulate.MIN_PADDING = 0 |
| 28 |
|
| 29 |
tabulate._table_formats["psql_unicode"] = tabulate.TableFormat( |
| 30 |
lineabove=tabulate.Line("┌", "─", "┬", "┐"), |
| 31 |
linebelowheader=tabulate.Line("├", "─", "┼", "┤"), |
| 32 |
linebetweenrows=None, |
| 33 |
linebelow=tabulate.Line("└", "─", "┴", "┘"), |
| 34 |
headerrow=tabulate.DataRow("│", "│", "│"), |
| 35 |
datarow=tabulate.DataRow("│", "│", "│"), |
| 36 |
padding=1, |
| 37 |
with_header_hide=None, |
| 38 |
) |
| 39 |
|
| 40 |
tabulate._table_formats["double"] = tabulate.TableFormat( |
| 41 |
lineabove=tabulate.Line("╔", "═", "╦", "╗"), |
| 42 |
linebelowheader=tabulate.Line("╠", "═", "╬", "╣"), |
| 43 |
linebetweenrows=None, |
| 44 |
linebelow=tabulate.Line("╚", "═", "╩", "╝"), |
| 45 |
headerrow=tabulate.DataRow("║", "║", "║"), |
| 46 |
datarow=tabulate.DataRow("║", "║", "║"), |
| 47 |
padding=1, |
| 48 |
with_header_hide=None, |
| 49 |
) |
| 50 |
|
| 51 |
tabulate._table_formats["ascii"] = tabulate.TableFormat( |
| 52 |
lineabove=tabulate.Line("+", "-", "+", "+"), |
| 53 |
linebelowheader=tabulate.Line("+", "-", "+", "+"), |
| 54 |
linebetweenrows=None, |
| 55 |
linebelow=tabulate.Line("+", "-", "+", "+"), |
| 56 |
headerrow=tabulate.DataRow("|", "|", "|"), |
| 57 |
datarow=tabulate.DataRow("|", "|", "|"), |
| 58 |
padding=1, |
| 59 |
with_header_hide=None, |
| 60 |
) |
| 61 |
|
| 62 |
tabulate._table_formats["ascii_escaped"] = tabulate.TableFormat( |
| 63 |
lineabove=tabulate.Line("+", "-", "+", "+"), |
| 64 |
linebelowheader=tabulate.Line("+", "-", "+", "+"), |
| 65 |
linebetweenrows=None, |
| 66 |
linebelow=tabulate.Line("+", "-", "+", "+"), |
| 67 |
headerrow=tabulate.DataRow("|", "|", "|"), |
| 68 |
datarow=tabulate.DataRow("|", "|", "|"), |
| 69 |
padding=1, |
| 70 |
with_header_hide=None, |
| 71 |
) |
| 72 |
|
| 73 |
tabulate._table_formats["mysql"] = tabulate.TableFormat( |
| 74 |
lineabove=tabulate.Line("+", "-", "+", "+"), |
| 75 |
linebelowheader=tabulate.Line("+", "-", "+", "+"), |
| 76 |
linebetweenrows=None, |
| 77 |
linebelow=tabulate.Line("+", "-", "+", "+"), |
| 78 |
headerrow=tabulate.DataRow("|", "|", "|"), |
| 79 |
datarow=tabulate.DataRow("|", "|", "|"), |
| 80 |
padding=1, |
| 81 |
with_header_hide=None, |
| 82 |
) |
| 83 |
|
| 84 |
tabulate._table_formats["mysql_unicode"] = tabulate.TableFormat( |
| 85 |
lineabove=tabulate.Line("┌", "─", "┬", "┐"), |
| 86 |
linebelowheader=tabulate.Line("├", "─", "┼", "┤"), |
| 87 |
linebetweenrows=None, |
| 88 |
linebelow=tabulate.Line("└", "─", "┴", "┘"), |
| 89 |
headerrow=tabulate.DataRow("│", "│", "│"), |
| 90 |
datarow=tabulate.DataRow("│", "│", "│"), |
| 91 |
padding=1, |
| 92 |
with_header_hide=None, |
| 93 |
) |
| 94 |
|
| 95 |
tabulate._table_formats["mysql_heavy"] = tabulate.TableFormat( |
| 96 |
lineabove=tabulate.Line("┏", "━", "┳", "┓"), |
| 97 |
linebelowheader=tabulate.Line("┣", "━", "╋", "┫"), |
| 98 |
linebetweenrows=None, |
| 99 |
linebelow=tabulate.Line("┗", "━", "┻", "┛"), |
| 100 |
headerrow=tabulate.DataRow("┃", "┃", "┃"), |
| 101 |
datarow=tabulate.DataRow("┃", "┃", "┃"), |
| 102 |
padding=1, |
| 103 |
with_header_hide=None, |
| 104 |
) |
| 105 |
|
| 106 |
# "minimal" is the same as "plain", but without headers |
| 107 |
tabulate._table_formats["minimal"] = tabulate._table_formats["plain"] |
| 108 |
|
| 109 |
tabulate.multiline_formats["psql_unicode"] = "psql_unicode" |
| 110 |
tabulate.multiline_formats["double"] = "double" |
| 111 |
tabulate.multiline_formats["ascii"] = "ascii" |
| 112 |
tabulate.multiline_formats["minimal"] = "minimal" |
| 113 |
tabulate.multiline_formats["mysql"] = "mysql" |
| 114 |
tabulate.multiline_formats["mysql_unicode"] = "mysql_unicode" |
| 115 |
|
| 116 |
supported_markup_formats = ( |
| 117 |
"mediawiki", |
| 118 |
"html", |
| 119 |
"latex", |
| 120 |
"latex_booktabs", |
| 121 |
"textile", |
| 122 |
"moinmoin", |
| 123 |
"jira", |
| 124 |
) |
| 125 |
supported_table_formats = ( |
| 126 |
"ascii", |
| 127 |
"ascii_escaped", |
| 128 |
"plain", |
| 129 |
"simple", |
| 130 |
"minimal", |
| 131 |
"grid", |
| 132 |
"fancy_grid", |
| 133 |
"pipe", |
| 134 |
"orgtbl", |
| 135 |
"psql", |
| 136 |
"psql_unicode", |
| 137 |
"rst", |
| 138 |
"github", |
| 139 |
"double", |
| 140 |
"mysql", |
| 141 |
"mysql_unicode", |
| 142 |
"mysql_heavy", |
| 143 |
) |
| 144 |
|
| 145 |
supported_formats = supported_markup_formats + supported_table_formats |
| 146 |
|
| 147 |
default_kwargs = { |
| 148 |
"ascii": {"numalign": "left"}, |
| 149 |
"ascii_escaped": {"numalign": "left"}, |
| 150 |
"mysql": {"numalign": "right"}, |
| 151 |
"mysql_unicode": {"numalign": "right"}, |
| 152 |
} |
| 153 |
headless_formats = ("minimal",) |
| 154 |
|
| 155 |
|
| 156 |
def has_new_preserve_whitespace_arg(): |
| 157 |
return version_as_tuple(tabulate.__version__) >= (0, 10, 0) |
| 158 |
|
| 159 |
|
| 160 |
def get_preprocessors(format_name): |
| 161 |
common_formatters = ( |
| 162 |
override_missing_value, |
| 163 |
convert_to_string, |
| 164 |
truncate_string, |
| 165 |
style_output, |
| 166 |
) |
| 167 |
|
| 168 |
if tabulate.multiline_formats.get(format_name): |
| 169 |
return common_formatters + (style_output_table(format_name),) |
| 170 |
else: |
| 171 |
return common_formatters + (escape_newlines, style_output_table(format_name)) |
| 172 |
|
| 173 |
|
| 174 |
def style_output_table(format_name=""): |
| 175 |
def style_output( |
| 176 |
data, |
| 177 |
headers, |
| 178 |
style=None, |
| 179 |
table_separator_token=Token.Output.TableSeparator, |
| 180 |
**_, |
| 181 |
): |
| 182 |
"""Style the *table* a(e.g. bold, italic, and colors) |
| 183 |
|
| 184 |
.. NOTE:: |
| 185 |
This requires the `Pygments <http://pygments.org/>`_ library to |
| 186 |
be installed. You can install it with CLI Helpers as an extra:: |
| 187 |
$ pip install cli_helpers[styles] |
| 188 |
|
| 189 |
Example usage:: |
| 190 |
|
| 191 |
from cli_helpers.tabular_output import tabulate_adapter |
| 192 |
from pygments.style import Style |
| 193 |
from pygments.token import Token |
| 194 |
|
| 195 |
class YourStyle(Style): |
| 196 |
default_style = "" |
| 197 |
styles = { |
| 198 |
Token.Output.TableSeparator: '#ansigray' |
| 199 |
} |
| 200 |
|
| 201 |
headers = ('First Name', 'Last Name') |
| 202 |
data = [['Fred', 'Roberts'], ['George', 'Smith']] |
| 203 |
style_output_table = tabulate_adapter.style_output_table('psql') |
| 204 |
style_output_table(data, headers, style=CliStyle) |
| 205 |
|
| 206 |
data, headers = style_output(data, headers, style=YourStyle) |
| 207 |
output = tabulate_adapter.adapter(data, headers, style=YourStyle) |
| 208 |
|
| 209 |
:param iterable data: An :term:`iterable` (e.g. list) of rows. |
| 210 |
:param iterable headers: The column headers. |
| 211 |
:param str/pygments.style.Style style: A Pygments style. You can `create |
| 212 |
your own styles <https://pygments.org/docs/styles#creating-own-styles>`_. |
| 213 |
:param str table_separator_token: The token type to be used for the table separator. |
| 214 |
:return: data and headers. |
| 215 |
:rtype: tuple |
| 216 |
|
| 217 |
""" |
| 218 |
if style and HAS_PYGMENTS and format_name in supported_table_formats: |
| 219 |
if "truecolor" in os.getenv("COLORTERM", "").lower(): |
| 220 |
formatter = TerminalTrueColorFormatter(style=style) |
| 221 |
else: |
| 222 |
formatter = Terminal256Formatter(style=style) |
| 223 |
|
| 224 |
def style_field(token, field): |
| 225 |
"""Get the styled text for a *field* using *token* type.""" |
| 226 |
s = StringIO() |
| 227 |
formatter.format(((token, field),), s) |
| 228 |
return s.getvalue() |
| 229 |
|
| 230 |
def addColorInElt(elt): |
| 231 |
if not elt: |
| 232 |
return elt |
| 233 |
if elt.__class__ == tabulate.Line: |
| 234 |
return tabulate.Line( |
| 235 |
*(style_field(table_separator_token, val) for val in elt) |
| 236 |
) |
| 237 |
if elt.__class__ == tabulate.DataRow: |
| 238 |
return tabulate.DataRow( |
| 239 |
*(style_field(table_separator_token, val) for val in elt) |
| 240 |
) |
| 241 |
return elt |
| 242 |
|
| 243 |
srcfmt = tabulate._table_formats[format_name] |
| 244 |
newfmt = tabulate.TableFormat(*(addColorInElt(val) for val in srcfmt)) |
| 245 |
tabulate._table_formats[format_name] = newfmt |
| 246 |
|
| 247 |
return iter(data), headers |
| 248 |
|
| 249 |
return style_output |
| 250 |
|
| 251 |
|
| 252 |
def adapter(data, headers, table_format=None, preserve_whitespace=False, **kwargs): |
| 253 |
"""Wrap tabulate inside a function for TabularOutputFormatter.""" |
| 254 |
keys = ( |
| 255 |
"floatfmt", |
| 256 |
"numalign", |
| 257 |
"stralign", |
| 258 |
"showindex", |
| 259 |
"disable_numparse", |
| 260 |
"colalign", |
| 261 |
) |
| 262 |
tkwargs = {"tablefmt": table_format} |
| 263 |
tkwargs.update(filter_dict_by_key(kwargs, keys)) |
| 264 |
|
| 265 |
if table_format in supported_markup_formats: |
| 266 |
tkwargs.update(numalign=None, stralign=None) |
| 267 |
|
| 268 |
if has_new_preserve_whitespace_arg(): |
| 269 |
tkwargs.update(preserve_whitespace=preserve_whitespace) |
| 270 |
else: |
| 271 |
tabulate.PRESERVE_WHITESPACE = preserve_whitespace |
| 272 |
|
| 273 |
tkwargs.update(default_kwargs.get(table_format, {})) |
| 274 |
if table_format in headless_formats: |
| 275 |
headers = [] |
| 276 |
return iter(tabulate.tabulate(data, headers, **tkwargs).split("\n")) |