|
1 | 1 | import sys |
2 | | -import time |
3 | 2 | from threading import Thread |
4 | 3 | from unittest import mock |
5 | 4 |
|
6 | | -from clint.textui import colored |
7 | 5 | from watchdog.observers import Observer |
8 | 6 |
|
| 7 | +from dojo_toolkit import dojo_thread |
9 | 8 | from dojo_toolkit.code_handler import DojoCodeHandler |
10 | | -from dojo_toolkit.notifier import notifier |
11 | 9 | from dojo_toolkit.sound_handler import SoundHandler |
12 | 10 | from dojo_toolkit.test_runner import get_test_runner |
13 | 11 | from dojo_toolkit.timer import Timer |
14 | 12 |
|
15 | 13 |
|
16 | 14 | class Dojo: |
17 | 15 | ROUND_TIME = 5 |
18 | | - round_started = False |
19 | 16 |
|
20 | 17 | def __init__(self, code_path, round_time=None, mute=False, test_runner=None, runner="doctest"): |
21 | 18 | self.code_path = code_path |
22 | 19 | self.round_time = round_time or self.ROUND_TIME |
23 | 20 | self.sound_player = mock.Mock() if mute or sys.platform != "linux" else SoundHandler() |
24 | 21 | self.info_notified = False |
| 22 | + self.timer = Timer(self.round_time) |
25 | 23 |
|
26 | 24 | test_runner = get_test_runner(test_runner, runner, self.code_path, self.sound_player) |
| 25 | + self.controller = dojo_thread.DojoController(self.timer, self.sound_player) |
27 | 26 |
|
28 | | - event_handler = DojoCodeHandler(dojo=self, test_runner=test_runner) |
| 27 | + event_handler = DojoCodeHandler(dojo=self.controller, test_runner=test_runner) |
29 | 28 |
|
30 | 29 | self.observer = Observer() |
31 | 30 | self.observer.schedule(event_handler, self.code_path, recursive=False) |
32 | 31 |
|
33 | | - self.timer = Timer(self.round_time) |
34 | | - |
35 | 32 | def start(self): |
36 | 33 | self.observer.start() |
37 | 34 | print("\nWatching: {} folder".format(self.code_path)) |
38 | 35 |
|
39 | 36 | self.is_running = True |
40 | 37 | print("Dojo toolkit started!") |
41 | | - self.thread = Thread(target=self.dojo) |
| 38 | + self.thread = Thread(target=dojo_thread.main, args=(self.controller,)) |
42 | 39 | self.thread.daemon = True |
43 | 40 | self.thread.start() |
44 | 41 |
|
45 | 42 | self.thread.join() |
46 | 43 | self.observer.join() |
47 | | - |
48 | | - def stop(self): |
49 | | - self.is_running = False |
50 | | - |
51 | | - def await_pilot_exchange(self): |
52 | | - print("Awaiting the pilot and co-pilot to enter their positions.") |
53 | | - print("Press <Enter> when they are ready") |
54 | | - input() |
55 | | - |
56 | | - def round_start(self): |
57 | | - self.timer.start() |
58 | | - self.sound_player.play_start() |
59 | | - self.round_started = True |
60 | | - self.info_notified = False |
61 | | - |
62 | | - print("Round started! {} minutes left...".format(self.round_time)) |
63 | | - |
64 | | - def round_info(self): |
65 | | - if self.timer.ellapsed_time == self.timer.duration - 60 and not self.info_notified: |
66 | | - notifier.notify("60 seconds to round finish...") |
67 | | - print((getattr(colored, "yellow"))("Round is going to finish in 60 seconds")) |
68 | | - self.info_notified = True |
69 | | - |
70 | | - def round_finished(self): |
71 | | - notifier.notify("Time Up", timeout=15 * 1000) |
72 | | - self.sound_player.play_timeup() |
73 | | - self.round_started = False |
74 | | - print("Round finished!\n") |
75 | | - |
76 | | - def dojo(self): |
77 | | - while self.is_running: |
78 | | - self.await_pilot_exchange() |
79 | | - self.round_start() |
80 | | - while self.timer.is_running: |
81 | | - self.round_info() |
82 | | - time.sleep(0.8) |
83 | | - self.round_finished() |
0 commit comments