Repair broken methods

This commit is contained in:
Igor Dunaev 2023-11-06 21:18:57 +03:00
parent c2c68123de
commit 820f9d3fb5
2 changed files with 20 additions and 9 deletions

View File

@ -1,6 +1,6 @@
from turtle import Turtle from turtle import Turtle
from typing import Any, Collection, Optional from typing import Any, Collection, Optional
from types import FunctionType from types import FunctionType, MethodType
from inspect import getfullargspec from inspect import getfullargspec
from controls.device import SynchronyDevice from controls.device import SynchronyDevice
@ -33,16 +33,21 @@ class TurtleDevice(SynchronyDevice):
super().close() super().close()
def action_descriptors(self) -> Collection[ActionDescriptor]: def action_descriptors(self) -> Collection[ActionDescriptor]:
def get_args(f: FunctionType) -> dict[str, type]: if self.state != DeviceLifecycleState.OPEN:
spec = getfullargspec(f) raise DeviceError("Device is not opened")
def get_args(name: str) -> dict[str, type]:
spec = getfullargspec(getattr(self._device, name))
return {arg: spec.annotations.get(arg, object) for arg in spec.args} return {arg: spec.annotations.get(arg, object) for arg in spec.args}
actions = (dict(filter(lambda i: not i[0].startswith('_'), actions = (filter(lambda name: not name.startswith("_"),
filter(lambda i: isinstance(i[1], FunctionType), filter(lambda name: isinstance(getattr(self._device, name), MethodType),
self._device.__dict__.items())))) # lisp :3 dir(self._device)))) # lisp :3
return [ActionDescriptor(name=action[0],arguments=get_args(action[1])) for action in actions.items()] return [ActionDescriptor(name=action,arguments=get_args(action)) for action in actions]
def trait_descriptors(self) -> Collection[TraitDescriptor]: def trait_descriptors(self) -> Collection[TraitDescriptor]:
if self.state != DeviceLifecycleState.OPEN:
raise DeviceError("Device is not opened")
traits = dict(filter(lambda i: not i[0].startswith('_'), traits = dict(filter(lambda i: not i[0].startswith('_'),
filter(lambda i: not isinstance(i[1], FunctionType), filter(lambda i: not isinstance(i[1], FunctionType),
self._device.__dict__.items()))) self._device.__dict__.items())))
@ -66,7 +71,7 @@ class TurtleDevice(SynchronyDevice):
raise DeviceError("Device is not opened") raise DeviceError("Device is not opened")
if action_name not in self._actions: if action_name not in self._actions:
raise KeyError(f"\"{action_name}\" is not an action of {self.__class__}") raise KeyError(f"\"{action_name}\" is not an action of {self.__class__}")
return self._device.__dict__[action_name](args, kwargs) return getattr(self._device, action_name)(*args, **kwargs)
def invalidate(self, trait_name: str): def invalidate(self, trait_name: str):
if trait_name in self._logical_traits: if trait_name in self._logical_traits:

View File

@ -11,3 +11,9 @@ class TurtleDeviceTest(TestCase):
def test_open(self): def test_open(self):
self.device.open() self.device.open()
self.device.close() self.device.close()
def test_execute(self):
self.device.open()
dist = self.device.execute("distance", 1.0, 1.0)
self.assertAlmostEqual(dist, 1.41421, delta=1e-5)
self.device.close()