Skip to content

Commit

Permalink
Issue #112 Support aggregator_config as config var name to simplify…
Browse files Browse the repository at this point in the history
… config migration
  • Loading branch information
soxofaan committed Feb 6, 2024
1 parent 85c3a5e commit d8c73af
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 19 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.

The format is roughly based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [0.17.0]

- Support `aggregator_config` as `AggregatorConfig` variable name to simplify config system migration
(define `AggregatorConfig` and `AggregatorBackendConfig` in same config file)
([#112](https://github.com/Open-EO/openeo-aggregator/issues/112))


## [0.16.x]

- Disable `auth_entitlement_check` (check on EGI VO entitlements) in all configs ([#133](https://github.com/Open-EO/openeo-aggregator/issues/133))
Expand Down
2 changes: 1 addition & 1 deletion conf/aggregator.dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

ZK_HOSTS = "epod-master1.vgt.vito.be:2181,epod-master2.vgt.vito.be:2181,epod-master3.vgt.vito.be:2181"

config = AggregatorConfig(
aggregator_config = config = AggregatorConfig(
config_source=__file__,
aggregator_backends={
"vito": "https://openeo-dev.vito.be/openeo/1.1/",
Expand Down
2 changes: 1 addition & 1 deletion conf/aggregator.dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from openeo_aggregator.config import AggregatorConfig

config = AggregatorConfig(
aggregator_config = config = AggregatorConfig(
config_source=__file__,
aggregator_backends={
"dummy": "https://openeo.example/openeo/1.1/",
Expand Down
2 changes: 1 addition & 1 deletion conf/aggregator.prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

ZK_HOSTS = "epod-master1.vgt.vito.be:2181,epod-master2.vgt.vito.be:2181,epod-master3.vgt.vito.be:2181"

config = AggregatorConfig(
aggregator_config = config = AggregatorConfig(
config_source=__file__,
aggregator_backends={
"vito": "https://openeo.vito.be/openeo/1.1/",
Expand Down
2 changes: 1 addition & 1 deletion src/openeo_aggregator/about.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
from typing import Optional

__version__ = "0.16.0a1"
__version__ = "0.17.0a1"


def log_version_info(logger: Optional[logging.Logger] = None):
Expand Down
8 changes: 5 additions & 3 deletions src/openeo_aggregator/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ def from_py_file(path: Union[str, Path]) -> 'AggregatorConfig':
code = compile(f.read(), path, "exec")
globals = {"__file__": str(path)}
exec(code, globals)
try:
config = globals["config"]
except KeyError:
for var_name in ["aggregator_config", "config"]:
if var_name in globals:
config = globals[var_name]
break
else:
raise ConfigException(f"No 'config' variable defined in config file {path}")
if not isinstance(config, AggregatorConfig):
raise ConfigException(f"Variable 'config' from {path} is not AggregatorConfig but {type(config)}")
Expand Down
38 changes: 26 additions & 12 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import textwrap
from pathlib import Path
from unittest import mock

Expand All @@ -8,17 +9,22 @@
OPENEO_AGGREGATOR_CONFIG,
STREAM_CHUNK_SIZE_DEFAULT,
AggregatorConfig,
ConfigException,
get_config,
)

CONFIG_PY_EXAMPLE = """
from openeo_aggregator.config import AggregatorConfig
config = AggregatorConfig(
config_source=__file__,
aggregator_backends={"b1": "https://b1.test"},
streaming_chunk_size=123
)
"""

def _get_config_content(config_var_name: str = "config"):
return textwrap.dedent(
f"""
from openeo_aggregator.config import AggregatorConfig
{config_var_name} = AggregatorConfig(
config_source=__file__,
aggregator_backends={{"b1": "https://b1.test"}},
streaming_chunk_size=123
)
"""
)


def test_config_defaults():
Expand All @@ -36,15 +42,23 @@ def test_config_aggregator_backends():
assert config.aggregator_backends == {"b1": "https://b1.test"}


def test_config_from_py_file(tmp_path):
@pytest.mark.parametrize("config_var_name", ["aggregator_config", "config"])
def test_config_from_py_file(tmp_path, config_var_name):
path = tmp_path / "aggregator-conf.py"
path.write_text(CONFIG_PY_EXAMPLE)
path.write_text(_get_config_content(config_var_name=config_var_name))
config = AggregatorConfig.from_py_file(path)
assert config.config_source == str(path)
assert config.aggregator_backends == {"b1": "https://b1.test"}
assert config.streaming_chunk_size == 123


def test_config_from_py_file_wrong_config_var_name(tmp_path):
path = tmp_path / "aggregator-conf.py"
path.write_text(_get_config_content(config_var_name="meh"))
with pytest.raises(ConfigException, match="No 'config' variable defined in config file"):
AggregatorConfig.from_py_file(path)


def test_get_config_default_no_env():
assert OPENEO_AGGREGATOR_CONFIG not in os.environ
config = get_config()
Expand All @@ -54,7 +68,7 @@ def test_get_config_default_no_env():
@pytest.mark.parametrize("convertor", [str, Path])
def test_get_config_py_file_path(tmp_path, convertor):
config_path = tmp_path / "aggregator-conf.py"
config_path.write_text(CONFIG_PY_EXAMPLE)
config_path.write_text(_get_config_content())
config = get_config(convertor(config_path))
assert config.config_source == str(config_path)
assert config.aggregator_backends == {"b1": "https://b1.test"}
Expand All @@ -63,7 +77,7 @@ def test_get_config_py_file_path(tmp_path, convertor):

def test_get_config_env_py_file(tmp_path):
path = tmp_path / "aggregator-conf.py"
path.write_text(CONFIG_PY_EXAMPLE)
path.write_text(_get_config_content())

with mock.patch.dict(os.environ, {OPENEO_AGGREGATOR_CONFIG: str(path)}):
config = get_config()
Expand Down

0 comments on commit d8c73af

Please sign in to comment.