registry.py

clew/propose/registry.py

Run every declaration section over one repo and assemble the Proposal.

_blocked_section

SectionProposal clew.propose.registry._blocked_section(str name, tuple[SectionStatus, str] blocking)

A detector section that could not run, carrying the reason it could not.

clew/propose/registry.py:385

Show on the map →

Called by _sections

Calls none

Source · 6 lines
def _blocked_section(name: str, blocking: tuple[SectionStatus, str]) -> SectionProposal:
    """@brief Report a detector that was never able to look."""
    status, reason = blocking
    return SectionProposal(name=name, status=status, reason=reason)

Activity · 2 steps
lines 387–387status, reason = blockinglines 388–388return SectionProposal(name=name, status=status, reason=reason)

Flows

_blocking

tuple[SectionStatus, str] | None clew.propose.registry._blocking(Context ctx)

Why the AST detectors cannot run, when they cannot.

clew/propose/registry.py:347

Source · 32 lines
def _blocking(ctx: Context) -> tuple[SectionStatus, str] | None:
    """Ordered most-fundamental first, so the reported reason is the root cause
    rather than a downstream symptom of it (a missing grammar also produces zero
    in-scope files).

    The last check counts files a C/C++ grammar handles, NOT files that parsed.
    The parser router covers Python as well, and a Python file parses cleanly
    while contributing no definition and no call site these detectors can read —
    so counting parses would report a Python codebase as a MEASURED empty C repo,
    which is a claim about the repo where the truth is a claim about the detector.

    @brief Decide whether the AST detectors can produce a measured answer.
    @version 4
    """
    corpus = ctx.corpus
    readable = ast_readable_in_scope(ctx.files, ctx.in_scope)
    checks = (
        (
            not ctx.scope.is_derived(),
            SectionStatus.NOT_ANALYSED,
            _NO_DERIVED_SCOPE.format(reason=ctx.scope.reason),
        ),
        (corpus.files_parsed == 0, SectionStatus.NOT_APPLICABLE, _NO_SOURCE),
        (
            readable == 0,
            SectionStatus.NOT_APPLICABLE,
            _NONE_IN_SCOPE.format(in_scope=corpus.files_in_scope, source=ctx.scope.source),
        ),
    )
    return next(((status, reason) for cond, status, reason in checks if cond), None)

Activity · 4 steps
lines 361–361corpus = ctx.corpuslines 362–362readable = ast_readable_in_scope(ctx.files, ctx.in_scope)lines 363–375checks = ( ( not ctx.scope.is_derived(), SectionStatus.NOT_ANALYSED, _NO_DERIVED_SCOPE.format(reason=ctx.scope.reason), ), (corpus.files_parsed == 0, SectionStatus.NOT_APPLICABLE, _NO_SOURCE), ( readable == 0, SectionStatus.NOT_APPLICABLE, _NONE_IN_SCOPE.format(in_scope=corpus.files_in_scope, source=ctx.scope.source), ), )lines 376–376return next(((status, reason) for cond, status, reason in checks if cond), None)

Flows

_hand_section

SectionProposal clew.propose.registry._hand_section(Context ctx, str name, str reason)

A section clew refuses to detect: hand-declared, or already declared.

clew/propose/registry.py:398

Show on the map →

Called by _sections

Calls none

Source · 17 lines
def _hand_section(ctx: Context, name: str, reason: str) -> SectionProposal:
    """A declared section reports ALREADY_DECLARED rather than repeating the
    refusal: the owner has clearly already read this, and the draft's job then is
    to confirm clew sees it, not to lecture.

    @brief Report one hand-declared section.
    @version 2
    """
    declared = ctx.declared.get(name) is not None
    return SectionProposal(
        name=name,
        status=SectionStatus.ALREADY_DECLARED if declared else SectionStatus.NOT_ANALYSED,
        reason=_ALREADY_DECLARED if declared else f"NOT PROPOSED BY DESIGN — {reason}",
        checked={"declared_by_the_repo": declared},
    )

Activity · 2 steps
lines 406–406declared = ctx.declared.get(name) is not Nonelines 407–412return SectionProposal( name=name, status=SectionStatus.ALREADY_DECLARED if declared else SectionStatus.NOT_ANALYSED, reason=_ALREADY_DECLARED if declared else f"NOT PROPOSED BY DESIGN — {reason}", checked={"declared_by_the_repo": declared}, )

Flows

_index_scope_section

SectionProposal clew.propose.registry._index_scope_section(Context ctx, tuple[SectionProposal,...] detected)

The report-only index_scope section, plus the no-corpus caveat.

clew/propose/registry.py:421

