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.
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
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.
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
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.
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
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.
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
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.
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 tagged1 more lines, not shown
Activity · 10 steps
Requirements
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.
Called by build_files_contract, collect_tagged_functions
Calls _git_ls_files, _rglob_source_files, excluded_by, gate_exclusion, get_validate
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