Skip to content

Commit 89a7fd4

Browse files
committed
Separe PlayCtrl in gui and functionality classes
Previously playCtrl had part of gui handling, and functionality due to untoggle the play button when playback stops. Make play_once and play_many be just one function Before when many playbacks were needed, play_many was executed in its own thread and created a thread for play_once. But now just a "play_once" thread is created and it calls a playback how many times are needed
1 parent 36aac90 commit 89a7fd4

2 files changed

Lines changed: 101 additions & 1 deletion

File tree

atbswp/control.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,104 @@ def update_timer(self, event):
348348
self.timer = self.countdown_dialog.update_ui()
349349

350350

351+
class PlayControl:
352+
"""Control class for playback functionality"""
353+
354+
def __init__(self, count=1, infinite=False):
355+
self.count = count
356+
self.infinite = infinite
357+
self._stop_locks = [True]
358+
self._current_count = 0
359+
self._play_thread = None
360+
361+
def _play(self, capture, append_function=None):
362+
for line in capture:
363+
if self.is_stoped(): break
364+
exec(line)
365+
self._current_count += 1
366+
if self._current_count < self.count or self.infinite \
367+
and not self.is_stoped():
368+
self._play(capture, append_function)
369+
else:
370+
self.stop()
371+
if append_function:
372+
append_function()
373+
374+
def _start_thread(self, target, *args, daemon=False):
375+
thread = Thread(target=target, args=(*args,))
376+
thread.daemon = daemon
377+
thread.start()
378+
return thread
379+
380+
def set_config(self, count, infinite):
381+
"""Set the configuration to the next playback"""
382+
self.count = count
383+
self.infinite = infinite
384+
385+
def get_current_count(self):
386+
"""Returns the current playback time"""
387+
return self._current_count
388+
389+
def is_stoped(self):
390+
return self._stop_locks[0]
391+
392+
def play(self, capture, append_function=None):
393+
"""Starts playback according with configs setted
394+
395+
:param capture: list of commands to be executed
396+
:param append_function: a function to be executed after
397+
a playback
398+
"""
399+
self._stop_locks[0] = False
400+
self._current_count = 0
401+
self._play_thread = self._start_thread(self._play, capture,
402+
append_function)
403+
404+
def stop(self):
405+
self._stop_locks[0] = True
406+
407+
408+
class PlayInterface:
409+
def __init__(self):
410+
self.play_ctrl = PlayControl()
411+
self.capture = None
412+
self._config_was_updated = False
413+
self._toggle_button = None
414+
415+
def _load_capture_file(self):
416+
if TMP_PATH is None or not os.path.isfile(TMP_PATH):
417+
wx.LogError("No capture loaded")
418+
self._toggle_button.Value = False
419+
return False
420+
with open(TMP_PATH, 'r') as f:
421+
self.capture = f.readlines()
422+
return True
423+
424+
def _set_config(self):
425+
if self._config_was_updated: return
426+
count = settings.CONFIG.getint('DEFAULT', 'Repeat Count')
427+
infinite = settings.CONFIG.getboolean('DEFAULT', 'Infinite Playback')
428+
self.play_ctrl.set_config(count, infinite)
429+
self._config_was_updated = False
430+
431+
def _stop(self):
432+
self._toggle_button.Value = False
433+
self.play_ctrl.stop()
434+
self._config_was_updated = False
435+
settings.save_config()
436+
437+
def action(self, event):
438+
self._toggle_button = event.GetEventObject()
439+
self._toggle_button.Parent.panel.SetFocus()
440+
if self._toggle_button.Value:
441+
if self.play_ctrl.is_stoped():
442+
if not self._load_capture_file(): return
443+
self._set_config()
444+
self.play_ctrl.play(self.capture, self._stop)
445+
else:
446+
self._stop()
447+
448+
351449
class PlayCtrl:
352450
"""Control class for the play button."""
353451

@@ -527,3 +625,5 @@ def end(self):
527625

528626
def ended(self):
529627
return self._end.isSet()
628+
629+

atbswp/gui.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def __add_bindings(self):
203203
self.Bind(wx.EVT_TOGGLEBUTTON, self.rbc.action, self.record_button)
204204

205205
# play_button_ctrl
206-
self.pbc = control.PlayCtrl()
206+
self.pbc = control.PlayInterface()
207207
self.Bind(wx.EVT_TOGGLEBUTTON, self.pbc.action, self.play_button)
208208

209209
# compile_button_ctrl

0 commit comments

Comments
 (0)