forked from Advanced_Python/advanced-python-homework-2023
modified: equipment/turtle_device.py
modified: noblocking_turtle_shell.py
This commit is contained in:
parent
7742a74729
commit
8a621ebbe3
@ -1,56 +1,73 @@
|
|||||||
|
import time
|
||||||
from turtle import Turtle
|
from turtle import Turtle
|
||||||
from typing import Any, Collection, Optional
|
from equipment.device import SynchronyDevice
|
||||||
from controls.device import ActionDescriptor, SynchronyDevice, TraitDescriptor
|
from equipment.device import Device
|
||||||
|
import inspect
|
||||||
|
from typing import Optional, Collection, Any
|
||||||
|
|
||||||
class TurtleDevice(SynchronyDevice):
|
class TurtleDevice(SynchronyDevice):
|
||||||
def open(self, name, **kwargs):
|
def open(self):
|
||||||
self.turtle = Turtle(kwargs)
|
|
||||||
super().open()
|
super().open()
|
||||||
|
self.turtle = Turtle()
|
||||||
|
|
||||||
def close (self):
|
def close (self):
|
||||||
del self.turtle
|
|
||||||
super().close()
|
super().close()
|
||||||
|
del self.turtle
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def trait_descriptors(self):
|
def trait_descriptors(self):
|
||||||
traits = [attribute for attribute in vars(self.turtle).items()
|
l = list()
|
||||||
if (attribute[0].startswith('_') == False)]
|
for i in inspect.getmembers(TurtleDevice):
|
||||||
|
if not (i[0].startswith("__") or i[0].startswith("_")) and not inspect.isfunction(i[1]):
|
||||||
self.traits = [TraitDescriptor(*tr) for tr in traits]
|
l.append(i[0])
|
||||||
return self.traits
|
return l
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def action_descriptors(self):
|
def action_descriptors(self):
|
||||||
actions = [(attribute, getattr(t, attribute)) for attribute in dir(t)
|
l = list()
|
||||||
if (attribute.startswith('_') == False)]
|
#print(inspect.getmembers(TurtleDevice))
|
||||||
|
#for i in dir(TurtleDevice):
|
||||||
self.actions = [TraitDescriptor(*ac) for ac in actions]
|
# # print(type(getattr(TurtleDevice(), i))=='method')
|
||||||
return self.actions
|
# # if not i.startswith("__") and type(getattr(TurtleDevice(), i)) == types.MethodType:
|
||||||
|
# if not i.startswith("__") and inspect.isroutine(inspect.getattr_static(TurtleDevice, i)):
|
||||||
def read(self, trait_name):
|
# sig = inspect.signature(getattr(TurtleDevice, i))
|
||||||
collection = self.traits
|
# l.append(i + " " + str(sig))
|
||||||
return ([collection[i].info for i in range(len(collection))
|
#return l
|
||||||
if collection[i].name == trait_name])
|
for i in inspect.getmembers(TurtleDevice):
|
||||||
|
# print(type(getattr(TurtleDevice, i)))
|
||||||
def write(self, trait_name: str, value: Any) -> bool:
|
# print(type(getattr(TurtleDevice(), i))=='method')
|
||||||
collection = self.traits
|
if not (i[0].startswith("__") or i[0].startswith("_")) and inspect.isfunction(i[1]):
|
||||||
for i in range(len(collection)):
|
sig = inspect.signature(getattr(TurtleDevice, i[0]))
|
||||||
if collection[i].name == trait_name:
|
l.append(i[0]+str(sig))
|
||||||
collection[i].info=value
|
return l
|
||||||
|
|
||||||
def execute(self, action_name: str, *args, **kwargs):
|
def execute(self, action_name: str, *args, **kwargs):
|
||||||
self.turtle.action_name(args, *kwargs)
|
"""Execute action `action_name`, using `args` and `kwargs` as action argument."""
|
||||||
|
if self.state == 1:
|
||||||
|
f = getattr(self.turtle, action_name)
|
||||||
|
f(int(*args), **kwargs)
|
||||||
|
else:
|
||||||
|
print("It's not opened")
|
||||||
|
self.open()
|
||||||
|
#time.sleep(30)
|
||||||
|
f = getattr(self.turtle, action_name)
|
||||||
|
f(int(*args), **kwargs)
|
||||||
|
|
||||||
|
def read(self, trait_name: str):
|
||||||
|
"""Read physical state of trait `trait_name` from device."""
|
||||||
|
return getattr(self.turtle, trait_name)
|
||||||
|
|
||||||
|
def write(self, trait_name: str, value: Any) -> bool:
|
||||||
|
"""Pass `value` to trait `trait_name` of device."""
|
||||||
|
setattr(self.turtle, trait_name, value)
|
||||||
|
|
||||||
def invalidate(self, trait_name: str):
|
def invalidate(self, trait_name: str):
|
||||||
return super().invalidate(trait_name)
|
"""Invalidate logical state of trait `trait_name`"""
|
||||||
|
if self.read(trait_name) == self.__getitem__(trait_name):
|
||||||
def __getitem__(self, trait_name: str) -> Any | None:
|
print("Everything's okey")
|
||||||
collection = self.traits
|
else:
|
||||||
return ([collection[i] for i in range(len(collection))
|
print(f"Something went wrong with {trait_name}.")
|
||||||
if collection[i].name == trait_name])
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def __getitem__(self, trait_name: str) -> Optional[Any]:
|
||||||
|
"""Return logical state of trait `trait_name`."""
|
||||||
|
return getattr(self, trait_name)
|
@ -2,6 +2,8 @@ import cmd
|
|||||||
import threading
|
import threading
|
||||||
from queue import Empty, Queue
|
from queue import Empty, Queue
|
||||||
from equipment.turtle_device import TurtleDevice
|
from equipment.turtle_device import TurtleDevice
|
||||||
|
import argparse
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
class TurtleDeviceThread(threading.Thread):
|
class TurtleDeviceThread(threading.Thread):
|
||||||
@ -10,6 +12,7 @@ class TurtleDeviceThread(threading.Thread):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
self.device = TurtleDevice()
|
self.device = TurtleDevice()
|
||||||
self.queue = Queue()
|
self.queue = Queue()
|
||||||
|
self.event = Event()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while True:
|
while True:
|
||||||
@ -22,6 +25,20 @@ class TurtleDeviceThread(threading.Thread):
|
|||||||
break
|
break
|
||||||
self.device.execute(item[0], item[1:])
|
self.device.execute(item[0], item[1:])
|
||||||
self.queue.task_done()
|
self.queue.task_done()
|
||||||
|
def __message_processing__(self):
|
||||||
|
while True:
|
||||||
|
new_message = self.queue.get()
|
||||||
|
#print(type(new_message))
|
||||||
|
#print(new_message)
|
||||||
|
#print("Name and args of action are {}".format(new_message))
|
||||||
|
self.device.execute(*new_message)
|
||||||
|
#time.sleep(30)
|
||||||
|
self.queue.task_done()
|
||||||
|
if self.event.is_set():
|
||||||
|
break
|
||||||
|
def __message_thread__(self):
|
||||||
|
thread = Thread(target = self.__message_processing__, daemon=True)
|
||||||
|
thread.start()
|
||||||
|
|
||||||
class NoBlockingTurtleShell(cmd.Cmd):
|
class NoBlockingTurtleShell(cmd.Cmd):
|
||||||
intro = 'Welcome to the turtle shell. Type help or ? to list commands.\n'
|
intro = 'Welcome to the turtle shell. Type help or ? to list commands.\n'
|
||||||
@ -30,13 +47,13 @@ class NoBlockingTurtleShell(cmd.Cmd):
|
|||||||
|
|
||||||
def __init__(self, turtle_thread: TurtleDeviceThread):
|
def __init__(self, turtle_thread: TurtleDeviceThread):
|
||||||
super(NoBlockingTurtleShell, self).__init__()
|
super(NoBlockingTurtleShell, self).__init__()
|
||||||
self.turtle_thread = TurtleDeviceThread()
|
turtle_thread.__message_thread__()
|
||||||
|
|
||||||
def do_execute(self, arg):
|
def do_execute(self, arg):
|
||||||
self.turtle_thread.queue.put(arg)
|
turtle_thread.queue.put(parse(arg))
|
||||||
|
|
||||||
def do_exit(self, arg):
|
def do_exit(self, arg):
|
||||||
self.turtle_thread.queue.put('exit')
|
turtle_thread.event.set()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
Reference in New Issue
Block a user