Coverage for src / check_datapackage / cli.py: 91%

23 statements  

« prev     ^ index     » next       coverage.py v7.14.0, created at 2026-05-21 12:38 +0000

1"""Functions for the exposed CLI.""" 

2 

3from typing import Annotated, Any 

4 

5from cyclopts import CycloptsPanel, Parameter 

6from seedcase_soil import ( 

7 Address, 

8 parse_source, 

9 pretty_print, 

10 read_properties, 

11 run_without_tracebacks, 

12 setup_cli, 

13) 

14 

15from check_datapackage.check import check 

16from check_datapackage.config import Config 

17from check_datapackage.exclusion import Exclusion 

18from check_datapackage.extensions import CUSTOM_CHECKS_CONFIG_ERROR, Extensions 

19 

20 

21def _format_cli_error(error: Any) -> Any: 

22 message = str(error) 

23 if CUSTOM_CHECKS_CONFIG_ERROR in message: 

24 return CycloptsPanel(CUSTOM_CHECKS_CONFIG_ERROR) 

25 return CycloptsPanel(error) 

26 

27 

28app = setup_cli( 

29 name="check-datapackage", 

30 help=( 

31 "check-datapackage checks if metadata is compliant with the Data Package" 

32 "standard" 

33 ), 

34 config_name=".cdp.toml", 

35) 

36app.error_formatter = _format_cli_error 

37 

38 

39@app.command(name="check") 

40def check_cmd( 

41 source: str = "datapackage.json", 

42 /, # End of positional-only args 

43 *, # Start of keyword-only params 

44 strict: bool = False, 

45 exclusions: Annotated[list[Exclusion], Parameter(show=False)] = [], 

46 extensions: Annotated[Extensions, Parameter(show=False)] = Extensions(), 

47) -> None: 

48 """Check a Data Package's metadata against the Data Package standard. 

49 

50 Outputs a human-readable explanation of any issues found. 

51 

52 Args: 

53 source: The location of a `datapackage.json`, defaults to a file or folder 

54 path. Can also be an `https:` source to a remote `datapackage.json` or a 

55 `github:` / `gh:` pointing to a repo with a `datapackage.json` 

56 in the repo root (in the format `gh:org/repo`, which can also include 

57 reference to a tag or branch, such as `gh:org/repo@main` or 

58 `gh:org/repo@1.0.1`). 

59 strict: If True, check "SHOULD" properties in addition to "MUST" 

60 properties from the Data Package standard. 

61 exclusions: A hidden CLI/config parameter for excluding issues by JSONPath 

62 and/or issue type. 

63 extensions: A hidden CLI/config parameter for adding extra checks. 

64 """ 

65 address: Address = parse_source(source) 

66 properties: dict[str, Any] = read_properties(address) 

67 config = Config( 

68 strict=strict, 

69 exclusions=exclusions, 

70 extensions=extensions, 

71 ) 

72 check(properties, config=config, error=True) 

73 pretty_print("[green]All checks passed![/green]") 

74 

75 

76def main() -> None: 

77 """Create an entry point to run the cli without tracebacks.""" 

78 run_without_tracebacks(app)