fix issue 29191: only try partial matches when a wildcard has been specified

This commit is contained in:
Bastiaan Bakker 2015-11-25 16:03:33 +01:00 committed by rallytime
parent b225263279
commit 44713cdb95
2 changed files with 39 additions and 18 deletions

View file

@ -1365,7 +1365,8 @@ def subdict_match(data,
return fnmatch.fnmatch(str(target).lower(), pattern.lower())
def _dict_match(target, pattern, regex_match=False, exact_match=False):
if pattern.startswith('*:'):
wildcard = pattern.startswith('*:')
if wildcard:
pattern = pattern[2:]
if pattern == '*':
@ -1379,25 +1380,26 @@ def subdict_match(data,
regex_match=regex_match,
exact_match=exact_match):
return True
for key in target.keys():
if _match(key,
pattern,
regex_match=regex_match,
exact_match=exact_match):
return True
if isinstance(target[key], dict):
if _dict_match(target[key],
pattern,
regex_match=regex_match,
exact_match=exact_match):
if wildcard:
for key in target.keys():
if _match(key,
pattern,
regex_match=regex_match,
exact_match=exact_match):
return True
elif isinstance(target[key], list):
for item in target[key]:
if _match(item,
pattern,
regex_match=regex_match,
exact_match=exact_match):
if isinstance(target[key], dict):
if _dict_match(target[key],
pattern,
regex_match=regex_match,
exact_match=exact_match):
return True
elif isinstance(target[key], list):
for item in target[key]:
if _match(item,
pattern,
regex_match=regex_match,
exact_match=exact_match):
return True
return False
for idx in range(1, expr.count(delimiter) + 1):

View file

@ -216,6 +216,7 @@ class UtilsTestCase(TestCase):
test_two_level_dict_and_list = {
'abc': ['def', 'ghi', {'lorem': {'ipsum': [{'dolor': 'sit'}]}}],
}
test_four_level_dict = {'a': {'b': {'c': 'v'}}}
self.assertTrue(
utils.subdict_match(
@ -264,6 +265,24 @@ class UtilsTestCase(TestCase):
test_two_level_dict_and_list, 'abc:lorem:ipsum:dolor:sit'
)
)
# Test four level dict match for reference
self.assertTrue(
utils.subdict_match(
test_four_level_dict, 'a:b:c:v'
)
)
self.assertFalse(
# Test regression in 2015.8 where 'a:v' would match 'a:b:c:v'
utils.subdict_match(
test_four_level_dict, 'a:v'
)
)
# Test wildcard match
self.assertTrue(
utils.subdict_match(
test_four_level_dict, 'a:*:v'
)
)
def test_traverse_dict(self):
test_two_level_dict = {'foo': {'bar': 'baz'}}