summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorQMK Bot <hello@qmk.fm>2024-11-21 06:22:51 +0000
committerQMK Bot <hello@qmk.fm>2024-11-21 06:22:51 +0000
commit074bbbfb2129c738a3abfcdcb3e754c7ffcd1b04 (patch)
tree330c9e3494ee9c040f2f81681a2749bd8bd70f72
parent638b22d05778f30c1f2bd347e48d523a7858bb51 (diff)
parent9bea332a210aed22cd6775eaf726da595bd5e9ce (diff)
Merge remote-tracking branch 'origin/master' into develop
-rwxr-xr-xlib/python/qmk/cli/new/keymap.py12
-rwxr-xr-xlib/python/qmk/cli/via2json.py34
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"""
3import re
3import shutil 4import shutil
4 5
5from milc import cli 6from milc import cli
@@ -13,6 +14,13 @@ from qmk.keyboard import keyboard_completer, keyboard_folder
13from qmk.userspace import UserspaceDefs 14from qmk.userspace import UserspaceDefs
14 15
15 16
17def 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
16def prompt_keyboard(): 24def prompt_keyboard():
17 prompt = """{fg_yellow}Select Keyboard{style_reset_all} 25 prompt = """{fg_yellow}Select Keyboard{style_reset_all}
18If you`re unsure you can view a full list of supported keyboards with {fg_yellow}qmk list-keyboards{style_reset_all}. 26If 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
56def _fix_macro_keys(keymap_data): 72def _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