]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blobdiff - music_sampler/mapping.py
Add a 'common' section in key properties
[perso/Immae/Projets/Python/MusicSampler.git] / music_sampler / mapping.py
index 74feec886f6f37b623b484314ac0012b927abafb..50b68a9d903df95b0236cf76b4c39245628e9403 100644 (file)
@@ -124,22 +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.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()
-
+            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 = []
@@ -222,7 +224,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))
 
@@ -334,6 +337,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": {},
@@ -341,6 +352,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):
@@ -349,6 +363,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