diff --git a/pyobs/robotic/scripts/cases.py b/pyobs/robotic/scripts/cases.py index 965065d3..0a8b8478 100644 --- a/pyobs/robotic/scripts/cases.py +++ b/pyobs/robotic/scripts/cases.py @@ -32,25 +32,29 @@ def __init__( self.expression = expression self.cases = cases - async def can_run(self) -> bool: - return True - - async def run( - self, - task_runner: TaskRunner, - task_schedule: Optional[TaskSchedule] = None, - task_archive: Optional[TaskArchive] = None, - ) -> None: + def __get_script(self) -> Script: # evaluate condition value = eval(self.expression, {"now": datetime.now(timezone.utc), "config": self.configuration}) # check in cases if value in self.cases: - script = self.get_object(self.cases[value], Script, configuration=self.configuration) + return self.get_object(self.cases[value], Script, configuration=self.configuration) elif "else" in self.cases: - script = self.get_object(self.cases["else"], Script, configuration=self.configuration) + return self.get_object(self.cases["else"], Script, configuration=self.configuration) else: raise ValueError("Invalid choice") + + async def can_run(self) -> bool: + script = self.__get_script() + return await script.can_run() + + async def run( + self, + task_runner: TaskRunner, + task_schedule: Optional[TaskSchedule] = None, + task_archive: Optional[TaskArchive] = None, + ) -> None: + script = self.__get_script() await script.run(task_runner, task_schedule, task_archive) diff --git a/pyobs/robotic/scripts/conditional.py b/pyobs/robotic/scripts/conditional.py index e3dbc091..7a9b4790 100644 --- a/pyobs/robotic/scripts/conditional.py +++ b/pyobs/robotic/scripts/conditional.py @@ -35,8 +35,20 @@ def __init__( self.true = true self.false = false + def __get_script(self) -> Script: + # evaluate condition + ret = eval(self.condition, {"now": datetime.now(timezone.utc)}) + + # run scripts + if ret: + return self.get_object(self.true, Script) + else: + if self.false is not None: + return self.get_object(self.false, Script) + async def can_run(self) -> bool: - return True + script = self.__get_script() + return await script.can_run() async def run( self, @@ -44,15 +56,8 @@ async def run( task_schedule: Optional[TaskSchedule] = None, task_archive: Optional[TaskArchive] = None, ) -> None: - # evaluate condition - ret = eval(self.condition, {"now": datetime.now(timezone.utc)}) - - # run scripts - if ret: - await self.get_object(self.true, Script).run(task_runner, task_schedule, task_archive) - else: - if self.false is not None: - await self.get_object(self.false, Script).run(task_runner, task_schedule, task_archive) + script = self.__get_script() + await script.run(task_runner, task_schedule, task_archive) __all__ = ["ConditionalRunner"] diff --git a/pyproject.toml b/pyproject.toml index c17acde7..3c43fc75 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [tool.poetry] name = "pyobs-core" packages = [{ include = "pyobs" }] -version = "1.17.3" +version = "1.17.5" description = "robotic telescope software" authors = ["Tim-Oliver Husser "] license = "MIT"