X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=music_sampler%2Fmapping.py;h=e589ac0a93edfdb234587e25ab6cb1d15a4d7d1b;hb=ca3264c828e5220e9d729152572de7a4600149b4;hp=bb20e679b1e4a9eb8312e0404a913411b178e93c;hpb=63ba5a8dc2aa4ec3e6f203b0ba4db249ecf0b00e;p=perso%2FImmae%2FProjets%2FPython%2FMusicSampler.git diff --git a/music_sampler/mapping.py b/music_sampler/mapping.py index bb20e67..e589ac0 100644 --- a/music_sampler/mapping.py +++ b/music_sampler/mapping.py @@ -12,7 +12,7 @@ from transitions.extensions import HierarchicalMachine as Machine from .music_file import MusicFile from .mixer import Mixer -from . import Config, gain, error_print, warn_print +from .helpers import Config, gain, error_print, warn_print from .action import Action class Mapping(RelativeLayout): @@ -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 = [] @@ -306,12 +312,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']