diff options
Diffstat (limited to 'lib/python/qmk/cli')
-rw-r--r-- | lib/python/qmk/cli/__init__.py | 2 | ||||
-rw-r--r-- | lib/python/qmk/cli/cformat.py | 11 | ||||
-rwxr-xr-x | lib/python/qmk/cli/compile.py | 16 | ||||
-rw-r--r-- | lib/python/qmk/cli/config.py | 96 | ||||
-rw-r--r-- | lib/python/qmk/cli/docs.py | 10 | ||||
-rwxr-xr-x | lib/python/qmk/cli/doctor.py | 13 | ||||
-rw-r--r-- | lib/python/qmk/cli/flash.py | 83 | ||||
-rwxr-xr-x | lib/python/qmk/cli/json/keymap.py | 5 | ||||
-rwxr-xr-x | lib/python/qmk/cli/kle2json.py | 75 | ||||
-rw-r--r-- | lib/python/qmk/cli/list/keyboards.py | 27 | ||||
-rw-r--r-- | lib/python/qmk/cli/pytest.py | 16 |
11 files changed, 268 insertions, 86 deletions
diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index e41cc3dcb2..72ee38f562 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py @@ -7,9 +7,11 @@ from . import compile from . import config from . import docs from . import doctor +from . import flash from . import hello from . import json from . import list +from . import kle2json from . import new from . import pyformat from . import pytest diff --git a/lib/python/qmk/cli/cformat.py b/lib/python/qmk/cli/cformat.py index d2382bdbde..17ca91b3b5 100644 --- a/lib/python/qmk/cli/cformat.py +++ b/lib/python/qmk/cli/cformat.py @@ -2,6 +2,7 @@ """ import os import subprocess +from shutil import which from milc import cli @@ -11,10 +12,18 @@ from milc import cli def cformat(cli): """Format C code according to QMK's style. """ + # Determine which version of clang-format to use clang_format = ['clang-format', '-i'] + for clang_version in [10, 9, 8, 7]: + binary = 'clang-format-%d' % clang_version + if which(binary): + clang_format[0] = binary + break # Find the list of files to format - if not cli.args.files: + if cli.args.files: + cli.args.files = [os.path.join(os.environ['ORIG_CWD'], file) for file in cli.args.files] + else: for dir in ['drivers', 'quantum', 'tests', 'tmk_core']: for dirpath, dirnames, filenames in os.walk(dir): if 'tmk_core/protocol/usb_hid' in dirpath: diff --git a/lib/python/qmk/cli/compile.py b/lib/python/qmk/cli/compile.py index 6646891b30..8e2d0cdbf4 100755 --- a/lib/python/qmk/cli/compile.py +++ b/lib/python/qmk/cli/compile.py @@ -2,13 +2,13 @@ You can compile a keymap already in the repo or using a QMK Configurator export. """ -import json -import os -import sys import subprocess from argparse import FileType from milc import cli +from qmk.commands import create_make_command +from qmk.commands import parse_configurator_json +from qmk.commands import compile_configurator_json import qmk.keymap import qmk.path @@ -30,20 +30,20 @@ def compile(cli): """ if cli.args.filename: # Parse the configurator json - user_keymap = json.load(cli.args.filename) + user_keymap = parse_configurator_json(cli.args.filename) # Generate the keymap keymap_path = qmk.path.keymap(user_keymap['keyboard']) cli.log.info('Creating {fg_cyan}%s{style_reset_all} keymap in {fg_cyan}%s', user_keymap['keymap'], keymap_path) - qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers']) - cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) # Compile the keymap - command = ['make', ':'.join((user_keymap['keyboard'], user_keymap['keymap']))] + command = compile_configurator_json(user_keymap) + + cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) elif cli.config.compile.keyboard and cli.config.compile.keymap: # Generate the make command for a specific keyboard/keymap. - command = ['make', ':'.join((cli.config.compile.keyboard, cli.config.compile.keymap))] + command = create_make_command(cli.config.compile.keyboard, cli.config.compile.keymap) else: cli.log.error('You must supply a configurator export or both `--keyboard` and `--keymap`.') diff --git a/lib/python/qmk/cli/config.py b/lib/python/qmk/cli/config.py index d6c774e651..e17d8bb9ba 100644 --- a/lib/python/qmk/cli/config.py +++ b/lib/python/qmk/cli/config.py @@ -1,8 +1,5 @@ """Read and write configuration settings """ -import os -import subprocess - from milc import cli @@ -12,7 +9,55 @@ def print_config(section, key): cli.echo('%s.%s{fg_cyan}={fg_reset}%s', section, key, cli.config[section][key]) -@cli.argument('-ro', '--read-only', action='store_true', help='Operate in read-only mode.') +def show_config(): + """Print the current configuration to stdout. + """ + for section in cli.config: + for key in cli.config[section]: + print_config(section, key) + + +def parse_config_token(config_token): + """Split a user-supplied configuration-token into its components. + """ + section = option = value = None + + if '=' in config_token and '.' not in config_token: + cli.log.error('Invalid configuration token, the key must be of the form <section>.<option>: %s', config_token) + return section, option, value + + # Separate the key (<section>.<option>) from the value + if '=' in config_token: + key, value = config_token.split('=') + else: + key = config_token + + # Extract the section and option from the key + if '.' in key: + section, option = key.split('.', 1) + else: + section = key + + return section, option, value + + +def set_config(section, option, value): + """Set a config key in the running config. + """ + log_string = '%s.%s{fg_cyan}:{fg_reset} %s {fg_cyan}->{fg_reset} %s' + if cli.args.read_only: + log_string += ' {fg_red}(change not written)' + + cli.echo(log_string, section, option, cli.config[section][option], value) + + if not cli.args.read_only: + if value == 'None': + del cli.config[section][option] + else: + cli.config[section][option] = value + + +@cli.argument('-ro', '--read-only', arg_only=True, action='store_true', help='Operate in read-only mode.') @cli.argument('configs', nargs='*', arg_only=True, help='Configuration options to read or write.') @cli.subcommand("Read and write configuration settings.") def config(cli): @@ -33,12 +78,7 @@ def config(cli): No validation is done to ensure that the supplied section.key is actually used by qmk scripts. """ if not cli.args.configs: - # Walk the config tree - for section in cli.config: - for key in cli.config[section]: - print_config(section, key) - - return True + return show_config() # Process config_tokens save_config = False @@ -46,43 +86,23 @@ def config(cli): for argument in cli.args.configs: # Split on space in case they quoted multiple config tokens for config_token in argument.split(' '): - # Extract the section, config_key, and value to write from the supplied config_token. - if '=' in config_token: - key, value = config_token.split('=') - else: - key = config_token - value = None - - if '.' in key: - section, config_key = key.split('.', 1) - else: - section = key - config_key = None + section, option, value = parse_config_token(config_token) # Validation - if config_key and '.' in config_key: - cli.log.error('Config keys may not have more than one period! "%s" is not valid.', key) + if option and '.' in option: + cli.log.error('Config keys may not have more than one period! "%s" is not valid.', config_token) return False # Do what the user wants - if section and config_key and value: - # Write a config key - log_string = '%s.%s{fg_cyan}:{fg_reset} %s {fg_cyan}->{fg_reset} %s' - if cli.args.read_only: - log_string += ' {fg_red}(change not written)' - - cli.echo(log_string, section, config_key, cli.config[section][config_key], value) - + if section and option and value: + # Write a configuration option + set_config(section, option, value) if not cli.args.read_only: - if value == 'None': - del cli.config[section][config_key] - else: - cli.config[section][config_key] = value save_config = True - elif section and config_key: + elif section and option: # Display a single key - print_config(section, config_key) + print_config(section, option) elif section: # Display an entire section diff --git a/lib/python/qmk/cli/docs.py b/lib/python/qmk/cli/docs.py index b419891396..163c8b8015 100644 --- a/lib/python/qmk/cli/docs.py +++ b/lib/python/qmk/cli/docs.py @@ -1,21 +1,19 @@ """Serve QMK documentation locally """ import http.server +import os from milc import cli -class DocsHandler(http.server.SimpleHTTPRequestHandler): - def __init__(self, *args, **kwargs): - super().__init__(*args, directory='docs', **kwargs) - - @cli.argument('-p', '--port', default=8936, type=int, help='Port number to use.') @cli.subcommand('Run a local webserver for QMK documentation.') def docs(cli): """Spin up a local HTTPServer instance for the QMK docs. """ - with http.server.HTTPServer(('', cli.config.docs.port), DocsHandler) as httpd: + os.chdir('docs') + + with http.server.HTTPServer(('', cli.config.docs.port), http.server.SimpleHTTPRequestHandler) as httpd: cli.log.info("Serving QMK docs at http://localhost:%d/", cli.config.docs.port) cli.log.info("Press Control+C to exit.") diff --git a/lib/python/qmk/cli/doctor.py b/lib/python/qmk/cli/doctor.py index 309de0c671..2421e520e0 100755 --- a/lib/python/qmk/cli/doctor.py +++ b/lib/python/qmk/cli/doctor.py @@ -2,11 +2,9 @@ Check up for QMK environment. """ -import os import platform import shutil import subprocess -from glob import glob from milc import cli @@ -24,8 +22,7 @@ def doctor(cli): cli.log.info('QMK Doctor is checking your environment.') # Make sure the basic CLI tools we need are available and can be executed. - binaries = ['dfu-programmer', 'avrdude', 'dfu-util', 'avr-gcc', 'arm-none-eabi-gcc'] - binaries += glob('bin/qmk-*') + binaries = ['dfu-programmer', 'avrdude', 'dfu-util', 'avr-gcc', 'arm-none-eabi-gcc', 'bin/qmk'] ok = True for binary in binaries: @@ -34,15 +31,15 @@ def doctor(cli): cli.log.error("{fg_red}QMK can't find %s in your path.", binary) ok = False else: - try: - subprocess.run([binary, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5, check=True) + check = subprocess.run([binary, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5) + if check.returncode in [0, 1]: cli.log.info('Found {fg_cyan}%s', binary) - except subprocess.CalledProcessError: + else: cli.log.error("{fg_red}Can't run `%s --version`", binary) ok = False # Determine our OS and run platform specific tests - OS = platform.system() + OS = platform.system() # noqa (N806), uppercase name is ok in this instance if OS == "Darwin": cli.log.info("Detected {fg_cyan}macOS.") diff --git a/lib/python/qmk/cli/flash.py b/lib/python/qmk/cli/flash.py new file mode 100644 index 0000000000..e897174a20 --- /dev/null +++ b/lib/python/qmk/cli/flash.py @@ -0,0 +1,83 @@ +"""Compile and flash QMK Firmware + +You can compile a keymap already in the repo or using a QMK Configurator export. +A bootloader must be specified. +""" +import subprocess +from argparse import FileType + +import qmk.path +from milc import cli +from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json + + +def print_bootloader_help(): + """Prints the available bootloaders listed in docs.qmk.fm. + """ + cli.log.info('Here are the available bootloaders:') + cli.echo('\tdfu') + cli.echo('\tdfu-ee') + cli.echo('\tdfu-split-left') + cli.echo('\tdfu-split-right') + cli.echo('\tavrdude') + cli.echo('\tBootloadHID') + cli.echo('\tdfu-util') + cli.echo('\tdfu-util-split-left') + cli.echo('\tdfu-util-split-right') + cli.echo('\tst-link-cli') + cli.echo('For more info, visit https://docs.qmk.fm/#/flashing') + + +@cli.argument('-bl', '--bootloader', default='flash', help='The flash command, corresponding to qmk\'s make options of bootloaders.') +@cli.argument('filename', nargs='?', arg_only=True, type=FileType('r'), help='The configurator export JSON to compile.') +@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', 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('-b', '--bootloaders', action='store_true', help='List the available bootloaders.') +@cli.subcommand('QMK Flash.') +def flash(cli): + """Compile and or flash QMK Firmware or keyboard/layout + + If a Configurator JSON export is supplied this command will create a new keymap. Keymap and Keyboard arguments + will be ignored. + + If no file is supplied, keymap and keyboard are expected. + + If bootloader is omitted, the one according to the rules.mk will be used. + + """ + command = [] + if cli.args.bootloaders: + # Provide usage and list bootloaders + cli.echo('usage: qmk flash [-h] [-b] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]') + print_bootloader_help() + return False + + elif cli.args.keymap and not cli.args.keyboard: + # If only a keymap was given but no keyboard, suggest listing keyboards + cli.echo('usage: qmk flash [-h] [-b] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]') + cli.log.error('run \'qmk list_keyboards\' to find out the supported keyboards') + return False + + elif cli.args.filename: + # Get keymap path to log info + user_keymap = parse_configurator_json(cli.args.filename) + keymap_path = qmk.path.keymap(user_keymap['keyboard']) + + cli.log.info('Creating {fg_cyan}%s{style_reset_all} keymap in {fg_cyan}%s', user_keymap['keymap'], keymap_path) + + # Convert the JSON into a C file and write it to disk. + command = compile_configurator_json(user_keymap, cli.args.bootloader) + + cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) + + elif cli.args.keyboard and cli.args.keymap: + # Generate the make command for a specific keyboard/keymap. + command = create_make_command(cli.config.flash.keyboard, cli.config.flash.keymap, cli.args.bootloader) + + else: + cli.echo('usage: qmk flash [-h] [-b] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]') + cli.log.error('You must supply a configurator export or both `--keyboard` and `--keymap`. You can also specify a bootloader with --bootloader. Use --bootloaders to list the available bootloaders.') + return False + + cli.log.info('Flashing keymap with {fg_cyan}%s\n\n', ' '.join(command)) + subprocess.run(command) diff --git a/lib/python/qmk/cli/json/keymap.py b/lib/python/qmk/cli/json/keymap.py index 207ac278ca..a030ab53d6 100755 --- a/lib/python/qmk/cli/json/keymap.py +++ b/lib/python/qmk/cli/json/keymap.py @@ -2,7 +2,6 @@ """ import json import os -import sys from milc import cli @@ -10,6 +9,7 @@ import qmk.keymap @cli.argument('-o', '--output', arg_only=True, help='File to write to') +@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") @cli.argument('filename', arg_only=True, help='Configurator JSON file') @cli.subcommand('Creates a keymap.c from a QMK Configurator export.') def json_keymap(cli): @@ -48,7 +48,8 @@ def json_keymap(cli): with open(output_file, 'w') as keymap_fd: keymap_fd.write(keymap_c) - cli.log.info('Wrote keymap to %s.', cli.args.output) + if not cli.args.quiet: + cli.log.info('Wrote keymap to %s.', cli.args.output) else: print(keymap_c) diff --git a/lib/python/qmk/cli/kle2json.py b/lib/python/qmk/cli/kle2json.py new file mode 100755 index 0000000000..00f63d3622 --- /dev/null +++ b/lib/python/qmk/cli/kle2json.py @@ -0,0 +1,75 @@ +"""Convert raw KLE to JSON +""" +import json +import os +from pathlib import Path +from decimal import Decimal +from collections import OrderedDict + +from milc import cli +from kle2xy import KLE2xy + +from qmk.converter import kle2qmk + + +class CustomJSONEncoder(json.JSONEncoder): + def default(self, obj): + try: + if isinstance(obj, Decimal): + if obj % 2 in (Decimal(0), Decimal(1)): + return int(obj) + return float(obj) + except TypeError: + pass + return json.JSONEncoder.default(self, obj) + + +@cli.argument('filename', help='The KLE raw txt to convert') +@cli.argument('-f', '--force', action='store_true', help='Flag to overwrite current info.json') +@cli.subcommand('Convert a KLE layout to a Configurator JSON') +def kle2json(cli): + """Convert a KLE layout to QMK's layout format. + """ # If filename is a path + if cli.args.filename.startswith("/") or cli.args.filename.startswith("./"): + file_path = Path(cli.args.filename) + # Otherwise assume it is a file name + else: + file_path = Path(os.environ['ORIG_CWD'], cli.args.filename) + # Check for valid file_path for more graceful failure + if not file_path.exists(): + return cli.log.error('File {fg_cyan}%s{style_reset_all} was not found.', str(file_path)) + out_path = file_path.parent + raw_code = file_path.open().read() + # Check if info.json exists, allow overwrite with force + if Path(out_path, "info.json").exists() and not cli.args.force: + cli.log.error('File {fg_cyan}%s/info.json{style_reset_all} already exists, use -f or --force to overwrite.', str(out_path)) + return False + try: + # Convert KLE raw to x/y coordinates (using kle2xy package from skullydazed) + kle = KLE2xy(raw_code) + except Exception as e: + cli.log.error('Could not parse KLE raw data: %s', raw_code) + cli.log.exception(e) + # FIXME: This should be better + return cli.log.error('Could not parse KLE raw data.') + keyboard = OrderedDict( + keyboard_name=kle.name, + url='', + maintainer='qmk', + width=kle.columns, + height=kle.rows, + layouts={'LAYOUT': { + 'layout': 'LAYOUT_JSON_HERE' + }}, + ) + # Initialize keyboard with json encoded from ordered dict + keyboard = json.dumps(keyboard, indent=4, separators=(', ', ': '), sort_keys=False, cls=CustomJSONEncoder) + # Initialize layout with kle2qmk from converter module + layout = json.dumps(kle2qmk(kle), separators=(', ', ':'), cls=CustomJSONEncoder) + # Replace layout in keyboard json + keyboard = keyboard.replace('"LAYOUT_JSON_HERE"', layout) + # Write our info.json + file = open(str(out_path) + "/info.json", "w") + file.write(keyboard) + file.close() + cli.log.info('Wrote out {fg_cyan}%s/info.json', str(out_path)) diff --git a/lib/python/qmk/cli/list/keyboards.py b/lib/python/qmk/cli/list/keyboards.py index 53a7af75c6..76e7760e84 100644 --- a/lib/python/qmk/cli/list/keyboards.py +++ b/lib/python/qmk/cli/list/keyboards.py @@ -1,26 +1,27 @@ """List the keyboards currently defined within QMK """ import os -import re import glob from milc import cli +BASE_PATH = os.path.join(os.getcwd(), "keyboards") + os.path.sep +KB_WILDCARD = os.path.join(BASE_PATH, "**", "rules.mk") + + +def find_name(path): + """Determine the keyboard name by stripping off the base_path and rules.mk. + """ + return path.replace(BASE_PATH, "").replace(os.path.sep + "rules.mk", "") + + @cli.subcommand("List the keyboards currently defined within QMK") def list_keyboards(cli): """List the keyboards currently defined within QMK """ - - base_path = os.path.join(os.getcwd(), "keyboards") + os.path.sep - kb_path_wildcard = os.path.join(base_path, "**", "rules.mk") - # find everywhere we have rules.mk where keymaps isn't in the path - paths = [path for path in glob.iglob(kb_path_wildcard, recursive=True) if 'keymaps' not in path] - - # strip the keyboard directory path prefix and rules.mk suffix and alphabetize - find_name = lambda path: path.replace(base_path, "").replace(os.path.sep + "rules.mk", "") - names = sorted(map(find_name, paths)) + paths = [path for path in glob.iglob(KB_WILDCARD, recursive=True) if 'keymaps' not in path] - for name in names: - # We echo instead of cli.log.info to allow easier piping of this output - cli.echo(name) + # Extract the keyboard name from the path and print it + for keyboard_name in sorted(map(find_name, paths)): + print(keyboard_name) diff --git a/lib/python/qmk/cli/pytest.py b/lib/python/qmk/cli/pytest.py index 14613e1d96..09611d750f 100644 --- a/lib/python/qmk/cli/pytest.py +++ b/lib/python/qmk/cli/pytest.py @@ -2,19 +2,15 @@ QMK script to run unit and integration tests against our python code. """ -import sys +import subprocess + from milc import cli @cli.subcommand('QMK Python Unit Tests') def pytest(cli): - """Use nose2 to run unittests + """Run several linting/testing commands. """ - try: - import nose2 - - except ImportError: - cli.log.error('Could not import nose2! Please install it with {fg_cyan}pip3 install nose2') - return False - - nose2.discover(argv=['nose2', '-v']) + flake8 = subprocess.run(['flake8', 'lib/python', 'bin/qmk']) + nose2 = subprocess.run(['nose2', '-v']) + return flake8.returncode | nose2.returncode |