advanced-python-homework-2023/turtle/equipment/turtle_device.py

86 lines
3.4 KiB
Python
Raw Normal View History

2023-10-23 13:44:07 +03:00
from turtle import Turtle
from typing import Optional, Collection, Any
from controls.device import SynchronyDevice
from controls.device import TraitDescriptor
from controls.device import ActionDescriptor
2023-10-26 17:28:48 +03:00
from controls.device import DeviceError
import inspect
2023-10-23 13:44:07 +03:00
2023-10-26 17:28:48 +03:00
2023-10-23 13:44:07 +03:00
class TurtleDevice(SynchronyDevice):
def open(self):
self.turtle = Turtle()
2023-10-26 17:28:48 +03:00
self.descriptors = dict(traits=dict(), actions=dict())
2023-10-23 13:44:07 +03:00
super().open()
def close(self):
self.turtle.clear()
super().close()
def trait_descriptors(self) -> Collection[TraitDescriptor]:
2023-10-24 10:35:32 +03:00
return self.get_descriptors()["traits"]
2023-10-23 13:44:07 +03:00
def action_descriptors(self) -> Collection[ActionDescriptor]:
2023-10-24 10:35:32 +03:00
return self.get_descriptors()["actions"]
2023-10-23 13:44:07 +03:00
def __getitem__(self, trait_name: str) -> Optional[Any]:
"""Return logical state of trait `trait_name`."""
2023-10-26 17:28:48 +03:00
return self.descriptors[trait_name]
def read_descriptors(self, arg):
self.descriptors = self.get_descriptors()
return self.descriptors
2023-10-23 13:44:07 +03:00
def read(self, trait_name: str) -> Any:
2023-10-26 17:28:48 +03:00
"""Read physical state of trait `trait_name` from device."""
self.read_descriptors()
return self.descriptors[trait_name]
2023-10-23 13:44:07 +03:00
def write(self, trait_name: str, value: Any) -> bool:
2023-10-26 17:28:48 +03:00
"""Turtle traits are not writable"""
# trait_desc = self.trait_descriptors.get(trait_name)
# if trait_desc.writable:
# return getattr(self.turtle, trait_name)(value)
super().write()
2023-10-23 13:44:07 +03:00
2023-10-24 10:11:30 +03:00
def execute(self, action_name: str, *args, **kwargs):
"""Execute action `action_name`, using `args` and `kwargs` as action argument."""
2023-10-26 17:28:48 +03:00
action = self.get_descriptors()["actions"].get(action_name)
if action:
return getattr(self.turtle, action_name)(*args, **kwargs)
super().execute()
2023-10-24 10:11:30 +03:00
2023-10-23 13:44:07 +03:00
def invalidate(self, trait_name: str):
2023-10-26 17:28:48 +03:00
"""Invalidate logical state of trait `trait_name`"""
if self.trait_descriptors.get(trait_name):
self.trait_descriptors[trait_name] = None
raise DeviceError()
def get_descriptors(self):
descriptors = dict(actions=dict(), traits=dict())
for k, v in self.turtle.__dict__.items():
if not k.startswith("_"):
2023-10-24 10:34:02 +03:00
descriptors["traits"][k] = TraitDescriptor(k,
inspect.getdoc(v),
readable=True,
writable=False)
for m_name, member in inspect.getmembers(Turtle):
if m_name.startswith("_"): continue
if m_name.lower() != m_name: continue
doc = inspect.getdoc(member)
if doc is None: continue
if not inspect.isfunction(member):
descriptors["traits"][m_name] = TraitDescriptor(m_name,
doc,
readable=True,
writable=False)
else:
sig = inspect.signature(member)
params_dict = dict(sig.parameters)
descriptors["actions"][m_name] = ActionDescriptor(m_name,
arguments=params_dict,
info=doc)
return descriptors