Coverage for flowr / cli / serve.py: 65%
40 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-22 18:42 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-22 18:42 +0000
1"""CLI ``serve`` command — start the viz visualization server."""
3import argparse
4import os
5import sys
6from pathlib import Path
9def add_serve_parser(sub: argparse._SubParsersAction) -> None:
10 """Register the ``serve`` sub-command on the argument parser.
12 Args:
13 sub: The ``_SubParsersAction`` provided by the parent parser.
14 """
15 p = sub.add_parser("serve", help="Start the viz visualization server")
16 p.add_argument("--host", default="localhost", help="Host to bind to")
17 p.add_argument("--port", type=int, default=8080, help="Port to listen on")
18 p.add_argument("--path", default=".", help="Path to project directory")
19 p.add_argument("--edit", action="store_true", help="Enable edit mode")
22def cmd_serve(args: argparse.Namespace) -> int:
23 """Execute the ``serve`` sub-command.
25 Args:
26 args: Parsed command-line arguments.
28 Returns:
29 0 on success, 1 on any error.
30 """
31 path = Path(args.path)
32 if not path.exists():
33 print( # noqa: T201
34 f"error: path does not exist: {args.path}", file=sys.stderr
35 )
36 return 1
37 if not path.is_dir():
38 print( # noqa: T201
39 f"error: path is not a directory: {args.path}", file=sys.stderr
40 )
41 return 1
42 if not os.access(path, os.R_OK):
43 print( # noqa: T201
44 f"error: path is not readable: {args.path}", file=sys.stderr
45 )
46 return 1
47 try:
48 import fastapi # noqa: F401
49 import uvicorn # noqa: F401
50 except ImportError:
51 print( # noqa: T201
52 "error: viz dependencies not installed (pip install flowr[viz])",
53 file=sys.stderr,
54 )
55 return 1
56 try:
57 from flowr.server.app import run_server
58 from flowr.server.config import ServerConfig
59 except ImportError as e:
60 print(f"error: {e}", file=sys.stderr) # noqa: T201
61 return 1
62 try:
63 config = ServerConfig(
64 host=args.host,
65 port=args.port,
66 path=path,
67 edit_mode=args.edit,
68 )
69 run_server(config)
70 return 0
71 except OSError as e:
72 print(f"error: {e}", file=sys.stderr) # noqa: T201
73 return 1