Coverage for datakiln/arguments.py: 0%
17 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-16 23:23 +0000
« 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
9def add_directory(parser: ArgumentParser):
10 """Adds the directory of the dataset, which every command takes.
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.")
19def add_log(parser: ArgumentParser):
20 """Adds the option to show the log, which commands write to a file.
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.")
30def add_include(parser: ArgumentParser, what: str):
31 """Adds the option that limits a command to some of the things it works
32 on.
34 :param parser: Parser of a command.
35 :type parser: argparse.ArgumentParser
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)
44def add_export(parser: ArgumentParser):
45 """Adds the options that tell what a command that reads from a source
46 writes to the dataset.
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.")
59def setup_logging(stdout: bool, log_file: str):
60 """Sets up logging for a command.
62 :param stdout: Whether to output logs to standard output.
63 :type stdout: bool
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))
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 )