forked from Advanced_Python/advanced-python-homework-2023
46 lines
1.2 KiB
Python
46 lines
1.2 KiB
Python
import cmd
|
|
import threading
|
|
|
|
from queue import Queue, Empty
|
|
|
|
from equipment.turtle_device import TurtleDevice
|
|
|
|
|
|
class TurtleDeviceThread(threading.Thread):
|
|
# TODO(Homework 4)
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.device = TurtleDevice()
|
|
self.queue = Queue()
|
|
|
|
def run(self):
|
|
while True:
|
|
try:
|
|
item = self.queue.get()
|
|
except self.queue.Empty:
|
|
continue
|
|
else:
|
|
if (item == 'exit'):
|
|
break
|
|
self.device.execute(item[0], item[1:])
|
|
self.queue.task_done()
|
|
|
|
class NoBlockingTurtleShell(cmd.Cmd):
|
|
intro = 'Welcome to the turtle shell. Type help or ? to list commands.\n'
|
|
prompt = '(turtle) '
|
|
file = None
|
|
|
|
def __init__(self, turtle_thread: TurtleDeviceThread):
|
|
self.turtle_thread = TurtleDeviceThread()
|
|
|
|
def do_execute(self, arg):
|
|
self.turtle_thread.queue.put(arg)
|
|
|
|
def do_exit(self, arg):
|
|
self.turtle_thread.queue.put('exit')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
turtle_thread = TurtleDeviceThread()
|
|
turtle_thread.start()
|
|
NoBlockingTurtleShell(turtle_thread).cmdloop() |