Skip to content

Commit

Permalink
Export cleaning up modifiers, modified template to detect all variants
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexeh committed Feb 23, 2024
1 parent fdeaa1e commit 4c9052e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
7 changes: 4 additions & 3 deletions joystick_diagrams/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

_logger = logging.getLogger(__name__)


TEMPLATE_NAMING_KEY = "TEMPLATE_NAME"
TEMPLATE_DATING_KEY = "CURRENT_DATE"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}"
Expand Down Expand Up @@ -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 = []
Expand All @@ -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)

Expand Down
20 changes: 18 additions & 2 deletions joystick_diagrams/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion tests/data/template_test.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4c9052e

Please sign in to comment.