Coverage for lode / cli.py: 0%

43 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-11-22 15:54 +0000

1# lode/cli.py 

2""" 

3lode serve [--port 8000] 

4lode build --url <url> --read-as <owl|rdf|skos> [--out ./docs] [--lang en] [--artefact-name index] [--imported] [--closure] 

5lode build --file <path> --read-as <owl|rdf|skos> [--out ./docs] [--lang en] [--artefact-name index] [--imported] [--closure] 

6""" 

7 

8import argparse 

9import sys 

10from pathlib import Path 

11 

12def cmd_serve(args): 

13 """Start the FastAPI web service with uvicorn. 

14 

15 uvicorn is imported lazily because it belongs to the optional [server] 

16 extra: the build command must keep working without it. Blocks until the 

17 server is stopped; binds to all interfaces so it works inside Docker. 

18 """ 

19 try: 

20 import uvicorn 

21 except ImportError: 

22 sys.exit("Server dependencies missing: pip install oc-lode[server]") 

23 uvicorn.run("lode.api:app", host="0.0.0.0", port=args.port, reload=False) 

24 

25 

26def cmd_build(args): 

27 """Generate the static HTML documentation for one artefact. 

28 

29 Goes through lode.core only, imported lazily so that `lode serve` and `--help` 

30 do not pay its import cost. Creates the output directory if missing and 

31 overwrites existing pages in it. Prints the output path on success. 

32 """ 

33 from lode.core import build_site, read 

34 

35 out_dir = Path(args.out) 

36 artefact = read( 

37 args.url or args.file, args.read_as, 

38 imported=args.imported or None, 

39 closure=args.closure or None, 

40 partial_import=args.partial_import or None, 

41 warnings=args.warnings, 

42 ) 

43 build_site(artefact, out_dir, lang=args.lang, artefact_name=args.artefact_name) 

44 

45 if args.warnings: 

46 warnings = artefact.viewer.format_warnings(artefact.warnings) 

47 for w in warnings: 

48 print(f"[{w['code']}] {w['message']}") 

49 if warnings: 

50 print(f"Warnings report -> {out_dir / 'report.txt'}") 

51 else: 

52 print("No reader warnings") 

53 print(f"Done -> {out_dir}") 

54 

55 

56def main(): 

57 """Parse the command line and dispatch to the chosen subcommand. 

58 

59 Entry point of the `lode` script declared in pyproject.toml. Adds no logic 

60 of its own: serve and build are thin adapters over the core. 

61 """ 

62 parser =argparse.ArgumentParser(prog="lode") 

63 sub = parser.add_subparsers(dest="cmd", required=True) 

64 

65 # --- serve --- 

66 p_serve = sub.add_parser("serve", help="Start the FastAPI web service") 

67 p_serve.add_argument("--port", type=int, default=8000) 

68 

69 # --- build --- 

70 p_build = sub.add_parser("build", help="Generate static HTML documentation") 

71 src = p_build.add_mutually_exclusive_group(required=True) 

72 src.add_argument("--url") 

73 src.add_argument("--file") 

74 p_build.add_argument("--read-as", required=True, 

75 help="Artefact format: a registered profile, e.g. owl or skos") 

76 p_build.add_argument("--out", default="./lode_output") 

77 p_build.add_argument("--lang", default="en") 

78 p_build.add_argument("--artefact-name", default="index", 

79 help="File stem of the root page and its serializations (default: index)") 

80 p_build.add_argument("--imported", action="store_true") 

81 p_build.add_argument("--closure", action="store_true") 

82 p_build.add_argument("--partial-import", action="store_true") 

83 p_build.add_argument("--warnings", action="store_true", 

84 help="Print reader warnings and write them to report.txt in the output folder") 

85 

86 args = parser.parse_args() 

87 {"serve": cmd_serve, "build": cmd_build}[args.cmd](args) 

88 

89 

90if __name__ == "__main__": 

91 main()