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.

clew/guard/filters.py:29

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
lines 30–30from ..toolchain import compile_globs, load_toolchain_configlines 32–32config = load_toolchain_config(Path(root))lines 33–33return config, compile_globs(config.gate_globs) if config.gate_globs else None

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.

clew/guard/filters.py:55

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
lines 56–56◇ if not patternslines 57–57return Nonelines 58–58from ..toolchain import compile_globs, matcheslines 59–59from .config import EXCLUDE_REGEX_PREFIXlines 61–61path = Path(file_path)lines 62–62trylines 63–63rel = (path.relative_to(Path.cwd()) if path.is_absolute() else path).as_posix()lines 64–64except ValueErrorlines 66–66rel = path.as_posix()lines 67–67parts = rel.split("/")lines 68–68dirs = ["/".join(parts[:i]) for i in range(1, len(parts))]lines 69–69◇ for pattern in patternslines 70–70◇ if pattern.startswith(EXCLUDE_REGEX_PREFIX)lines 71–71◇ if re.search(pattern[len(EXCLUDE_REGEX_PREFIX) :], rel)lines 72–72return patternlines 73–73continuelines 74–74matcher = compile_globs((pattern,))lines 75–77◇ if matcher is not None and (lines 78–78return patternlines 79–79return None

Requirements

REQ-DDB-GUARD-013

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.

clew/guard/filters.py:89

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 None

1 more lines, not shown

Activity · 18 steps
lines 90–90root = Path.cwd()lines 91–91config, matcher = _gate_matcher(str(root))lines 92–92path = Path(file_path)lines 93–93trylines 94–94rel = (path if not path.is_absolute() else path.relative_to(root)).as_posix()lines 95–95except ValueErrorlines 96–96rel = Nonelines 97–97◇ if matcher is not None and rel is not Nonelines 98–98from ..toolchain import matcheslines 100–100parts = rel.split("/")lines 101–101dirs = ["/".join(parts[:i]) for i in range(1, len(parts))]lines 102–102◇ if matches(matcher, rel) or any(matches(matcher, d, is_dir=True) for d in dirs)lines 103–103return f"ignored by {config.sources(root)}"lines 104–104from ..filetypes import binary_filelines 106–106skipped = binary_file(path)lines 107–107◇ if skipped is not Nonelines 108–108return f"{skipped.binary_kind} binary, not source"lines 109–109return None

Requirements

REQ-DDB-CONFIG-009

Flows

toolchain_contract

dict[str, Any] clew.guard.filters.toolchain_contract()

The toolchain ignores the gate honours here, for the files contract.

clew/guard/filters.py:40

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)}

Activity · 3 steps
lines 41–41root = Path.cwd()lines 42–42config, _ = _gate_matcher(str(root))lines 43–43return {"toolchain_config": config.sources(root), "toolchain_ignores": list(config.gate_globs)}

Requirements

REQ-DDB-CONFIG-009