|
14 | 14 |
|
15 | 15 | import json |
16 | 16 | import logging |
17 | | -import re |
18 | 17 | import typing |
19 | | -from typing import Any, Callable, Dict, Generator, Optional, Pattern |
| 18 | +from typing import Any, Callable, Dict, Generator, Optional |
20 | 19 |
|
21 | 20 | import attr |
22 | 21 | from frozendict import frozendict |
|
35 | 34 | logger = logging.getLogger(__name__) |
36 | 35 |
|
37 | 36 |
|
38 | | -_WILDCARD_RUN = re.compile(r"([\?\*]+)") |
39 | | - |
40 | | - |
41 | 37 | def _reject_invalid_json(val: Any) -> None: |
42 | 38 | """Do not allow Infinity, -Infinity, or NaN values in JSON.""" |
43 | 39 | raise ValueError("Invalid JSON value: '%s'" % val) |
@@ -185,56 +181,3 @@ def log_failure( |
185 | 181 | if not consumeErrors: |
186 | 182 | return failure |
187 | 183 | return None |
188 | | - |
189 | | - |
190 | | -def glob_to_regex(glob: str, word_boundary: bool = False) -> Pattern: |
191 | | - """Converts a glob to a compiled regex object. |
192 | | -
|
193 | | - Args: |
194 | | - glob: pattern to match |
195 | | - word_boundary: If True, the pattern will be allowed to match at word boundaries |
196 | | - anywhere in the string. Otherwise, the pattern is anchored at the start and |
197 | | - end of the string. |
198 | | -
|
199 | | - Returns: |
200 | | - compiled regex pattern |
201 | | - """ |
202 | | - |
203 | | - # Patterns with wildcards must be simplified to avoid performance cliffs |
204 | | - # - The glob `?**?**?` is equivalent to the glob `???*` |
205 | | - # - The glob `???*` is equivalent to the regex `.{3,}` |
206 | | - chunks = [] |
207 | | - for chunk in _WILDCARD_RUN.split(glob): |
208 | | - # No wildcards? re.escape() |
209 | | - if not _WILDCARD_RUN.match(chunk): |
210 | | - chunks.append(re.escape(chunk)) |
211 | | - continue |
212 | | - |
213 | | - # Wildcards? Simplify. |
214 | | - qmarks = chunk.count("?") |
215 | | - if "*" in chunk: |
216 | | - chunks.append(".{%d,}" % qmarks) |
217 | | - else: |
218 | | - chunks.append(".{%d}" % qmarks) |
219 | | - |
220 | | - res = "".join(chunks) |
221 | | - |
222 | | - if word_boundary: |
223 | | - res = re_word_boundary(res) |
224 | | - else: |
225 | | - # \A anchors at start of string, \Z at end of string |
226 | | - res = r"\A" + res + r"\Z" |
227 | | - |
228 | | - return re.compile(res, re.IGNORECASE) |
229 | | - |
230 | | - |
231 | | -def re_word_boundary(r: str) -> str: |
232 | | - """ |
233 | | - Adds word boundary characters to the start and end of an |
234 | | - expression to require that the match occur as a whole word, |
235 | | - but do so respecting the fact that strings starting or ending |
236 | | - with non-word characters will change word boundaries. |
237 | | - """ |
238 | | - # we can't use \b as it chokes on unicode. however \W seems to be okay |
239 | | - # as shorthand for [^0-9A-Za-z_]. |
240 | | - return r"(^|\W)%s(\W|$)" % (r,) |
0 commit comments