summaryrefslogtreecommitdiffstats
path: root/lib/python
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python')
-rw-r--r--lib/python/qmk/cli/__init__.py5
-rw-r--r--lib/python/qmk/cli/doctor/check.py2
-rwxr-xr-xlib/python/qmk/cli/doctor/main.py2
-rw-r--r--lib/python/qmk/cli/flash.py46
-rwxr-xr-xlib/python/qmk/cli/generate/api.py9
-rw-r--r--lib/python/qmk/cli/generate/keycodes.py55
-rw-r--r--lib/python/qmk/cli/generate/keycodes_tests.py39
-rw-r--r--lib/python/qmk/cli/git/__init__.py0
-rw-r--r--lib/python/qmk/cli/git/submodule.py22
-rw-r--r--lib/python/qmk/cli/list/keyboards.py3
-rw-r--r--lib/python/qmk/cli/migrate.py81
-rw-r--r--lib/python/qmk/cli/new/keyboard.py10
-rwxr-xr-xlib/python/qmk/cli/new/keymap.py56
-rw-r--r--lib/python/qmk/flashers.py14
-rw-r--r--lib/python/qmk/json_schema.py36
-rw-r--r--lib/python/qmk/keyboard.py10
-rw-r--r--lib/python/qmk/keycodes.py99
-rw-r--r--lib/python/qmk/submodules.py26
18 files changed, 426 insertions, 89 deletions
diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py
index 1da4d25741..16edf88ad6 100644
--- a/lib/python/qmk/cli/__init__.py
+++ b/lib/python/qmk/cli/__init__.py
@@ -57,9 +57,11 @@ subcommands = [
'qmk.cli.generate.keyboard_c',
'qmk.cli.generate.keyboard_h',
'qmk.cli.generate.keycodes',
+ 'qmk.cli.generate.keycodes_tests',
'qmk.cli.generate.rgb_breathe_table',
'qmk.cli.generate.rules_mk',
'qmk.cli.generate.version_h',
+ 'qmk.cli.git.submodule',
'qmk.cli.hello',
'qmk.cli.import.kbfirmware',
'qmk.cli.import.keyboard',
@@ -67,11 +69,12 @@ subcommands = [
'qmk.cli.info',
'qmk.cli.json2c',
'qmk.cli.lint',
+ 'qmk.cli.kle2json',
'qmk.cli.list.keyboards',
'qmk.cli.list.keymaps',
'qmk.cli.list.layouts',
- 'qmk.cli.kle2json',
'qmk.cli.mass_compile',
+ 'qmk.cli.migrate',
'qmk.cli.multibuild',
'qmk.cli.new.keyboard',
'qmk.cli.new.keymap',
diff --git a/lib/python/qmk/cli/doctor/check.py b/lib/python/qmk/cli/doctor/check.py
index 8a0422ba72..a368242fdb 100644
--- a/lib/python/qmk/cli/doctor/check.py
+++ b/lib/python/qmk/cli/doctor/check.py
@@ -119,10 +119,8 @@ def check_submodules():
"""
for submodule in submodules.status().values():
if submodule['status'] is None:
- cli.log.error('Submodule %s has not yet been cloned!', submodule['name'])
return CheckStatus.ERROR
elif not submodule['status']:
- cli.log.warning('Submodule %s is not up to date!', submodule['name'])
return CheckStatus.WARNING
return CheckStatus.OK
diff --git a/lib/python/qmk/cli/doctor/main.py b/lib/python/qmk/cli/doctor/main.py
index 1600ab8dd4..d55a11e5fd 100755
--- a/lib/python/qmk/cli/doctor/main.py
+++ b/lib/python/qmk/cli/doctor/main.py
@@ -142,7 +142,7 @@ def doctor(cli):
if sub_ok == CheckStatus.OK:
cli.log.info('Submodules are up to date.')
else:
- if yesno('Would you like to clone the submodules?', default=True):
+ if git_check_repo() and yesno('Would you like to clone the submodules?', default=True):
submodules.update()
sub_ok = check_submodules()
diff --git a/lib/python/qmk/cli/flash.py b/lib/python/qmk/cli/flash.py
index 40bfbdab56..52defb5f0d 100644
--- a/lib/python/qmk/cli/flash.py
+++ b/lib/python/qmk/cli/flash.py
@@ -11,12 +11,14 @@ import qmk.path
from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json, build_environment
from qmk.keyboard import keyboard_completer, keyboard_folder
+from qmk.keymap import keymap_completer
from qmk.flashers import flasher
-def print_bootloader_help():
+def _list_bootloaders():
"""Prints the available bootloaders listed in docs.qmk.fm.
"""
+ cli.print_help()
cli.log.info('Here are the available bootloaders:')
cli.echo('\tavrdude')
cli.echo('\tbootloadhid')
@@ -36,14 +38,29 @@ def print_bootloader_help():
cli.echo('\tuf2-split-left')
cli.echo('\tuf2-split-right')
cli.echo('For more info, visit https://docs.qmk.fm/#/flashing')
+ return False
+
+
+def _flash_binary(filename, mcu):
+ """Try to flash binary firmware
+ """
+ cli.echo('Flashing binary firmware...\nPlease reset your keyboard into bootloader mode now!\nPress Ctrl-C to exit.\n')
+ try:
+ err, msg = flasher(mcu, filename)
+ if err:
+ cli.log.error(msg)
+ return False
+ except KeyboardInterrupt:
+ cli.log.info('Ctrl-C was pressed, exiting...')
+ return True
@cli.argument('filename', nargs='?', arg_only=True, type=qmk.path.FileType('r'), completer=FilesCompleter('.json'), help='A configurator export JSON to be compiled and flashed or a pre-compiled binary firmware file (bin/hex) to be flashed.')
@cli.argument('-b', '--bootloaders', action='store_true', help='List the available bootloaders.')
@cli.argument('-bl', '--bootloader', default='flash', help='The flash command, corresponding to qmk\'s make options of bootloaders.')
@cli.argument('-m', '--mcu', help='The MCU name. Required for HalfKay, HID, USBAspLoader and ISP flashing.')
-@cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.')
-@cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='The keyboard to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.')
+@cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='The keyboard to build a firmware for. Ignored when a configurator export is supplied.')
+@cli.argument('-km', '--keymap', completer=keymap_completer, help='The keymap to build a firmware for. Ignored when a configurator export is supplied.')
@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the make command to be run.")
@cli.argument('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs; 0 means unlimited.")
@cli.argument('-e', '--env', arg_only=True, action='append', default=[], help="Set a variable to be passed to make. May be passed multiple times.")
@@ -56,30 +73,17 @@ def flash(cli):
If a binary firmware is supplied, try to flash that.
- If a Configurator JSON export is supplied this command will create a new keymap. Keymap and Keyboard arguments
- will be ignored.
+ If a Configurator export is supplied this command will create a new keymap, overwriting an existing keymap if one exists.
- If no file is supplied, keymap and keyboard are expected.
+ If a keyboard and keymap are provided this command will build a firmware based on that.
If bootloader is omitted the make system will use the configured bootloader for that keyboard.
"""
- if cli.args.filename and cli.args.filename.suffix in ['.bin', '.hex']:
- # Try to flash binary firmware
- cli.echo('Flashing binary firmware...\nPlease reset your keyboard into bootloader mode now!\nPress Ctrl-C to exit.\n')
- try:
- err, msg = flasher(cli.args.mcu, cli.args.filename)
- if err:
- cli.log.error(msg)
- return False
- except KeyboardInterrupt:
- cli.log.info('Ctrl-C was pressed, exiting...')
- return True
+ if cli.args.filename and cli.args.filename.suffix in ['.bin', '.hex', '.uf2']:
+ return _flash_binary(cli.args.filename, cli.args.mcu)
if cli.args.bootloaders:
- # Provide usage and list bootloaders
- cli.print_help()
- print_bootloader_help()
- return False
+ return _list_bootloaders()
# Build the environment vars
envs = build_environment(cli.args.env)
diff --git a/lib/python/qmk/cli/generate/api.py b/lib/python/qmk/cli/generate/api.py
index 8650a36b84..dd4830f543 100755
--- a/lib/python/qmk/cli/generate/api.py
+++ b/lib/python/qmk/cli/generate/api.py
@@ -11,7 +11,7 @@ from qmk.info import info_json
from qmk.json_encoders import InfoJSONEncoder
from qmk.json_schema import json_load
from qmk.keyboard import find_readme, list_keyboards
-from qmk.keycodes import load_spec, list_versions
+from qmk.keycodes import load_spec, list_versions, list_languages
DATA_PATH = Path('data')
TEMPLATE_PATH = DATA_PATH / 'templates/api/'
@@ -44,6 +44,13 @@ def _resolve_keycode_specs(output_folder):
output_file = output_folder / f'constants/keycodes_{version}.json'
output_file.write_text(json.dumps(overall, indent=4), encoding='utf-8')
+ for lang in list_languages():
+ for version in list_versions(lang):
+ overall = load_spec(version, lang)
+
+ output_file = output_folder / f'constants/keycodes_{lang}_{version}.json'
+ output_file.write_text(json.dumps(overall, indent=4), encoding='utf-8')
+
# Purge files consumed by 'load_spec'
shutil.rmtree(output_folder / 'constants/keycodes/')
diff --git a/lib/python/qmk/cli/generate/keycodes.py b/lib/python/qmk/cli/generate/keycodes.py
index 29b7db3c80..2ed84cd589 100644
--- a/lib/python/qmk/cli/generate/keycodes.py
+++ b/lib/python/qmk/cli/generate/keycodes.py
@@ -8,6 +8,24 @@ from qmk.path import normpath
from qmk.keycodes import load_spec
+def _render_key(key):
+ width = 7
+ if 'S(' in key:
+ width += len('S()')
+ if 'A(' in key:
+ width += len('A()')
+ if 'RCTL(' in key:
+ width += len('RCTL()')
+ if 'ALGR(' in key:
+ width += len('ALGR()')
+ return key.ljust(width)
+
+
+def _render_label(label):
+ label = label.replace("\\", "(backslash)")
+ return label
+
+
def _generate_ranges(lines, keycodes):
lines.append('')
lines.append('enum qk_keycode_ranges {')
@@ -67,6 +85,23 @@ def _generate_helpers(lines, keycodes):
lines.append(f'#define IS_{ group.upper() }_KEYCODE(code) ((code) >= {lo} && (code) <= {hi})')
+def _generate_aliases(lines, keycodes):
+ lines.append('')
+ lines.append('// Aliases')
+ for key, value in keycodes["aliases"].items():
+ define = _render_key(value.get("key"))
+ val = _render_key(key)
+ if 'label' in value:
+ lines.append(f'#define {define} {val} // {_render_label(value.get("label"))}')
+ else:
+ lines.append(f'#define {define} {val}')
+
+ lines.append('')
+ for key, value in keycodes["aliases"].items():
+ for alias in value.get("aliases", []):
+ lines.append(f'#define {alias} {value.get("key")}')
+
+
@cli.argument('-v', '--version', arg_only=True, required=True, help='Version of keycodes to generate.')
@cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
@@ -86,3 +121,23 @@ def generate_keycodes(cli):
# Show the results
dump_lines(cli.args.output, keycodes_h_lines, cli.args.quiet)
+
+
+@cli.argument('-v', '--version', arg_only=True, required=True, help='Version of keycodes to generate.')
+@cli.argument('-l', '--lang', arg_only=True, required=True, help='Language of keycodes to generate.')
+@cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
+@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
+@cli.subcommand('Used by the make system to generate keymap_{lang}.h from keycodes_{lang}_{version}.json', hidden=True)
+def generate_keycode_extras(cli):
+ """Generates the header file.
+ """
+
+ # Build the header file.
+ keycodes_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once', '#include "keymap.h"', '// clang-format off']
+
+ keycodes = load_spec(cli.args.version, cli.args.lang)
+
+ _generate_aliases(keycodes_h_lines, keycodes)
+
+ # Show the results
+ dump_lines(cli.args.output, keycodes_h_lines, cli.args.quiet)
diff --git a/lib/python/qmk/cli/generate/keycodes_tests.py b/lib/python/qmk/cli/generate/keycodes_tests.py
new file mode 100644
index 0000000000..453b4693a7
--- /dev/null
+++ b/lib/python/qmk/cli/generate/keycodes_tests.py
@@ -0,0 +1,39 @@
+"""Used by the make system to generate a keycode lookup table from keycodes_{version}.json
+"""
+from milc import cli
+
+from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
+from qmk.commands import dump_lines
+from qmk.path import normpath
+from qmk.keycodes import load_spec
+
+
+def _generate_defines(lines, keycodes):
+ lines.append('')
+ lines.append('std::map<uint16_t, std::string> KEYCODE_ID_TABLE = {')
+ for key, value in keycodes["keycodes"].items():
+ lines.append(f' {{{value.get("key")}, "{value.get("key")}"}},')
+ lines.append('};')
+
+
+@cli.argument('-v', '--version', arg_only=True, required=True, help='Version of keycodes to generate.')
+@cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
+@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
+@cli.subcommand('Used by the make system to generate a keycode lookup table from keycodes_{version}.json', hidden=True)
+def generate_keycodes_tests(cli):
+ """Generates a keycode to identifier lookup table for unit test output.
+ """
+
+ # Build the keycodes.h file.
+ keycodes_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '// clang-format off']
+ keycodes_h_lines.append('extern "C" {\n#include <keycode.h>\n}')
+ keycodes_h_lines.append('#include <map>')
+ keycodes_h_lines.append('#include <string>')
+ keycodes_h_lines.append('#include <cstdint>')
+
+ keycodes = load_spec(cli.args.version)
+
+ _generate_defines(keycodes_h_lines, keycodes)
+
+ # Show the results
+ dump_lines(cli.args.output, keycodes_h_lines, cli.args.quiet)
diff --git a/lib/python/qmk/cli/git/__init__.py b/lib/python/qmk/cli/git/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/lib/python/qmk/cli/git/__init__.py
diff --git a/lib/python/qmk/cli/git/submodule.py b/lib/python/qmk/cli/git/submodule.py
new file mode 100644
index 0000000000..bcc3868a52
--- /dev/null
+++ b/lib/python/qmk/cli/git/submodule.py
@@ -0,0 +1,22 @@
+import shutil
+from qmk.path import normpath
+
+from milc import cli
+
+REMOVE_DIRS = [
+ 'lib/ugfx',
+ 'lib/pico-sdk',
+ 'lib/chibios-contrib/ext/mcux-sdk',
+ 'lib/lvgl',
+]
+
+
+@cli.subcommand('Git Submodule actions.')
+def git_submodule(cli):
+ for folder in REMOVE_DIRS:
+ if normpath(folder).is_dir():
+ print(f"Removing '{folder}'")
+ shutil.rmtree(folder)
+
+ cli.run(['git', 'submodule', 'sync', '--recursive'], capture_output=False)
+ cli.run(['git', 'submodule', 'update', '--init', '--recursive', '--progress'], capture_output=False)
diff --git a/lib/python/qmk/cli/list/keyboards.py b/lib/python/qmk/cli/list/keyboards.py
index 8b6c451673..405b9210e4 100644
--- a/lib/python/qmk/cli/list/keyboards.py
+++ b/lib/python/qmk/cli/list/keyboards.py
@@ -5,9 +5,10 @@ from milc import cli
import qmk.keyboard
+@cli.argument('--no-resolve-defaults', arg_only=True, action='store_false', help='Ignore any "DEFAULT_FOLDER" within keyboards rules.mk')
@cli.subcommand("List the keyboards currently defined within QMK")
def list_keyboards(cli):
"""List the keyboards currently defined within QMK
"""
- for keyboard_name in qmk.keyboard.list_keyboards():
+ for keyboard_name in qmk.keyboard.list_keyboards(cli.args.no_resolve_defaults):
print(keyboard_name)
diff --git a/lib/python/qmk/cli/migrate.py b/lib/python/qmk/cli/migrate.py
new file mode 100644
index 0000000000..4164f9c8ad
--- /dev/null
+++ b/lib/python/qmk/cli/migrate.py
@@ -0,0 +1,81 @@
+"""Migrate keyboard configuration to "Data Driven"
+"""
+import json
+from pathlib import Path
+from dotty_dict import dotty
+
+from milc import cli
+
+from qmk.keyboard import keyboard_completer, keyboard_folder, resolve_keyboard
+from qmk.info import info_json, find_info_json
+from qmk.json_encoders import InfoJSONEncoder
+from qmk.json_schema import json_load
+
+
+def _candidate_files(keyboard):
+ kb_dir = Path(resolve_keyboard(keyboard))
+
+ cur_dir = Path('keyboards')
+ files = []
+ for dir in kb_dir.parts:
+ cur_dir = cur_dir / dir
+ files.append(cur_dir / 'config.h')
+ files.append(cur_dir / 'rules.mk')
+
+ return [file for file in files if file.exists()]
+
+
+@cli.argument('-f', '--filter', arg_only=True, action='append', default=[], help="Filter the performed migrations based on the supplied value. Supported format is 'KEY' located from 'data/mappings'. May be passed multiple times.")
+@cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, required=True, help='The keyboard\'s name')
+@cli.subcommand('Migrate keyboard config to "Data Driven".', hidden=True)
+def migrate(cli):
+ """Migrate keyboard configuration to "Data Driven"
+ """
+ # Merge mappings as we do not care to where "KEY" is found just that its removed
+ info_config_map = json_load(Path('data/mappings/info_config.hjson'))
+ info_rules_map = json_load(Path('data/mappings/info_rules.hjson'))
+ info_map = {**info_config_map, **info_rules_map}
+
+ # Parse target info.json which will receive updates
+ target_info = Path(find_info_json(cli.args.keyboard)[0])
+ info_data = dotty(json_load(target_info))
+
+ # Already parsed used for updates
+ kb_info_json = dotty(info_json(cli.args.keyboard))
+
+ # List of candidate files
+ files = _candidate_files(cli.args.keyboard)
+
+ # Filter down keys if requested
+ keys = info_map.keys()
+ if cli.args.filter:
+ keys = list(set(keys) & set(cli.args.filter))
+
+ cli.log.info(f'{{fg_green}}Migrating keyboard {{fg_cyan}}{cli.args.keyboard}{{fg_green}}.{{fg_reset}}')
+
+ # Start migration
+ for file in files:
+ cli.log.info(f' Migrating file {file}')
+ file_contents = file.read_text(encoding='utf-8').split('\n')
+ for key in keys:
+ for num, line in enumerate(file_contents):
+ if line.startswith(f'{key} =') or line.startswith(f'#define {key} '):
+ cli.log.info(f' Migrating {key}...')
+
+ while line.rstrip().endswith('\\'):
+ file_contents.pop(num)
+ line = file_contents[num]
+ file_contents.pop(num)
+
+ update_key = info_map[key]["info_key"]
+ if update_key in kb_info_json:
+ info_data[update_key] = kb_info_json[update_key]
+
+ file.write_text('\n'.join(file_contents), encoding='utf-8')
+
+ # Finally write out updated info.json
+ cli.log.info(f' Updating {target_info}')
+ target_info.write_text(json.dumps(info_data.to_dict(), cls=InfoJSONEncoder))
+
+ cli.log.info(f'{{fg_green}}Migration of keyboard {{fg_cyan}}{cli.args.keyboard}{{fg_green}} complete!{{fg_reset}}')
+ cli.log.info(f"Verify build with {{fg_yellow}}qmk compile -kb {cli.args.keyboard} -km default{{fg_reset}}.")
diff --git a/lib/python/qmk/cli/new/keyboard.py b/lib/python/qmk/cli/new/keyboard.py
index 251ad919dd..cdd3919168 100644
--- a/lib/python/qmk/cli/new/keyboard.py
+++ b/lib/python/qmk/cli/new/keyboard.py
@@ -195,11 +195,6 @@ def new_keyboard(cli):
cli.echo('')
kb_name = cli.args.keyboard if cli.args.keyboard else prompt_keyboard()
- user_name = cli.config.new_keyboard.name if cli.config.new_keyboard.name else prompt_user()
- real_name = cli.args.realname or cli.config.new_keyboard.name if cli.args.realname or cli.config.new_keyboard.name else prompt_name(user_name)
- default_layout = cli.args.layout if cli.args.layout else prompt_layout()
- mcu = cli.args.type if cli.args.type else prompt_mcu()
-
if not validate_keyboard_name(kb_name):
cli.log.error('Keyboard 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 1
@@ -208,6 +203,11 @@ def new_keyboard(cli):
cli.log.error(f'Keyboard {{fg_cyan}}{kb_name}{{fg_reset}} already exists! Please choose a different name.')
return 1
+ user_name = cli.config.new_keyboard.name if cli.config.new_keyboard.name else prompt_user()
+ real_name = cli.args.realname or cli.config.new_keyboard.name if cli.args.realname or cli.config.new_keyboard.name else prompt_name(user_name)
+ default_layout = cli.args.layout if cli.args.layout else prompt_layout()
+ mcu = cli.args.type if cli.args.type else prompt_mcu()
+
# Preprocess any development_board presets
if mcu in dev_boards:
defaults_map = json_load(Path('data/mappings/defaults.hjson'))
diff --git a/lib/python/qmk/cli/new/keymap.py b/lib/python/qmk/cli/new/keymap.py
index 60cb743cb6..e7823bc46d 100755
--- a/lib/python/qmk/cli/new/keymap.py
+++ b/lib/python/qmk/cli/new/keymap.py
@@ -1,12 +1,32 @@
"""This script automates the copying of the default keymap into your own keymap.
"""
import shutil
-from pathlib import Path
-import qmk.path
+from milc import cli
+from milc.questions import question
+
+from qmk.path import is_keyboard, keymap
+from qmk.git import git_get_username
from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.keyboard import keyboard_completer, keyboard_folder
-from milc import cli
+
+
+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}.
+
+Keyboard Name? """
+
+ return question(prompt)
+
+
+def prompt_user():
+ prompt = """
+{fg_yellow}Name Your Keymap{style_reset_all}
+Used for maintainer, copyright, etc
+
+Your GitHub Username? """
+ return question(prompt, default=git_get_username())
@cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='Specify keyboard name. Example: 1upkeyboards/1up60hse')
@@ -17,32 +37,34 @@ from milc import cli
def new_keymap(cli):
"""Creates a new keymap for the keyboard of your choosing.
"""
- # ask for user input if keyboard or keymap was not provided in the command line
- keyboard = cli.config.new_keymap.keyboard if cli.config.new_keymap.keyboard else input("Keyboard Name: ")
- keymap = cli.config.new_keymap.keymap if cli.config.new_keymap.keymap else input("Keymap Name: ")
+ cli.log.info('{style_bright}Generating a new keymap{style_normal}')
+ cli.echo('')
- # generate keymap paths
- kb_path = Path('keyboards') / keyboard
- keymap_path = qmk.path.keymap(keyboard)
- keymap_path_default = keymap_path / 'default'
- keymap_path_new = keymap_path / keymap
+ # ask for user input if keyboard or keymap was not provided in the command line
+ kb_name = cli.config.new_keymap.keyboard if cli.config.new_keymap.keyboard else prompt_keyboard()
+ user_name = cli.config.new_keymap.keymap if cli.config.new_keymap.keymap else prompt_user()
# check directories
- if not kb_path.exists():
- cli.log.error('Keyboard %s does not exist!', kb_path)
+ if not is_keyboard(kb_name):
+ cli.log.error(f'Keyboard {{fg_cyan}}{kb_name}{{fg_reset}} does not exist! Please choose a valid name.')
return False
+ # generate keymap paths
+ km_path = keymap(kb_name)
+ keymap_path_default = km_path / 'default'
+ keymap_path_new = km_path / user_name
+
if not keymap_path_default.exists():
- cli.log.error('Keyboard default %s does not exist!', keymap_path_default)
+ cli.log.error(f'Default keymap {{fg_cyan}}{keymap_path_default}{{fg_reset}} does not exist!')
return False
if keymap_path_new.exists():
- cli.log.error('Keymap %s already exists!', keymap_path_new)
+ cli.log.error(f'Keymap {{fg_cyan}}{user_name}{{fg_reset}} already exists! Please choose a different name.')
return False
# create user directory with default keymap files
shutil.copytree(keymap_path_default, keymap_path_new, symlinks=True)
# end message to user
- cli.log.info("%s keymap directory created in: %s", keymap, keymap_path_new)
- cli.log.info("Compile a firmware with your new keymap by typing: \n\n\tqmk compile -kb %s -km %s\n", keyboard, keymap)
+ cli.log.info(f'{{fg_green}}Created a new keymap called {{fg_cyan}}{user_name}{{fg_green}} in: {{fg_cyan}}{keymap_path_new}.{{fg_reset}}')
+ cli.log.info(f"Compile a firmware with your new keymap by typing: {{fg_yellow}}qmk compile -kb {kb_name} -km {user_name}{{fg_reset}}.")
diff --git a/lib/python/qmk/flashers.py b/lib/python/qmk/flashers.py
index e902e5072f..f83665d9ac 100644
--- a/lib/python/qmk/flashers.py
+++ b/lib/python/qmk/flashers.py
@@ -71,6 +71,12 @@ def _find_usb_device(vid_hex, pid_hex):
return usb.core.find(idVendor=vid_hex, idProduct=pid_hex)
+def _find_uf2_devices():
+ """Delegate to uf2conv.py as VID:PID pairs can potentially fluctuate more than other bootloaders
+ """
+ return cli.run(['util/uf2conv.py', '--list']).stdout.splitlines()
+
+
def _find_bootloader():
# To avoid running forever in the background, only look for bootloaders for 10min
start_time = time.time()
@@ -95,6 +101,8 @@ def _find_bootloader():
else:
details = None
return (bl, details)
+ if _find_uf2_devices():
+ return ('_uf2_compatible_', None)
time.sleep(0.1)
return (None, None)
@@ -184,6 +192,10 @@ def _flash_mdloader(file):
cli.run(['mdloader', '--first', '--download', file, '--restart'], capture_output=False)
+def _flash_uf2(file):
+ cli.run(['util/uf2conv.py', '--deploy', file], capture_output=False)
+
+
def flasher(mcu, file):
bl, details = _find_bootloader()
# Add a small sleep to avoid race conditions
@@ -208,6 +220,8 @@ def flasher(mcu, file):
return (True, "Specifying the MCU with '-m' is necessary for ISP flashing!")
elif bl == 'md-boot':
_flash_mdloader(file)
+ elif bl == '_uf2_compatible_':
+ _flash_uf2(file)
else:
return (True, "Known bootloader found but flashing not currently supported!")
diff --git a/lib/python/qmk/json_schema.py b/lib/python/qmk/json_schema.py
index 934e2f841f..c886a0d868 100644
--- a/lib/python/qmk/json_schema.py
+++ b/lib/python/qmk/json_schema.py
@@ -1,12 +1,13 @@
"""Functions that help us generate and use info.json files.
"""
import json
+import hjson
+import jsonschema
from collections.abc import Mapping
from functools import lru_cache
+from typing import OrderedDict
from pathlib import Path
-import hjson
-import jsonschema
from milc import cli
@@ -101,3 +102,34 @@ def deep_update(origdict, newdict):
origdict[key] = value
return origdict
+
+
+def merge_ordered_dicts(dicts):
+ """Merges nested OrderedDict objects resulting from reading a hjson file.
+ Later input dicts overrides earlier dicts for plain values.
+ Arrays will be appended. If the first entry of an array is "!reset!", the contents of the array will be cleared and replaced with RHS.
+ Dictionaries will be recursively merged. If any entry is "!reset!", the contents of the dictionary will be cleared and replaced with RHS.
+ """
+ result = OrderedDict()
+
+ def add_entry(target, k, v):
+ if k in target and isinstance(v, (OrderedDict, dict)):
+ if "!reset!" in v:
+ target[k] = v
+ else:
+ target[k] = merge_ordered_dicts([target[k], v])
+ if "!reset!" in target[k]:
+ del target[k]["!reset!"]
+ elif k in target and isinstance(v, list):
+ if v[0] == '!reset!':
+ target[k] = v[1:]
+ else:
+ target[k] = target[k] + v
+ else:
+ target[k] = v
+
+ for d in dicts:
+ for (k, v) in d.items():
+ add_entry(result, k, v)
+
+ return result
diff --git a/lib/python/qmk/keyboard.py b/lib/python/qmk/keyboard.py
index 6ddbba8fa5..0c980faf2b 100644
--- a/lib/python/qmk/keyboard.py
+++ b/lib/python/qmk/keyboard.py
@@ -98,14 +98,18 @@ def keyboard_completer(prefix, action, parser, parsed_args):
return list_keyboards()
-def list_keyboards():
- """Returns a list of all keyboards.
+def list_keyboards(resolve_defaults=True):
+ """Returns a list of all keyboards - optionally processing any DEFAULT_FOLDER.
"""
# We avoid pathlib here because this is performance critical code.
kb_wildcard = os.path.join(base_path, "**", "rules.mk")
paths = [path for path in glob(kb_wildcard, recursive=True) if os.path.sep + 'keymaps' + os.path.sep not in path]
- return sorted(set(map(resolve_keyboard, map(_find_name, paths))))
+ found = map(_find_name, paths)
+ if resolve_defaults:
+ found = map(resolve_keyboard, found)
+
+ return sorted(set(found))
def resolve_keyboard(keyboard):
diff --git a/lib/python/qmk/keycodes.py b/lib/python/qmk/keycodes.py
index cf1ee0767a..d2f2492829 100644
--- a/lib/python/qmk/keycodes.py
+++ b/lib/python/qmk/keycodes.py
@@ -1,8 +1,64 @@
from pathlib import Path
-from qmk.json_schema import deep_update, json_load, validate
+from qmk.json_schema import merge_ordered_dicts, deep_update, json_load, validate
-CONSTANTS_PATH = Path('data/constants/keycodes/')
+CONSTANTS_PATH = Path('data/constants/')
+KEYCODES_PATH = CONSTANTS_PATH / 'keycodes'
+EXTRAS_PATH = KEYCODES_PATH / 'extras'
+
+
+def _find_versions(path, prefix):
+ ret = []
+ for file in path.glob(f'{prefix}_[0-9].[0-9].[0-9].hjson'):
+ ret.append(file.stem.split('_')[-1])
+
+ ret.sort(reverse=True)
+ return ret
+
+
+def _potential_search_versions(version, lang=None):
+ versions = list_versions(lang)
+ versions.reverse()
+
+ loc = versions.index(version) + 1
+
+ return versions[:loc]
+
+
+def _search_path(lang=None):
+ return EXTRAS_PATH if lang else KEYCODES_PATH
+
+
+def _search_prefix(lang=None):
+ return f'keycodes_{lang}' if lang else 'keycodes'
+
+
+def _locate_files(path, prefix, versions):
+ # collate files by fragment "type"
+ files = {'_': []}
+ for version in versions:
+ files['_'].append(path / f'{prefix}_{version}.hjson')
+
+ for file in path.glob(f'{prefix}_{version}_*.hjson'):
+ fragment = file.stem.replace(f'{prefix}_{version}_', '')
+ if fragment not in files:
+ files[fragment] = []
+ files[fragment].append(file)
+
+ return files
+
+
+def _process_files(files):
+ # allow override within types of fragments - but not globally
+ spec = {}
+ for category in files.values():
+ specs = []
+ for file in category:
+ specs.append(json_load(file))
+
+ deep_update(spec, merge_ordered_dicts(specs))
+
+ return spec
def _validate(spec):
@@ -19,26 +75,21 @@ def _validate(spec):
raise ValueError(f'Keycode spec contains duplicate keycodes! ({",".join(duplicates)})')
-def load_spec(version):
+def load_spec(version, lang=None):
"""Build keycode data from the requested spec file
"""
if version == 'latest':
- version = list_versions()[0]
+ version = list_versions(lang)[0]
- file = CONSTANTS_PATH / f'keycodes_{version}.hjson'
- if not file.exists():
- raise ValueError(f'Requested keycode spec ({version}) is invalid!')
+ path = _search_path(lang)
+ prefix = _search_prefix(lang)
+ versions = _potential_search_versions(version, lang)
- # Load base
- spec = json_load(file)
-
- # Merge in fragments
- fragments = CONSTANTS_PATH.glob(f'keycodes_{version}_*.hjson')
- for file in fragments:
- deep_update(spec, json_load(file))
+ # Load bases + any fragments
+ spec = _process_files(_locate_files(path, prefix, versions))
# Sort?
- spec['keycodes'] = dict(sorted(spec['keycodes'].items()))
+ spec['keycodes'] = dict(sorted(spec.get('keycodes', {}).items()))
# Validate?
_validate(spec)
@@ -46,12 +97,20 @@ def load_spec(version):
return spec
-def list_versions():
+def list_versions(lang=None):
"""Return available versions - sorted newest first
"""
- ret = []
- for file in CONSTANTS_PATH.glob('keycodes_[0-9].[0-9].[0-9].hjson'):
- ret.append(file.stem.split('_')[1])
+ path = _search_path(lang)
+ prefix = _search_prefix(lang)
+
+ return _find_versions(path, prefix)
+
+
+def list_languages():
+ """Return available languages
+