]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blobdiff - music_sampler/mapping.py
Make 'interrupt_wait' able to interrupt all waits
[perso/Immae/Projets/Python/MusicSampler.git] / music_sampler / mapping.py
index ca471ef7115a3762870c3e5177e996a9c56273af..a04c2f6ec550021dacc65ec891a4d4e7a5f6f731 100644 (file)
@@ -93,8 +93,7 @@ class Mapping(RelativeLayout):
             self.key_config, self.open_files = self.parse_config()
         except Exception as e:
             error_print("Error while loading configuration: {}".format(e),
-                    with_trace=True)
-            sys.exit()
+                    with_trace=True, exit=True)
         else:
             self.success()
 
@@ -125,17 +124,24 @@ class Mapping(RelativeLayout):
             threading.Thread(name="MSKeyAction", target=key.run,
                     args=['-'.join(modifiers)]).start()
         elif 'ctrl' in modifiers and (keycode[0] == 113 or keycode[0] == '99'):
-            self.stop_all_running()
-            for thread in threading.enumerate():
-                if thread.getName()[0:2] != "MS":
-                    continue
-                thread.join()
-
+            self.leave_application()
             sys.exit()
         elif 'ctrl' in modifiers and keycode[0] == 114:
             threading.Thread(name="MSReload", target=self.reload).start()
         return True
 
+    def leave_application(self):
+        self.keyboard.unbind(on_key_down=self.on_keyboard_down)
+        self.stop_all_running()
+        for music in self.open_files.values():
+            music.stop()
+        for thread in threading.enumerate():
+            if thread.getName()[0:2] == "MS":
+                thread.join()
+            elif thread.__class__ == threading.Timer:
+                thread.cancel()
+                thread.join()
+
     # Helpers
     def allowed_modifiers(self, modifiers):
         allowed = []
@@ -191,17 +197,30 @@ class Mapping(RelativeLayout):
             music.set_gain_with_effect(db_gain, fade=fade)
 
     # Wait handler methods
-    def add_wait_id(self, wait_id, action_or_wait):
-        self.wait_ids[wait_id] = action_or_wait
-
-    def interrupt_wait(self, wait_id):
-        if wait_id in self.wait_ids:
-            action_or_wait = self.wait_ids[wait_id]
-            del(self.wait_ids[wait_id])
-            if isinstance(action_or_wait, Action):
-                action_or_wait.interrupt()
-            else:
-                action_or_wait.set()
+    def add_wait(self, action_or_wait, wait_id=None):
+        if wait_id is not None:
+            self.wait_ids[wait_id] = [action_or_wait]
+        else:
+            if None not in self.wait_ids:
+                self.wait_ids[None] = []
+            self.wait_ids[None].append(action_or_wait)
+
+    def interrupt_wait(self, wait_id=None):
+        if wait_id is None:
+            ids_to_interrupt = list(self.wait_ids.keys())
+        elif wait_id in self.wait_ids:
+            ids_to_interrupt = [wait_id]
+        else:
+            ids_to_interrupt = []
+
+        for _wait_id in ids_to_interrupt:
+            action_or_waits = self.wait_ids[_wait_id]
+            del(self.wait_ids[_wait_id])
+            for action_or_wait in action_or_waits:
+                if isinstance(action_or_wait, Action):
+                    action_or_wait.interrupt()
+                else:
+                    action_or_wait.set()
 
     # Methods to control running keys
     def start_running(self, key, start_time):
@@ -218,7 +237,8 @@ class Mapping(RelativeLayout):
     def parse_config(self):
         def update_alias(prop_hash, aliases, key):
             if isinstance(aliases[key], dict):
-                prop_hash.update(aliases[key], **prop_hash)
+                for alias in aliases[key]:
+                    prop_hash.setdefault(alias, aliases[key][alias])
             else:
                 warn_print("Alias {} is not a hash, ignored".format(key))
 
@@ -306,12 +326,13 @@ class Mapping(RelativeLayout):
         try:
             config = yaml.safe_load(stream)
         except Exception as e:
-            error_print("Error while loading config file: {}".format(e))
-            sys.exit()
+            error_print("Error while loading config file: {}".format(e),
+                    exit=True)
         stream.close()
 
         if not isinstance(config, dict):
-            raise Exception("Top level config is supposed to be a hash")
+            error_print("Top level config is supposed to be a hash",
+                    exit=True)
 
         if 'aliases' in config and isinstance(config['aliases'], dict):
             aliases = config['aliases']
@@ -329,6 +350,14 @@ class Mapping(RelativeLayout):
 
         seen_files = {}
 
+        common_key_properties = {}
+        if 'common' in config['key_properties'] and\
+                isinstance(config['key_properties'], dict):
+            common_key_properties = config['key_properties']['common']
+            include_aliases(common_key_properties, aliases)
+        elif 'common' in config['key_properties']:
+            warn_print("'common' key in key_properties is not a hash, ignored")
+
         key_properties = defaultdict(lambda: {
                 "actions":    [],
                 "properties": {},
@@ -336,6 +365,9 @@ class Mapping(RelativeLayout):
             })
 
         for key in check_key_properties(config):
+            if key == 'common':
+                continue
+
             key_prop = config['key_properties'][key]
 
             if not isinstance(key_prop, dict):
@@ -344,6 +376,9 @@ class Mapping(RelativeLayout):
                 continue
 
             include_aliases(key_prop, aliases)
+            for _key in common_key_properties:
+                key_prop.setdefault(_key, common_key_properties[_key])
+
             check_key_property(key_prop, key)
 
             key_properties[key]["properties"] = key_prop