Coverage for jstark / demo / cli.py: 0%

21 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-04-30 09:29 +0000

1# SPDX-FileCopyrightText: 2022-present Jamie Thomson 

2# 

3# SPDX-License-Identifier: MIT 

4"""CLI that copies the bundled demo notebook into the user's cwd.""" 

5 

6from __future__ import annotations 

7 

8import argparse 

9import shutil 

10import sys 

11from importlib.resources import files 

12from pathlib import Path 

13 

14NOTEBOOK_NAME = "GroceryFeatures_demo.ipynb" 

15 

16 

17def _build_parser() -> argparse.ArgumentParser: 

18 """Create the argparse parser for jstark-demo.""" 

19 parser = argparse.ArgumentParser( 

20 prog="jstark-demo", 

21 description=( 

22 f"Copy the bundled {NOTEBOOK_NAME} notebook into the current directory." 

23 ), 

24 ) 

25 parser.add_argument( 

26 "-f", 

27 "--force", 

28 action="store_true", 

29 help="Overwrite an existing file of the same name.", 

30 ) 

31 return parser 

32 

33 

34def main(argv: list[str] | None = None) -> int: 

35 """Run the jstark-demo CLI and return the process exit code.""" 

36 args = _build_parser().parse_args(argv) 

37 

38 source = Path(str(files("jstark.demo") / NOTEBOOK_NAME)) 

39 target = Path.cwd() / NOTEBOOK_NAME 

40 

41 if target.exists() and not args.force: 

42 print( 

43 f"{NOTEBOOK_NAME} already exists; refusing to overwrite. " 

44 "Use --force to overwrite.", 

45 file=sys.stderr, 

46 ) 

47 return 1 

48 

49 shutil.copyfile(source, target) 

50 print( 

51 f"Copied to ./{NOTEBOOK_NAME} — run " 

52 f"'jupyter notebook {NOTEBOOK_NAME}' to open it." 

53 ) 

54 return 0 

55 

56 

57if __name__ == "__main__": 

58 sys.exit(main())