Coverage for datakiln/cli.py: 0%

25 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-16 23:23 +0000

1""" 

2Command line of datakiln, which builds a database from a repository of files. 

3""" 

4from argparse import ArgumentParser 

5from typing import List, Optional 

6 

7from datakiln import ( 

8 build_database, build_dbms, build_metadata, build_redcap, show_graph 

9) 

10from datakiln.arguments import setup_logging 

11 

12 

13# Name of each command, with what runs it, what it does and the log it writes 

14COMMANDS = [ 

15 ("build", build_database, "Build the database from the repository, " 

16 "loading only what changed.", "build.log"), 

17 ("metadata", build_metadata, "Generate the metadata of the tables from " 

18 "the data files.", "metadata.log"), 

19 ("graph", show_graph, "Show what the tables and scripts depend on.", None), 

20] 

21 

22# The same for the commands that read from a source 

23SOURCES = [ 

24 ("dbms", build_dbms, "Extract data, and optionally the metadata of the " 

25 "tables, from a source database."), 

26 ("redcap", build_redcap, "Extract data, and optionally the metadata of " 

27 "the tables, from a REDCap project."), 

28] 

29 

30 

31def get_parser() -> ArgumentParser: 

32 """Builds the parser of the commands. 

33 

34 :return: Parser, whose result has the function that runs the command and 

35 the log it writes. 

36 :rtype: argparse.ArgumentParser 

37 """ 

38 parser = ArgumentParser(prog="datakiln", description=__doc__) 

39 commands = parser.add_subparsers(dest="command", required=True, 

40 metavar="command") 

41 

42 for name, module, summary, log_file in COMMANDS: 

43 command = commands.add_parser(name, help=summary, description=summary) 

44 module.add_arguments(command) 

45 command.set_defaults(run=module.run, log_file=log_file) 

46 

47 extract = commands.add_parser( 

48 "extract", help="Extract data from a source into the dataset.", 

49 description="Extract data from a source into the dataset.") 

50 sources = extract.add_subparsers(dest="source", required=True, 

51 metavar="source") 

52 for name, module, summary in SOURCES: 

53 source = sources.add_parser(name, help=summary, description=summary) 

54 module.add_arguments(source) 

55 source.set_defaults(run=module.run, log_file="extract.log") 

56 

57 return parser 

58 

59 

60def main(args: Optional[List[str]] = None): 

61 """Runs the command that was asked for. 

62 

63 :param args: Arguments of the command line. Those of the process by 

64 default. 

65 :type args: list[str] 

66 """ 

67 args = get_parser().parse_args(args) 

68 if args.log_file: 

69 setup_logging(args.stdout, args.log_file) 

70 args.run(args)