From 28f15f222d4f1a5a5de2754ebedf730b427899d0 Mon Sep 17 00:00:00 2001 From: Goffi Date: Thu, 21 Jun 2018 01:21:34 +0200 Subject: [PATCH] jp (common): allow tables filters callbacks to have only one argument --- sat_frontends/jp/common.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/sat_frontends/jp/common.py b/sat_frontends/jp/common.py index ab3e6b31..e062a9ec 100644 --- a/sat_frontends/jp/common.py +++ b/sat_frontends/jp/common.py @@ -410,7 +410,11 @@ class Table(object): @param headers(iterable[unicode], None): names/titles of the columns if not None, must have same number of columns as data @param filters(iterable[(callable, unicode)], None): values filters - the callable will get col value as argument and must return a string + the callable will get 2 arguments: + - current column value + - RowData with all columns values + if may also only use 1 argument, which will then be current col value. + 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 not None, must have same number of columns as data @param use_buffer(bool): if True, bufferise output instead of printing it directly @@ -440,7 +444,10 @@ class Table(object): if isinstance(filter_, basestring): col_value = filter_.format(value) else: - col_value = filter_(value, row_cls(*row_data_list)) + try: + col_value = filter_(value, row_cls(*row_data_list)) + except TypeError: + col_value = filter_(value) # we count size without ANSI code as they will change length of the string # when it's mostly style/color changes. col_size = len(regex.ansiRemove(col_value)) @@ -497,8 +504,7 @@ class Table(object): @param headers(iterable[unicode], None): name of the columns names must be in same order as keys @param filters(dict[unicode, (callable,unicode)), None): filter to use on values - keys correspond to keys to filter, and value is a callable or unicode which - will get the value as argument and must return a string + keys correspond to keys to filter, and value is the same as for Table.__init__ @param defaults(dict[unicode, unicode]): default value to use if None, an exception will be raised if not value is found """