From b9776503f98f145f7aaaa4f61b73e238c92c534c Mon Sep 17 00:00:00 2001 From: Robert Niederreiter Date: Wed, 1 May 2019 07:30:50 +0200 Subject: [PATCH] Use ``logger.warning`` instead of deprecated ``logger.warn``. Add ``yafowil.utils.callable_value``. --- CHANGES.rst | 6 ++++ src/yafowil/utils.py | 76 ++++++++++++++++++++++++++++---------------- 2 files changed, 54 insertions(+), 28 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 9e14d25..19cc27c 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,6 +5,12 @@ History 2.3.2 (unreleased) ------------------ +- Use ``logger.warning`` instead of deprecated ``logger.warn``. + [rnix] + +- Add ``yafowil.utils.callable_value``. + [rnix] + - Fix ``yafowil.utils.cssid`` to return unicode in order to prevent malformed rendering in python 3. [rnix] diff --git a/src/yafowil/utils.py b/src/yafowil/utils.py index 197c8d6..ba9ac6f 100644 --- a/src/yafowil/utils.py +++ b/src/yafowil/utils.py @@ -171,8 +171,10 @@ def __call__(self, tag_name, *inners, **attributes): # Deprecation message def _deprecated_null_localization(msg): - logging.warn("Deprecated usage of 'yafowil.utils.tag', please use the " + - "tag factory on RuntimeData instead.") + logging.warning( + "Deprecated usage of 'yafowil.utils.tag', please " + "use the tag factory on RuntimeData instead." + ) return msg @@ -201,34 +203,52 @@ def cssid(widget, prefix, postfix=None): .replace(b' ', b'_').decode() -def attr_value(key, widget, data, default=None): - attr = widget.attrs.get(key, default) - if callable(attr): +def callable_value(value, widget, data): + """Call value if callable with widget and data as arguments and return + the callables return value. If value not callable, return as is. + As B/C mode, if callable accepts no arguments, try to call without + arguments. + """ + if not callable(value): + return value + try: + # assume property factory signature + # XXX: use keyword arguments? + # XXX: if callable raises TypeError we get non clear follow up + # errors. + return value(widget, data) + except TypeError: try: - # assume property factory signature - # XXX: use keyword arguments? - # XXX: if callable raises TypeError we get non clear follow up - # errors. - return attr(widget, data) + # assume function or class + spec = inspect.getargspec(value) except TypeError: - try: - # assume function or class - spec = inspect.getargspec(attr) - except TypeError: - spec = None - if spec is not None: - # assume B/C property factory signature if argument specs found - if len(spec.args) <= 1 and not spec.keywords: - try: - res = attr() - logging.warn( - "Deprecated usage of callback attributes. Please " - "accept 'widget' and 'data' as arguments." - ) - return res - except TypeError: - return attr - return attr + spec = None + if spec is not None: + # assume B/C property factory signature if argument specs found + if len(spec.args) <= 1 and not spec.keywords: + try: + res = value() + logging.warning( + "Deprecated usage of callback attributes. Please " + "accept 'widget' and 'data' as arguments." + ) + return res + except TypeError: + # XXX: raise here? + return value + # XXX: raise here? + return value + + +def attr_value(key, widget, data, default=None): + """Return widget attribute value by key or default. If value is callable, + it's return value is used. + """ + return callable_value( + widget.attrs.get(key, default), + widget, + data + ) def as_data_attrs(data):