Skip to content

Commit 95dd098

Browse files
committed
feat: add env var overrides for conversion parameters
Add OVERRIDE_GROUPS, OVERRIDE_EXTRA_FLAGS, OVERRIDE_SPATIAL_CHUNK, OVERRIDE_TILE_WIDTH environment variables to override collection registry defaults at runtime. Enables production parameter tuning and testing without code deployment. Tests: 97 passing (+7), coverage: 91% for get_conversion_params.py
1 parent 9f518bd commit 95dd098

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ packages = ["scripts"]
5555
[tool.pytest.ini_options]
5656
minversion = "8.0"
5757
testpaths = ["tests"]
58-
pythonpath = ["scripts"] # Fix import resolution for tests
58+
pythonpath = ["scripts"] # Fix import resolution for tests
5959
python_files = ["test_*.py"]
6060
python_classes = ["Test*"]
6161
python_functions = ["test_*"]

scripts/get_conversion_params.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,24 @@
55
different satellite collections, enabling the workflow template to use
66
data-driven configuration instead of hard-coded bash conditionals.
77
8+
Environment Variable Overrides (for testing/debugging):
9+
OVERRIDE_GROUPS: Override groups parameter
10+
OVERRIDE_EXTRA_FLAGS: Override extra_flags parameter
11+
OVERRIDE_SPATIAL_CHUNK: Override spatial_chunk parameter
12+
OVERRIDE_TILE_WIDTH: Override tile_width parameter
13+
814
Usage:
915
python3 get_conversion_params.py --collection sentinel-1-l1-grd
1016
python3 get_conversion_params.py --collection sentinel-2-l2a --format json
1117
python3 get_conversion_params.py --collection sentinel-2-l2a --param groups
18+
OVERRIDE_GROUPS="/custom/path" python3 get_conversion_params.py --collection sentinel-2-l2a
1219
"""
1320

1421
from __future__ import annotations
1522

1623
import argparse
1724
import json
25+
import os
1826
import sys
1927
from typing import Any, cast
2028

@@ -58,6 +66,12 @@ def _match_collection_config(collection_id: str) -> dict[str, Any] | None:
5866
def get_conversion_params(collection_id: str) -> dict[str, Any]:
5967
"""Get conversion parameters for collection.
6068
69+
Environment variables can override configuration values:
70+
- OVERRIDE_GROUPS: Override groups parameter
71+
- OVERRIDE_EXTRA_FLAGS: Override extra_flags parameter
72+
- OVERRIDE_SPATIAL_CHUNK: Override spatial_chunk parameter (integer)
73+
- OVERRIDE_TILE_WIDTH: Override tile_width parameter (integer)
74+
6175
Args:
6276
collection_id: Collection identifier (e.g., sentinel-1-l1-grd-dp-test)
6377
@@ -75,7 +89,19 @@ def get_conversion_params(collection_id: str) -> dict[str, Any]:
7589
raise ValueError(f"No config for collection {collection_id}")
7690
config = default_config
7791

78-
return cast(dict[str, Any], config.get("conversion", {}))
92+
conversion_params = cast(dict[str, Any], config.get("conversion", {}))
93+
94+
# Apply environment variable overrides (useful for testing/debugging)
95+
return {
96+
"groups": os.getenv("OVERRIDE_GROUPS", conversion_params.get("groups", "")),
97+
"extra_flags": os.getenv("OVERRIDE_EXTRA_FLAGS", conversion_params.get("extra_flags", "")),
98+
"spatial_chunk": int(
99+
os.getenv("OVERRIDE_SPATIAL_CHUNK", str(conversion_params.get("spatial_chunk", 4096)))
100+
),
101+
"tile_width": int(
102+
os.getenv("OVERRIDE_TILE_WIDTH", str(conversion_params.get("tile_width", 512)))
103+
),
104+
}
79105

80106

81107
def main(argv: list[str] | None = None) -> int:

tests/unit/test_get_conversion_params.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Tests for get_conversion_params.py - Collection registry logic."""
22

33
import json
4+
import os
45

56
import pytest
67

@@ -160,3 +161,66 @@ def test_unknown_collection_uses_default(self, capsys):
160161
captured = capsys.readouterr()
161162
# Should fall back to S2 default
162163
assert "ZARR_GROUPS='/quality/l2a_quicklook/r10m'" in captured.out
164+
165+
166+
class TestEnvironmentVariableOverrides:
167+
"""Test environment variable override functionality."""
168+
169+
def test_override_groups(self, monkeypatch):
170+
"""OVERRIDE_GROUPS overrides default groups."""
171+
monkeypatch.setenv("OVERRIDE_GROUPS", "/custom/groups")
172+
params = get_conversion_params("sentinel-2-l2a")
173+
assert params["groups"] == "/custom/groups"
174+
assert params["spatial_chunk"] == 4096 # Other params unchanged
175+
176+
def test_override_extra_flags(self, monkeypatch):
177+
"""OVERRIDE_EXTRA_FLAGS overrides default flags."""
178+
monkeypatch.setenv("OVERRIDE_EXTRA_FLAGS", "--custom-flag")
179+
params = get_conversion_params("sentinel-1-l1-grd")
180+
assert params["extra_flags"] == "--custom-flag"
181+
182+
def test_override_spatial_chunk(self, monkeypatch):
183+
"""OVERRIDE_SPATIAL_CHUNK overrides default chunk size."""
184+
monkeypatch.setenv("OVERRIDE_SPATIAL_CHUNK", "8192")
185+
params = get_conversion_params("sentinel-2-l2a")
186+
assert params["spatial_chunk"] == 8192
187+
assert isinstance(params["spatial_chunk"], int)
188+
189+
def test_override_tile_width(self, monkeypatch):
190+
"""OVERRIDE_TILE_WIDTH overrides default tile width."""
191+
monkeypatch.setenv("OVERRIDE_TILE_WIDTH", "1024")
192+
params = get_conversion_params("sentinel-1-l1-grd")
193+
assert params["tile_width"] == 1024
194+
assert isinstance(params["tile_width"], int)
195+
196+
def test_multiple_overrides(self, monkeypatch):
197+
"""Multiple overrides work together."""
198+
monkeypatch.setenv("OVERRIDE_GROUPS", "/test/path")
199+
monkeypatch.setenv("OVERRIDE_SPATIAL_CHUNK", "2048")
200+
params = get_conversion_params("sentinel-2-l2a")
201+
assert params["groups"] == "/test/path"
202+
assert params["spatial_chunk"] == 2048
203+
# Non-overridden values remain default
204+
assert params["extra_flags"] == "--crs-groups /quality/l2a_quicklook/r10m"
205+
206+
def test_override_empty_string(self, monkeypatch):
207+
"""Empty string override is allowed."""
208+
monkeypatch.setenv("OVERRIDE_EXTRA_FLAGS", "")
209+
params = get_conversion_params("sentinel-1-l1-grd")
210+
assert params["extra_flags"] == ""
211+
212+
def test_no_override_uses_default(self):
213+
"""Without env vars, uses configuration defaults."""
214+
# Ensure no env vars are set
215+
for var in [
216+
"OVERRIDE_GROUPS",
217+
"OVERRIDE_EXTRA_FLAGS",
218+
"OVERRIDE_SPATIAL_CHUNK",
219+
"OVERRIDE_TILE_WIDTH",
220+
]:
221+
if var in os.environ:
222+
del os.environ[var]
223+
224+
params = get_conversion_params("sentinel-2-l2a")
225+
assert params["groups"] == "/quality/l2a_quicklook/r10m"
226+
assert params["spatial_chunk"] == 4096

0 commit comments

Comments
 (0)