Coverage for packages / dqm-ml / src / dqm_ml / __main__.py: 92%

40 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-15 10:11 +0000

1"""Main CLI entry point for DQM-ML v2. 

2 

3This module provides the command-line interface for running DQM-ML 

4commands including version display, listing available plugins, and 

5processing data quality assessment jobs. 

6""" 

7 

8import argparse 

9from collections.abc import Iterable 

10import logging 

11from typing import Any 

12 

13from typing_extensions import override 

14 

15from dqm_ml.cli_tools import CustomFormatter 

16from dqm_ml.dependency import get_available_command 

17 

18logger = logging.getLogger(__name__) 

19 

20 

21class _HelpAction(argparse._HelpAction): 

22 """Custom help action to support command-specific help.""" 

23 

24 @override 

25 def __call__( 

26 self, 

27 parser: argparse.ArgumentParser, 

28 namespace: argparse.Namespace, 

29 values: str | Iterable[Any] | None, 

30 option_string: str | None = None, 

31 ) -> None: 

32 if namespace.command: 

33 # print help for the specific command 

34 command_list = get_available_command() 

35 if namespace.command in command_list and command_list[namespace.command] is not None: 

36 command_list[namespace.command](["-h"]) 

37 else: 

38 raise ValueError(f"Unknown command {namespace.command}") 

39 else: 

40 parser.print_help() 

41 parser.exit() 

42 

43 

44def parse_args(arg_list: list[str] | None, command_list: Iterable[str]) -> Any: 

45 parser = argparse.ArgumentParser( 

46 prog="dqm-ml", 

47 description="DQM-ML Job client", 

48 epilog="for more informations see README", 

49 add_help=False, 

50 ) 

51 

52 parser.add_argument("-h", "--help", action=_HelpAction, help="help for help if you need some help") 

53 

54 parser.add_argument("command", choices=command_list, help="Available command for your dqm-ml installation") 

55 

56 parser.add_argument("-v", "--verbose", action="store_true") 

57 parser.add_argument("-q", "--quiet", action="store_true") 

58 

59 cli_args, remaining = parser.parse_known_args(arg_list) 

60 

61 return cli_args, remaining 

62 

63 

64# TODO get parameters, logs, ... 

65def execute(arg_list: list[str] | None = None) -> None: 

66 # Exemple of other optional dependencies command 

67 # with optional_dependencies(optional_dep_mode): 

68 # import dqm_ml_dummy_command.cli 

69 # command_list["dqm_ml_dummy_command"] = dqm_ml_dummy_command.cli 

70 command_list = get_available_command() 

71 

72 args, remaining = parse_args(arg_list, command_list) 

73 

74 if args.verbose: 

75 CustomFormatter.init_log(format="%(name)s - %(message)s (%(filename)s:%(lineno)d)", level=logging.DEBUG) # noqa: E501 

76 elif args.quiet: 76 ↛ 77line 76 didn't jump to line 77 because the condition on line 76 was never true

77 CustomFormatter.init_log(format="%(message)s", level=logging.ERROR) 

78 else: 

79 CustomFormatter.init_log(format="%(message)s", level=logging.INFO) 

80 

81 logger.debug(f"Execution dqm-ml with {arg_list}") 

82 

83 if args.command in command_list and command_list[args.command] is not None: 

84 command_list[args.command](remaining) 

85 else: 

86 raise ValueError(f"Unknown command {args.command}") 

87 

88 

89if __name__ == "__main__": 89 ↛ 90line 89 didn't jump to line 90 because the condition on line 89 was never true

90 execute()