Source · 22 lines
def _index_scope_section(ctx: Context, detected: tuple[SectionProposal, ...]) -> SectionProposal:
    """`report_not_indexed` derives its directory list from the AST corpus, so
    with no corpus its "nothing outside the scope" line would be an artefact of
    not having looked. The caveat is appended rather than the section suppressed,
    because the scope facts it reports are still true and still worth reading.

    @brief Build the index_scope report, caveated when there is no corpus.
    @version 1
    """
    section = report_not_indexed(ctx, detected)
    if ctx.corpus.files_parsed:
        return section
    return replace(
        section,
        notes=(
            *section.notes,
            "CAVEAT: no C/C++ source was parsed, so the directory list above is empty "
            "because nothing was scanned — not because nothing is outside the scope.",
        ),
    )

Activity · 4 steps
lines 430–430section = report_not_indexed(ctx, detected)lines 431–431◇ if ctx.corpus.files_parsedlines 432–432return sectionlines 433–440return replace( section, notes=( *section.notes, "CAVEAT: no C/C++ source was parsed, so the directory list above is empty " "because nothing was scanned — not because nothing is outside the scope.", ), )

Flows

_sections

tuple[SectionProposal, ...] clew.propose.registry._sections(Context ctx)

Every section's result, in render order.

clew/propose/registry.py:331

Source · 11 lines
def _sections(ctx: Context) -> tuple[SectionProposal, ...]:
    """@brief Run the detectors, then the hand-declared and report-only sections."""
    blocking = _blocking(ctx)
    detected = tuple(
        _blocked_section(name, blocking) if blocking is not None else detector(ctx)
        for name, detector in _DETECTED
    )
    hand = tuple(_hand_section(ctx, name, reason) for name, reason in HAND_DECLARED.items())
    return (*detected, *hand, _index_scope_section(ctx, detected))

Activity · 4 steps
lines 333–333blocking = _blocking(ctx)lines 334–337detected = tuple( _blocked_section(name, blocking) if blocking is not None else detector(ctx) for name, detector in _DETECTED )lines 338–338hand = tuple(_hand_section(ctx, name, reason) for name, reason in HAND_DECLARED.items())lines 339–339return (*detected, *hand, _index_scope_section(ctx, detected))

Flows

build_context

Context clew.propose.registry.build_context(Path|str repo_root, Path|str|None db_path=None, *bool dry_run=True, bool use_declaration=True)

Assemble everything the detectors are allowed to read, once.

clew/propose/registry.py:249

Source · 35 lines
def build_context(
    repo_root: Path | str,
    db_path: Path | str | None = None,
    *,
    dry_run: bool = True,
    use_declaration: bool = True,
) -> Context:
    """`use_declaration=False` is how an owner AUDITS a declaration they already
    wrote: it runs detection as if the repo declared nothing, so a hand-written
    entry shows up as a fresh proposal instead of being refused as
    already-covered. It deliberately does NOT affect scope derivation, which
    reads the same file — the scope a proposal is computed against must be the
    one the build actually uses, or every measured number describes a different
    repo than the one being indexed.

    @brief Build the shared detector context for one repo.
    @version 2
    """
    root = Path(repo_root).expanduser().resolve()
    scope = derive_scope(root)
    files = repo_source_files(root)
    in_scope = scope_membership(scope)
    corpus = scan_repo(root, files, in_scope, split_accessor)
    return Context(
        repo_root=root,
        db_path=Path(db_path).expanduser().resolve() if db_path else None,
        scope=scope,
        declared=load_declaration(root) if use_declaration else {},
        files=files,
        in_scope=in_scope,
        corpus=corpus,
        dry_run=dry_run,
    )

Activity · 6 steps
lines 267–267root = Path(repo_root).expanduser().resolve()lines 268–268scope = derive_scope(root)lines 269–269files = repo_source_files(root)lines 270–270in_scope = scope_membership(scope)lines 271–271corpus = scan_repo(root, files, in_scope, split_accessor)lines 272–281return Context( repo_root=root, db_path=Path(db_path).expanduser().resolve() if db_path else None, scope=scope, declared=load_declaration(root) if use_declaration else {}, files=files, in_scope=in_scope, corpus=corpus, dry_run=dry_run, )

Requirements

REQ-DDB-CONFIG-001

Flows

db_status_summary

dict[str, Any] clew.propose.registry.db_status_summary(Path|None db_path)

Freshness and usability summary for the index a proposal measures against.

clew/propose/registry.py:448

Source · 25 lines
def db_status_summary(db_path: Path | None) -> dict[str, Any]:
    """Surfaced in the rendered header because every MEASURED number in the draft
    is a number FROM this database. A proposal measured against a stale index is
    still useful, but the reader has to know that is what they are holding — and a
    file that is not a clew index at all has to be named as rejected rather
    than silently treated as absent.

    @brief Summarise the measuring index.
    @version 3
    """
    if db_path is None or not db_path.is_file():
        return {"path": str(db_path) if db_path else None, "exists": False, "usable": False}
    defect = index_defect(db_path)
    version = read_build_signature(db_path)
    return {
        "path": str(db_path),
        "exists": True,
        "usable": not defect,
        "defect": defect,
        "build_version": version,
        "expected_build_version": CLEW_BUILD_VERSION,
        "stale": version != CLEW_BUILD_VERSION,
    }

