Skip to content

Commit

Permalink
Simplify funcs & add test cases to delete files test
Browse files Browse the repository at this point in the history
  • Loading branch information
ritvje authored and RadAdm committed Nov 20, 2023
1 parent 9394ee3 commit b98daf8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
5 changes: 2 additions & 3 deletions georest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,10 @@ def _get_store_name_from_filename(config, fname):
if "layer_id" in config:
layer_id = file_parts[config["layer_id"]]
return config["layers"][layer_id]
elif "layer_name_template" in config:
if "layer_name_template" in config:
layer_name = trollsift.compose(config["layer_name_template"], file_parts)
return layer_name
else:
raise ValueError("Either 'layer_id' or 'layer_name_template' must be defined in config")
raise ValueError("Either 'layer_id' or 'layer_name_template' must be defined in config")


def delete_file_from_mosaic(config, fname):
Expand Down
17 changes: 11 additions & 6 deletions georest/tests/test_geoserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,22 +500,27 @@ def test_delete_file_from_mosaic_layertemplate(connect_to_gs_catalog):
"geoserver_target_dir": "/mnt/data",
"file_pattern": "{area}_{productname}.tif",
"layer_name_template": "{productname}_{area}",
"delete_granule_layer_options": {"area": ["europe"], "productname": ["airmass"]},
"delete_granule_layer_options": {"area": ["europe", "global"], "productname": ["airmass1", "airmass2"]},
}
fname = "europe_airmass.tif"
fnames = ["europe_airmass1.tif", "global_airmass1.tif", "europe_airmass2.tif", "global_airmass2.tif"]

delete_file_from_mosaic(config, fname)
for fname in fnames:
delete_file_from_mosaic(config, fname)

connect_to_gs_catalog.assert_called_with(config)
cat.get_store.assert_called_with("airmass_europe", "satellite")
assert cat.get_store.call_count == len(fnames)
assert "call('airmass1_europe', 'satellite')" in str(cat.get_store.call_args_list)
assert "call('airmass2_europe', 'satellite')" in str(cat.get_store.call_args_list)
assert "call('airmass1_global', 'satellite')" in str(cat.get_store.call_args_list)
assert "call('airmass1_global', 'satellite')" in str(cat.get_store.call_args_list)
cat.list_granules.assert_called()
cat.delete_granule.assert_not_called()

# This is the structure returned by cat.list_granules()
granules = {"features": [{"properties": {"location": "/mnt/data/europe_airmass.tif"}, "id": "file-id"}]}
granules = {"features": [{"properties": {"location": "/mnt/data/europe_airmass1.tif"}, "id": "file-id"}]}
cat.list_granules.return_value = granules

delete_file_from_mosaic(config, fname)
delete_file_from_mosaic(config, fnames[0])
cat.delete_granule.assert_called()


Expand Down
14 changes: 6 additions & 8 deletions georest/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,15 @@ def get_exposed_layer_directories(config):
def get_layers_for_delete_granules(config):
"""Get list of layers for deleting granules."""
if config.get("layer_id", False):
layers = list(config["layers"].values())
elif config.get("layer_name_template", False) and config.get("delete_granule_layer_options", False):
return list(config["layers"].values())
if config.get("layer_name_template", False) and config.get("delete_granule_layer_options", False):
keys = sorted(config["delete_granule_layer_options"].keys())
combinations = list(itertools.product(*[config["delete_granule_layer_options"][k] for k in keys]))
options = [dict(zip(keys, l)) for l in combinations]
layers = [config["layer_name_template"].format(**opt) for opt in options]
else:
raise ValueError(
"Either 'layer_id' or 'layer_name_template' (with 'delete_granule_layer_options') must be defined in config"
)
return layers
return [config["layer_name_template"].format(**opt) for opt in options]
raise ValueError(
"Either 'layer_id' or 'layer_name_template' (with 'delete_granule_layer_options') must be defined in config"
)


def write_wkt(config, image_fname):
Expand Down

0 comments on commit b98daf8

Please sign in to comment.