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.
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
Flows
_blocking
tuple[SectionStatus, str] | None clew.propose.registry._blocking(Context ctx)
Why the AST detectors cannot run, when they cannot.
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
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.
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
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.
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
Flows
_sections
tuple[SectionProposal, ...] clew.propose.registry._sections(Context ctx)
Every section's result, in render order.
Called by propose
Calls _blocked_section, _blocking, _hand_section, _index_scope_section
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
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.
Called by propose
Calls derive_scope, load_declaration, repo_source_files, scan_repo, scope_membership
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
Requirements
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.
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
Requirements
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.
Called by _run_propose, propose_main
Tested by 14 tests
test_a_declared_hand_section_is_reported_as_already_declared, test_a_draft_with_no_candidates_states_nothing, test_a_non_c_indexed_scope_is_not_applicable_not_no_candidates, test_a_zero_byte_docs_db_is_rejected_rather_than_crashed_on, test_an_undeclared_scope_blocks_detection_rather_than_guessing, test_demobot_recovers_the_ingot_convention_from_call_sites, test_draft_becomes_a_statable_option_without_a_file, test_ignore_declaration_re_proposes_a_declared_convention, test_index_scope_is_report_only, test_no_convention_yields_an_empty_but_valid_draft_that_says_so, test_no_dry_run_is_fail_closed, test_rejections_appear_in_the_rendered_draft_with_reasons
Calls _sections, build_context, db_status_summary, render_declaration, scope_summary
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
Requirements
Flows
scope_summary
dict[str, Any] clew.propose.registry.scope_summary(Context ctx)
Scope and corpus summary for the rendered header.
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
Requirements
Flows
section_names
tuple[str, ...] clew.propose.registry.section_names()
Every .clew.yaml section this registry accounts for.
Called by none
Tested by
Calls none
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