Skip to content

config

Helpers for reading and validating ontokit configuration.

create_config(path, defaults)

Create ontokit config at path from default values.

Source code in ontopy/ontokit/config.py
def create_config(path, defaults):
    """Create ontokit config at `path` from default values."""
    config = {
        key: _as_string(defaults.get(key, "")) for key in REQUIRED_CONFIG_KEYS
    }
    _write_config_with_reference_indices_comment(path, config)
    return config

get_config_path(root)

Return the path to the ontokit configuration file for root.

Source code in ontopy/ontokit/config.py
def get_config_path(root):
    """Return the path to the ontokit configuration file for `root`."""
    return Path(root) / CONFIG_FILENAME

load_config(path)

Load ontokit config from path and return it as a dictionary.

Source code in ontopy/ontokit/config.py
def load_config(path):
    """Load ontokit config from `path` and return it as a dictionary."""
    try:
        content = yaml.safe_load(Path(path).read_text())
    except yaml.YAMLError as exc:
        raise ValueError(f"Invalid YAML in {path}: {exc}") from exc

    if content is None:
        return {}
    if not isinstance(content, dict):
        raise ValueError(f"Expected a mapping in {path}, got {type(content)}")
    return content

missing_required_variables(config)

Return required keys that are missing or empty in config.

Source code in ontopy/ontokit/config.py
def missing_required_variables(config):
    """Return required keys that are missing or empty in `config`."""
    missing = []
    for key in REQUIRED_CONFIG_KEYS:
        value = config.get(key)
        if value is None or str(value).strip() == "":
            missing.append(key)
    return missing

print_config(config, stream=None)

Print required ontokit variables and their values.

Source code in ontopy/ontokit/config.py
def print_config(config, stream=None):
    """Print required ontokit variables and their values."""
    out = stream.write if stream else print

    def emit(line):
        if stream:
            out(f"{line}\n")
        else:
            out(line)

    for key in REQUIRED_CONFIG_KEYS:
        emit(f"  {key}: {config.get(key, '')}")

update_config(path, config, defaults)

Add any missing required keys to config from defaults and save.

Keys that are already present in config are never overwritten. Returns the (possibly updated) config dict and a list of keys that were added.

Source code in ontopy/ontokit/config.py
def update_config(path, config, defaults):
    """Add any missing required keys to `config` from `defaults` and save.

    Keys that are already present in `config` are never overwritten.
    Returns the (possibly updated) config dict and a list of keys that
    were added.
    """
    added = []
    for key in REQUIRED_CONFIG_KEYS:
        value = config.get(key)
        if value is None or str(value).strip() == "":
            config[key] = _as_string(defaults.get(key, ""))
            added.append(key)
    if added:
        _write_config_with_reference_indices_comment(path, config)
    return config, added