def _load_config_from_yaml(path: str = "orion.yml") -> dict: kwargs = {} try: with open(path) as file: config = eyaml.load(file) kwargs = SinkOrion._load_config_from_dict(config) except Exception as e: raise SinkException(f"Cannot read config from file {path}") from e return kwargs
def load_from_yaml(cls, path: str = DEFAULT_CONFIG_FILE): logger.debug(f"Load config from yaml file {path}") config = {} try: with open(path) as file: config = eyaml.load(file) except Exception as e: raise ConfigError(f"Cannot read config from file {path}") from e logger.debug(f"{config=}") return cls(config)
def test_raw_yaml(): d = eyaml.load(""" config: username: admin password: secret service: https://localhost/service """) assert d == { 'config': None, 'password': '******', 'service': 'https://localhost/service', 'username': '******' }
def test_yaml_with_env_and_bad_tags(mocker): d = None mocker.patch.dict(os.environ, { 'SERVICE_PASSWORD': '******', 'SERVICE_HOST': 'localhost' }) with pytest.raises(ConstructorError): d = eyaml.load(""" config: username: admin password: !ENW ${SERVICE_PASSWORD} service: !ENW https://${SERVICE_HOST}/service """) assert d is None
def test_yaml_with_env_but_var_not_set(mocker): mocker.patch.dict( os.environ, {'SERVICE_HOST': 'localhost'}) # env var SERVICE_PASSWORD not set d = eyaml.load(""" config: username: admin password: ${SERVICE_PASSWORD} service: https://${SERVICE_HOST}/service """) assert d == { 'config': None, 'password': '', 'service': 'https://localhost/service', 'username': '******' }
def test_yaml_with_env_and_tags(mocker): mocker.patch.dict(os.environ, { 'SERVICE_PASSWORD': '******', 'SERVICE_HOST': 'localhost' }) d = eyaml.load(""" config: username: admin password: !ENV ${SERVICE_PASSWORD} service: !ENV https://${SERVICE_HOST}/service """) assert d == { 'config': None, 'password': '******', 'service': 'https://localhost/service', 'username': '******' }