>>> Translator('xyz') Traceback (most recent call last): ... AssertionError: Invalid separators >>> Translator('') Traceback (most recent call last): ... AssertionError: Invalid separators
| 5 | |
| 6 | |
| 7 | class Translator: |
| 8 | """ |
| 9 | >>> Translator('xyz') |
| 10 | Traceback (most recent call last): |
| 11 | ... |
| 12 | AssertionError: Invalid separators |
| 13 | |
| 14 | >>> Translator('') |
| 15 | Traceback (most recent call last): |
| 16 | ... |
| 17 | AssertionError: Invalid separators |
| 18 | """ |
| 19 | |
| 20 | seps: str |
| 21 | |
| 22 | def __init__(self, seps: str = _default_seps): |
| 23 | assert seps and set(seps) <= set(_default_seps), "Invalid separators" |
| 24 | self.seps = seps |
| 25 | |
| 26 | def translate(self, pattern): |
| 27 | """ |
| 28 | Given a glob pattern, produce a regex that matches it. |
| 29 | """ |
| 30 | return self.extend(self.match_dirs(self.translate_core(pattern))) |
| 31 | |
| 32 | def extend(self, pattern): |
| 33 | r""" |
| 34 | Extend regex for pattern-wide concerns. |
| 35 | |
| 36 | Apply '(?s:)' to create a non-matching group that |
| 37 | matches newlines (valid on Unix). |
| 38 | |
| 39 | Append '\z' to imply fullmatch even when match is used. |
| 40 | """ |
| 41 | return rf'(?s:{pattern})\z' |
| 42 | |
| 43 | def match_dirs(self, pattern): |
| 44 | """ |
| 45 | Ensure that zipfile.Path directory names are matched. |
| 46 | |
| 47 | zipfile.Path directory names always end in a slash. |
| 48 | """ |
| 49 | return rf'{pattern}[/]?' |
| 50 | |
| 51 | def translate_core(self, pattern): |
| 52 | r""" |
| 53 | Given a glob pattern, produce a regex that matches it. |
| 54 | |
| 55 | >>> t = Translator() |
| 56 | >>> t.translate_core('*.txt').replace('\\\\', '') |
| 57 | '[^/]*\\.txt' |
| 58 | >>> t.translate_core('a?txt') |
| 59 | 'a[^/]txt' |
| 60 | >>> t.translate_core('**/*').replace('\\\\', '') |
| 61 | '.*/[^/][^/]*' |
| 62 | """ |
| 63 | self.restrict_rglob(pattern) |
| 64 | return ''.join(map(self.replace, separate(self.star_not_empty(pattern)))) |
no outgoing calls
no test coverage detected
searching dependent graphs…