jp (common): various Table fixes:
- renamed fromDict to more accurate fromListDict - fixed handling of None filter in fromListDict - fixes show_header when no header exist - escape headers when using namedtuple, so it won't fail
This commit is contained in:
parent
1d91db6f2c
commit
da3b7b5fea
3 changed files with 20 additions and 12 deletions
|
|
@ -316,7 +316,7 @@ class InviteesList(base.CommandBase):
|
||||||
|
|
||||||
show_table = OUTPUT_OPT_TABLE in self.args.output_opts
|
show_table = OUTPUT_OPT_TABLE in self.args.output_opts
|
||||||
|
|
||||||
table = common.Table.fromDict(
|
table = common.Table.fromListDict(
|
||||||
self.host,
|
self.host,
|
||||||
data,
|
data,
|
||||||
("nick",)
|
("nick",)
|
||||||
|
|
|
||||||
|
|
@ -642,7 +642,7 @@ class ShareList(base.CommandBase):
|
||||||
show_header = True
|
show_header = True
|
||||||
keys = ("name", "type", "size", "file_hash")
|
keys = ("name", "type", "size", "file_hash")
|
||||||
headers = ("name", "type", "size", "hash")
|
headers = ("name", "type", "size", "hash")
|
||||||
table = common.Table.fromDict(
|
table = common.Table.fromListDict(
|
||||||
self.host,
|
self.host,
|
||||||
files_data,
|
files_data,
|
||||||
keys=keys,
|
keys=keys,
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@ import time
|
||||||
import tempfile
|
import tempfile
|
||||||
import asyncio
|
import asyncio
|
||||||
import shlex
|
import shlex
|
||||||
|
import re
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from sat_frontends.jp.constants import Const as C
|
from sat_frontends.jp.constants import Const as C
|
||||||
from sat.core.i18n import _
|
from sat.core.i18n import _
|
||||||
|
|
@ -479,7 +480,8 @@ class Table(object):
|
||||||
- RowData with all columns values
|
- RowData with all columns values
|
||||||
if may also only use 1 argument, which will then be current col value.
|
if may also only use 1 argument, which will then be current col value.
|
||||||
the callable must return a string
|
the callable must return a string
|
||||||
if it's unicode, it will be used with .format and must countain u'{}' which will be replaced with the string
|
if it's unicode, it will be used with .format and must countain u'{}' which
|
||||||
|
will be replaced with the string.
|
||||||
if not None, must have same number of columns as data
|
if not None, must have same number of columns as data
|
||||||
@param use_buffer(bool): if True, bufferise output instead of printing it directly
|
@param use_buffer(bool): if True, bufferise output instead of printing it directly
|
||||||
"""
|
"""
|
||||||
|
|
@ -495,7 +497,9 @@ class Table(object):
|
||||||
|
|
||||||
size = None
|
size = None
|
||||||
if headers:
|
if headers:
|
||||||
row_cls = namedtuple("RowData", headers)
|
# we use a namedtuple to make the value easily accessible from filters
|
||||||
|
headers_safe = [re.sub(r'[^a-zA-Z_]', '_', h) for h in headers]
|
||||||
|
row_cls = namedtuple("RowData", headers_safe)
|
||||||
else:
|
else:
|
||||||
row_cls = tuple
|
row_cls = tuple
|
||||||
|
|
||||||
|
|
@ -512,8 +516,8 @@ class Table(object):
|
||||||
col_value = filter_(value, row_cls(*row_data_list))
|
col_value = filter_(value, row_cls(*row_data_list))
|
||||||
except TypeError:
|
except TypeError:
|
||||||
col_value = filter_(value)
|
col_value = filter_(value)
|
||||||
# we count size without ANSI code as they will change length of the string
|
# we count size without ANSI code as they will change length of the
|
||||||
# when it's mostly style/color changes.
|
# string when it's mostly style/color changes.
|
||||||
col_size = len(regex.ansiRemove(col_value))
|
col_size = len(regex.ansiRemove(col_value))
|
||||||
else:
|
else:
|
||||||
col_value = str(value)
|
col_value = str(value)
|
||||||
|
|
@ -557,11 +561,12 @@ class Table(object):
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def fromDict(cls, host, data, keys=None, headers=None, filters=None, defaults=None):
|
def fromListDict(
|
||||||
"""Prepare a table to display it
|
cls, host, data, keys=None, headers=None, filters=None, defaults=None):
|
||||||
|
"""Create a table from a list of dictionaries
|
||||||
|
|
||||||
the whole data will be read and kept into memory,
|
each dictionary is a row of the table, keys being columns names.
|
||||||
to be printed
|
the whole data will be read and kept into memory, to be printed
|
||||||
@param data(list[dict[unicode, unicode]]): data to create the table from
|
@param data(list[dict[unicode, unicode]]): data to create the table from
|
||||||
@param keys(iterable[unicode], None): keys to get
|
@param keys(iterable[unicode], None): keys to get
|
||||||
if None, all keys will be used
|
if None, all keys will be used
|
||||||
|
|
@ -579,6 +584,8 @@ class Table(object):
|
||||||
keys = list(data[0].keys())
|
keys = list(data[0].keys())
|
||||||
if headers is None:
|
if headers is None:
|
||||||
headers = keys
|
headers = keys
|
||||||
|
if filters is None:
|
||||||
|
filters = {}
|
||||||
filters = [filters.get(k) for k in keys]
|
filters = [filters.get(k) for k in keys]
|
||||||
return cls(
|
return cls(
|
||||||
host, (cls.readDictValues(d, keys, defaults) for d in data), headers, filters
|
host, (cls.readDictValues(d, keys, defaults) for d in data), headers, filters
|
||||||
|
|
@ -650,7 +657,8 @@ class Table(object):
|
||||||
@param show_borders(bool): True if borders need no be shown
|
@param show_borders(bool): True if borders need no be shown
|
||||||
@param hide_cols(None, iterable(unicode)): columns which should not be displayed
|
@param hide_cols(None, iterable(unicode)): columns which should not be displayed
|
||||||
@param head_alignment(unicode): how to align headers, can be left, center or right
|
@param head_alignment(unicode): how to align headers, can be left, center or right
|
||||||
@param columns_alignment(unicode): how to align columns, can be left, center or right
|
@param columns_alignment(unicode): how to align columns, can be left, center or
|
||||||
|
right
|
||||||
@param col_sep(unicode): separator betweens columns
|
@param col_sep(unicode): separator betweens columns
|
||||||
@param head_line(unicode): character to use to make line under head
|
@param head_line(unicode): character to use to make line under head
|
||||||
@param disp(callable, None): method to use to display the table
|
@param disp(callable, None): method to use to display the table
|
||||||
|
|
@ -693,7 +701,7 @@ class Table(object):
|
||||||
)
|
)
|
||||||
|
|
||||||
# headers
|
# headers
|
||||||
if show_header:
|
if show_header and self.headers is not None:
|
||||||
self._disp(
|
self._disp(
|
||||||
left
|
left
|
||||||
+ self._headers(head_sep, headers, sizes, head_alignment, head_style)
|
+ self._headers(head_sep, headers, sizes, head_alignment, head_style)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue