cli.py

clew/cli.py

argparse + pipeline glue for `python -m clew`.

main

main()

clew/cli.py:3964

Show on the map →

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
lines 3995–3995◇ if sys.argv[1:2] == [PROPOSE_COMMAND]lines 3996–3998from .propose.command import ( propose_main, )lines 4000–4000sys.exit(propose_main(sys.argv[2:]))lines 4001–4001◇ if sys.argv[1:2] == [EXPORT_COMMAND]lines 4002–4004from .export_command import ( export_main, )lines 4006–4006sys.exit(export_main(sys.argv[2:]))lines 4007–4007◇ if sys.argv[1:2] == [INIT_COMMAND]lines 4008–4010from .init_command import ( init_main, )lines 4012–4012sys.exit(init_main(sys.argv[2:]))lines 4013–4013◇ if sys.argv[1:2] == [GUARD_COMMAND]lines 4014–4014from .guard.main import main as guard_mainlines 4016–4016sys.exit(guard_main(sys.argv[2:]))lines 4017–4017◇ if sys.argv[1:2] == [QUERY_COMMAND]lines 4018–4018from .query_command import query_mainlines 4020–4020sys.exit(query_main(sys.argv[2:]))lines 4021–4021◇ if sys.argv[1:2] == [REFRESH_COMMAND]lines 4022–4022from .refresh_command import refresh_mainlines 4024–4024sys.exit(refresh_main(sys.argv[2:]))lines 4025–4025◇ if sys.argv[1:2] == [WIKI_COMMAND]lines 4026–4026from .wiki.command import wiki_mainlines 4028–4028sys.exit(wiki_main(sys.argv[2:]))lines 4029–4029◇ if sys.argv[1:2] == [CULL_COMMAND]lines 4030–4030from .cull_command import cull_mainlines 4032–4032sys.exit(cull_main(sys.argv[2:]))lines 4033–4033args = _build_argparser().parse_args()lines 4034–4034_configure_logging(args.verbose)lines 4035–4035_resolve_output(args)lines 4036–4036trylines 4037–4037_run_pipeline(args)lines 4038–4038except (DeclarationError, BuildOptionError) as exclines 4039–4039logger.error("invalid declaration — %s", exc)lines 4040–4040sys.exit(2)lines 4041–4041except (DoxygenUnavailableError, RustdocUnavailableError) as exclines 4049–4049logger.error("cannot build — %s", exc)lines 4050–4050sys.exit(2)

Flows

report_stats

None clew.cli.report_stats(Path db_path)

Print summary statistics of the generated database.

clew/cli.py:310

Show on the map →

Called by _run_pipeline_inner

Calls active_console

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)

Activity · 8 steps
lines 322–322conn = sqlite3.connect(str(db_path))lines 323–328tables = [ r[0] for r in conn.execute( "SELECT name FROM sqlite_master WHERE type='table'", ).fetchall() ]lines 329–329lines = [f"\n Database: {db_path}", f" Tables: {len(tables)}"]lines 330–330◇ for table in sorted(tables)lines 331–331count = conn.execute(f"SELECT COUNT(*) FROM [{table}]").fetchone()[0]lines 332–332lines.append(f" {table}: {count} rows")lines 333–333conn.close()lines 334–334active_console().print("\n".join(lines), markup=False, highlight=False, soft_wrap=True)

Requirements

REQ-DDB-CLI-001