]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blobdiff - helpers/action.py
Use machine for key handling
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / action.py
index 218c3167c934ada44a8213d22a5400d80215fe19..010a6cafb1f93db7442e22cc8999d44aec54bb94 100644 (file)
@@ -1,11 +1,13 @@
 import threading
 import time
 
-from . import debug_print
+from transitions.extensions import HierarchicalMachine as Machine
+from . import debug_print, error_print
 
 class Action:
-    action_types = [
+    ACTION_TYPES = [
         'command',
+        'interrupt_wait',
         'pause',
         'play',
         'seek',
@@ -16,40 +18,109 @@ class Action:
         'wait',
     ]
 
+    STATES = [
+        'initial',
+        'loading',
+        'failed',
+        {
+            'name': 'loaded',
+            'children': ['running']
+        }
+    ]
+
+    TRANSITIONS = [
+        {
+            'trigger': 'load',
+            'source': 'initial',
+            'dest': 'loading'
+        },
+        {
+            'trigger': 'fail',
+            'source': 'loading',
+            'dest': 'failed',
+            'after': 'poll_loaded'
+        },
+        {
+            'trigger': 'success',
+            'source': 'loading',
+            'dest': 'loaded',
+            'after': 'poll_loaded'
+        },
+        {
+            'trigger': 'run',
+            'source': 'loaded',
+            'dest': 'loaded_running',
+            'after': 'finish_action',
+            # if a child has no transitions, then it is bubbled to the parent,
+            # and we don't want that. Not useful in that machine precisely.
+            'conditions': ['is_loaded']
+        },
+        {
+            'trigger': 'finish_action',
+            'source': 'loaded_running',
+            'dest': 'loaded'
+        }
+    ]
+
     def __init__(self, action, key, **kwargs):
-        if action in self.action_types:
-            self.action = action
-        else:
-            raise Exception("Unknown action {}".format(action))
+        Machine(model=self, states=self.STATES,
+                transitions=self.TRANSITIONS, initial='initial',
+                ignore_invalid_triggers=True, queued=True)
 
+        self.action = action
         self.key = key
         self.mapping = key.parent
         self.arguments = kwargs
         self.sleep_event = None
+        self.waiting_music = None
+
+    def is_loaded_or_failed(self):
+        return self.is_loaded(allow_substates=True) or self.is_failed()
 
-    def ready(self):
-        if 'music' in self.arguments:
-            return self.arguments['music'].check_is_loaded()
+    def callback_music_loaded(self, success):
+        if success:
+            self.success()
         else:
-            return True
+            self.fail()
 
-    def run(self):
+    # Machine states / events
+    def on_enter_loading(self):
+        if self.action in self.ACTION_TYPES:
+            if 'music' in self.arguments:
+                self.arguments['music'].subscribe_loaded(self.callback_music_loaded)
+            else:
+                self.success()
+        else:
+            error_print("Unknown action {}".format(self.action))
+            self.fail()
+
+    def on_enter_loaded_running(self):
         debug_print(self.description())
         getattr(self, self.action)(**self.arguments)
 
-    def description(self):
-        return getattr(self, self.action + "_print")(**self.arguments)
+    def poll_loaded(self):
+        self.key.callback_action_ready(self,
+                self.is_loaded(allow_substates=True))
 
+    # This one cannot be in the Machine state since it would be queued to run
+    # *after* the wait is ended...
     def interrupt(self):
         if getattr(self, self.action + "_interrupt", None):
             return getattr(self, self.action + "_interrupt")(**self.arguments)
 
+    # Helpers
     def music_list(self, music):
         if music is not None:
             return [music]
         else:
             return self.mapping.open_files.values()
 
+    def description(self):
+        if getattr(self, self.action + "_print", None):
+            return getattr(self, self.action + "_print")(**self.arguments)
+        else:
+            return "unknown action {}".format(self.action)
+
     # Actions
     def command(self, command="", **kwargs):
         # FIXME: todo
@@ -70,14 +141,14 @@ class Action:
             loop=0, **kwargs):
         for music in self.music_list(music):
             if restart_if_running:
-                if music.is_not_stopped():
+                if music.is_in_use():
                     music.stop()
                 music.play(
                         volume=volume,
                         fade_in=fade_in,
                         start_at=start_at,
                         loop=loop)
