command.py¶
clew/propose/command.py
`clew propose` — write a starter `.clew.yaml` draft for a repo.
_configure_logging
None clew.propose.command._configure_logging(bool verbose)
Send logging to stderr so the draft on stdout stays parseable.
Source · 12 lines
def _configure_logging(verbose: bool) -> None:
"""Deliberately NOT the pipeline's rich handler, which writes to stdout — a
single INFO line there lands inside the emitted YAML.
@brief Configure stderr logging for the proposer.
@version 1
"""
logging.basicConfig(
level=logging.INFO if verbose else logging.WARNING,
format="%(levelname)s %(message)s",
stream=sys.stderr,
)1 more lines, not shown
Activity · 1 steps
Flows
_emit
None clew.propose.command._emit(str text, Path|None target)
Write the draft to a file or to stdout.
Source · 10 lines
def _emit(text: str, target: Path | None) -> None:
"""@brief Emit the draft, reporting a file write on stderr."""
if target is None:
sys.stdout.write(text)
return
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text(text, encoding="utf-8")
print(f"wrote {target} ({len(text.splitlines())} lines, all comments)", file=sys.stderr)
Activity · 6 steps
Flows
_refusal
str clew.propose.command._refusal(Path|None target, bool force)
Why the output path must not be written, if it must not.
Source · 20 lines
def _refusal(target: Path | None, force: bool) -> str:
"""Checked BEFORE the analysis runs: a scan of a large tree takes tens of
seconds, and refusing after it has finished wastes the wait for an outcome
that was decidable up front.
@brief Decide whether the requested output path may be written.
@version 1
"""
if target is None or force or not target.exists():
return ""
return (
f"refusing to overwrite {target} — it already exists.\n"
"That file is the repo's own authoritative declaration; replacing it with a "
"draft would DISABLE every convention it declares while the build kept "
"succeeding.\n"
"Write elsewhere, review, and merge by hand — or pass --force if you really "
"mean to replace it."
)
Activity · 3 steps
Flows
_resolve_db
Path | None clew.propose.command._resolve_db(str|None explicit, Path repo_root)
Resolve the index to measure candidates against.
Source · 15 lines
def _resolve_db(explicit: str | None, repo_root: Path) -> Path | None:
"""Discovery order: the explicit flag, then the MCP server's registry (the
database an agent would already have built for this repo), then
`<repo>/clew.db`. Never invented — a nonexistent path is reported as "no
index" so the gated sections refuse, rather than as a measurement of nothing.
@brief Find the database a proposal can be measured against.
@version 2
"""
## 2.1 (clew#74): one rule, shared with `clew guard impact` (`clew.indexlocate`).
from ..indexlocate import find_index
return find_index(repo_root, explicit)
Activity · 2 steps
Flows
build_parser
argparse.ArgumentParser clew.propose.command.build_parser()
Build the clew propose argument parser.
Source · 60 lines
def build_parser() -> argparse.ArgumentParser:
"""@brief Construct the proposer's CLI parser."""
parser = argparse.ArgumentParser(
prog="clew propose",
description="Propose a starter .clew.yaml for a repo, from evidence.",
epilog=_EPILOG,
)
parser.add_argument(
"--repo-root",
default=".",
help="Repo to analyse (default: the current directory).",
)
parser.add_argument(
"--db",
help=(
"Index to measure candidates against. Defaults to the registered "
"database for this repo, then <repo-root>/clew.db. Without one, every "
"dry-run-gated section refuses to propose."
),
)
parser.add_argument(
"-o",
"--output",
help=(
f"Write the draft here instead of stdout (typically <repo>/{DECLARATION_NAME}). "
"An existing file is REFUSED unless --force is also given."
),
)
parser.add_argument(
"--force",
action="store_true",
help=(
"Overwrite the --output file if it exists. The repo's own declaration is "
"authoritative, so this is never implied."
),
)
parser.add_argument(
"--no-dry-run",
action="store_true",
help=(
"Do not measure candidates against the index. FAIL-CLOSED: the gated "
"sections then propose NOTHING, because an unmeasured shared-key "
"declaration can mint a blob of fabricated causal edges."
),
)
parser.add_argument(
"--ignore-declaration",
action="store_true",
help=(
"Detect as if the repo declared nothing, so entries its .clew.yaml "
"already covers are proposed again instead of being refused as covered. "
"Use it to AUDIT an existing declaration against the evidence. The index "
"scope is still derived normally — every measured number must come from "
"the scope the build actually uses."
),
)
parser.add_argument("--verbose", action="store_true", help="Verbose logging (to stderr).")
return parser
Activity · 9 steps
Requirements
Flows
propose_main
int clew.propose.command.propose_main(list[str]|None argv=None)
Run clew propose .
Called by main
Tested by
Calls _configure_logging, _emit, _refusal, _resolve_db, build_parser, propose
Source · 30 lines
def propose_main(argv: list[str] | None = None) -> int:
"""Stdout is redirected to stderr FOR THE ANALYSIS. Every dry run re-runs a
pipeline importer, and those render a rich progress bar onto the shared
console — whose file is `sys.stdout`. Piping the draft (`clew propose >
.clew.yaml`, the obvious first thing anyone does) would otherwise land
progress bars inside the YAML, ahead of the header. Only the finished document
reaches the real stdout.
@brief Parse arguments, build the proposal, emit or refuse.
@version 4
"""
args = build_parser().parse_args(argv)
_configure_logging(args.verbose)
repo_root = Path(args.repo_root).expanduser().resolve()
target = Path(args.output).expanduser().resolve() if args.output else None
refusal = _refusal(target, args.force)
if refusal:
print(refusal, file=sys.stderr)
return 2
with contextlib.redirect_stdout(sys.stderr):
proposal = propose(
repo_root,
_resolve_db(args.db, repo_root),
dry_run=not args.no_dry_run,
use_declaration=not args.ignore_declaration,
)
_emit(proposal.yaml_text, target)
return 0