Skip to content

Commit

Permalink
Add unload feature on invalid profile switching
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-smtv committed Apr 7, 2022
1 parent 2501def commit bbb12e3
Showing 1 changed file with 38 additions and 28 deletions.
66 changes: 38 additions & 28 deletions src/loader/profile_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit bbb12e3

Please sign in to comment.