edq.config.cmd.unset

Unset the first matching instance of a configuration option.

Does nothing if there is no matching config.

 1"""
 2Unset the first matching instance of a configuration option.
 3
 4Does nothing if there is no matching config.
 5"""
 6
 7import argparse
 8import os
 9import typing
10
11import edq.config.argparser
12import edq.config.load
13import edq.config.util
14
15def run(args: argparse.Namespace) -> int:
16    """ Run the target command and return the suggested exit status. """
17
18    if ((args.scope_file is not None) and (not (edq.util.dirent.exists(args.scope_file)))):
19        print(f"Specified config file does not exist: '{os.path.abspath(args.scope_file)}'.")
20        return 1
21
22    for key in args.config_to_unset:
23        path = args.scope_file
24        if (path is None):
25            path = _get_path_from_source(key, args)
26
27        if (path is None):
28            print(f"Could not find a file where '{key}' was set.")
29            continue
30
31        path = os.path.abspath(path)
32        edq.config.util.remove_options_in_config_file(path, [key])
33        print(f"Unset config option ('{key}') from: '{path}'.")
34
35    return 0
36
37def modify_parser(parser: argparse.ArgumentParser) -> None:
38    """ Add this CLI's flags to the given parser. """
39
40    parser.add_argument('config_to_unset', metavar = "KEY",
41        action = 'store', nargs = '+', type = str,
42        help = ("Configuration key to unset."),
43    )
44
45    edq.config.argparser.add_config_location_argument_group(parser)
46
47def _get_path_from_source(key: str, args: argparse.Namespace) -> typing.Union[str, None]:
48    """ Look through a key's sources for the first file-based entry that matches the specified config. """
49
50    for source in args._config_info.sources.get(key, []):
51        source = typing.cast(edq.config.load.ConfigLoadResult, source)
52
53        if (source.path is None):
54            continue
55
56        # If nothing was specified, match the first path.
57        if ((not args.scope_local) and (not args.scope_project) and (not args.scope_global)):
58            return source.path
59
60        if (args.scope_local and isinstance(source.spec, edq.config.source.LocalSpec)):
61            return source.path
62
63        if (args.scope_project and isinstance(source.spec, edq.config.source.ProjectSpec)):
64            return source.path
65
66        if (args.scope_global and isinstance(source.spec, edq.config.source.GlobalSpec)):
67            return source.path
68
69    return None
def run(args: argparse.Namespace) -> int:
16def run(args: argparse.Namespace) -> int:
17    """ Run the target command and return the suggested exit status. """
18
19    if ((args.scope_file is not None) and (not (edq.util.dirent.exists(args.scope_file)))):
20        print(f"Specified config file does not exist: '{os.path.abspath(args.scope_file)}'.")
21        return 1
22
23    for key in args.config_to_unset:
24        path = args.scope_file
25        if (path is None):
26            path = _get_path_from_source(key, args)
27
28        if (path is None):
29            print(f"Could not find a file where '{key}' was set.")
30            continue
31
32        path = os.path.abspath(path)
33        edq.config.util.remove_options_in_config_file(path, [key])
34        print(f"Unset config option ('{key}') from: '{path}'.")
35
36    return 0

Run the target command and return the suggested exit status.

def modify_parser(parser: argparse.ArgumentParser) -> None:
38def modify_parser(parser: argparse.ArgumentParser) -> None:
39    """ Add this CLI's flags to the given parser. """
40
41    parser.add_argument('config_to_unset', metavar = "KEY",
42        action = 'store', nargs = '+', type = str,
43        help = ("Configuration key to unset."),
44    )
45
46    edq.config.argparser.add_config_location_argument_group(parser)

Add this CLI's flags to the given parser.