aboutsummaryrefslogtreecommitdiff
path: root/helpers/action.py
diff options
context:
space:
mode:
Diffstat (limited to 'helpers/action.py')
-rw-r--r--helpers/action.py38
1 files changed, 29 insertions, 9 deletions
diff --git a/helpers/action.py b/helpers/action.py
index 69ae96f..9145629 100644
--- a/helpers/action.py
+++ b/helpers/action.py
@@ -1,4 +1,4 @@
1import pygame 1import threading
2import time 2import time
3 3
4class Action: 4class Action:
@@ -21,6 +21,7 @@ class Action:
21 21
22 self.key = key 22 self.key = key
23 self.arguments = kwargs 23 self.arguments = kwargs
24 self.sleep_event = None
24 25
25 def ready(self): 26 def ready(self):
26 if 'music' in self.arguments: 27 if 'music' in self.arguments:
@@ -35,7 +36,13 @@ class Action:
35 def description(self): 36 def description(self):
36 return getattr(self, self.action + "_print")(**self.arguments) 37 return getattr(self, self.action + "_print")(**self.arguments)
37 38
39 def interrupt(self):
40 if getattr(self, self.action + "_interrupt", None):
41 return getattr(self, self.action + "_interrupt")(**self.arguments)
42
43 # Actions
38 def command(self, command = "", **kwargs): 44 def command(self, command = "", **kwargs):
45 # FIXME: todo
39 pass 46 pass
40 47
41 def music_list(self, music): 48 def music_list(self, music):
@@ -81,14 +88,15 @@ class Action:
81 pass 88 pass
82 89
83 def wait(self, duration = 0, music = None, **kwargs): 90 def wait(self, duration = 0, music = None, **kwargs):
84 # FIXME: Make it stoppable 91 self.sleep_event = threading.Event()
85 # http://stackoverflow.com/questions/29082268/python-time-sleep-vs-event-wait 92
86 if music is None: 93 if music is not None:
87 time.sleep(duration)
88 else:
89 # TODO
90 music.wait_end() 94 music.wait_end()
91 95
96 threading.Timer(duration, self.sleep_event.set).start()
97 self.sleep_event.wait()
98
99 # Action messages
92 def command_print(self, command = "", **kwargs): 100 def command_print(self, command = "", **kwargs):
93 return "running command {}".format(command) 101 return "running command {}".format(command)
94 102
@@ -146,7 +154,19 @@ class Action:
146 else: 154 else:
147 return "setting volume to {}%".format(value) 155 return "setting volume to {}%".format(value)
148 156
149 def wait_print(self, duration, **kwargs): 157 def wait_print(self, duration = 0, music = None, **kwargs):
150 return "waiting {}s".format(duration) 158 if music is None:
159 return "waiting {}s".format(duration)
160 elif duration == 0:
161 return "waiting the end of « {} »".format(music.name)
162 else:
163 return "waiting the end of « {} » + {}s".format(music.name, duration)
151 164
152 165
166 # Interruptions
167 def wait_interrupt(self, duration = 0, music = None, **kwargs):
168 if self.sleep_event is not None:
169 self.sleep_event.set()
170 if music is not None:
171 music.wait_event.set()
172