|
| 1 | +# ruff: noqa |
| 2 | +# fmt: off |
| 3 | +""" |
| 4 | +This file includes unmodified functions copied from Python 3.13's standard library |
| 5 | +for use on older Python versions. |
| 6 | +
|
| 7 | +Functions copied: |
| 8 | +- glob.translate (from Lib/glob.py) |
| 9 | +- fnmatch._translate (from Lib/fnmatch.py) |
| 10 | +
|
| 11 | +Source: https://github.com/python/cpython |
| 12 | +License: Python Software Foundation License Version 2 |
| 13 | +Copyright (c) 2001-2024 Python Software Foundation; All Rights Reserved |
| 14 | +
|
| 15 | +Please delete me if/when this project releases forcing python >= 3.13 |
| 16 | +""" |
| 17 | + |
| 18 | +import os |
| 19 | +import re |
| 20 | + |
| 21 | + |
| 22 | +# Copied from python 3.13 fnmatch._translate |
| 23 | +def _translate(pat, STAR, QUESTION_MARK): |
| 24 | + res = [] |
| 25 | + add = res.append |
| 26 | + i, n = 0, len(pat) |
| 27 | + while i < n: |
| 28 | + c = pat[i] |
| 29 | + i = i+1 |
| 30 | + if c == '*': |
| 31 | + # compress consecutive `*` into one |
| 32 | + if (not res) or res[-1] is not STAR: |
| 33 | + add(STAR) |
| 34 | + elif c == '?': |
| 35 | + add(QUESTION_MARK) |
| 36 | + elif c == '[': |
| 37 | + j = i |
| 38 | + if j < n and pat[j] == '!': |
| 39 | + j = j+1 |
| 40 | + if j < n and pat[j] == ']': |
| 41 | + j = j+1 |
| 42 | + while j < n and pat[j] != ']': |
| 43 | + j = j+1 |
| 44 | + if j >= n: |
| 45 | + add('\\[') |
| 46 | + else: |
| 47 | + stuff = pat[i:j] |
| 48 | + if '-' not in stuff: |
| 49 | + stuff = stuff.replace('\\', r'\\') |
| 50 | + else: |
| 51 | + chunks = [] |
| 52 | + k = i+2 if pat[i] == '!' else i+1 |
| 53 | + while True: |
| 54 | + k = pat.find('-', k, j) |
| 55 | + if k < 0: |
| 56 | + break |
| 57 | + chunks.append(pat[i:k]) |
| 58 | + i = k+1 |
| 59 | + k = k+3 |
| 60 | + chunk = pat[i:j] |
| 61 | + if chunk: |
| 62 | + chunks.append(chunk) |
| 63 | + else: |
| 64 | + chunks[-1] += '-' |
| 65 | + # Remove empty ranges -- invalid in RE. |
| 66 | + for k in range(len(chunks)-1, 0, -1): |
| 67 | + if chunks[k-1][-1] > chunks[k][0]: |
| 68 | + chunks[k-1] = chunks[k-1][:-1] + chunks[k][1:] |
| 69 | + del chunks[k] |
| 70 | + # Escape backslashes and hyphens for set difference (--). |
| 71 | + # Hyphens that create ranges shouldn't be escaped. |
| 72 | + stuff = '-'.join(s.replace('\\', r'\\').replace('-', r'\-') |
| 73 | + for s in chunks) |
| 74 | + # Escape set operations (&&, ~~ and ||). |
| 75 | + stuff = re.sub(r'([&~|])', r'\\\1', stuff) |
| 76 | + i = j+1 |
| 77 | + if not stuff: |
| 78 | + # Empty range: never match. |
| 79 | + add('(?!)') |
| 80 | + elif stuff == '!': |
| 81 | + # Negated empty range: match any character. |
| 82 | + add('.') |
| 83 | + else: |
| 84 | + if stuff[0] == '!': |
| 85 | + stuff = '^' + stuff[1:] |
| 86 | + elif stuff[0] in ('^', '['): |
| 87 | + stuff = '\\' + stuff |
| 88 | + add(f'[{stuff}]') |
| 89 | + else: |
| 90 | + add(re.escape(c)) |
| 91 | + assert i == n |
| 92 | + return res |
| 93 | + |
| 94 | + |
| 95 | +def translate(pat, *, recursive=False, include_hidden=False, seps=None): |
| 96 | + """Translate a pathname with shell wildcards to a regular expression. |
| 97 | +
|
| 98 | + If `recursive` is true, the pattern segment '**' will match any number of |
| 99 | + path segments. |
| 100 | +
|
| 101 | + If `include_hidden` is true, wildcards can match path segments beginning |
| 102 | + with a dot ('.'). |
| 103 | +
|
| 104 | + If a sequence of separator characters is given to `seps`, they will be |
| 105 | + used to split the pattern into segments and match path separators. If not |
| 106 | + given, os.path.sep and os.path.altsep (where available) are used. |
| 107 | + """ |
| 108 | + if not seps: |
| 109 | + if os.path.altsep: |
| 110 | + seps = (os.path.sep, os.path.altsep) |
| 111 | + else: |
| 112 | + seps = os.path.sep |
| 113 | + escaped_seps = ''.join(map(re.escape, seps)) |
| 114 | + any_sep = f'[{escaped_seps}]' if len(seps) > 1 else escaped_seps |
| 115 | + not_sep = f'[^{escaped_seps}]' |
| 116 | + if include_hidden: |
| 117 | + one_last_segment = f'{not_sep}+' |
| 118 | + one_segment = f'{one_last_segment}{any_sep}' |
| 119 | + any_segments = f'(?:.+{any_sep})?' |
| 120 | + any_last_segments = '.*' |
| 121 | + else: |
| 122 | + one_last_segment = f'[^{escaped_seps}.]{not_sep}*' |
| 123 | + one_segment = f'{one_last_segment}{any_sep}' |
| 124 | + any_segments = f'(?:{one_segment})*' |
| 125 | + any_last_segments = f'{any_segments}(?:{one_last_segment})?' |
| 126 | + |
| 127 | + results = [] |
| 128 | + parts = re.split(any_sep, pat) |
| 129 | + last_part_idx = len(parts) - 1 |
| 130 | + for idx, part in enumerate(parts): |
| 131 | + if part == '*': |
| 132 | + results.append(one_segment if idx < last_part_idx else one_last_segment) |
| 133 | + elif recursive and part == '**': |
| 134 | + if idx < last_part_idx: |
| 135 | + if parts[idx + 1] != '**': |
| 136 | + results.append(any_segments) |
| 137 | + else: |
| 138 | + results.append(any_last_segments) |
| 139 | + else: |
| 140 | + if part: |
| 141 | + if not include_hidden and part[0] in '*?': |
| 142 | + results.append(r'(?!\.)') |
| 143 | + results.extend(_translate(part, f'{not_sep}*', not_sep)) |
| 144 | + if idx < last_part_idx: |
| 145 | + results.append(any_sep) |
| 146 | + res = ''.join(results) |
| 147 | + return fr'(?s:{res})\Z' |
0 commit comments