From 32cc831500aac7e9de479401fbd8d091bbcd624a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Fale=C5=A1n=C3=ADk?= Date: Tue, 6 Feb 2018 16:00:59 +0100 Subject: [PATCH] partial_match: Refactor and make it support None --- src/widgetastic/utils.py | 34 ++++++++++++++++++++++++++++++---- testing/test_utils.py | 12 ++++++++++-- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/widgetastic/utils.py b/src/widgetastic/utils.py index e5e44444..f3955e0f 100644 --- a/src/widgetastic/utils.py +++ b/src/widgetastic/utils.py @@ -547,15 +547,23 @@ def crop_string_middle(s, length=32, cropper='...'): return s[:half] + cropper + s[-half - 1:] -class partial_match(object): # noqa - """Use this to wrap values to be selected using partial matching in various objects. +class FillValueWrapper(object): + """Base class for fill value wrapping. Subclass to create your own for special treatment. It proxies all ``get`` operations to the underlying ``item``. It also proxies ``dir`` so you get the exactly same result of :py:func:`dir` as if you did it on the wrapped object. + If ``None`` is passed as the item, it is passed through so + :py:meth:`widgetastic.widget.View.fill` can skip the field + """ + def __new__(cls, item): + if item is None: + return None + return super(FillValueWrapper, cls).__new__(cls) + def __init__(self, item): self.item = item @@ -567,12 +575,30 @@ def __getattr__(self, attr): def __setattr__(self, attr, value): if attr == 'item': - super(partial_match, self).__setattr__(attr, value) + super(FillValueWrapper, self).__setattr__(attr, value) else: setattr(self.item, attr, value) def __repr__(self): - return 'partial_match({!r})'.format(self.item) + return '{}({!r})'.format(type(self).__name__, self.item) + + +class PartialMatch(FillValueWrapper): + """Use this to wrap values to be selected using partial matching in various objects. + + Example may be a ``