Mercurial > libervia-backend
comparison sat_frontends/jp/common.py @ 2610:c9dddf691d7b
jp (common): allow tables filters callbacks to have only one argument
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 21 Jun 2018 01:21:34 +0200 |
parents | 26edcf3a30eb |
children | 56f94936df1e |
comparison
equal
deleted
inserted
replaced
2609:75d2ac872965 | 2610:c9dddf691d7b |
---|---|
408 @param data(iterable[list]): table data | 408 @param data(iterable[list]): table data |
409 all lines must have the same number of columns | 409 all lines must have the same number of columns |
410 @param headers(iterable[unicode], None): names/titles of the columns | 410 @param headers(iterable[unicode], None): names/titles of the columns |
411 if not None, must have same number of columns as data | 411 if not None, must have same number of columns as data |
412 @param filters(iterable[(callable, unicode)], None): values filters | 412 @param filters(iterable[(callable, unicode)], None): values filters |
413 the callable will get col value as argument and must return a string | 413 the callable will get 2 arguments: |
414 - current column value | |
415 - RowData with all columns values | |
416 if may also only use 1 argument, which will then be current col value. | |
417 the callable must return a string | |
414 if it's unicode, it will be used with .format and must countain u'{}' which will be replaced with the string | 418 if it's unicode, it will be used with .format and must countain u'{}' which will be replaced with the string |
415 if not None, must have same number of columns as data | 419 if not None, must have same number of columns as data |
416 @param use_buffer(bool): if True, bufferise output instead of printing it directly | 420 @param use_buffer(bool): if True, bufferise output instead of printing it directly |
417 """ | 421 """ |
418 self.host = host | 422 self.host = host |
438 if filters is not None and filters[idx] is not None: | 442 if filters is not None and filters[idx] is not None: |
439 filter_ = filters[idx] | 443 filter_ = filters[idx] |
440 if isinstance(filter_, basestring): | 444 if isinstance(filter_, basestring): |
441 col_value = filter_.format(value) | 445 col_value = filter_.format(value) |
442 else: | 446 else: |
443 col_value = filter_(value, row_cls(*row_data_list)) | 447 try: |
448 col_value = filter_(value, row_cls(*row_data_list)) | |
449 except TypeError: | |
450 col_value = filter_(value) | |
444 # we count size without ANSI code as they will change length of the string | 451 # we count size without ANSI code as they will change length of the string |
445 # when it's mostly style/color changes. | 452 # when it's mostly style/color changes. |
446 col_size = len(regex.ansiRemove(col_value)) | 453 col_size = len(regex.ansiRemove(col_value)) |
447 else: | 454 else: |
448 col_value = unicode(value) | 455 col_value = unicode(value) |
495 @param keys(iterable[unicode], None): keys to get | 502 @param keys(iterable[unicode], None): keys to get |
496 if None, all keys will be used | 503 if None, all keys will be used |
497 @param headers(iterable[unicode], None): name of the columns | 504 @param headers(iterable[unicode], None): name of the columns |
498 names must be in same order as keys | 505 names must be in same order as keys |
499 @param filters(dict[unicode, (callable,unicode)), None): filter to use on values | 506 @param filters(dict[unicode, (callable,unicode)), None): filter to use on values |
500 keys correspond to keys to filter, and value is a callable or unicode which | 507 keys correspond to keys to filter, and value is the same as for Table.__init__ |
501 will get the value as argument and must return a string | |
502 @param defaults(dict[unicode, unicode]): default value to use | 508 @param defaults(dict[unicode, unicode]): default value to use |
503 if None, an exception will be raised if not value is found | 509 if None, an exception will be raised if not value is found |
504 """ | 510 """ |
505 if keys is None and headers is not None: | 511 if keys is None and headers is not None: |
506 # FIXME: keys are not needed with OrderedDict, | 512 # FIXME: keys are not needed with OrderedDict, |