| 1 |
import itertools |
| 2 |
import operator |
| 3 |
import re |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
_test_matchers = () |
| 8 |
_test_patterns = () |
| 9 |
|
| 10 |
|
| 11 |
def match_test(test): |
| 12 |
|
| 13 |
result = False |
| 14 |
for matcher, result in reversed(_test_matchers): |
| 15 |
if matcher(test.id()): |
| 16 |
return result |
| 17 |
return not result |
| 18 |
|
| 19 |
|
| 20 |
def _is_full_match_test(pattern): |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
return ('.' in pattern) and (not re.search(r'[?*\[\]]', pattern)) |
| 28 |
|
| 29 |
|
| 30 |
def get_match_tests(): |
| 31 |
global _test_patterns |
| 32 |
return _test_patterns |
| 33 |
|
| 34 |
|
| 35 |
def set_match_tests(patterns): |
| 36 |
global _test_matchers, _test_patterns |
| 37 |
|
| 38 |
if not patterns: |
| 39 |
_test_matchers = () |
| 40 |
_test_patterns = () |
| 41 |
else: |
| 42 |
itemgetter = operator.itemgetter |
| 43 |
patterns = tuple(patterns) |
| 44 |
if patterns != _test_patterns: |
| 45 |
_test_matchers = [ |
| 46 |
(_compile_match_function(map(itemgetter(0), it)), result) |
| 47 |
for result, it in itertools.groupby(patterns, itemgetter(1)) |
| 48 |
] |
| 49 |
_test_patterns = patterns |
| 50 |
|
| 51 |
|
| 52 |
def _compile_match_function(patterns): |
| 53 |
patterns = list(patterns) |
| 54 |
|
| 55 |
if all(map(_is_full_match_test, patterns)): |
| 56 |
|
| 57 |
|
| 58 |
return set(patterns).__contains__ |
| 59 |
else: |
| 60 |
import fnmatch |
| 61 |
regex = '|'.join(map(fnmatch.translate, patterns)) |
| 62 |
|
| 63 |
|
| 64 |
regex_match = re.compile(regex).match |
| 65 |
|
| 66 |
def match_test_regex(test_id, regex_match=regex_match): |
| 67 |
if regex_match(test_id): |
| 68 |
|
| 69 |
|
| 70 |
return True |
| 71 |
else: |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
return any(map(regex_match, test_id.split("."))) |
| 76 |
|
| 77 |
return match_test_regex |