This commit is contained in:
zefirova.am 2023-12-15 00:25:07 +03:00
parent 444f35b71f
commit 62bf45b432
3 changed files with 33 additions and 34 deletions

View File

@ -1,7 +1,7 @@
from dataclasses import dataclass from dataclasses import dataclass
from typing import Optional, Collection, Any from typing import Optional, Collection, Any
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from enum import Enum, auto from enum import Enum
class DeviceLifecycleState(Enum): class DeviceLifecycleState(Enum):
INIT = "init" INIT = "init"
@ -85,7 +85,4 @@ class SynchronyDevice(Device):
@abstractmethod @abstractmethod
def invalidate(self, trait_name: str): def invalidate(self, trait_name: str):
"""Invalidate logical state of trait `trait_name`""" """Invalidate logical state of trait `trait_name`"""
pass pass
def close(self):
return super().close()

View File

@ -1,22 +1,16 @@
from turtle import Turtle from turtle import Turtle
from typing import Any, Collection, Optional from typing import Any, Collection, Optional
from controls.device import ActionDescriptor, SynchronyDevice, TraitDescriptor from controls.device import SynchronyDevice, TraitDescriptor, DeviceLifecycleState
class TurtleDevice(SynchronyDevice): class TurtleDevice(SynchronyDevice):
def open(self, name, **kwargs): def open(self):
self.turtle = Turtle(kwargs) self.state == DeviceLifecycleState.OPEN
self.turtle = Turtle()
super().open() super().open()
def close(self): def close(self):
del self.turtle del self.turtle
super().close() super().close()
# @property
# def trait_descriptors(self):
# traits = [attribute for attribute in vars(self.turtle).items()
# if (attribute[0].startswith('_') == False)]
# return Collection[[TraitDescriptor(*tr) for tr in traits]]
@property @property
def trait_descriptors(self): def trait_descriptors(self):
@ -26,13 +20,6 @@ class TurtleDevice(SynchronyDevice):
self.traits = [TraitDescriptor(*tr) for tr in traits] self.traits = [TraitDescriptor(*tr) for tr in traits]
return self.traits return self.traits
# @property
# def action_descriptors(self):
# actions = [(attribute, getattr(t, attribute)) for attribute in dir(t)
# if (attribute.startswith('_') == False)]
# return Collection[[TraitDescriptor(*ac) for ac in actions]]
@property @property
def action_descriptors(self): def action_descriptors(self):
actions = [(attribute, getattr(self.turtle, attribute)) for attribute in dir(self.turtle) actions = [(attribute, getattr(self.turtle, attribute)) for attribute in dir(self.turtle)
@ -53,7 +40,11 @@ class TurtleDevice(SynchronyDevice):
collection[i].info=value collection[i].info=value
def execute(self, action_name: str, *args, **kwargs): def execute(self, action_name: str, *args, **kwargs):
self.turtle.action_name(args, *kwargs) if self.state == DeviceLifecycleState.INIT:
self.open()
execution = getattr(self.turtle, action_name)
execution(int(*args), **kwargs)
def invalidate(self, trait_name: str): def invalidate(self, trait_name: str):
return super().invalidate(trait_name) return super().invalidate(trait_name)

View File

@ -1,13 +1,12 @@
import cmd import cmd
import threading from threading import Thread, Event
from threading import Thread from turtle import bye
from queue import Queue, Empty
from queue import Empty, Queue
from equipment.turtle_device import TurtleDevice from equipment.turtle_device import TurtleDevice
class TurtleDeviceThread(threading.Thread): class TurtleDeviceThread(Thread):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.device = TurtleDevice() self.device = TurtleDevice()
@ -24,7 +23,7 @@ class TurtleDeviceThread(threading.Thread):
break break
self.device.execute(*item) self.device.execute(*item)
self.queue.task_done() self.queue.task_done()
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'
@ -33,15 +32,27 @@ 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() self.turtle_thread = turtle_thread
def do_execute(self, arg): def do_execute(self, arg):
self.turtle_thread.queue.put(arg) self.turtle_thread.queue.put(parse(arg))
def do_exit(self, arg): def do_exit(self, arg):
self.turtle_thread.queue.put('exit') self.close()
self.turtle_thread.queue.put("exit")
print('Thank you for using Turtle')
return True
def close(self):
if self.file:
self.file.close()
self.file = None
def parse(arg):
'Convert a series of zero or more numbers to an argument tuple'
return tuple(arg.split())
if __name__ == '__main__': if __name__ == '__main__':
turtle_thread = TurtleDeviceThread() turtle_thread = TurtleDeviceThread()