filters.py¶
clew/guard/filters.py
File filters the gate shares with clew's index: shared toolchain ignores and file typing.
_gate_matcher
tuple[Any, Any] clew.guard.filters._gate_matcher(str root)
The toolchain config and compiled gate matcher for one working directory.
Called by gate_exclusion, toolchain_contract
Source · 7 lines
def _gate_matcher(root: str) -> tuple[Any, Any]:
from ..toolchain import compile_globs, load_toolchain_config
config = load_toolchain_config(Path(root))
return config, compile_globs(config.gate_globs) if config.gate_globs else None
Activity · 3 steps
Flows
excluded_by
str | None clew.guard.filters.excluded_by(str|Path file_path, list[str] patterns)
The validate.exclude entry that excludes a path, or None.
Source · 27 lines
def excluded_by(file_path: str | Path, patterns: list[str]) -> str | None:
if not patterns:
return None
from ..toolchain import compile_globs, matches
from .config import EXCLUDE_REGEX_PREFIX
path = Path(file_path)
try:
rel = (path.relative_to(Path.cwd()) if path.is_absolute() else path).as_posix()
except ValueError:
## Outside the repository: no repo-relative path exists, so only a `re:` entry can match.
rel = path.as_posix()
parts = rel.split("/")
dirs = ["/".join(parts[:i]) for i in range(1, len(parts))]
for pattern in patterns:
if pattern.startswith(EXCLUDE_REGEX_PREFIX):
if re.search(pattern[len(EXCLUDE_REGEX_PREFIX) :], rel):
return pattern
continue
matcher = compile_globs((pattern,))
if matcher is not None and (
matches(matcher, rel) or any(matches(matcher, d, is_dir=True) for d in dirs)
):
return pattern
return None
Activity · 20 steps
Requirements
Flows
gate_exclusion
str | None clew.guard.filters.gate_exclusion(str|Path file_path)
Why the gate should not read a file, or None when it should.
Source · 21 lines
def gate_exclusion(file_path: str | Path) -> str | None:
root = Path.cwd()
config, matcher = _gate_matcher(str(root))
path = Path(file_path)
try:
rel = (path if not path.is_absolute() else path.relative_to(root)).as_posix()
except ValueError:
rel = None
if matcher is not None and rel is not None:
from ..toolchain import matches
parts = rel.split("/")
dirs = ["/".join(parts[:i]) for i in range(1, len(parts))]
if matches(matcher, rel) or any(matches(matcher, d, is_dir=True) for d in dirs):
return f"ignored by {config.sources(root)}"
from ..filetypes import binary_file
skipped = binary_file(path)
if skipped is not None:
return f"{skipped.binary_kind} binary, not source"
return None1 more lines, not shown
Activity · 18 steps
Requirements
Flows
toolchain_contract
dict[str, Any] clew.guard.filters.toolchain_contract()
The toolchain ignores the gate honours here, for the files contract.
Source · 6 lines
def toolchain_contract() -> dict[str, Any]:
root = Path.cwd()
config, _ = _gate_matcher(str(root))
return {"toolchain_config": config.sources(root), "toolchain_ignores": list(config.gate_globs)}