From ccec129b3715e4596560b937ee5b5f28aa461614 Mon Sep 17 00:00:00 2001 From: fauxpark Date: Fri, 5 Apr 2024 13:51:36 +1100 Subject: [PATCH 1/2] `qmk new-keymap`: validate keymap name --- lib/python/qmk/cli/new/keymap.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/python/qmk/cli/new/keymap.py b/lib/python/qmk/cli/new/keymap.py index d4339bc9ef00..79e7dc821e1e 100755 --- a/lib/python/qmk/cli/new/keymap.py +++ b/lib/python/qmk/cli/new/keymap.py @@ -1,5 +1,6 @@ """This script automates the copying of the default keymap into your own keymap. """ +import re import shutil from milc import cli @@ -13,6 +14,13 @@ from qmk.userspace import UserspaceDefs +def validate_keymap_name(name): + """Returns True if the given keymap name contains only lowercase a-z, 0-9 and underscore characters. + """ + regex = re.compile(r'^[a-z0-9][a-z0-9_]+$') + return bool(regex.match(name)) + + def prompt_keyboard(): prompt = """{fg_yellow}Select Keyboard{style_reset_all} 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): cli.log.error(f'Default keymap {{fg_cyan}}{keymap_path_default}{{fg_reset}} does not exist!') return False + if not validate_keymap_name(user_name): + cli.log.error('Keymap names must contain only {fg_cyan}lowercase a-z{fg_reset}, {fg_cyan}0-9{fg_reset} and {fg_cyan}_{fg_reset}! Please choose a different name.') + return False + if keymap_path_new.exists(): cli.log.error(f'Keymap {{fg_cyan}}{user_name}{{fg_reset}} already exists! Please choose a different name.') return False From b7f59bbe4b089af4413783fc577f78f30127c049 Mon Sep 17 00:00:00 2001 From: fauxpark Date: Mon, 15 Jul 2024 12:21:27 +1000 Subject: [PATCH 2/2] Allow uppercase for the time being --- lib/python/qmk/cli/new/keymap.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/python/qmk/cli/new/keymap.py b/lib/python/qmk/cli/new/keymap.py index 79e7dc821e1e..f1df05636ce3 100755 --- a/lib/python/qmk/cli/new/keymap.py +++ b/lib/python/qmk/cli/new/keymap.py @@ -15,9 +15,9 @@ def validate_keymap_name(name): - """Returns True if the given keymap name contains only lowercase a-z, 0-9 and underscore characters. + """Returns True if the given keymap name contains only a-z, 0-9 and underscore characters. """ - regex = re.compile(r'^[a-z0-9][a-z0-9_]+$') + regex = re.compile(r'^[a-zA-Z0-9][a-zA-Z0-9_]+$') return bool(regex.match(name)) @@ -69,7 +69,7 @@ def new_keymap(cli): return False if not validate_keymap_name(user_name): - cli.log.error('Keymap names must contain only {fg_cyan}lowercase a-z{fg_reset}, {fg_cyan}0-9{fg_reset} and {fg_cyan}_{fg_reset}! Please choose a different name.') + 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.') return False if keymap_path_new.exists():