From bbb12e3f2002340e990c0bdeb3e53e12fbf3425f Mon Sep 17 00:00:00 2001 From: Alexandre Simoes Tavares <13024466+alex-smtv@users.noreply.github.com> Date: Thu, 7 Apr 2022 03:19:18 +0200 Subject: [PATCH] Add unload feature on invalid profile switching --- src/loader/profile_loader.py | 66 +++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/src/loader/profile_loader.py b/src/loader/profile_loader.py index a0d2c9d..efd450e 100644 --- a/src/loader/profile_loader.py +++ b/src/loader/profile_loader.py @@ -72,6 +72,11 @@ def _watch_profile(self): message = 'Profile Selection Watcher >> The following profile is selected, but could not be found: ' + potential_selected_file FreePieVars.diagnostics.debug(message) FreePieVars.diagnostics.notify(message) + + self._is_profile_selection_changed = True + self._active_profile = None + self._active_profile_path = None + self._pretty_profile_print = None return # If the selected profile file is the same as the current active profile, then nothing to do @@ -204,45 +209,50 @@ def __init__(self): def _profile_load(self, profile_file_name, pretty_profile_print = None): # TODO: Check FreePieVars is correctly setup, this handle the case where the user is modifying a line he shouldn't - profile_module = 'profiles.' + profile_file_name + # None case: a default state will be loaded (no mappings) + if profile_file_name is None: + self.joys_mappings = [] + + else: + profile_module = 'profiles.' + profile_file_name - # Reload a profile already loaded in FreePie so we can account for any change made - # in the profile. - # WARNING: be mindful of reload subtlety https://stackoverflow.com/a/7274356 - # From all the registered modules, if the module is related to the profile we need then reload it. - for key, value in sys.modules.items(): - if value is not None and profile_module in str(value): - reload(sys.modules[key]) + # Reload a profile already loaded in FreePie so we can account for any change made + # in the profile. + # WARNING: be mindful of reload subtlety https://stackoverflow.com/a/7274356 + # From all the registered modules, if the module is related to the profile we need then reload it. + for key, value in sys.modules.items(): + if value is not None and profile_module in str(value): + reload(sys.modules[key]) - exec 'import ' + profile_module + exec 'import ' + profile_module - candidates_func = [] + candidates_func = [] - for member in dir(eval(profile_module)): - member_access = eval(profile_module + '.' + member) + for member in dir(eval(profile_module)): + member_access = eval(profile_module + '.' + member) - if member.endswith('_mapping') and callable(member_access): - candidates_func.append(member_access) + if member.endswith('_mapping') and callable(member_access): + candidates_func.append(member_access) - joys_mappings = [] + joys_mappings = [] - for mapping_func in candidates_func: - joys_mappings.append( - mapping_func().build( - FreePieVars.joysticks, - FreePieVars.vjoys, - FreePieVars.keyboard, - FreePieVars.speech + for mapping_func in candidates_func: + joys_mappings.append( + mapping_func().build( + FreePieVars.joysticks, + FreePieVars.vjoys, + FreePieVars.keyboard, + FreePieVars.speech + ) ) - ) - self.joys_mappings = tuple_it_if_needed(joys_mappings) + self.joys_mappings = tuple_it_if_needed(joys_mappings) - profile_print = profile_file_name if pretty_profile_print is None else pretty_profile_print - message = 'Profile ' + profile_print + ' loaded.' + profile_print = profile_file_name if pretty_profile_print is None else pretty_profile_print + message = 'Profile ' + profile_print + ' loaded.' - FreePieVars.diagnostics.notify(message) - FreePieVars.diagnostics.debug(message) + FreePieVars.diagnostics.notify(message) + FreePieVars.diagnostics.debug(message) # At end free up the memory gc.collect()