summaryrefslogtreecommitdiff
path: root/lib/python/qmk/cli
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python/qmk/cli')
-rw-r--r--lib/python/qmk/cli/__init__.py1
-rw-r--r--lib/python/qmk/cli/migrate.py81
-rwxr-xr-xlib/python/qmk/cli/new/keymap.py56
3 files changed, 121 insertions, 17 deletions
diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py
index 9190af4e50..4e3ce63da3 100644
--- a/lib/python/qmk/cli/__init__.py
+++ b/lib/python/qmk/cli/__init__.py
@@ -71,6 +71,7 @@ subcommands = [
'qmk.cli.list.keymaps',
'qmk.cli.list.layouts',
'qmk.cli.kle2json',
+ 'qmk.cli.migrate',
'qmk.cli.multibuild',
'qmk.cli.new.keyboard',
'qmk.cli.new.keymap',
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/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}}.")