Activity · 5 steps
lines 458–458◇ if db_path is None or not db_path.is_file()lines 459–459return {"path": str(db_path) if db_path else None, "exists": False, "usable": False}lines 460–460defect = index_defect(db_path)lines 461–461version = read_build_signature(db_path)lines 462–470return { "path": str(db_path), "exists": True, "usable": not defect, "defect": defect, "build_version": version, "expected_build_version": CLEW_BUILD_VERSION, "stale": version != CLEW_BUILD_VERSION, }

Requirements

REQ-DDB-CONFIG-001

Flows

propose

Proposal clew.propose.registry.propose(Path|str repo_root, Path|str|None db_path=None, *bool dry_run=True, bool use_declaration=True)

Propose a starter .clew.yaml for one repo.

clew/propose/registry.py:292

Source · 34 lines
def propose(
    repo_root: Path | str,
    db_path: Path | str | None = None,
    *,
    dry_run: bool = True,
    use_declaration: bool = True,
) -> Proposal:
    """An index that exists but cannot be measured against is passed to the
    detectors as NO index, so they fail closed exactly as they do when none was
    found. The defect is still reported in the header — a reader who sees "NO
    INDEX" while holding a `clew.db` needs to know it was rejected and why.

    @brief Detect every section and render the commented draft.
    @version 3
    """
    status = db_status_summary(Path(db_path).expanduser().resolve() if db_path else None)
    ctx = build_context(
        repo_root,
        db_path if status.get("usable") else None,
        dry_run=dry_run,
        use_declaration=use_declaration,
    )
    sections = _sections(ctx)
    scope = scope_summary(ctx)
    return Proposal(
        repo_root=ctx.repo_root,
        db_path=ctx.db_path,
        db_status=status,
        scope=scope,
        sections=sections,
        yaml_text=render_declaration(ctx.repo_root, status, scope, sections),
    )

Activity · 5 steps
lines 307–307status = db_status_summary(Path(db_path).expanduser().resolve() if db_path else None)lines 308–313ctx = build_context( repo_root, db_path if status.get("usable") else None, dry_run=dry_run, use_declaration=use_declaration, )lines 314–314sections = _sections(ctx)lines 315–315scope = scope_summary(ctx)lines 316–323return Proposal( repo_root=ctx.repo_root, db_path=ctx.db_path, db_status=status, scope=scope, sections=sections, yaml_text=render_declaration(ctx.repo_root, status, scope, sections), )

Requirements

REQ-DDB-CONFIG-001

Flows

scope_summary

dict[str, Any] clew.propose.registry.scope_summary(Context ctx)

Scope and corpus summary for the rendered header.

clew/propose/registry.py:478

Source · 21 lines
def scope_summary(ctx: Context) -> dict[str, Any]:
    """Reports the C/C++ subset separately from the parsed total: the parser
    router also covers Python, and only the C/C++ count describes what the R1 AST
    detectors were able to read.

    @brief Summarise the derived scope and the scanned corpus.
    @version 2
    """
    return {
        "source": ctx.scope.source,
        "reason": ctx.scope.reason,
        "derived": ctx.scope.is_derived(),
        "roots": len(ctx.scope.roots),
        "excludes": len(ctx.scope.excludes),
        "files_parsed": ctx.corpus.files_parsed,
        "files_in_scope": ctx.corpus.files_in_scope,
        "ast_readable_in_scope": ast_readable_in_scope(ctx.files, ctx.in_scope),
        "corpus_truncated": ctx.corpus.truncated,
    }

Activity · 1 steps
lines 486–496return { "source": ctx.scope.source, "reason": ctx.scope.reason, "derived": ctx.scope.is_derived(), "roots": len(ctx.scope.roots), "excludes": len(ctx.scope.excludes), "files_parsed": ctx.corpus.files_parsed, "files_in_scope": ctx.corpus.files_in_scope, "ast_readable_in_scope": ast_readable_in_scope(ctx.files, ctx.in_scope), "corpus_truncated": ctx.corpus.truncated, }

Requirements

REQ-DDB-CONFIG-001

Flows

section_names

tuple[str, ...] clew.propose.registry.section_names()

Every .clew.yaml section this registry accounts for.

clew/propose/registry.py:503

Source · 9 lines
def section_names() -> tuple[str, ...]:
    """Exists so a test can assert the registry covers the declaration module's
    whole surface — the guarantee that a newly-added section cannot be silently
    omitted from a draft.

    @brief List the sections the proposer reports on.
    @version 1
    """
    return (*(name for name, _ in _DETECTED), *HAND_DECLARED, INDEX_SCOPE_SECTION)

1 more lines, not shown

Activity · 1 steps
lines 511–511return (*(name for name, _ in _DETECTED), *HAND_DECLARED, INDEX_SCOPE_SECTION)

Requirements

REQ-DDB-CONFIG-001