cli.py¶
clew/cli.py
argparse + pipeline glue for `python -m clew`.
main
main()
Called by none
Calls _build_argparser, _resolve_output, _run_pipeline, cull_main, export_main, init_main, propose_main, query_main, refresh_main, wiki_main
Source · 87 lines
def main() -> None:
"""Entry point — parse args, run doxygen, optionally enrich.
`clew propose` and `init` are dispatched on the FIRST word
rather than by converting the build parser into subparsers: the build's flat
flags are the interface every existing caller uses — `python -m
clew --output ...` — and moving them under a `build` subcommand would
break all of them for a cosmetic gain. Every subcommand module is imported
LAZILY, inside its branch, so a build never pays for them and `init` in
particular stays runnable on an install missing the pipeline's heavier
optional dependencies.
An embedding caller does not come through here at all: `build_index` runs the
same pipeline from typed arguments, so nothing has to survive a command line.
A `DeclarationError` is caught here and reported as one line rather than a
traceback: it means the target repo's own `.clew.yaml` (or a standalone
manifest) names a value no schema vocabulary allows, and the message already
carries the file, the token and the allowed set. It is the owner's config to
fix, not a clew crash, so it exits 2 to separate it from a build failure.
A `BuildOptionError` gets the SAME treatment and it is the same kind of fact one door
over: the caller's own `--declare` document names a section nothing reads, or shapes an
entry so no loader will see it. Without this it surfaced as a traceback out of
`apply_options`, which reads as a crash in the tool for what is a typo in a YAML file —
and the message already carries the key, the allowed set and why a silent default would
have been worse.
@brief Parse CLI arguments and run the build pipeline.
@version 18
"""
if sys.argv[1:2] == [PROPOSE_COMMAND]:
from .propose.command import (
propose_main,
)
sys.exit(propose_main(sys.argv[2:]))
if sys.argv[1:2] == [EXPORT_COMMAND]:
from .export_command import (
export_main,
)
sys.exit(export_main(sys.argv[2:]))
if sys.argv[1:2] == [INIT_COMMAND]:
from .init_command import (
init_main,
)
sys.exit(init_main(sys.argv[2:]))
if sys.argv[1:2] == [GUARD_COMMAND]:
from .guard.main import main as guard_main
sys.exit(guard_main(sys.argv[2:]))
if sys.argv[1:2] == [QUERY_COMMAND]:
from .query_command import query_main
sys.exit(query_main(sys.argv[2:]))
if sys.argv[1:2] == [REFRESH_COMMAND]:
from .refresh_command import refresh_main
sys.exit(refresh_main(sys.argv[2:]))
if sys.argv[1:2] == [WIKI_COMMAND]:
from .wiki.command import wiki_main
sys.exit(wiki_main(sys.argv[2:]))
if sys.argv[1:2] == [CULL_COMMAND]:
from .cull_command import cull_main
sys.exit(cull_main(sys.argv[2:]))
args = _build_argparser().parse_args()
_configure_logging(args.verbose)
_resolve_output(args)
try:
_run_pipeline(args)
except (DeclarationError, BuildOptionError) as exc:
logger.error("invalid declaration — %s", exc)
sys.exit(2)
except (DoxygenUnavailableError, RustdocUnavailableError) as exc:
# Same treatment as a bad declaration, for the same reason: this is the
# environment being wrong, not clew failing. It used to surface as a
# twelve-frame FileNotFoundError naming 'doxygen', which reads as a crash
# in this tool and is the first thing a new user on a clean machine sees.
# RustdocUnavailableError is the Rust-repo analog — same refusal shape,
# different missing prerequisite (a nightly toolchain instead of a
# system package).
logger.error("cannot build — %s", exc)
sys.exit(2)Activity · 35 steps
Flows
report_stats
None clew.cli.report_stats(Path db_path)
Print summary statistics of the generated database.
Source · 27 lines
def report_stats(db_path: Path) -> None:
"""Print summary statistics of the generated database onto the active console.
The second of the two build-path writers that render outside `make_progress`,
so it goes through the same seam: a caller whose stdout is a protocol transport
captures this summary along with everything else. Rendered as one block with
markup and highlighting off — a table name is a SQL identifier, not markup, and
`soft_wrap` keeps a long database path on one line.
@brief Report database contents summary.
@version 2
"""
conn = sqlite3.connect(str(db_path))
tables = [
r[0]
for r in conn.execute(
"SELECT name FROM sqlite_master WHERE type='table'",
).fetchall()
]
lines = [f"\n Database: {db_path}", f" Tables: {len(tables)}"]
for table in sorted(tables):
count = conn.execute(f"SELECT COUNT(*) FROM [{table}]").fetchone()[0]
lines.append(f" {table}: {count} rows")
conn.close()
active_console().print("\n".join(lines), markup=False, highlight=False, soft_wrap=True)