-            elif not music.is_not_stopped():
+            elif not music.is_in_use():
                 music.play(
                         volume=volume,
                         fade_in=fade_in,
@@ -88,39 +159,56 @@ class Action:
         for music in self.music_list(music):
             music.seek(value=value, delta=delta)
 
-    def stop(self, music=None, fade_out=0, wait=False, **kwargs):
+    def interrupt_wait(self, wait_id=None):
+        self.mapping.interrupt_wait(wait_id)
+
+    def stop(self, music=None, fade_out=0, wait=False,
+            set_wait_id=None, **kwargs):
         previous = None
         for music in self.music_list(music):
             if music.is_loaded_paused() or music.is_loaded_playing():
                 if previous is not None:
                     previous.stop(fade_out=fade_out)
                 previous = music
+            else:
+                music.stop(fade_out=fade_out)
 
         if previous is not None:
-            previous.stop(fade_out=fade_out, wait=wait)
+            self.waiting_music = previous
+            previous.stop(
+                    fade_out=fade_out,
+                    wait=wait,
+                    set_wait_id=set_wait_id)
 
     def stop_all_actions(self, **kwargs):
         self.mapping.stop_all_running()
 
-    def volume(self, music=None, value=100, delta=False, **kwargs):
+    def volume(self, music=None, value=100, fade=0, delta=False, **kwargs):
         if music is not None:
-            music.set_volume(value, delta=delta)
+            music.set_volume(value, delta=delta, fade=fade)
         else:
-            self.mapping.set_master_volume(value, delta=delta)
+            self.mapping.set_master_volume(value, delta=delta, fade=fade)
+
+    def wait(self, duration=0, music=None, set_wait_id=None, **kwargs):
+        if set_wait_id is not None:
+            self.mapping.add_wait_id(set_wait_id, self)
 
-    def wait(self, duration=0, music=None, **kwargs):
         self.sleep_event = threading.Event()
+        self.sleep_event_timer = threading.Timer(duration, self.sleep_event.set)
 
         if music is not None:
             music.wait_end()
 
-        threading.Timer(duration, self.sleep_event.set).start()
+        self.sleep_event_timer.start()
         self.sleep_event.wait()
 
     # Action messages
     def command_print(self, command="", **kwargs):
         return "running command {}".format(command)
 
+    def interrupt_wait_print(self, wait_id=None, **kwargs):
+        return "interrupt wait with id {}".format(wait_id)
+
     def pause_print(self, music=None, **kwargs):
         if music is not None:
             return "pausing « {} »".format(music.name)
@@ -159,7 +247,9 @@ class Action:
 
         return message
 
-    def stop_print(self, music=None, fade_out=0, wait=False, **kwargs):
+    def stop_print(self, music=None, fade_out=0, wait=False,
+            set_wait_id=None, **kwargs):
+
         message = "stopping "
         if music is not None:
             message += "music « {} »".format(music.name)
@@ -169,7 +259,11 @@ class Action:
         if fade_out > 0:
             message += " with {}s fadeout".format(fade_out)
             if wait:
-                message += " (waiting the end of fadeout)"
+                if set_wait_id is not None:
+                    message += " (waiting the end of fadeout, with id {})"\
+                            .format(set_wait_id)
+                else:
+                    message += " (waiting the end of fadeout)"
 
         return message
 
@@ -192,38 +286,55 @@ class Action:
                 return "moving all musics to position {}s" \
                         .format(value)
 
-    def volume_print(self, music=None, value=100, delta=False, **kwargs):
+    def volume_print(self, music=None,
+            value=100, delta=False, fade=0, **kwargs):
+        message = ""
         if delta:
             if music is not None:
-                return "{:+d}% to volume of « {} »" \
+                message += "{:+d}% to volume of « {} »" \
                         .format(value, music.name)
             else:
-                return "{:+d}% to volume" \
+                message += "{:+d}% to volume" \
                         .format(value)
         else:
             if music is not None:
-                return "setting volume of « {} » to {}%" \
+                message += "setting volume of « {} » to {}%" \
                         .format(music.name, value)
             else:
-                return "setting volume to {}%" \
+                message += "setting volume to {}%" \
                         .format(value)
 
-    def wait_print(self, duration=0, music=None, **kwargs):
+        if fade > 0:
+            message += " with {}s fade".format(fade)
+
+        return message
+
+    def wait_print(self, duration=0, music=None, set_wait_id=None, **kwargs):
+        message = ""
         if music is None:
-            return "waiting {}s" \
+            message += "waiting {}s" \
                     .format(duration)
         elif duration == 0:
-            return "waiting the end of « {} »" \
+            message += "waiting the end of « {} »" \
                     .format(music.name)
         else:
-            return "waiting the end of « {} » + {}s" \
+            message += "waiting the end of « {} » + {}s" \
                     .format(music.name, duration)
 
+        if set_wait_id is not None:
+            message += " (setting id = {})".format(set_wait_id)
+
+        return message
 
-    # Interruptions
+    # Interruptions (only for non-"atomic" actions)
     def wait_interrupt(self, duration=0, music=None, **kwargs):
         if self.sleep_event is not None:
             self.sleep_event.set()
+            self.sleep_event_timer.cancel()
         if music is not None:
             music.wait_event.set()
 
+    def stop_interrupt(self, music=None, fade_out=0, wait=False,
+            set_wait_id=None, **kwargs):
+        if self.waiting_music is not None:
+            self.waiting_music.wait_event.set()