Skip to content

Commit 542f480

Browse files
author
Milan Falešník
committed
partial_match: Refactor and make it support None
1 parent 99e40cf commit 542f480

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

src/widgetastic/utils.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -547,15 +547,23 @@ def crop_string_middle(s, length=32, cropper='...'):
547547
return s[:half] + cropper + s[-half - 1:]
548548

549549

550-
class partial_match(object): # noqa
551-
"""Use this to wrap values to be selected using partial matching in various objects.
550+
class FillValueWrapper(object):
551+
"""Base class for fill value wrapping. Subclass to create your own for special treatment.
552552
553553
It proxies all ``get`` operations to the underlying ``item``.
554554
555555
It also proxies ``dir`` so you get the exactly same result of :py:func:`dir` as if you did it
556556
on the wrapped object.
557557
558+
If ``None`` is passed as the item, it is passed through so
559+
:py:meth:`widgetastic.widget.View.fill` can skip the field
560+
558561
"""
562+
def __new__(cls, item):
563+
if item is None:
564+
return None
565+
return super(FillValueWrapper, cls).__new__(cls)
566+
559567
def __init__(self, item):
560568
self.item = item
561569

@@ -567,12 +575,30 @@ def __getattr__(self, attr):
567575

568576
def __setattr__(self, attr, value):
569577
if attr == 'item':
570-
super(partial_match, self).__setattr__(attr, value)
578+
super(FillValueWrapper, self).__setattr__(attr, value)
571579
else:
572580
setattr(self.item, attr, value)
573581

574582
def __repr__(self):
575-
return 'partial_match({!r})'.format(self.item)
583+
return '{}({!r})'.format(type(self).__name__, self.item)
584+
585+
586+
class PartialMatch(FillValueWrapper):
587+
"""Use this to wrap values to be selected using partial matching in various objects.
588+
589+
Example may be a ``<select>``. This will select the first item in the select containing "bat":
590+
591+
.. code-block:: python
592+
593+
some_view.fill({
594+
'foo': 'bar',
595+
'baz': PartialMatch('bat')
596+
})
597+
"""
598+
599+
600+
# Backwards compatibility
601+
partial_match = PartialMatch
576602

577603

578604
class Ignore(object):

testing/test_utils.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22
import pytest
33

4-
from widgetastic.utils import nested_getattr, partial_match
4+
from widgetastic.utils import PartialMatch, nested_getattr, partial_match
55

66

77
def test_nested_getattr_wrong_type():
@@ -34,10 +34,18 @@ class bar(object): # noqa
3434

3535
def test_partial_match_wrapping():
3636
value = ' foobar '
37-
wrapped = partial_match(value)
37+
wrapped = PartialMatch(value)
3838

3939
assert dir(wrapped) == dir(value)
4040

4141
assert wrapped.item is value
4242

4343
assert wrapped.strip() == value.strip()
44+
45+
46+
def test_partial_match_none():
47+
assert PartialMatch(None) is None
48+
49+
50+
def test_partial_match_is_partialmatch():
51+
assert partial_match is PartialMatch

0 commit comments

Comments
 (0)