Shared path-identity machinery for the semantic-scope guards (#1757/#2926). ``save_semantic_cache``'s write allowlist and ``scope_semantic_result``'s graph-feed filter must agree on exactly which ``source_file`` values are in scope, so both derive their matching from this one implementa
(
root: Path,
)
| 2060 | |
| 2061 | |
| 2062 | def _semantic_source_matcher( |
| 2063 | root: Path, |
| 2064 | ) -> tuple[Callable[[str | Path], Path], Callable[[str | Path], str]]: |
| 2065 | """Shared path-identity machinery for the semantic-scope guards (#1757/#2926). |
| 2066 | |
| 2067 | ``save_semantic_cache``'s write allowlist and ``scope_semantic_result``'s |
| 2068 | graph-feed filter must agree on exactly which ``source_file`` values are in |
| 2069 | scope, so both derive their matching from this one implementation rather |
| 2070 | than parallel copies that could drift apart. |
| 2071 | |
| 2072 | Returns ``(source_identity, normalize_value)`` closed over ``root``: |
| 2073 | |
| 2074 | - ``normalize_value(src)`` maps a raw ``source_file`` to its portable |
| 2075 | relative forward-slash form (#2197), |
| 2076 | - ``source_identity(value)`` maps any form (relative or absolute, against |
| 2077 | the walked or the resolved root) to the single canonical walked path |
| 2078 | that identities are compared against. |
| 2079 | """ |
| 2080 | root_walked = _normalize_path(Path(os.path.abspath(root))) |
| 2081 | root_resolved = _normalize_path(Path(root).resolve()) |
| 2082 | |
| 2083 | def normalize_value(src: str | Path) -> str: |
| 2084 | norm = _normalize_source_file_value(src, root_walked) |
| 2085 | if Path(norm).is_absolute() and root_walked != root_resolved: |
| 2086 | norm = _normalize_source_file_value(src, root_resolved) |
| 2087 | return norm |
| 2088 | |
| 2089 | def source_identity(value: str | Path) -> Path: |
| 2090 | path = Path(value) |
| 2091 | if not path.is_absolute(): |
| 2092 | path = root_walked / path |
| 2093 | elif root_walked != root_resolved: |
| 2094 | normalized = _normalize_path(Path(os.path.abspath(path))) |
| 2095 | try: |
| 2096 | relative = normalized.relative_to(root_resolved) |
| 2097 | except ValueError: |
| 2098 | pass |
| 2099 | else: |
| 2100 | path = root_walked / relative |
| 2101 | return _normalize_path(Path(os.path.abspath(path))) |
| 2102 | |
| 2103 | return source_identity, normalize_value |
| 2104 | |
| 2105 | |
| 2106 | def save_semantic_cache( |
no test coverage detected