"""The policy file limits.json: the numbers, what is out of scope, and which Python is held to the same limits.""" import json from dataclasses import dataclass TEST_DIRS = frozenset(("tests", "benches", "examples")) @dataclass(frozen=True) class Policy: limits: dict tab_width: int exclude: tuple python_include: tuple base_candidates: tuple def load(root): data = json.loads((root / "limits.json").read_text(encoding="utf-8")) return Policy(data["limits"], data["tab_width"], tuple(data["exclude"]), tuple(data["python_include"]), tuple(data["base_candidates"])) def language(policy, rel): """'rust', 'python' or None for a repository-relative posix path.""" if "target" in rel.split("/") or rel.startswith(policy.exclude): return None if rel.endswith(".rs"): return "rust" return "python" if rel.endswith(".py") and rel.startswith(policy.python_include) else None def is_test_path(rel): """Files that only exist for tests, benches and examples; inline `#[cfg(test)]` modules are found by the scanner.""" parts = rel.split("/") name = parts[-1] test_name = name in ("tests.rs", "conftest.py") or name.endswith(("_tests.rs", "_test.py")) return bool(TEST_DIRS & set(parts[:-1])) or test_name