]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blobdiff - music_sampler/mapping.py
Add config
[perso/Immae/Projets/Python/MusicSampler.git] / music_sampler / mapping.py
index 5c61f8a5be6eb318949400bfb80398e3cd03472a..fc9768b7c665b1bbd709e500ebcf5f04ce6b5b97 100644 (file)
@@ -72,9 +72,9 @@ class Mapping(RelativeLayout):
         self.keyboard = Window.request_keyboard(self.on_keyboard_closed, self)
         self.keyboard.bind(on_key_down=self.on_keyboard_down)
 
-        self.configure()
+        self.configure(initial=True)
 
-    def on_enter_configuring(self):
+    def on_enter_configuring(self, initial=True):
         if Config.builtin_mixing:
             self.mixer = Mixer()
         else:
@@ -84,9 +84,9 @@ 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, exit=True)
-        else:
-            self.success()
+                    with_trace=False, exit=initial)
+
+        self.success()
 
     def on_enter_loading(self):
         for key in self.keys:
@@ -118,7 +118,7 @@ class Mapping(RelativeLayout):
             self.leave_application()
             sys.exit()
         elif 'ctrl' in modifiers and keycode[0] == 114 and self.is_loaded():
-            self.reload()
+            self.reload(initial=False)
         return True
 
     def leave_application(self):
@@ -320,13 +320,60 @@ class Mapping(RelativeLayout):
             else:
                 return {}
 
-        def check_mapped_key(mapped_keys, key):
-            if not isinstance(mapped_keys[key], list):
+        def check_mapped_key(actions, key):
+            if not isinstance(actions, list):
                 warn_print("key config '{}' is not an array, ignored"
                         .format(key))
                 return []
             else:
-                return mapped_keys[key]
+                return actions
+
+        def append_actions_to_key(mapped_key, actions, aliases, seen_files, music_properties, key_properties):
+            for index, action in enumerate(check_mapped_key(actions, mapped_key)):
+                if not isinstance(action, dict) or\
+                        not len(action) == 1 or\
+                        not isinstance(list(action.values())[0] or {}, dict):
+                    warn_print("action number {} of key '{}' is invalid, "
+                            "ignored".format(index + 1, mapped_key))
+                    continue
+                append_action_to_key(action, mapped_key, aliases, seen_files, music_properties, key_properties)
+
+        def append_action_to_key(action, mapped_key, aliases, seen_files, music_properties, key_properties):
+            action_name = list(action)[0]
+            action_args = {}
+            if action[action_name] is None:
+                action[action_name] = {}
+
+            include_aliases(action[action_name], aliases)
+
+            for argument in action[action_name]:
+                if argument == 'file':
+                    filename = str(action[action_name]['file'])
+                    if filename not in seen_files:
+                        music_property = check_music_property(
+                                music_properties[filename],
+                                filename)
+
+                        if filename in self.open_files:
+                            self.open_files[filename]\
+                                    .reload_properties(**music_property)
+
+                            seen_files[filename] =\
+                                    self.open_files[filename]
+                        else:
+                            seen_files[filename] = MusicFile(
+                                    filename, self, **music_property)
+
+                    if filename not in key_properties[mapped_key]['files']:
+                        key_properties[mapped_key]['files'] \
+                                .append(seen_files[filename])
+
+                    action_args['music'] = seen_files[filename]
+                else:
+                    action_args[argument] = action[action_name][argument]
+
+            key_properties[mapped_key]['actions'] \
+                    .append([action_name, action_args])
 
         def check_music_property(music_property, filename):
             if not isinstance(music_property, dict):
@@ -344,17 +391,15 @@ class Mapping(RelativeLayout):
                             "a float, ignored".format(filename))
             return music_property
 
-        stream = open(Config.yml_file, "r")
+        stream = open(Config.yml_file, "r", encoding='utf8')
         try:
             config = yaml.safe_load(stream)
         except Exception as e:
-            error_print("Error while loading config file: {}".format(e),
-                    exit=True)
+            raise Exception("Error while loading config file: {}".format(e)) from e
         stream.close()
 
         if not isinstance(config, dict):
-            error_print("Top level config is supposed to be a hash",
-                    exit=True)
+            raise Exception("Top level config is supposed to be a hash")
 
         if 'aliases' in config and isinstance(config['aliases'], dict):
             aliases = config['aliases']
@@ -402,52 +447,11 @@ class Mapping(RelativeLayout):
             check_key_property(key_prop, key)
 
             key_properties[key]["properties"].update(key_prop)
+            if 'actions' in key_prop:
+                append_actions_to_key(key, key_prop['actions'], aliases, seen_files, music_properties, key_properties)
 
         for mapped_key in check_mapped_keys(config):
-            for index, action in enumerate(check_mapped_key(
-                    config['keys'], mapped_key)):
-                if not isinstance(action, dict) or\
-                        not len(action) == 1 or\
-                        not isinstance(list(action.values())[0] or {}, dict):
-                    warn_print("action number {} of key '{}' is invalid, "
-                            "ignored".format(index + 1, mapped_key))
-                    continue
-
-                action_name = list(action)[0]
-                action_args = {}
-                if action[action_name] is None:
-                    action[action_name] = {}
-
-                include_aliases(action[action_name], aliases)
-
-                for argument in action[action_name]:
-                    if argument == 'file':
-                        filename = str(action[action_name]['file'])
-                        if filename not in seen_files:
-                            music_property = check_music_property(
-                                    music_properties[filename],
-                                    filename)
-
-                            if filename in self.open_files:
-                                self.open_files[filename]\
-                                        .reload_properties(**music_property)
-
-                                seen_files[filename] =\
-                                        self.open_files[filename]
-                            else:
-                                seen_files[filename] = MusicFile(
-                                        filename, self, **music_property)
-
-                        if filename not in key_properties[mapped_key]['files']:
-                            key_properties[mapped_key]['files'] \
-                                    .append(seen_files[filename])
-
-                        action_args['music'] = seen_files[filename]
-                    else:
-                        action_args[argument] = action[action_name][argument]
-
-                key_properties[mapped_key]['actions'] \
-                        .append([action_name, action_args])
+            append_actions_to_key(mapped_key, config['keys'][mapped_key], aliases, seen_files, music_properties, key_properties)
 
         return (key_properties, seen_files)