]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - music_sampler/actions/wait.py
wait actions are now pausable and resettable
[perso/Immae/Projets/Python/MusicSampler.git] / music_sampler / actions / wait.py
1 import threading
2 import time
3
4 def run(action, duration=0, music=None, set_wait_id=None, **kwargs):
5 action.mapping.add_wait(action, wait_id=set_wait_id)
6
7 action.sleep_event = threading.Event()
8 action.sleep_event_timer = threading.Timer(
9 duration,
10 action.sleep_event.set)
11
12 action.sleep_event_initial_duration = duration
13 action.sleep_event_paused = False
14 action.sleep_event_left_time = duration
15
16 if music is not None:
17 music.wait_end()
18
19 if duration <= 0 or not action.sleep_event_paused:
20 action.sleep_event_timer.start()
21 action.sleep_event_started_time = time.time()
22
23 action.sleep_event.wait()
24
25 def description(action, duration=0, music=None, set_wait_id=None, **kwargs):
26 formats = []
27 message = ""
28 if music is None:
29 message += "waiting {}s"
30 formats.append(duration)
31 elif duration == 0:
32 message += "waiting the end of « {} »"
33 formats.append(music.name)
34 else:
35 message += "waiting the end of « {} » + {}s"
36 formats.append(music.name)
37 formats.append(duration)
38
39 if set_wait_id is not None:
40 message += " (setting id = {})"
41 formats.append(set_wait_id)
42
43 return _(message).format(*formats)
44
45 def pause(action, **kwargs):
46 if action.sleep_event_paused:
47 return
48
49 action.sleep_event_paused = True
50
51 if not action.sleep_event_timer.is_alive():
52 return
53
54 action.sleep_event_timer.cancel()
55
56 action.sleep_event_left_time = action.sleep_event_left_time\
57 - (time.time() - action.sleep_event_started_time)
58 if action.sleep_event_left_time < 0:
59 action.sleep_event.set()
60
61 def unpause(action, **kwargs):
62 if not action.sleep_event_paused:
63 return
64
65 action.sleep_event_paused = False
66
67 action.sleep_event_timer = threading.Timer(
68 action.sleep_event_left_time,
69 action.sleep_event.set)
70
71 action.sleep_event_timer.start()
72 action.sleep_event_started_time = time.time()
73
74 def reset(action, **kwargs):
75 action.sleep_event_timer.cancel()
76
77 action.sleep_event_left_time = action.sleep_event_initial_duration
78
79 if action.sleep_event_paused:
80 return
81
82 action.sleep_event_timer = threading.Timer(
83 action.sleep_event_left_time,
84 action.sleep_event.set)
85
86 action.sleep_event_timer.start()
87 action.sleep_event_started_time = time.time()
88
89 def interrupt(action, duration=0, music=None, **kwargs):
90 if action.sleep_event is not None:
91 action.sleep_event.set()
92 action.sleep_event_timer.cancel()
93 if music is not None:
94 music.wait_event.set()