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_XAXIS_YAXIS_RXAXIS_SLIDER_1POV_1_UPOV_1_DPOV_2_UAXIS TESTSPOV TESTSPOV_1_LPOV_1_RPOV_1_URPOV_1_DRPOV_1_DLPOV_1_ULPOV_2_DPOV_2_RPOV_2_LBUTTON TESTSBUTTON_1button_1BUTTON_2BUTTON_10MODIFIER TESTSBUTTON_1_MOD_1_KEYBUTTON_1_MOD_ACTIONBUTTON_1_MOD_2_KEYBUTTON_1_MOD__2_ACTIONbutton_2_mod_1_keybutton_2_mod_1_actionTEMPLATE_NAME ADDITIONAL ATTRIBUTESCURRENT_DATEaxis_zaxis_xpov_2_rbutton_1_mod_1_key +AXIS_XAXIS_XAXIS_YAXIS_YAXIS_RXAXIS_RXAXIS_SLIDER_1AXIS_SLIDER_1POV_1_UPOV_1_UPOV_1_DPOV_1_DPOV_2_UPOV_2_UAXIS TESTSAXIS TESTSPOV TESTSPOV TESTSPOV_1_LPOV_1_LPOV_1_RPOV_1_RPOV_1_URPOV_1_URPOV_1_DRPOV_1_DRPOV_1_DLPOV_1_DLPOV_1_ULPOV_1_ULPOV_2_DPOV_2_DPOV_2_RPOV_2_RPOV_2_LPOV_2_LBUTTON TESTSBUTTON TESTSBUTTON_1BUTTON_1button_1button_1BUTTON_2BUTTON_2BUTTON_10BUTTON_10MODIFIER TESTSMODIFIER TESTSBUTTON_1_MODIFIER_1_KEYBUTTON_1_MODIFIER_1_KEYBUTTON_1_MODIFIER_1_ACTIONBUTTON_1_MODIFIER_1_ACTIONBUTTON_1_MODIFIER_2_KEYBUTTON_1_MODIFIER_2_KEYBUTTON_1_MODIFIER_2_ACTIONBUTTON_1_MODIFIER_2_ACTIONBUTTON_1_MODIFIERSBUTTON_1_MODIFIERSBUTTON_2_MODIFIERSBUTTON_2_MODIFIERSTEMPLATE_NAME TEMPLATE_NAME ADDITIONAL ATTRIBUTESADDITIONAL ATTRIBUTESCURRENT_DATECURRENT_DATEaxis_zaxis_zaxis_xaxis_xpov_2_rpov_2_rbutton_1_modifier_1_keybutton_1_modifier_1_keyText is not SVG - cannot display