2023-10-13 22:33:07 +03:00
|
|
|
from turtle import Turtle
|
2023-10-26 22:41:54 +03:00
|
|
|
from typing import Any, Collection, Optional
|
2023-12-15 00:25:07 +03:00
|
|
|
from controls.device import SynchronyDevice, TraitDescriptor, DeviceLifecycleState
|
2023-10-13 22:33:07 +03:00
|
|
|
|
|
|
|
class TurtleDevice(SynchronyDevice):
|
2023-12-15 00:25:07 +03:00
|
|
|
def open(self):
|
|
|
|
self.state == DeviceLifecycleState.OPEN
|
|
|
|
self.turtle = Turtle()
|
2023-10-26 22:41:54 +03:00
|
|
|
super().open()
|
|
|
|
|
|
|
|
def close(self):
|
2023-12-15 00:25:07 +03:00
|
|
|
del self.turtle
|
|
|
|
super().close()
|
2023-10-26 22:41:54 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def trait_descriptors(self):
|
|
|
|
traits = [attribute for attribute in vars(self.turtle).items()
|
|
|
|
if (attribute[0].startswith('_') == False)]
|
|
|
|
|
|
|
|
self.traits = [TraitDescriptor(*tr) for tr in traits]
|
|
|
|
return self.traits
|
|
|
|
|
|
|
|
@property
|
|
|
|
def action_descriptors(self):
|
2023-12-13 22:48:01 +03:00
|
|
|
actions = [(attribute, getattr(self.turtle, attribute)) for attribute in dir(self.turtle)
|
2023-10-26 22:41:54 +03:00
|
|
|
if (attribute.startswith('_') == False)]
|
|
|
|
|
|
|
|
self.actions = [TraitDescriptor(*ac) for ac in actions]
|
2023-12-13 22:48:01 +03:00
|
|
|
return self.actions
|
2023-10-26 22:41:54 +03:00
|
|
|
|
|
|
|
def read(self, trait_name):
|
|
|
|
collection = self.traits
|
|
|
|
return ([collection[i].info for i in range(len(collection))
|
|
|
|
if collection[i].name == trait_name])
|
|
|
|
|
|
|
|
def write(self, trait_name: str, value: Any) -> bool:
|
|
|
|
collection = self.traits
|
|
|
|
for i in range(len(collection)):
|
|
|
|
if collection[i].name == trait_name:
|
|
|
|
collection[i].info=value
|
|
|
|
|
|
|
|
def execute(self, action_name: str, *args, **kwargs):
|
2023-12-15 00:25:07 +03:00
|
|
|
if self.state == DeviceLifecycleState.INIT:
|
|
|
|
self.open()
|
|
|
|
execution = getattr(self.turtle, action_name)
|
|
|
|
execution(int(*args), **kwargs)
|
|
|
|
|
2023-10-26 22:41:54 +03:00
|
|
|
|
|
|
|
def invalidate(self, trait_name: str):
|
|
|
|
return super().invalidate(trait_name)
|
|
|
|
|
|
|
|
def __getitem__(self, trait_name: str) -> Any | None:
|
|
|
|
collection = self.traits
|
|
|
|
return ([collection[i] for i in range(len(collection))
|
|
|
|
if collection[i].name == trait_name])
|
2023-10-13 22:33:07 +03:00
|
|
|
|
|
|
|
|