catalogs.py¶
clew/wiki/catalogs.py
Pinned sub-catalogs and external rules: traceability across repositories (1.4.0 W8, #67).
catalog_requirements
tuple[list[CatalogRequirement], list[str]] clew.wiki.catalogs.catalog_requirements(Path repo_root, CatalogSpec spec)
The requirements of one sub-catalog, read at its pin.
Source · 24 lines
def catalog_requirements(
repo_root: Path, spec: CatalogSpec
) -> tuple[list[CatalogRequirement], list[str]]:
"""THE FLAT `[{id, title}]` SHAPE or an `{id: {title|name}}` mapping; anything else yields a
problem rather than a guess.
@brief Read a catalog at its pin.
@return Rows and problems.
"""
from ..requirements import read_catalog_at
try:
found = read_catalog_at(
repo_root, {"repo": spec.repo, "rev": spec.pin, "path": spec.catalog}
)
except ValueError as exc:
return [], [f"catalog {spec.name!r}: {exc}"]
rows = [
CatalogRequirement(spec.name, req_id, row.get("title") or row.get("name") or "", spec.pin)
for req_id, row in found.items()
]
return rows, []
Activity · 7 steps
Requirements
declared_catalog
tuple[str, dict[str, dict]] clew.wiki.catalogs.declared_catalog(Path repo_root)
The requirements catalog the index reads, as declared entries by id.
Called by _catalog_files, _root_source, gather
Calls _is_flat_req_list, _keyed_req_entries, load_guard_config, resolve_catalog_columns, resolve_catalog_path
Source · 29 lines
def declared_catalog(repo_root: Path) -> tuple[str, dict[str, dict]]:
"""THE INDEX'S OWN RESOLVER (`resolve_catalog_path`), so the site and the index never read
different catalogs. Both shapes the index accepts — a flat `[{id, ...}]` list and the
`{requirements: {ID: {...}}}` mapping — are read; anything else is an empty catalog.
@brief Read the catalog.
@return Path and entries.
"""
import yaml
from ..requirements import (
_is_flat_req_list,
_keyed_req_entries,
load_guard_config,
resolve_catalog_columns,
resolve_catalog_path,
)
guard = load_guard_config(repo_root)
path = resolve_catalog_path(guard, Path(repo_root))
if path is None or not path.is_file():
return "", {}
data = yaml.safe_load(path.read_text(encoding="utf-8"))
id_col, _title = resolve_catalog_columns(guard)
entries = data if _is_flat_req_list(data, id_col) else _keyed_req_entries(data)
rel = path.resolve().relative_to(Path(repo_root).resolve()).as_posix()
return rel, {str(e.get(id_col) or e.get("id")): e for e in entries if isinstance(e, dict)}
Activity · 11 steps
Requirements
pin_problems
list[str] clew.wiki.catalogs.pin_problems(Path repo_root, CatalogSpec spec)
Problems with one sub-catalog's pin.
Source · 19 lines
def pin_problems(repo_root: Path, spec: CatalogSpec) -> list[str]:
"""@brief Check a pin. @return Problems."""
from ..requirements import git_text
sibling = (Path(repo_root) / spec.repo).resolve()
if not sibling.is_dir():
return [f"catalog {spec.name!r}: sibling checkout {spec.repo} does not exist"]
if git_text(sibling, "rev-parse", "--verify", "--quiet", f"{spec.pin}^{{commit}}") is None:
return [f"catalog {spec.name!r}: pin {spec.pin} does not resolve in {spec.repo}"]
pinned = git_text(sibling, "show", f"{spec.pin}:{spec.catalog}")
head = git_text(sibling, "show", f"HEAD:{spec.catalog}")
if pinned is None:
return [f"catalog {spec.name!r}: {spec.catalog} does not exist at pin {spec.pin}"]
if head != pinned:
return [
f"catalog {spec.name!r}: {spec.catalog} moved since pin {spec.pin} "
f"(it differs at {spec.repo} HEAD) — re-pin or trace against the pinned rows"
]
return []1 more lines, not shown