edq.config.util

 1import os
 2import typing
 3
 4import edq.util.dirent
 5import edq.util.json
 6
 7def update_options_in_config_file(path: str, config_to_write: typing.Dict[str, str]) -> None:
 8    """
 9    Write configs to the specified path.
10    Create the path if it does not exist.
11    Existing keys in the file will be overwritten with the new values.
12    """
13
14    config = {}
15    if (edq.util.dirent.exists(path)):
16        config = edq.util.json.load_path(path)
17
18    config.update(config_to_write)
19
20    edq.util.dirent.mkdir(os.path.dirname(path))
21    edq.util.json.dump_path(config, path, indent = 4)
22
23def remove_options_in_config_file(path: str, config_to_remove: typing.List[str]) -> None:
24    """
25    Remove configs from the specified path.
26    Raises an exception if the given path doesn't exist.
27    """
28
29    config = edq.util.json.load_path(path)
30    for config_option in config_to_remove:
31        config.pop(config_option, None)
32
33    edq.util.json.dump_path(config, path, indent = 4)
34
35def parse_string_config_option(config_option: str) -> typing.Tuple[str, str]:
36    """
37    Parse and validate a configuration option string in the format of '<key>=<value>'.
38    Returns the resulting config option as a key-value pair.
39    """
40
41    if ("=" not in config_option):
42        raise ValueError(
43            f"Invalid configuration option string '{config_option}'."
44            + " Configuration options must be provided in the format '<key>=<value>'.")
45
46    (key, value) = config_option.split('=', maxsplit = 1)
47    key = validate_config_key(key, value)
48
49    return key, value
50
51def validate_config_key(config_key: str, config_value: str) -> str:
52    """ Validate a configuration key and return its clean version. """
53
54    key = config_key.strip()
55    if (key == ''):
56        raise ValueError(f"Found an empty configuration option key associated with the value '{config_value}'.")
57
58    return key
def update_options_in_config_file(path: str, config_to_write: Dict[str, str]) -> None:
 8def update_options_in_config_file(path: str, config_to_write: typing.Dict[str, str]) -> None:
 9    """
10    Write configs to the specified path.
11    Create the path if it does not exist.
12    Existing keys in the file will be overwritten with the new values.
13    """
14
15    config = {}
16    if (edq.util.dirent.exists(path)):
17        config = edq.util.json.load_path(path)
18
19    config.update(config_to_write)
20
21    edq.util.dirent.mkdir(os.path.dirname(path))
22    edq.util.json.dump_path(config, path, indent = 4)

Write configs to the specified path. Create the path if it does not exist. Existing keys in the file will be overwritten with the new values.

def remove_options_in_config_file(path: str, config_to_remove: List[str]) -> None:
24def remove_options_in_config_file(path: str, config_to_remove: typing.List[str]) -> None:
25    """
26    Remove configs from the specified path.
27    Raises an exception if the given path doesn't exist.
28    """
29
30    config = edq.util.json.load_path(path)
31    for config_option in config_to_remove:
32        config.pop(config_option, None)
33
34    edq.util.json.dump_path(config, path, indent = 4)

Remove configs from the specified path. Raises an exception if the given path doesn't exist.

def parse_string_config_option(config_option: str) -> Tuple[str, str]:
36def parse_string_config_option(config_option: str) -> typing.Tuple[str, str]:
37    """
38    Parse and validate a configuration option string in the format of '<key>=<value>'.
39    Returns the resulting config option as a key-value pair.
40    """
41
42    if ("=" not in config_option):
43        raise ValueError(
44            f"Invalid configuration option string '{config_option}'."
45            + " Configuration options must be provided in the format '<key>=<value>'.")
46
47    (key, value) = config_option.split('=', maxsplit = 1)
48    key = validate_config_key(key, value)
49
50    return key, value

Parse and validate a configuration option string in the format of '='. Returns the resulting config option as a key-value pair.

def validate_config_key(config_key: str, config_value: str) -> str:
52def validate_config_key(config_key: str, config_value: str) -> str:
53    """ Validate a configuration key and return its clean version. """
54
55    key = config_key.strip()
56    if (key == ''):
57        raise ValueError(f"Found an empty configuration option key associated with the value '{config_value}'.")
58
59    return key

Validate a configuration key and return its clean version.