1
0
Fork 0

Merging upstream version 2.2.0.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-02-07 00:48:48 +01:00
parent ab1302c465
commit 95bca6b33d
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
42 changed files with 1085 additions and 840 deletions

View file

@ -4,24 +4,110 @@
from __future__ import unicode_literals
from cli_helpers.utils import filter_dict_by_key
from cli_helpers.compat import (Terminal256Formatter, StringIO)
from .preprocessors import (convert_to_string, truncate_string, override_missing_value,
style_output, HAS_PYGMENTS)
from cli_helpers.compat import Terminal256Formatter, StringIO
from .preprocessors import (
convert_to_string,
truncate_string,
override_missing_value,
style_output,
HAS_PYGMENTS,
escape_newlines,
)
import tabulate
supported_markup_formats = ('mediawiki', 'html', 'latex', 'latex_booktabs',
'textile', 'moinmoin', 'jira')
supported_table_formats = ('plain', 'simple', 'grid', 'fancy_grid', 'pipe',
'orgtbl', 'psql', 'rst')
tabulate.MIN_PADDING = 0
tabulate._table_formats["psql_unicode"] = tabulate.TableFormat(
lineabove=tabulate.Line("", "", "", ""),
linebelowheader=tabulate.Line("", "", "", ""),
linebetweenrows=None,
linebelow=tabulate.Line("", "", "", ""),
headerrow=tabulate.DataRow("", "", ""),
datarow=tabulate.DataRow("", "", ""),
padding=1,
with_header_hide=None,
)
tabulate._table_formats["double"] = tabulate.TableFormat(
lineabove=tabulate.Line("", "", "", ""),
linebelowheader=tabulate.Line("", "", "", ""),
linebetweenrows=None,
linebelow=tabulate.Line("", "", "", ""),
headerrow=tabulate.DataRow("", "", ""),
datarow=tabulate.DataRow("", "", ""),
padding=1,
with_header_hide=None,
)
tabulate._table_formats["ascii"] = tabulate.TableFormat(
lineabove=tabulate.Line("+", "-", "+", "+"),
linebelowheader=tabulate.Line("+", "-", "+", "+"),
linebetweenrows=None,
linebelow=tabulate.Line("+", "-", "+", "+"),
headerrow=tabulate.DataRow("|", "|", "|"),
datarow=tabulate.DataRow("|", "|", "|"),
padding=1,
with_header_hide=None,
)
# "minimal" is the same as "plain", but without headers
tabulate._table_formats["minimal"] = tabulate._table_formats["plain"]
supported_markup_formats = (
"mediawiki",
"html",
"latex",
"latex_booktabs",
"textile",
"moinmoin",
"jira",
)
supported_table_formats = (
"ascii",
"plain",
"simple",
"minimal",
"grid",
"fancy_grid",
"pipe",
"orgtbl",
"psql",
"psql_unicode",
"rst",
"github",
"double",
)
supported_formats = supported_markup_formats + supported_table_formats
preprocessors = (override_missing_value, convert_to_string, truncate_string, style_output)
default_kwargs = {"ascii": {"numalign": "left"}}
headless_formats = ("minimal",)
def get_preprocessors(format_name):
common_formatters = (
override_missing_value,
convert_to_string,
truncate_string,
style_output,
)
if tabulate.multiline_formats.get(format_name):
return common_formatters + (style_output_table(format_name),)
else:
return common_formatters + (escape_newlines, style_output_table(format_name))
def style_output_table(format_name=""):
def style_output(data, headers, style=None,
table_separator_token='Token.Output.TableSeparator', **_):
def style_output(
data,
headers,
style=None,
table_separator_token="Token.Output.TableSeparator",
**_
):
"""Style the *table* a(e.g. bold, italic, and colors)
.. NOTE::
@ -71,24 +157,28 @@ def style_output_table(format_name=""):
if not elt:
return elt
if elt.__class__ == tabulate.Line:
return tabulate.Line(*(style_field(table_separator_token, val) for val in elt))
return tabulate.Line(
*(style_field(table_separator_token, val) for val in elt)
)
if elt.__class__ == tabulate.DataRow:
return tabulate.DataRow(*(style_field(table_separator_token, val) for val in elt))
return tabulate.DataRow(
*(style_field(table_separator_token, val) for val in elt)
)
return elt
srcfmt = tabulate._table_formats[format_name]
newfmt = tabulate.TableFormat(
*(addColorInElt(val) for val in srcfmt))
newfmt = tabulate.TableFormat(*(addColorInElt(val) for val in srcfmt))
tabulate._table_formats[format_name] = newfmt
return iter(data), headers
return style_output
def adapter(data, headers, table_format=None, preserve_whitespace=False,
**kwargs):
def adapter(data, headers, table_format=None, preserve_whitespace=False, **kwargs):
"""Wrap tabulate inside a function for TabularOutputFormatter."""
keys = ('floatfmt', 'numalign', 'stralign', 'showindex', 'disable_numparse')
tkwargs = {'tablefmt': table_format}
keys = ("floatfmt", "numalign", "stralign", "showindex", "disable_numparse")
tkwargs = {"tablefmt": table_format}
tkwargs.update(filter_dict_by_key(kwargs, keys))
if table_format in supported_markup_formats:
@ -96,4 +186,7 @@ def adapter(data, headers, table_format=None, preserve_whitespace=False,
tabulate.PRESERVE_WHITESPACE = preserve_whitespace
return iter(tabulate.tabulate(data, headers, **tkwargs).split('\n'))
tkwargs.update(default_kwargs.get(table_format, {}))
if table_format in headless_formats:
headers = []
return iter(tabulate.tabulate(data, headers, **tkwargs).split("\n"))