diff options
Diffstat (limited to 'lib')
| -rwxr-xr-x | lib/python/qmk/cli/new/keymap.py | 12 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/via2json.py | 34 |
2 files changed, 37 insertions, 9 deletions
diff --git a/lib/python/qmk/cli/new/keymap.py b/lib/python/qmk/cli/new/keymap.py index d4339bc9ef..f1df05636c 100755 --- a/lib/python/qmk/cli/new/keymap.py +++ b/lib/python/qmk/cli/new/keymap.py | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | """This script automates the copying of the default keymap into your own keymap. | 1 | """This script automates the copying of the default keymap into your own keymap. |
| 2 | """ | 2 | """ |
| 3 | import re | ||
| 3 | import shutil | 4 | import shutil |
| 4 | 5 | ||
| 5 | from milc import cli | 6 | from milc import cli |
| @@ -13,6 +14,13 @@ from qmk.keyboard import keyboard_completer, keyboard_folder | |||
| 13 | from qmk.userspace import UserspaceDefs | 14 | from qmk.userspace import UserspaceDefs |
| 14 | 15 | ||
| 15 | 16 | ||
| 17 | def validate_keymap_name(name): | ||
| 18 | """Returns True if the given keymap name contains only a-z, 0-9 and underscore characters. | ||
| 19 | """ | ||
| 20 | regex = re.compile(r'^[a-zA-Z0-9][a-zA-Z0-9_]+$') | ||
| 21 | return bool(regex.match(name)) | ||
| 22 | |||
| 23 | |||
| 16 | def prompt_keyboard(): | 24 | def prompt_keyboard(): |
| 17 | prompt = """{fg_yellow}Select Keyboard{style_reset_all} | 25 | prompt = """{fg_yellow}Select Keyboard{style_reset_all} |
| 18 | If you`re unsure you can view a full list of supported keyboards with {fg_yellow}qmk list-keyboards{style_reset_all}. | 26 | If you`re unsure you can view a full list of supported keyboards with {fg_yellow}qmk list-keyboards{style_reset_all}. |
| @@ -60,6 +68,10 @@ def new_keymap(cli): | |||
| 60 | cli.log.error(f'Default keymap {{fg_cyan}}{keymap_path_default}{{fg_reset}} does not exist!') | 68 | cli.log.error(f'Default keymap {{fg_cyan}}{keymap_path_default}{{fg_reset}} does not exist!') |
| 61 | return False | 69 | return False |
| 62 | 70 | ||
| 71 | if not validate_keymap_name(user_name): | ||
| 72 | cli.log.error('Keymap names must contain only {fg_cyan}a-z{fg_reset}, {fg_cyan}0-9{fg_reset} and {fg_cyan}_{fg_reset}! Please choose a different name.') | ||
| 73 | return False | ||
| 74 | |||
| 63 | if keymap_path_new.exists(): | 75 | if keymap_path_new.exists(): |
| 64 | cli.log.error(f'Keymap {{fg_cyan}}{user_name}{{fg_reset}} already exists! Please choose a different name.') | 76 | cli.log.error(f'Keymap {{fg_cyan}}{user_name}{{fg_reset}} already exists! Please choose a different name.') |
| 65 | return False | 77 | return False |
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 | ||
