From 53abd8de26b44bd9f741d1f6c732eae582464d7b Mon Sep 17 00:00:00 2001 From: Terry Fu Date: Tue, 3 Sep 2024 20:54:36 -0400 Subject: [PATCH] Bug fixed. (#378) (#379) --- leads/leads.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/leads/leads.py b/leads/leads.py index 51ce0c1..d7e35e4 100644 --- a/leads/leads.py +++ b/leads/leads.py @@ -11,6 +11,12 @@ T = _TypeVar("T", bound=DataContainer) +class _SuspensionException(Exception): + def __init__(self, event: SuspensionEvent) -> None: + super().__init__() + self.event = event + + class LEADS(Context[T]): def __init__(self, initial_data: T | None = None, data_seq_size: int = 100, num_laps_timed: int = 3) -> None: super().__init__(initial_data, data_seq_size, num_laps_timed) @@ -39,15 +45,18 @@ def _acquire_data(self, name: str, key: str, mandatory: bool = True) -> _Any | N return getattr(self.data(), name) except AttributeError: if mandatory: - self.suspend(SuspensionEvent(self, key, f"No data for `{name}`")) + raise _SuspensionException(SuspensionEvent(self, key, f"No data for `{name}`")) def _do_plugin_callback(self, method: _Literal["pre_push", "post_push", "pre_update", "post_update"]) -> None: for key, plugin in self._plugins.items(): if plugin.enabled(): - for tag in plugin.required_devices(): - if not has_device(tag) or not SFT.device_ok(tag): - self.suspend(SuspensionEvent(self, key, f"Device {tag} not ok")) - getattr(plugin, method)(self, {d: self._acquire_data(d, key) for d in plugin.required_data()}) + try: + for tag in plugin.required_devices(): + if not has_device(tag) or not SFT.device_ok(tag): + raise _SuspensionException(SuspensionEvent(self, key, f"Device {tag} not ok")) + getattr(plugin, method)(self, {d: self._acquire_data(d, key) for d in plugin.required_data()}) + except _SuspensionException as e: + self.suspend(e.event) @_override def push(self, data: T) -> None: