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

55 lines
1.9 KiB
Python
Raw Normal View History

2023-11-02 22:22:22 +03:00
from turtle import Turtle
2023-10-27 00:21:53 +03:00
from typing import Optional, Collection, Any
from controls.device import SynchronyDevice
2023-11-02 22:22:22 +03:00
from controls.device import TraitDescriptor
from controls.device import ActionDescriptor
2023-10-27 00:21:53 +03:00
import inspect
class TurtleDevice(SynchronyDevice):
def open(self):
2023-11-02 22:22:22 +03:00
self.turtle = Turtle()
2023-10-27 00:21:53 +03:00
super().open()
def close(self):
2023-11-02 22:22:22 +03:00
self.turtle.clear()
2023-10-27 00:21:53 +03:00
super().close()
def execute(self, action_name: str, *args, **kwargs):
2023-11-02 22:22:22 +03:00
getattr(self.turtle, action_name)(*args, **kwargs)
2023-10-27 00:21:53 +03:00
def read(self, trait_name: str):
2023-11-02 22:22:22 +03:00
"""Read physical state of trait `trait_name` from device."""
trait = self.trait_descriptors()[trait_name]
if not trait.readable : super().read(trait_name)
return trait.info
2023-10-27 00:21:53 +03:00
def write(self, trait_name: str, value: Any) -> bool:
2023-11-02 22:22:22 +03:00
"""Pass `value` to trait `trait_name` of device."""
if not self.trait_descriptors()[trait_name] : super().write(trait_name, value)
setattr(self.turtle, trait_name, value)
2023-10-27 00:21:53 +03:00
def invalidate(self, trait_name: str):
2023-11-02 22:22:22 +03:00
"""Invalidate logical state of trait `trait_name`"""
2023-10-27 00:21:53 +03:00
pass
def trait_descriptors(self):
2023-11-02 22:22:22 +03:00
traits = dict()
for name, value in inspect.getmembers(self.turtle):
if not callable(value) and not name.startswith('_'):
info = f"{value}"
traits[name] = TraitDescriptor(name, info)
2023-10-27 00:21:53 +03:00
return traits
def action_descriptors(self):
2023-11-02 22:22:22 +03:00
actions = dict()
for name, func in inspect.getmembers(self.turtle):
if callable(func) and not name.startswith('_'):
args = dict(inspect.signature(func).parameters)
info = inspect.getdoc(func)
actions[name] = ActionDescriptor(name, args, info)
2023-10-27 00:21:53 +03:00
return actions
def __getitem__(self, trait_name: str):
"""Return logical state of trait `trait_name`."""
pass