Skip to content

Commit 43ef6de

Browse files
committed
Remove xcengine.util dependency on pystac
write_stac now fails silently if pystac is not available.
1 parent dd62a0b commit 43ef6de

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

test/test_util.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,29 @@ def test_write_stac(tmp_path, dataset, write_datasets, pre_existing_catalog):
8585
}
8686

8787

88+
orig_import = __import__
89+
def import_mock(name, *args):
90+
if name == 'pystac':
91+
raise ModuleNotFoundError("No module named 'pystac'")
92+
return orig_import(name, *args)
93+
94+
95+
def test_write_stac_no_pystac(tmp_path, dataset):
96+
# Import hooks are the recommended "clean" way to do this, but don't work
97+
# in this case.
98+
with mock.patch('builtins.__import__', side_effect=import_mock):
99+
# pytest imports xcengine.util long before we can patch __import__,
100+
# so we delete pystac from util's namespace (if present) instead.
101+
# This gives a NameError on access rather than a ModuleNotFoundError
102+
# on import, but the important thing is to break any implementation
103+
# that tries to import pystac without checking if it's available.
104+
import xcengine.util
105+
xcengine.util.__dict__.pop("pystac", None)
106+
from xcengine.util import write_stac
107+
write_stac({"ds1": dataset}, tmp_path)
108+
# We want nothing to happen here, so no explicit assertions.
109+
110+
88111
@pytest.mark.parametrize("eoap_mode", [False, True])
89112
@pytest.mark.parametrize("ds2_format", [None, "zarr", "netcdf"])
90113
def test_save_datasets(tmp_path, dataset, eoap_mode, ds2_format):

xcengine/util.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
import shutil
99
from typing import NamedTuple, Mapping
1010

11-
import pystac
1211
import xarray as xr
1312
from xarray import Dataset
1413

15-
1614
def clear_directory(directory: pathlib.Path) -> None:
1715
for path in directory.iterdir():
1816
if path.is_dir():
@@ -24,6 +22,12 @@ def clear_directory(directory: pathlib.Path) -> None:
2422
def write_stac(
2523
datasets: Mapping[str, xr.Dataset], stac_root: pathlib.Path
2624
) -> None:
25+
try:
26+
import pystac
27+
except ModuleNotFoundError:
28+
# If pystac isn't present, we assume that stage-out is not required
29+
# and exit quietly.
30+
return
2731
catalog_path = stac_root / "catalog.json"
2832
if catalog_path.exists():
2933
# Assume that the user code generated its own stage-out data

0 commit comments

Comments
 (0)