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 typing import Optional, Collection, Any
from abc import ABC, abstractmethod
from enum import Enum, auto
from enum import Enum
class DeviceLifecycleState(Enum):
INIT = "init"
@ -86,6 +86,3 @@ class SynchronyDevice(Device):
def invalidate(self, trait_name: str):
"""Invalidate logical state of trait `trait_name`"""
pass
def close(self):
return super().close()

View File

@ -1,23 +1,17 @@
from turtle import Turtle
from typing import Any, Collection, Optional
from controls.device import ActionDescriptor, SynchronyDevice, TraitDescriptor
from controls.device import SynchronyDevice, TraitDescriptor, DeviceLifecycleState
class TurtleDevice(SynchronyDevice):
def open(self, name, **kwargs):
self.turtle = Turtle(kwargs)
def open(self):
self.state == DeviceLifecycleState.OPEN
self.turtle = Turtle()
super().open()
def close(self):
del self.turtle
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
def trait_descriptors(self):
traits = [attribute for attribute in vars(self.turtle).items()
@ -26,13 +20,6 @@ class TurtleDevice(SynchronyDevice):
self.traits = [TraitDescriptor(*tr) for tr in 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
def action_descriptors(self):
actions = [(attribute, getattr(self.turtle, attribute)) for attribute in dir(self.turtle)
@ -53,7 +40,11 @@ class TurtleDevice(SynchronyDevice):
collection[i].info=value
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):
return super().invalidate(trait_name)

View File

@ -1,13 +1,12 @@
import cmd
import threading
from threading import Thread
from queue import Empty, Queue
from threading import Thread, Event
from turtle import bye
from queue import Queue, Empty
from equipment.turtle_device import TurtleDevice
class TurtleDeviceThread(threading.Thread):
class TurtleDeviceThread(Thread):
def __init__(self):
super().__init__()
self.device = TurtleDevice()
@ -33,16 +32,28 @@ class NoBlockingTurtleShell(cmd.Cmd):
def __init__(self, turtle_thread: TurtleDeviceThread):
super(NoBlockingTurtleShell, self).__init__()
self.turtle_thread = TurtleDeviceThread()
self.turtle_thread = turtle_thread
def do_execute(self, arg):
self.turtle_thread.queue.put(arg)
self.turtle_thread.queue.put(parse(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__':
turtle_thread = TurtleDeviceThread()
turtle_thread.start()