From 4c9052ed31c9949d66f4ae2e1298ac8d15a78ea5 Mon Sep 17 00:00:00 2001 From: Rob <5183487+Rexeh@users.noreply.github.com> Date: Fri, 23 Feb 2024 14:11:25 +0000 Subject: [PATCH] Export cleaning up modifiers, modified template to detect all variants --- joystick_diagrams/export.py | 7 ++++--- joystick_diagrams/template.py | 20 ++++++++++++++++++-- tests/data/template_test.svg | 2 +- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/joystick_diagrams/export.py b/joystick_diagrams/export.py index c514b4c..688000c 100644 --- a/joystick_diagrams/export.py +++ b/joystick_diagrams/export.py @@ -18,7 +18,6 @@ _logger = logging.getLogger(__name__) - TEMPLATE_NAMING_KEY = "TEMPLATE_NAME" TEMPLATE_DATING_KEY = "CURRENT_DATE" @@ -71,12 +70,10 @@ def populate_template(export_device: ExportDevice) -> str: ) if input_object.modifiers: - # TODO optimise only if we know the template uses these modified_template_data = replace_input_modifiers_string( input_key, input_object.modifiers, modified_template_data ) - # TODO Optimise to only run if we need to for modifier_number, modifier in enumerate(input_object.modifiers, 1): modified_template_data = replace_input_modifier_id_key( input_key, modifier_number, modifier, modified_template_data @@ -113,6 +110,7 @@ def replace_input_modifiers_string( def replace_input_modifier_id_key( input_key: str, modifier_number: int, modifier: Modifier, data: str ) -> str: + """Replaces instances where a particular Modifier key has been used, either with an overall ID, or with specific ID/Value combinations""" # Handle INPUT_KEY_MODIFIER_X search = re.compile(rf"\b{input_key}_Modifier_{modifier_number}\b", re.IGNORECASE) replacement = f"{modifier.modifiers} - {modifier.command}" @@ -148,6 +146,7 @@ def replace_input_string(search_key: str, replacement: str, data: str) -> str: def replace_unused_keys(data: str) -> str: """Replaces all unused keys in the template with default values""" search_keys = [Template.BUTTON_KEY, Template.AXIS_KEY, Template.HAT_KEY] + search_keys.extend(Template.MODIFIER_KEYS) def find_keys(search_keys: list[re.Pattern]) -> list[str]: found_keys = [] @@ -167,12 +166,14 @@ def find_keys(search_keys: list[re.Pattern]) -> list[str]: def replace_template_date_string(data: str) -> str: + """Basic replacement of the key with a date at time of run""" search = re.compile(rf"\b{TEMPLATE_DATING_KEY}\b", re.IGNORECASE) return re.sub(search, datetime.now().strftime("%d/%m/%Y"), data) def replace_template_name_string(replacement: str, data: str) -> str: + """Basic replacement of the key with a replacement as name""" search = re.compile(rf"\b{TEMPLATE_NAMING_KEY}\b", re.IGNORECASE) return re.sub(search, replacement, data) diff --git a/joystick_diagrams/template.py b/joystick_diagrams/template.py index 822d179..985324c 100644 --- a/joystick_diagrams/template.py +++ b/joystick_diagrams/template.py @@ -15,7 +15,17 @@ class Template: BUTTON_KEY = re.compile(r"\bBUTTON_\d+\b", flags=re.IGNORECASE) - MODIFIER_KEY = re.compile(r"\b[a-zA-Z]+_\d+_Modifier_\d+", flags=re.IGNORECASE) + + # Modifiers + MODIFIER_KEYS = [ + # All Modifiers Key + re.compile(r"\b[a-zA-Z]+_\d+_Modifiers", flags=re.IGNORECASE), + # Handles Modifier_X + re.compile(r"\b[a-zA-Z]+_\d+_Modifier_\d+", flags=re.IGNORECASE), + # Handles Specific Keys Modifier_X_Item + re.compile(r"\b[a-zA-Z]+_\d+_Modifier_\d+_[a-zA-Z]+", flags=re.IGNORECASE), + ] + HAT_KEY = re.compile(r"\bPOV_\d+_[URDL]+\b", flags=re.IGNORECASE) AXIS_KEY = re.compile(r"\bAXIS_[a-zA-Z]+_?\d?+\b", flags=re.IGNORECASE) TEMPLATE_NAMING_KEY = re.compile(r"\bTEMPLATE_NAME\b", flags=re.IGNORECASE) @@ -38,7 +48,13 @@ def get_template_data(self, template_path: Path): def get_template_modifiers(self) -> set[str]: "Returns the available MODIFIER NUMBERS supported for a given CONTROL from the template" - return {x.lower() for x in re.findall(self.MODIFIER_KEY, self.raw_data)} + + result = [] + for modifier_search_key in self.MODIFIER_KEYS: + matches = re.findall(modifier_search_key, self.raw_data) + result.extend(matches) + + return {x.lower() for x in result} def get_template_hats(self) -> set[str]: "Returns the available HAT controls from the template" diff --git a/tests/data/template_test.svg b/tests/data/template_test.svg index 336bc2f..38d913b 100644 --- a/tests/data/template_test.svg +++ b/tests/data/template_test.svg @@ -1,4 +1,4 @@ -
AXIS_X
AXIS_Y
AXIS_RX
AXIS_SLIDER_1
POV_1_U
POV_1_D
POV_2_U
AXIS TESTS
POV TESTS
POV_1_L
POV_1_R
POV_1_UR
POV_1_DR
POV_1_DL
POV_1_UL
POV_2_D
POV_2_R
POV_2_L
BUTTON TESTS
BUTTON_1
button_1
BUTTON_2
BUTTON_10
MODIFIER TESTS
BUTTON_1_MOD_1_KEY
BUTTON_1_MOD_ACTION
BUTTON_1_MOD_2_KEY
BUTTON_1_MOD__2_ACTION
button_2_mod_1_key
button_2_mod_1_action
TEMPLATE_NAME 
ADDITIONAL ATTRIBUTES
CURRENT_DATE
axis_z
axis_x
pov_2_r
button_1_mod_1_key
+
AXIS_X
AXIS_X
AXIS_Y
AXIS_Y
AXIS_RX
AXIS_RX
AXIS_SLIDER_1
AXIS_SLIDER_1
POV_1_U
POV_1_U
POV_1_D
POV_1_D
POV_2_U
POV_2_U
AXIS TESTS
AXIS TESTS
POV TESTS
POV TESTS
POV_1_L
POV_1_L
POV_1_R
POV_1_R
POV_1_UR
POV_1_UR
POV_1_DR
POV_1_DR
POV_1_DL
POV_1_DL
POV_1_UL
POV_1_UL
POV_2_D
POV_2_D
POV_2_R
POV_2_R
POV_2_L
POV_2_L
BUTTON TESTS
BUTTON TESTS
BUTTON_1
BUTTON_1
button_1
button_1
BUTTON_2
BUTTON_2
BUTTON_10
BUTTON_10
MODIFIER TESTS
MODIFIER TESTS
BUTTON_1_MODIFIER_1_KEY
BUTTON_1_MODIFIER_1_KEY
BUTTON_1_MODIFIER_1_ACTION
BUTTON_1_MODIFIER_1_ACTION
BUTTON_1_MODIFIER_2_KEY
BUTTON_1_MODIFIER_2_KEY
BUTTON_1_MODIFIER_2_ACTION
BUTTON_1_MODIFIER_2_ACTION
BUTTON_1_MODIFIERS
BUTTON_1_MODIFIERS
BUTTON_2_MODIFIERS
BUTTON_2_MODIFIERS
TEMPLATE_NAME 
TEMPLATE_NAME 
ADDITIONAL ATTRIBUTES
ADDITIONAL ATTRIBUTES
CURRENT_DATE
CURRENT_DATE
axis_z
axis_z
axis_x
axis_x
pov_2_r
pov_2_r
button_1_modifier_1_key
button_1_modifier_1_key
Text is not SVG - cannot display