Skip to content

Commit

Permalink
Refactor merge_profiles method to use new control structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexeh committed Jan 30, 2024
1 parent a51aebd commit 787d012
Showing 1 changed file with 23 additions and 29 deletions.
52 changes: 23 additions & 29 deletions joystick_diagrams/input/profile.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from copy import deepcopy

from joystick_diagrams.input.button import Button
from joystick_diagrams.input.device import Device_

_logger = logging.getLogger("__name__")
Expand Down Expand Up @@ -49,20 +50,21 @@ def merge_profiles(self, profile: "Profile_"):
else:
# If the device exists in the current profile, merge inputs
existing_device = src_profile.devices[guid]
for input_id, input_ in device.inputs.items():
if input_id not in existing_device.inputs:
# If the input is not in the existing device, deepcopy the input
existing_device.inputs[input_id] = deepcopy(input_)
else:
# If the input exists, merge modifiers
existing_input = existing_device.inputs[input_id]
for modifier in input_.modifiers:
existing_modifier = existing_input._check_existing_modifier(modifier.modifiers)
if existing_modifier is None:
existing_input.modifiers.append(deepcopy(modifier))
else:
# If the modifier exists, update the command with the supplied profile value
existing_modifier.command = modifier.command
for input_type, inputs in device.inputs.items():
for input_key, input_ in inputs.items():
if input_key not in existing_device.inputs[input_type]:
# If the input is not in the existing device, deepcopy the input
existing_device.inputs[input_type][input_key] = deepcopy(input_)
else:
# If the input exists, merge modifiers
existing_input = existing_device.inputs[input_type][input_key]
for modifier in input_.modifiers:
existing_modifier = existing_input._check_existing_modifier(modifier.modifiers)
if existing_modifier is None:
existing_input.modifiers.append(deepcopy(modifier))
else:
# If the modifier exists, update the command with the supplied profile value
existing_modifier.command = modifier.command

return src_profile

Expand All @@ -71,24 +73,16 @@ def merge_profiles(self, profile: "Profile_"):
profile1 = Profile_("Profile1")

dev1 = profile1.add_device("dev_1", "dev_1")
dev2 = profile1.add_device("dev_2", "dev_2")
dev1.create_input(Button(1), "shoot")

dev1.create_input("input1", "shoot")
dev2.create_input("input2", "fly")

dev1.add_modifier_to_input("input1", {"ctrl"}, "bang")
dev1.add_modifier_to_input("input1", {"alt"}, "bang again")
dev1.add_modifier_to_input(Button(1), {"ctrl"}, "bang")
dev1.add_modifier_to_input(Button(1), {"alt"}, "bang again")

profile2 = Profile_("Profile2")

dev3 = profile2.add_device("dev_1", "dev_1")
dev4 = profile2.add_device("dev_2", "dev_2")

dev3.create_input("input1", "potato")

dev3.add_modifier_to_input("input1", {"ctrl"}, "hello")
dev1.add_modifier_to_input("input1", {"ctrl", "alt", "space"}, "bang again again")

dev4.create_input("input4", "another")
dev3.create_input(Button(3), "potato")
dev3.add_modifier_to_input(Button(1), {"ctrl"}, "hello")

profile1.merge_profiles(profile2)
data = profile1.merge_profiles(profile2)
print(data)

0 comments on commit 787d012

Please sign in to comment.