From 83876eb7c0d690104754c151b256650b51a0107d Mon Sep 17 00:00:00 2001 From: Stefaan Lippens Date: Thu, 31 Oct 2024 15:06:39 +0100 Subject: [PATCH] Issue #653 fix paths no longer supporting context manager protocol in python 3.13 --- .../spectral_indices/spectral_indices.py | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/openeo/extra/spectral_indices/spectral_indices.py b/openeo/extra/spectral_indices/spectral_indices.py index 8ac3c0b93..710bc6f48 100644 --- a/openeo/extra/spectral_indices/spectral_indices.py +++ b/openeo/extra/spectral_indices/spectral_indices.py @@ -25,12 +25,12 @@ def load_indices() -> Dict[str, dict]: # and provide an alternative mechanism to work with custom indices "resources/extra-indices-dict.json", ]: - with importlib_resources.files("openeo.extra.spectral_indices") / path as resource_path: - data = json.loads(resource_path.read_text(encoding="utf8")) - overwrites = set(specs.keys()).intersection(data["SpectralIndices"].keys()) - if overwrites: - raise RuntimeError(f"Duplicate spectral indices: {overwrites} from {path}") - specs.update(data["SpectralIndices"]) + resource_path = importlib_resources.files("openeo.extra.spectral_indices") / path + data = json.loads(resource_path.read_text(encoding="utf8")) + overwrites = set(specs.keys()).intersection(data["SpectralIndices"].keys()) + if overwrites: + raise RuntimeError(f"Duplicate spectral indices: {overwrites} from {path}") + specs.update(data["SpectralIndices"]) return specs @@ -39,10 +39,10 @@ def load_indices() -> Dict[str, dict]: def load_constants() -> Dict[str, float]: """Load constants defined by Awesome Spectral Indices.""" # TODO: encapsulate all this json loading in a single Awesome Spectral Indices registry class? - with importlib_resources.files( - "openeo.extra.spectral_indices" - ) / "resources/awesome-spectral-indices/constants.json" as resource_path: - data = json.loads(resource_path.read_text(encoding="utf8")) + resource_path = ( + importlib_resources.files("openeo.extra.spectral_indices") / "resources/awesome-spectral-indices/constants.json" + ) + data = json.loads(resource_path.read_text(encoding="utf8")) return {k: v["default"] for k, v in data.items() if isinstance(v["default"], (int, float))} @@ -51,10 +51,10 @@ def load_constants() -> Dict[str, float]: def _load_bands() -> Dict[str, dict]: """Load band name mapping defined by Awesome Spectral Indices.""" # TODO: encapsulate all this json loading in a single Awesome Spectral Indices registry class? - with importlib_resources.files( - "openeo.extra.spectral_indices" - ) / "resources/awesome-spectral-indices/bands.json" as resource_path: - data = json.loads(resource_path.read_text(encoding="utf8")) + resource_path = ( + importlib_resources.files("openeo.extra.spectral_indices") / "resources/awesome-spectral-indices/bands.json" + ) + data = json.loads(resource_path.read_text(encoding="utf8")) return data