]>
Commit | Line | Data |
---|---|---|
1 | import threading | |
2 | import time | |
3 | ||
4 | class Action: | |
5 | action_types = [ | |
6 | 'command', | |
7 | 'pause', | |
8 | 'play', | |
9 | 'stop', | |
10 | 'stop_all_actions', | |
11 | 'unpause', | |
12 | 'volume', | |
13 | 'wait', | |
14 | ] | |
15 | ||
16 | def __init__(self, action, key, **kwargs): | |
17 | if action in self.action_types: | |
18 | self.action = action | |
19 | else: | |
20 | raise Exception("Unknown action {}".format(action)) | |
21 | ||
22 | self.key = key | |
23 | self.arguments = kwargs | |
24 | self.sleep_event = None | |
25 | ||
26 | def ready(self): | |
27 | if 'music' in self.arguments: | |
28 | return self.arguments['music'].check_is_loaded() | |
29 | else: | |
30 | return True | |
31 | ||
32 | def run(self): | |
33 | print(self.description()) | |
34 | getattr(self, self.action)(**self.arguments) | |
35 | ||
36 | def description(self): | |
37 | return getattr(self, self.action + "_print")(**self.arguments) | |
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 | |
44 | def command(self, command = "", **kwargs): | |
45 | # FIXME: todo | |
46 | pass | |
47 | ||
48 | def music_list(self, music): | |
49 | if music is not None: | |
50 | return [music] | |
51 | else: | |
52 | return self.key.parent.open_files.values() | |
53 | ||
54 | def pause(self, music = None, **kwargs): | |
55 | for music in self.music_list(music): | |
56 | if music.is_loaded_playing(): | |
57 | music.pause() | |
58 | ||
59 | def unpause(self, music = None, **kwargs): | |
60 | for music in self.music_list(music): | |
61 | if music.is_loaded_paused(): | |
62 | music.unpause() | |
63 | ||
64 | def play(self, music = None, fade_in = 0, start_at = 0, | |
65 | restart_if_running = False, volume = 100, **kwargs): | |
66 | if music is not None: | |
67 | if restart_if_running: | |
68 | if music.is_not_stopped(): | |
69 | music.stop() | |
70 | music.play(volume = volume, fade_in = fade_in, start_at = start_at) | |
71 | else: | |
72 | if not music.is_not_stopped(): | |
73 | music.play(volume = volume, fade_in = fade_in, start_at = start_at) | |
74 | ||
75 | def stop(self, music = None, fade_out = 0, **kwargs): | |
76 | for music in self.music_list(music): | |
77 | if music.is_loaded_paused() or music.is_loaded_playing(): | |
78 | music.stop(fade_out = fade_out) | |
79 | ||
80 | def stop_all_actions(self, **kwargs): | |
81 | self.key.parent.stop_all_running() | |
82 | ||
83 | def volume(self, music = None, value = 100, **kwargs): | |
84 | if music is not None: | |
85 | music.set_volume(value) | |
86 | else: | |
87 | # FIXME: todo | |
88 | pass | |
89 | ||
90 | def wait(self, duration = 0, music = None, **kwargs): | |
91 | self.sleep_event = threading.Event() | |
92 | ||
93 | if music is not None: | |
94 | music.wait_end() | |
95 | ||
96 | threading.Timer(duration, self.sleep_event.set).start() | |
97 | self.sleep_event.wait() | |
98 | ||
99 | # Action messages | |
100 | def command_print(self, command = "", **kwargs): | |
101 | return "running command {}".format(command) | |
102 | ||
103 | def pause_print(self, music = None, **kwargs): | |
104 | if music is not None: | |
105 | return "pausing « {} »".format(music.name) | |
106 | else: | |
107 | return "pausing all musics" | |
108 | ||
109 | def unpause_print(self, music = None, **kwargs): | |
110 | if music is not None: | |
111 | return "unpausing « {} »".format(music.name) | |
112 | else: | |
113 | return "unpausing all musics" | |
114 | ||
115 | def play_print(self, music = None, fade_in = 0, start_at = 0, | |
116 | restart_if_running = False, volume = 100, **kwargs): | |
117 | message = "starting " | |
118 | if music is not None: | |
119 | message += "« {} »".format(music.name) | |
120 | else: | |
121 | message += "music" | |
122 | ||
123 | if start_at != 0: | |
124 | message += " at {}s".format(start_at) | |
125 | ||
126 | if fade_in != 0: | |
127 | message += " with {}s fade_in".format(fade_in) | |
128 | ||
129 | message += " at volume {}%".format(volume) | |
130 | ||
131 | if restart_if_running: | |
132 | message += " (restarting if already running)" | |
133 | ||
134 | return message | |
135 | ||
136 | def stop_print(self, music = None, fade_out = 0, **kwargs): | |
137 | if music is not None: | |
138 | if fade_out == 0: | |
139 | return "stopping music « {} »".format(music.name) | |
140 | else: | |
141 | return "stopping music « {} » with {}s fadeout".format(music.name, fade_out) | |
142 | else: | |
143 | if fade_out == 0: | |
144 | return "stopping all musics" | |
145 | else: | |
146 | return "stopping all musics with {}s fadeout".format(fade_out) | |
147 | ||
148 | def stop_all_actions_print(self, **kwargs): | |
149 | return "stopping all actions" | |
150 | ||
151 | def volume_print(self, music = None, value = 100, **kwargs): | |
152 | if music is not None: | |
153 | return "setting volume of « {} » to {}%".format(music.name, value) | |
154 | else: | |
155 | return "setting volume to {}%".format(value) | |
156 | ||
157 | def wait_print(self, duration = 0, music = None, **kwargs): | |
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) | |
164 | ||
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 |