Skip to content

Commit f5b23a8

Browse files
committed
propagate preload params
1 parent e4db208 commit f5b23a8

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

xcube/core/store/preload.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class PreloadStatus(Enum):
2020

2121
waiting = "waiting"
2222
started = "started"
23-
stopped = "stopped"
23+
completed = "completed"
2424
cancelled = "cancelled"
2525
failed = "failed"
2626

@@ -195,10 +195,13 @@ class ExecutorPreloadHandle(PreloadHandle):
195195
def __init__(
196196
self,
197197
data_ids: tuple[str, ...],
198-
preload_data: Callable[[PreloadHandle, str], None] | None = None,
198+
preload_data: (
199+
Callable[[PreloadHandle, str, dict[str, Any]], None] | None
200+
) = None,
199201
executor: Executor | None = None,
200202
blocking: bool = True,
201203
silent: bool = False,
204+
**preload_params,
202205
):
203206
self._preload_data = preload_data
204207
self._executor = executor or ThreadPoolExecutor()
@@ -211,7 +214,9 @@ def __init__(
211214
self._lock = threading.Lock()
212215
self._futures: dict[str, Future[str]] = {}
213216
for data_id in data_ids:
214-
future: Future[str] = self._executor.submit(self._run_preload_data, data_id)
217+
future: Future[str] = self._executor.submit(
218+
self._run_preload_data, data_id, **preload_params
219+
)
215220
future.add_done_callback(self._handle_preload_data_done)
216221
self._futures[data_id] = future
217222

@@ -240,7 +245,7 @@ def notify(self, event: PreloadState):
240245
event.status is not None
241246
and event.status != state.status
242247
and state.status
243-
in (PreloadStatus.stopped, PreloadStatus.cancelled, PreloadStatus.failed)
248+
in (PreloadStatus.completed, PreloadStatus.cancelled, PreloadStatus.failed)
244249
):
245250
# Status cannot be changed
246251
return
@@ -249,7 +254,7 @@ def notify(self, event: PreloadState):
249254
if not self._silent:
250255
self._display.update()
251256

252-
def preload_data(self, data_id: str):
257+
def preload_data(self, data_id: str, **preload_params):
253258
"""Preload the data resource given by *data_id*.
254259
255260
Concurrently executes the *preload_data* passed to the constructor,
@@ -259,13 +264,14 @@ def preload_data(self, data_id: str):
259264
260265
Args:
261266
data_id: The data identifier of the data resource to be preloaded.
267+
preload_params: Specific parameters for the preloading workflow.
262268
"""
263269
if self._preload_data is not None:
264-
self._preload_data(self, data_id)
270+
self._preload_data(self, data_id, **preload_params)
265271

266-
def _run_preload_data(self, data_id: str) -> str:
272+
def _run_preload_data(self, data_id: str, **preload_params) -> str:
267273
self.notify(PreloadState(data_id, status=PreloadStatus.started))
268-
self.preload_data(data_id)
274+
self.preload_data(data_id, **preload_params)
269275
return data_id
270276

271277
def _handle_preload_data_done(self, f: Future[str]):
@@ -278,7 +284,7 @@ def _handle_preload_data_done(self, f: Future[str]):
278284
try:
279285
_value = f.result()
280286
# No exceptions, notify everything seems ok
281-
self.notify(PreloadState(data_id, status=PreloadStatus.stopped))
287+
self.notify(PreloadState(data_id, status=PreloadStatus.completed))
282288
except CancelledError as e:
283289
# Raised if future has been cancelled
284290
# while executing `_run_preload_data`
@@ -324,21 +330,23 @@ def __exit__(self, exc_type, exc_val, exc_tb):
324330

325331

326332
class PreloadDisplay(ABC):
333+
327334
@classmethod
328335
def create(
329336
cls, states: list[PreloadState], silent: bool | None = None
330337
) -> "PreloadDisplay":
331338
try:
332-
# noinspection PyUnresolvedReferences
333339
from IPython.display import display
340+
from IPython import get_ipython
334341

335-
if display is not None:
336-
try:
337-
return IPyWidgetsPreloadDisplay(states)
338-
except ImportError:
339-
return IPyPreloadDisplay(states)
340-
except ImportError:
342+
# Only use IPyGeneratorDisplay if we are actually inside a notebook
343+
shell = get_ipython().__class__.__name__
344+
if shell == "ZMQInteractiveShell":
345+
return IPyPreloadDisplay(states)
346+
except (ImportError, NameError, AttributeError):
341347
pass
348+
349+
# Default fallback: text-based display
342350
return PreloadDisplay(states)
343351

344352
def __init__(self, states: list[PreloadState]):

0 commit comments

Comments
 (0)