Coverage for datakiln/arguments.py: 0%

17 statements  

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

1""" 

2Options that the commands share, and the log they write. 

3""" 

4import logging 

5import sys 

6from argparse import ArgumentParser, BooleanOptionalAction 

7 

8 

9def add_directory(parser: ArgumentParser): 

10 """Adds the directory of the dataset, which every command takes. 

11 

12 :param parser: Parser of a command. 

13 :type parser: argparse.ArgumentParser 

14 """ 

15 parser.add_argument("directory", metavar="directory", type=str, 

16 help="Path of the directory of the dataset.") 

17 

18 

19def add_log(parser: ArgumentParser): 

20 """Adds the option to show the log, which commands write to a file. 

21 

22 :param parser: Parser of a command. 

23 :type parser: argparse.ArgumentParser 

24 """ 

25 parser.add_argument("--stdout", action="store_true", 

26 help="Output logs to standard output as well as to " 

27 "the log file.") 

28 

29 

30def add_include(parser: ArgumentParser, what: str): 

31 """Adds the option that limits a command to some of the things it works 

32 on. 

33 

34 :param parser: Parser of a command. 

35 :type parser: argparse.ArgumentParser 

36 

37 :param what: What the command does with the names, as a sentence. 

38 :type what: str 

39 """ 

40 parser.add_argument("--include", metavar="name", type=str, nargs="+", 

41 help=what) 

42 

43 

44def add_export(parser: ArgumentParser): 

45 """Adds the options that tell what a command that reads from a source 

46 writes to the dataset. 

47 

48 :param parser: Parser of a command. 

49 :type parser: argparse.ArgumentParser 

50 """ 

51 parser.add_argument("--data", action=BooleanOptionalAction, default=True, 

52 help="Whether to write the data files.") 

53 parser.add_argument("--metadata", action=BooleanOptionalAction, 

54 default=False, 

55 help="Whether to write the metadata files of the " 

56 "tables.") 

57 

58 

59def setup_logging(stdout: bool, log_file: str): 

60 """Sets up logging for a command. 

61 

62 :param stdout: Whether to output logs to standard output. 

63 :type stdout: bool 

64 

65 :param log_file: Path to the log file. 

66 :type log_file: str 

67 """ 

68 handlers = [logging.FileHandler(log_file, mode="w")] 

69 if stdout: 

70 handlers.append(logging.StreamHandler(sys.stdout)) 

71 

72 logging.basicConfig( 

73 level=logging.INFO, datefmt="%Y-%m-%d %H:%M:%S", 

74 format="%(asctime)s %(name)-12s %(levelname)-8s %(message)s", 

75 handlers=handlers 

76 )