From 257efebe187edfa4c97f8ecd220ccced6461422c Mon Sep 17 00:00:00 2001 From: Sven Augustin Date: Mon, 10 Aug 2020 19:06:13 +0200 Subject: [PATCH] added _as_task method to Adjustable, removed Task creation and setting current_task from subclasses; added stop and moved wait to Adjustable (adjusted Motor accordingly) --- slic/core/adjustable/adjustable.py | 15 +++++++++++++++ slic/core/adjustable/convenience.py | 5 ----- slic/core/adjustable/dummyadjustable.py | 4 +--- slic/core/adjustable/genericadjustable.py | 4 +--- slic/core/adjustable/pvadjustable.py | 4 +--- slic/core/adjustable/pvenumadjustable.py | 4 +--- slic/devices/general/motor.py | 7 ++----- 7 files changed, 21 insertions(+), 22 deletions(-) diff --git a/slic/core/adjustable/adjustable.py b/slic/core/adjustable/adjustable.py index 2ba1453f..373d1fcf 100644 --- a/slic/core/adjustable/adjustable.py +++ b/slic/core/adjustable/adjustable.py @@ -1,4 +1,5 @@ from slic.utils import typename +from slic.core.task import Task from .baseadjustable import BaseAdjustable from .convenience import SpecConvenience @@ -10,6 +11,20 @@ class Adjustable(BaseAdjustable, SpecConvenience): self.units = units self.current_task = None + + def _as_task(self, *args, **kwargs): + self.current_task = task = Task(*args, **kwargs) + return task + + def wait(self): + if self.current_task: + return self.current_task.wait() + + def stop(self): + if self.current_task: + return self.current_task.stop() + + def set(self, *args, **kwargs): return self.set_target_value(*args, **kwargs) diff --git a/slic/core/adjustable/convenience.py b/slic/core/adjustable/convenience.py index 26c35486..eba57ad3 100644 --- a/slic/core/adjustable/convenience.py +++ b/slic/core/adjustable/convenience.py @@ -20,11 +20,6 @@ class SpecConvenience: return self.current_task - def wait(self): - if self.current_task: - self.current_task.wait() - - def __call__(self, value=None): if not value is None: return self.set_target_value(value) diff --git a/slic/core/adjustable/dummyadjustable.py b/slic/core/adjustable/dummyadjustable.py index 96f17c05..040199bb 100644 --- a/slic/core/adjustable/dummyadjustable.py +++ b/slic/core/adjustable/dummyadjustable.py @@ -1,4 +1,3 @@ -from slic.core.task import Task from .adjustable import Adjustable @@ -14,8 +13,7 @@ class DummyAdjustable(Adjustable): def set_target_value(self, value, hold=False): def change(): self._current_value = value - self.current_task = task = Task(change, hold=hold) - return task + return self._as_task(change, hold=hold) def is_moving(self): return False diff --git a/slic/core/adjustable/genericadjustable.py b/slic/core/adjustable/genericadjustable.py index 8babb21c..d51e3c7c 100644 --- a/slic/core/adjustable/genericadjustable.py +++ b/slic/core/adjustable/genericadjustable.py @@ -1,4 +1,3 @@ -from slic.core.task import Task from .adjustable import Adjustable @@ -17,8 +16,7 @@ class GenericAdjustable(Adjustable): def set_target_value(self, value, hold=False): self._last_target = value change = lambda: self._set(value) - self.current_task = task = Task(change, hold=hold) - return task + return self._as_task(change, hold=hold) def is_moving(self): return not self._wait() diff --git a/slic/core/adjustable/pvadjustable.py b/slic/core/adjustable/pvadjustable.py index 2cfe55c9..6995b17f 100644 --- a/slic/core/adjustable/pvadjustable.py +++ b/slic/core/adjustable/pvadjustable.py @@ -1,6 +1,5 @@ from types import SimpleNamespace from epics import PV -from slic.core.task import Task from .adjustable import Adjustable @@ -37,8 +36,7 @@ class PVAdjustable(Adjustable): def change(): # use_complete=True enables status in PV.put_complete self.pvs.setvalue.put(value, wait=True, use_complete=True) - self.current_task = task = Task(change, hold=hold) - return task + return self._as_task(change, hold=hold) def is_moving(self): if self.accuracy is not None: diff --git a/slic/core/adjustable/pvenumadjustable.py b/slic/core/adjustable/pvenumadjustable.py index 4b83ce96..8c270da3 100644 --- a/slic/core/adjustable/pvenumadjustable.py +++ b/slic/core/adjustable/pvenumadjustable.py @@ -1,5 +1,4 @@ from epics import PV -from slic.core.task import Task from slic.utils.printing import printable_dict from .adjustable import Adjustable @@ -24,8 +23,7 @@ class PVEnumAdjustable(Adjustable): def set_target_value(self, value, hold=False): value = self.states.get(value) change = lambda: self.pv.put(value, wait=True, use_complete=True) - self.current_task = task = Task(change, hold=hold) - return task + return self._as_task(change, hold=hold) def is_moving(self): return not self.pv.put_complete diff --git a/slic/devices/general/motor.py b/slic/devices/general/motor.py index c63fc74b..bee4c2b3 100644 --- a/slic/devices/general/motor.py +++ b/slic/devices/general/motor.py @@ -3,7 +3,6 @@ from types import SimpleNamespace from contextlib import contextmanager import colorama -from slic.core.task import Task from slic.core.adjustable import Adjustable, AdjustableError from slic.utils.eco_epics.motor import Motor as EpicsMotor from slic.utils.eco_epics.utilities_epics import EpicsString @@ -97,8 +96,7 @@ class Motor(Adjustable): with self.use_callback(on_change): self._move(stop, ignore_limits=ignore_limits, wait=True) - self.current_task = task = Task(change, hold=hold, stopper=self._motor.stop) - return task + return self._as_task(change, hold=hold, stopper=self._motor.stop) def _move(self, *args, **kwargs): @@ -116,8 +114,7 @@ class Motor(Adjustable): def stop(self): try: - if self.current_task: - self.current_task.stop() + return super().stop() except: self._motor.stop()