tags.py

clew/guard/tags.py

Source discovery and @req tag collection.

_extract_tagged_function

TaggedFunction | None clew.guard.tags._extract_tagged_function(Function func, str file_path)

Build a TaggedFunction for a documented function, or None if undocumented.

clew/guard/tags.py:142

Source · 14 lines
def _extract_tagged_function(func: Function, file_path: str) -> TaggedFunction | None:
    if func.doxygen is None:
        return None
    tags = func.doxygen.tags
    return TaggedFunction(
        name=func.name,
        file_path=file_path,
        reqs=tags.get("req", []),
        is_exempt=bool(EXEMPTION_TAGS & set(tags)),
        qualified=func.qualified,
        exemptions=tuple(sorted(EXEMPTION_TAGS & set(tags))),
    )

Activity · 4 steps
lines 143–143◇ if func.doxygen is Nonelines 144–144return Nonelines 145–145tags = func.doxygen.tagslines 146–153return TaggedFunction( name=func.name, file_path=file_path, reqs=tags.get("req", []), is_exempt=bool(EXEMPTION_TAGS & set(tags)), qualified=func.qualified, exemptions=tuple(sorted(EXEMPTION_TAGS & set(tags))), )

Flows

_git_ls_files

list[Path] | None clew.guard.tags._git_ls_files(str source_dir, set[str] extensions)

List tracked source files under a directory via git ls-files.

clew/guard/tags.py:58

Source · 25 lines
def _git_ls_files(source_dir: str, extensions: set[str]) -> list[Path] | None:
    try:
        result = subprocess.run(
            git_argv("-C", source_dir, "ls-files", "--cached", "--others", "--exclude-standard"),
            capture_output=True,
            text=True,
            check=True,
            timeout=_GIT_LS_TIMEOUT_SECONDS,
            ## BOTH HALVES, and this site had neither: `git_argv` stops the inspected tree's
            ## own config from running commands, `git_env` stops a hook's exported `GIT_DIR`
            ## from repointing the query. The gate runs inside a pre-commit hook, so both
            ## conditions are its ordinary environment rather than an exotic one.
            env=git_env(),
        )
    except (subprocess.CalledProcessError, FileNotFoundError, OSError, subprocess.TimeoutExpired):
        logger.info("git ls-files unavailable for %s, falling back to filesystem walk", source_dir)
        return None
    base = Path(source_dir)
    return [
        base / line
        for line in result.stdout.splitlines()
        if line and any(line.endswith(ext) for ext in extensions)
    ]

Activity · 7 steps
lines 59–59trylines 60–71result = subprocess.run( git_argv("-C", source_dir, "ls-files", "--cached", "--others", "--exclude-standard"), capture_output=True, text=True, check=True, timeout=_GIT_LS_TIMEOUT_SECONDS, ## BOTH HALVES, and this site had neither: `git_argv` stops the inspected tree's ## own config from running commands, `git_env` stops a hook's exported `GIT_DIR` ## from repointing the query. The gate runs inside a pre-commit hook, so both ## conditions are its ordinary environment rather than an exotic one. env=git_env(), )lines 72–72except (subprocess.CalledProcessError, FileNotFoundError, OSError, subprocess.TimeoutExpired)lines 73–73logger.info("git ls-files unavailable for %s, falling back to filesystem walk", source_dir)lines 74–74return Nonelines 75–75base = Path(source_dir)lines 76–80return [ base / line for line in result.stdout.splitlines() if line and any(line.endswith(ext) for ext in extensions) ]

Flows

_process_source_file

list[TaggedFunction] clew.guard.tags._process_source_file(Path source_file, dict[str, Any] config)

Parse one source file into its documented functions.

clew/guard/tags.py:159

Source · 10 lines
def _process_source_file(source_file: Path, config: dict[str, Any]) -> list[TaggedFunction]:
    result = parse_source_file_with_content(str(source_file), config)
    if result is None:
        logger.info("No language config matched %s, skipping", source_file)
        return []
    functions = result[0]
    extracted = (_extract_tagged_function(func, str(source_file)) for func in functions)
    return [tf for tf in extracted if tf is not None]

Activity · 7 steps
lines 160–160result = parse_source_file_with_content(str(source_file), config)lines 161–161◇ if result is Nonelines 162–162logger.info("No language config matched %s, skipping", source_file)lines 163–163return []lines 164–164functions = result[0]lines 165–165extracted = (_extract_tagged_function(func, str(source_file)) for func in functions)lines 166–166return [tf for tf in extracted if tf is not None]

Flows

_rglob_source_files

list[Path] | None clew.guard.tags._rglob_source_files(str source_dir, set[str] extensions)

Fallback source discovery via filesystem glob.

clew/guard/tags.py:87

Source · 13 lines
def _rglob_source_files(source_dir: str, extensions: set[str]) -> list[Path] | None:
    source_path = Path(source_dir)
    if not source_path.exists():
        logger.warning("Source directory not found: %s", source_dir)
        return None
    files: list[Path] = []
    for ext in extensions:
        for f in source_path.rglob(f"*{ext}"):
            rel = str(f.relative_to(Path.cwd())) if f.is_absolute() else str(f)
            files.append(Path(rel))
    return files

Activity · 10 steps
lines 88–88source_path = Path(source_dir)lines 89–89◇ if not source_path.exists()lines 90–90logger.warning("Source directory not found: %s", source_dir)lines 91–91return Nonelines 92–92files: list[Path] = []lines 93–93◇ for ext in extensionslines 94–94◇ for f in source_path.rglob(f"*{ext}")lines 95–95rel = str(f.relative_to(Path.cwd())) if f.is_absolute() else str(f)lines 96–96files.append(Path(rel))lines 97–97return files

Flows

collect_tagged_functions

list[TaggedFunction] clew.guard.tags.collect_tagged_functions(list[str] source_dirs, dict[str, Any] config)

Walk source directories and collect every documented function.

clew/guard/tags.py:173

Source · 21 lines
def collect_tagged_functions(
    source_dirs: list[str],
    config: dict[str, Any],
) -> list[TaggedFunction]:
    tagged: list[TaggedFunction] = []
    file_count = 0
    for source_dir in source_dirs:
        source_files = [f for f in find_source_files(source_dir, config) if f.exists()]
        file_count += len(source_files)
        for source_file in source_files:
            tagged.extend(_process_source_file(source_file, config))

    with_reqs = sum(1 for tf in tagged if tf.reqs)
    logger.info(
        "Tag scan: %d file(s), %d documented function(s), %d with @req, %d without",
        file_count,
        len(tagged),
        with_reqs,
        len(tagged) - with_reqs,
    )
    return tagged

1 more lines, not shown

Activity · 10 steps
lines 177–177tagged: list[TaggedFunction] = []lines 178–178file_count = 0lines 179–179◇ for source_dir in source_dirslines 180–180source_files = [f for f in find_source_files(source_dir, config) if f.exists()]lines 181–181file_count += len(source_files)lines 182–182◇ for source_file in source_fileslines 183–183tagged.extend(_process_source_file(source_file, config))lines 185–185with_reqs = sum(1 for tf in tagged if tf.reqs)lines 186–192logger.info( "Tag scan: %d file(s), %d documented function(s), %d with @req, %d without", file_count, len(tagged), with_reqs, len(tagged) - with_reqs, )lines 193–193return tagged

Requirements

REQ-DDB-GUARD-022

Flows

find_source_files

list[Path] clew.guard.tags.find_source_files(str source_dir, dict[str, Any] config)

Find source files for configured languages, applying validate.exclude patterns.

clew/guard/tags.py:106

Source · 30 lines
def find_source_files(source_dir: str, config: dict[str, Any]) -> list[Path]:
    validate = get_validate(config)
    languages = validate.get("languages", {})
    exclude_patterns = validate.get("exclude", [])
    extensions: set[str] = set()
    for lang_config in languages.values():
        extensions.update(lang_config.get("extensions", []))

    git_files = _git_ls_files(source_dir, extensions)
    candidates = git_files if git_files is not None else _rglob_source_files(source_dir, extensions)
    if candidates is None:
        return []

    from .filters import excluded_by, gate_exclusion

    kept = sorted(
        f
        for f in candidates
        if excluded_by(f, exclude_patterns) is None and gate_exclusion(f) is None
    )
    logger.info(
        "Source scan %s: %d candidate(s), %d kept after %d exclude pattern(s)",
        source_dir,
        len(candidates),
        len(kept),
        len(exclude_patterns),
    )
    return kept

Activity · 14 steps
lines 107–107validate = get_validate(config)lines 108–108languages = validate.get("languages", {})lines 109–109exclude_patterns = validate.get("exclude", [])lines 110–110extensions: set[str] = set()lines 111–111◇ for lang_config in languages.values()lines 112–112extensions.update(lang_config.get("extensions", []))lines 114–114git_files = _git_ls_files(source_dir, extensions)lines 115–115candidates = git_files if git_files is not None else _rglob_source_files(source_dir, extensions)lines 116–116◇ if candidates is Nonelines 117–117return []lines 119–119from .filters import excluded_by, gate_exclusionlines 121–125kept = sorted( f for f in candidates if excluded_by(f, exclude_patterns) is None and gate_exclusion(f) is None )lines 126–132logger.info( "Source scan %s: %d candidate(s), %d kept after %d exclude pattern(s)", source_dir, len(candidates), len(kept), len(exclude_patterns), )lines 133–133return kept

Requirements

REQ-DDB-GUARD-013

Flows