Skip to content

Commit

Permalink
Merge pull request #692 from opendatacube/config_tweaks
Browse files Browse the repository at this point in the history
Config tweaks
  • Loading branch information
SpacemanPaul authored Aug 13, 2021
2 parents 0c17425 + 7d495a9 commit 26d0f42
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 11 deletions.
7 changes: 5 additions & 2 deletions datacube_ows/config_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,11 @@ def import_python_obj(path: str) -> RAW_CFG:
:return: a Python object, or None
"""
mod_name, obj_name = path.rsplit('.', 1)
mod = import_module(mod_name)
obj = getattr(mod, obj_name)
try:
mod = import_module(mod_name)
obj = getattr(mod, obj_name)
except (ImportError, ValueError, ModuleNotFoundError, AttributeError):
raise ConfigException(f"Could not import python object: {path}")
return cast(RAW_CFG, obj)


Expand Down
7 changes: 5 additions & 2 deletions datacube_ows/ogc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,11 @@ def get_function(func: Union[F, str]) -> F:
"""
if func is not None and not callable(func):
mod_name, func_name = func.rsplit('.', 1)
mod = import_module(mod_name)
func = getattr(mod, func_name)
try:
mod = import_module(mod_name)
func = getattr(mod, func_name)
except (ImportError, ModuleNotFoundError, ValueError, AttributeError):
raise ConfigException(f"Could not import python object: {func}")
assert callable(func)
return cast(F, func)

Expand Down
15 changes: 9 additions & 6 deletions datacube_ows/ows_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,15 @@ def __init__(self, cfg, global_cfg, parent_layer=None, sibling=0, **kwargs):
raise ConfigException("No layers section in folder layer %s" % self.title)
child = 0
for lyr_cfg in cfg["layers"]:
try:
lyr = parse_ows_layer(lyr_cfg, global_cfg=global_cfg, parent_layer=self, sibling=child)
self.unready_layers.append(lyr)
except ConfigException as e:
_LOG.error("Could not parse layer: %s", str(e))
child += 1
if isinstance(lyr_cfg, Mapping):
try:
lyr = parse_ows_layer(lyr_cfg, global_cfg=global_cfg, parent_layer=self, sibling=child)
self.unready_layers.append(lyr)
except ConfigException as e:
_LOG.error("Could not parse layer: %s", str(e))
child += 1
else:
_LOG.error("Non-dictionary where dictionary expected - check for trailing comma? %s...", repr(lyr_cfg)[0:50])
global_cfg.folder_index[obj_lbl] = self

def unready_layer_count(self):
Expand Down
13 changes: 13 additions & 0 deletions tests/cfg/broken_nested.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This file is part of datacube-ows, part of the Open Data Cube project.
# See https://opendatacube.org for more information.
#
# Copyright (c) 2017-2021 OWS Contributors
# SPDX-License-Identifier: Apache-2.0

mixed_3 = {
"test": 2634,
"subtest": {
"include": "tests.cfg.simple.doesnt_exist",
"type": "python"
}
}
9 changes: 9 additions & 0 deletions tests/test_cfg_inclusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,15 @@ def test_cfg_py_mixed_1(monkeypatch):
assert cfg["test"] == 1234


def test_cfg_py_broken_mixed(monkeypatch):
monkeypatch.chdir(src_dir)
monkeypatch.setenv("DATACUBE_OWS_CFG", "tests.cfg.broken_nested.mixed_3")
with pytest.raises(ConfigException) as e:
cfg = read_config()
assert "Could not import python object" in str(e.value)
assert "tests.cfg.simple.doesnt_exist" in str(e.value)


def test_cfg_py_mixed_2(monkeypatch):
monkeypatch.chdir(src_dir)
monkeypatch.setenv("DATACUBE_OWS_CFG", "tests.cfg.mixed_nested.mixed_2")
Expand Down
11 changes: 11 additions & 0 deletions tests/test_cfg_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ def test_catch_invalid_folder_layers(minimal_global_cfg):
assert len(lyr.unready_layers) == 0


def test_catch_folder_as_list(minimal_global_cfg):
lyr = OWSFolder({
"title": "The Title",
"abstract": "The Abstract",
"layers": [
[{"title": "wrong", "abstract": "wrong", "layers": []}]
]
}, global_cfg=minimal_global_cfg)
assert len(lyr.unready_layers) == 0


def test_duplicate_folder_label(minimal_global_cfg):
with pytest.raises(ConfigException) as e:
lyr = OWSFolder({
Expand Down
9 changes: 8 additions & 1 deletion tests/test_ows_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ def test_function_wrapper_lyr():
result = f()
assert result[0] == "abar bouple c3"
assert f.band_mapper is None

func_cfg = {
"function": "so_fake.not_real.not_a_function",
"args": ["bar", "ouple"]
}
with pytest.raises(datacube_ows.config_utils.ConfigException) as e:
f = datacube_ows.ogc_utils.FunctionWrapper(lyr, func_cfg)
assert "Could not import python object" in str(e.value)
assert "so_fake.not_real.not_a_function" in str(e.value)

def test_func_naked():
lyr = MagicMock()
Expand Down

0 comments on commit 26d0f42

Please sign in to comment.