Skip to content

Commit f1d989d

Browse files
committed
now supporting slice_source_kwargs setting
1 parent 9e69135 commit f1d989d

File tree

6 files changed

+69
-3
lines changed

6 files changed

+69
-3
lines changed

tests/config/test_config.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,24 @@ def test_slice_source_as_type(self):
162162
}
163163
)
164164

165+
def test_slice_source_kwargs(self):
166+
config = Config(
167+
{
168+
"target_dir": "memory://target.zarr",
169+
}
170+
)
171+
self.assertEqual(None, config.slice_source_kwargs)
172+
173+
config = Config(
174+
{
175+
"target_dir": "memory://target.zarr",
176+
"slice_source_kwargs": {"a": 1, "b": True, "c": "nearest"},
177+
}
178+
)
179+
self.assertEqual(
180+
{"a": 1, "b": True, "c": "nearest"}, config.slice_source_kwargs
181+
)
182+
165183

166184
def new_custom_slice_source(ctx: Context, index: int):
167185
return CustomSliceSource(ctx, index)

tests/config/test_schema.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def test_get_config_schema(self):
3131
"slice_engine",
3232
"slice_polling",
3333
"slice_source",
34+
"slice_source_kwargs",
3435
"slice_storage_options",
3536
"target_storage_options",
3637
"target_dir",

tests/slice/test_cm.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# noinspection PyUnusedLocal
2626

2727

28-
# noinspection PyShadowingBuiltins,PyRedeclaration
28+
# noinspection PyShadowingBuiltins,PyRedeclaration,PyMethodMayBeStatic
2929
class OpenSliceDatasetTest(unittest.TestCase):
3030
def setUp(self):
3131
clear_memory_fs()
@@ -35,6 +35,7 @@ def test_slice_item_is_slice_source(self):
3535
ctx = Context(dict(target_dir="memory://target.zarr"))
3636
slice_item = MemorySliceSource(dataset, 0)
3737
slice_cm = open_slice_dataset(ctx, slice_item)
38+
self.assertIsInstance(slice_cm, SliceSourceContextManager)
3839
self.assertIs(slice_item, slice_cm.slice_source)
3940

4041
def test_slice_item_is_dataset(self):
@@ -163,7 +164,7 @@ def get_dataset(name):
163164
with slice_cm as slice_ds:
164165
self.assertIsInstance(slice_ds, xr.Dataset)
165166

166-
def test_slice_item_is_slice_source(self):
167+
def test_slice_item_is_slice_source_arg(self):
167168
class MySliceSource(SliceSource):
168169
def __init__(self, name):
169170
self.uri = f"memory://{name}.zarr"
@@ -190,7 +191,7 @@ def close(self):
190191
with slice_cm as slice_ds:
191192
self.assertIsInstance(slice_ds, xr.Dataset)
192193

193-
def test_slice_item_is_deprecated_slice_source(self):
194+
def test_slice_item_is_deprecated_slice_source_arg(self):
194195
class MySliceSource(SliceSource):
195196
def __init__(self, name):
196197
self.uri = f"memory://{name}.zarr"
@@ -218,6 +219,33 @@ def dispose(self):
218219
with slice_cm as slice_ds:
219220
self.assertIsInstance(slice_ds, xr.Dataset)
220221

222+
def test_slice_item_is_slice_source_arg_with_extra_kwargs(self):
223+
class MySliceSource(SliceSource):
224+
def __init__(self, *args, **kwargs):
225+
self.args = args
226+
self.kwargs = kwargs
227+
228+
def get_dataset(self):
229+
return xr.Dataset()
230+
231+
ctx = Context(
232+
dict(
233+
target_dir="memory://target.zarr",
234+
slice_source=MySliceSource,
235+
slice_source_kwargs={"a": 1, "b": True, "c": "nearest"},
236+
)
237+
)
238+
slice_cm = open_slice_dataset(ctx, (["bibo"], {"a": 2, "d": 3.14}))
239+
self.assertIsInstance(slice_cm, SliceSourceContextManager)
240+
slice_source = slice_cm.slice_source
241+
self.assertIsInstance(slice_source, MySliceSource)
242+
with slice_cm as slice_ds:
243+
self.assertIsInstance(slice_ds, xr.Dataset)
244+
self.assertEquals(slice_source.args, ("bibo",))
245+
self.assertEquals(
246+
slice_source.kwargs, {"a": 2, "b": True, "c": "nearest", "d": 3.14}
247+
)
248+
221249

222250
class IsContextManagerTest(unittest.TestCase):
223251
"""Assert that context managers are identified by isinstance()"""

zappend/config/config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ def slice_source(self) -> Callable[[...], Any] | None:
128128
"""
129129
return self._slice_source
130130

131+
@property
132+
def slice_source_kwargs(self) -> dict[str, Any] | None:
133+
"""Extra keyword-arguments passed to a configured `slice_source`
134+
together with each slice item.
135+
"""
136+
return self._config.get("slice_source_kwargs")
137+
131138
@property
132139
def slice_storage_options(self) -> dict[str, Any] | None:
133140
"""The configured slice storage options to be used

zappend/config/schema.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,14 @@
643643
"type": "string",
644644
"minLength": 1,
645645
},
646+
slice_source_kwargs={
647+
"description": (
648+
"Extra keyword-arguments passed to a configured `slice_source`"
649+
" together with each slice item."
650+
),
651+
"type": "object",
652+
"additionalProperties": True,
653+
},
646654
slice_engine={
647655
"description": (
648656
"The name of the engine to be used for opening"

zappend/slice/callable.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ def invoke_slice_callable(
3030
A slice item of type `SliceItem`.
3131
"""
3232
slice_args, slice_kwargs = to_slice_args(slice_item)
33+
if ctx.config.slice_source_kwargs:
34+
extra_kwargs = dict(ctx.config.slice_source_kwargs)
35+
extra_kwargs.update(slice_kwargs)
36+
slice_kwargs = extra_kwargs
3337

3438
signature = inspect.signature(slice_callable)
3539
ctx_parameter = signature.parameters.get("ctx")

0 commit comments

Comments
 (0)