diff options
| author | Ryan <fauxpark@gmail.com> | 2024-11-21 17:20:05 +1100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-21 17:20:05 +1100 |
| commit | 9bea332a210aed22cd6775eaf726da595bd5e9ce (patch) | |
| tree | 0968fe29d0eed4f4054f09f0754db4d624055a13 /lib/python | |
| parent | 8cbcdcac62d58ff8faadfdb29bafbd12bef4d7ed (diff) | |
`qmk via2json`: Improve macro parsing (#24345)
Diffstat (limited to 'lib/python')
| -rwxr-xr-x | lib/python/qmk/cli/via2json.py | 34 |
1 files changed, 25 insertions, 9 deletions
diff --git a/lib/python/qmk/cli/via2json.py b/lib/python/qmk/cli/via2json.py index 73c9a61b3d..537e102640 100755 --- a/lib/python/qmk/cli/via2json.py +++ b/lib/python/qmk/cli/via2json.py | |||
| @@ -29,6 +29,7 @@ def _convert_macros(via_macros): | |||
| 29 | if len(via_macros) == 0: | 29 | if len(via_macros) == 0: |
| 30 | return list() | 30 | return list() |
| 31 | split_regex = re.compile(r'(}\,)|(\,{)') | 31 | split_regex = re.compile(r'(}\,)|(\,{)') |
| 32 | macro_group_regex = re.compile(r'({.+?})') | ||
| 32 | macros = list() | 33 | macros = list() |
| 33 | for via_macro in via_macros: | 34 | for via_macro in via_macros: |
| 34 | # Split VIA macro to its elements | 35 | # Split VIA macro to its elements |
| @@ -38,13 +39,28 @@ def _convert_macros(via_macros): | |||
| 38 | macro_data = list() | 39 | macro_data = list() |
| 39 | for m in macro: | 40 | for m in macro: |
| 40 | if '{' in m or '}' in m: | 41 | if '{' in m or '}' in m: |
| 41 | # Found keycode(s) | 42 | # Split macro groups |
| 42 | keycodes = m.split(',') | 43 | macro_groups = macro_group_regex.findall(m) |
| 43 | # Remove whitespaces and curly braces from around keycodes | 44 | for macro_group in macro_groups: |
| 44 | keycodes = list(map(lambda s: s.strip(' {}'), keycodes)) | 45 | # Remove whitespaces and curly braces from around group |
| 45 | # Remove the KC prefix | 46 | macro_group = macro_group.strip(' {}') |
| 46 | keycodes = list(map(lambda s: s.replace('KC_', ''), keycodes)) | 47 | |
| 47 | macro_data.append({"action": "tap", "keycodes": keycodes}) | 48 | macro_action = 'tap' |
| 49 | macro_keycodes = [] | ||
| 50 | |||
| 51 | if macro_group[0] == '+': | ||
| 52 | macro_action = 'down' | ||
| 53 | macro_keycodes.append(macro_group[1:]) | ||
| 54 | elif macro_group[0] == '-': | ||
| 55 | macro_action = 'up' | ||
| 56 | macro_keycodes.append(macro_group[1:]) | ||
| 57 | else: | ||
| 58 | macro_keycodes.extend(macro_group.split(',') if ',' in macro_group else [macro_group]) | ||
| 59 | |||
| 60 | # Remove the KC prefixes | ||
| 61 | macro_keycodes = list(map(lambda s: s.replace('KC_', ''), macro_keycodes)) | ||
| 62 | |||
| 63 | macro_data.append({"action": macro_action, "keycodes": macro_keycodes}) | ||
| 48 | else: | 64 | else: |
| 49 | # Found text | 65 | # Found text |
| 50 | macro_data.append(m) | 66 | macro_data.append(m) |
| @@ -54,13 +70,13 @@ def _convert_macros(via_macros): | |||
| 54 | 70 | ||
| 55 | 71 | ||
| 56 | def _fix_macro_keys(keymap_data): | 72 | def _fix_macro_keys(keymap_data): |
| 57 | macro_no = re.compile(r'MACRO0?([0-9]{1,2})') | 73 | macro_no = re.compile(r'MACRO0?\(([0-9]{1,2})\)') |
| 58 | for i in range(0, len(keymap_data)): | 74 | for i in range(0, len(keymap_data)): |
| 59 | for j in range(0, len(keymap_data[i])): | 75 | for j in range(0, len(keymap_data[i])): |
| 60 | kc = keymap_data[i][j] | 76 | kc = keymap_data[i][j] |
| 61 | m = macro_no.match(kc) | 77 | m = macro_no.match(kc) |
| 62 | if m: | 78 | if m: |
| 63 | keymap_data[i][j] = f'MACRO_{m.group(1)}' | 79 | keymap_data[i][j] = f'MC_{m.group(1)}' |
| 64 | return keymap_data | 80 | return keymap_data |
| 65 | 81 | ||
| 66 | 82 | ||
