Source code for vs_common.config

import json
import yaml
import jsonschema
from typing import TypeVar, cast, Type, Tuple, Optional

from omegaconf import OmegaConf

ServiceConfig = TypeVar("ServiceConfig")


[docs] def load_config( cfg_path: str, configuration_class: Type[ServiceConfig], overrides: Optional[Tuple[str]] = None, ) -> ServiceConfig: """Load the configuration as structured config Args: cfg_path (str): File path to configuration configuration_class(Type[ServiceConfig]): Main, top level model of \ configuration, a dataclass reference overrides: Optional[Tuple[str]]: Tuple of dotlist overrides. Example: \ (`"config.value=42"`, `"foo.bar=baz"`) overrides a yaml config and creates \ the following yaml .. highlight:: yaml .. code-block:: yaml config: value: 42 foo: bar: baz Returns: ServiceConfig: Structured configuration """ config = OmegaConf.load(cfg_path) if overrides is not None: overrides_config = OmegaConf.from_dotlist(list(overrides)) config = OmegaConf.merge(config, overrides_config) structured_config = OmegaConf.structured(configuration_class) final_config = OmegaConf.merge(structured_config, config) return cast(ServiceConfig, OmegaConf.to_object(final_config))
[docs] def validate_config(cfg_path: str, schema_path: str) -> None: """Validate config file against schema Args: cfg_path (str): path to the yaml configuration file schema_path (str): path to the schema file, usually sitting at top of source \ directory """ with open(schema_path) as f1, open(cfg_path) as f2: schema = json.load(f1) config = yaml.load(f2, Loader=yaml.SafeLoader) jsonschema.validate(config, schema)