]>
Commit | Line | Data |
---|---|---|
c4f4f2a1 | 1 | import threading |
d4217fda | 2 | import time |
c4f4f2a1 IB |
3 | |
4 | def run(action, duration=0, music=None, set_wait_id=None, **kwargs): | |
21ffec31 | 5 | action.mapping.add_wait(action, wait_id=set_wait_id) |
c4f4f2a1 IB |
6 | |
7 | action.sleep_event = threading.Event() | |
6c42e32d IB |
8 | action.sleep_event_timer = threading.Timer( |
9 | duration, | |
10 | action.sleep_event.set) | |
c4f4f2a1 | 11 | |
d4217fda IB |
12 | action.sleep_event_initial_duration = duration |
13 | action.sleep_event_paused = False | |
14 | action.sleep_event_left_time = duration | |
15 | ||
c4f4f2a1 IB |
16 | if music is not None: |
17 | music.wait_end() | |
18 | ||
d4217fda IB |
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 | ||
c4f4f2a1 IB |
23 | action.sleep_event.wait() |
24 | ||
25 | def description(action, duration=0, music=None, set_wait_id=None, **kwargs): | |
6a327173 | 26 | formats = [] |
c4f4f2a1 IB |
27 | message = "" |
28 | if music is None: | |
6a327173 IB |
29 | message += "waiting {}s" |
30 | formats.append(duration) | |
c4f4f2a1 | 31 | elif duration == 0: |
6a327173 IB |
32 | message += "waiting the end of « {} »" |
33 | formats.append(music.name) | |
c4f4f2a1 | 34 | else: |
6a327173 IB |
35 | message += "waiting the end of « {} » + {}s" |
36 | formats.append(music.name) | |
37 | formats.append(duration) | |
c4f4f2a1 IB |
38 | |
39 | if set_wait_id is not None: | |
6a327173 IB |
40 | message += " (setting id = {})" |
41 | formats.append(set_wait_id) | |
c4f4f2a1 | 42 | |
6a327173 | 43 | return _(message).format(*formats) |
c4f4f2a1 | 44 | |
d4217fda IB |
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 | ||
c4f4f2a1 IB |
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() |