A port of FNMatcher from py.path.common which works with PurePath() instances. The difference between this algorithm and PurePath.match() is that the latter matches "**" glob expressions for each part of the path, while this algorithm uses the whole path instead. For example:
(pattern: str, path: Union[str, "os.PathLike[str]"])
| 386 | |
| 387 | |
| 388 | def fnmatch_ex(pattern: str, path: Union[str, "os.PathLike[str]"]) -> bool: |
| 389 | """A port of FNMatcher from py.path.common which works with PurePath() instances. |
| 390 | |
| 391 | The difference between this algorithm and PurePath.match() is that the |
| 392 | latter matches "**" glob expressions for each part of the path, while |
| 393 | this algorithm uses the whole path instead. |
| 394 | |
| 395 | For example: |
| 396 | "tests/foo/bar/doc/test_foo.py" matches pattern "tests/**/doc/test*.py" |
| 397 | with this algorithm, but not with PurePath.match(). |
| 398 | |
| 399 | This algorithm was ported to keep backward-compatibility with existing |
| 400 | settings which assume paths match according this logic. |
| 401 | |
| 402 | References: |
| 403 | * https://bugs.python.org/issue29249 |
| 404 | * https://bugs.python.org/issue34731 |
| 405 | """ |
| 406 | path = PurePath(path) |
| 407 | iswin32 = sys.platform.startswith("win") |
| 408 | |
| 409 | if iswin32 and sep not in pattern and posix_sep in pattern: |
| 410 | # Running on Windows, the pattern has no Windows path separators, |
| 411 | # and the pattern has one or more Posix path separators. Replace |
| 412 | # the Posix path separators with the Windows path separator. |
| 413 | pattern = pattern.replace(posix_sep, sep) |
| 414 | |
| 415 | if sep not in pattern: |
| 416 | name = path.name |
| 417 | else: |
| 418 | name = str(path) |
| 419 | if path.is_absolute() and not os.path.isabs(pattern): |
| 420 | pattern = f"*{os.sep}{pattern}" |
| 421 | return fnmatch.fnmatch(name, pattern) |
| 422 | |
| 423 | |
| 424 | def parts(s: str) -> Set[str]: |
no outgoing calls