diff options
Diffstat (limited to 'lib/python')
-rw-r--r-- | lib/python/qmk/cli/__init__.py | 1 | ||||
-rw-r--r-- | lib/python/qmk/cli/c2json.py | 62 | ||||
-rwxr-xr-x | lib/python/qmk/cli/doctor.py | 4 | ||||
-rwxr-xr-x | lib/python/qmk/cli/info.py | 11 | ||||
-rwxr-xr-x | lib/python/qmk/cli/json/keymap.py | 2 | ||||
-rwxr-xr-x | lib/python/qmk/cli/json2c.py | 4 | ||||
-rwxr-xr-x | lib/python/qmk/cli/kle2json.py | 6 | ||||
-rw-r--r-- | lib/python/qmk/cli/list/keymaps.py | 2 | ||||
-rwxr-xr-x | lib/python/qmk/cli/new/keymap.py | 6 | ||||
-rw-r--r-- | lib/python/qmk/commands.py | 3 | ||||
-rw-r--r-- | lib/python/qmk/constants.py | 7 | ||||
-rw-r--r-- | lib/python/qmk/info.py | 35 | ||||
-rw-r--r-- | lib/python/qmk/keymap.py | 272 | ||||
-rw-r--r-- | lib/python/qmk/tests/.gitignore | 2 | ||||
-rw-r--r-- | lib/python/qmk/tests/test_cli_commands.py | 25 | ||||
-rw-r--r-- | lib/python/qmk/tests/test_qmk_keymap.py | 20 |
16 files changed, 397 insertions, 65 deletions
diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index 47f60c601b..ba964ebbbb 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py @@ -6,6 +6,7 @@ import sys from milc import cli +from . import c2json from . import cformat from . import compile from . import config diff --git a/lib/python/qmk/cli/c2json.py b/lib/python/qmk/cli/c2json.py new file mode 100644 index 0000000000..0267303fd2 --- /dev/null +++ b/lib/python/qmk/cli/c2json.py @@ -0,0 +1,62 @@ +"""Generate a keymap.json from a keymap.c file. +""" +import json +import sys + +from milc import cli + +import qmk.keymap +import qmk.path + + +@cli.argument('--no-cpp', arg_only=True, action='store_false', help='Do not use \'cpp\' on keymap.c') +@cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to') +@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") +@cli.argument('-kb', '--keyboard', arg_only=True, required=True, help='The keyboard\'s name') +@cli.argument('-km', '--keymap', arg_only=True, required=True, help='The keymap\'s name') +@cli.argument('filename', arg_only=True, help='keymap.c file') +@cli.subcommand('Creates a keymap.json from a keymap.c file.') +def c2json(cli): + """Generate a keymap.json from a keymap.c file. + + This command uses the `qmk.keymap` module to generate a keymap.json from a keymap.c file. The generated keymap is written to stdout, or to a file if -o is provided. + """ + cli.args.filename = qmk.path.normpath(cli.args.filename) + + # Error checking + if not cli.args.filename.exists(): + cli.log.error('C file does not exist!') + cli.print_usage() + exit(1) + + if str(cli.args.filename) == '-': + # TODO(skullydazed/anyone): Read file contents from STDIN + cli.log.error('Reading from STDIN is not (yet) supported.') + cli.print_usage() + exit(1) + + # Environment processing + if cli.args.output == ('-'): + cli.args.output = None + + # Parse the keymap.c + keymap_json = qmk.keymap.c2json(cli.args.keyboard, cli.args.keymap, cli.args.filename, use_cpp=cli.args.no_cpp) + + # Generate the keymap.json + try: + keymap_json = qmk.keymap.generate(keymap_json['keyboard'], keymap_json['layout'], keymap_json['layers'], type='json', keymap=keymap_json['keymap']) + except KeyError: + cli.log.error('Something went wrong. Try to use --no-cpp.') + sys.exit(1) + + if cli.args.output: + cli.args.output.parent.mkdir(parents=True, exist_ok=True) + if cli.args.output.exists(): + cli.args.output.replace(cli.args.output.name + '.bak') + cli.args.output.write_text(json.dumps(keymap_json)) + + if not cli.args.quiet: + cli.log.info('Wrote keymap to %s.', cli.args.output) + + else: + print(json.dumps(keymap_json)) diff --git a/lib/python/qmk/cli/doctor.py b/lib/python/qmk/cli/doctor.py index bad864f72d..7fafd57575 100755 --- a/lib/python/qmk/cli/doctor.py +++ b/lib/python/qmk/cli/doctor.py @@ -58,7 +58,7 @@ def parse_gcc_version(version): return { 'major': int(m.group(1)), 'minor': int(m.group(2)) if m.group(2) else 0, - 'patch': int(m.group(3)) if m.group(3) else 0 + 'patch': int(m.group(3)) if m.group(3) else 0, } @@ -364,3 +364,5 @@ def doctor(cli): else: cli.log.info('{fg_yellow}Problems detected, please fix these problems before proceeding.') # FIXME(skullydazed/unclaimed): Link to a document about troubleshooting, or discord or something + + return ok diff --git a/lib/python/qmk/cli/info.py b/lib/python/qmk/cli/info.py index 5e4b391411..0e64d40742 100755 --- a/lib/python/qmk/cli/info.py +++ b/lib/python/qmk/cli/info.py @@ -134,11 +134,11 @@ def info(cli): if not cli.config.info.keyboard: cli.log.error('Missing paramater: --keyboard') cli.subcommands['info'].print_help() - exit(1) + return False if not is_keyboard(cli.config.info.keyboard): cli.log.error('Invalid keyboard: "%s"', cli.config.info.keyboard) - exit(1) + return False # Build the info.json file kb_info_json = info_json(cli.config.info.keyboard) @@ -146,13 +146,10 @@ def info(cli): # Output in the requested format if cli.args.format == 'json': print(json.dumps(kb_info_json)) - exit() - - if cli.args.format == 'text': + elif cli.args.format == 'text': print_text_output(kb_info_json) - elif cli.args.format == 'friendly': print_friendly_output(kb_info_json) - else: cli.log.error('Unknown format: %s', cli.args.format) + return False diff --git a/lib/python/qmk/cli/json/keymap.py b/lib/python/qmk/cli/json/keymap.py index c97a2d0462..2af9faaa72 100755 --- a/lib/python/qmk/cli/json/keymap.py +++ b/lib/python/qmk/cli/json/keymap.py @@ -13,4 +13,4 @@ def json_keymap(cli): """Renamed to `qmk json2c`. """ cli.log.error('This command has been renamed to `qmk json2c`.') - exit(1) + return False diff --git a/lib/python/qmk/cli/json2c.py b/lib/python/qmk/cli/json2c.py index af0d80a9ac..2a90094368 100755 --- a/lib/python/qmk/cli/json2c.py +++ b/lib/python/qmk/cli/json2c.py @@ -22,12 +22,12 @@ def json2c(cli): # TODO(skullydazed/anyone): Read file contents from STDIN cli.log.error('Reading from STDIN is not (yet) supported.') cli.print_usage() - exit(1) + return False if not cli.args.filename.exists(): cli.log.error('JSON file does not exist!') cli.print_usage() - exit(1) + return False # Environment processing if cli.args.output and cli.args.output.name == '-': diff --git a/lib/python/qmk/cli/kle2json.py b/lib/python/qmk/cli/kle2json.py index 798f95fd19..3d1bb8c43c 100755 --- a/lib/python/qmk/cli/kle2json.py +++ b/lib/python/qmk/cli/kle2json.py @@ -37,7 +37,8 @@ def kle2json(cli): 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.', file_path) + cli.log.error('File {fg_cyan}%s{style_reset_all} was not found.', file_path) + return False out_path = file_path.parent raw_code = file_path.open().read() # Check if info.json exists, allow overwrite with force @@ -50,8 +51,7 @@ def kle2json(cli): 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.') + return False keyboard = OrderedDict( keyboard_name=kle.name, url='', diff --git a/lib/python/qmk/cli/list/keymaps.py b/lib/python/qmk/cli/list/keymaps.py index b18289eb35..49bc84b2ce 100644 --- a/lib/python/qmk/cli/list/keymaps.py +++ b/lib/python/qmk/cli/list/keymaps.py @@ -15,7 +15,7 @@ def list_keymaps(cli): """ if not is_keyboard(cli.config.list_keymaps.keyboard): cli.log.error('Keyboard %s does not exist!', cli.config.list_keymaps.keyboard) - exit(1) + return False for name in qmk.keymap.list_keymaps(cli.config.list_keymaps.keyboard): print(name) diff --git a/lib/python/qmk/cli/new/keymap.py b/lib/python/qmk/cli/new/keymap.py index 474fe7974f..52c564997b 100755 --- a/lib/python/qmk/cli/new/keymap.py +++ b/lib/python/qmk/cli/new/keymap.py @@ -29,15 +29,15 @@ def new_keymap(cli): # check directories if not kb_path.exists(): cli.log.error('Keyboard %s does not exist!', kb_path) - exit(1) + return False if not keymap_path_default.exists(): cli.log.error('Keyboard default %s does not exist!', keymap_path_default) - exit(1) + return False if keymap_path_new.exists(): cli.log.error('Keymap %s already exists!', keymap_path_new) - exit(1) + return False # create user directory with default keymap files shutil.copytree(keymap_path_default, keymap_path_new, symlinks=True) diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py index 4db4667a8e..5a6e60988a 100644 --- a/lib/python/qmk/commands.py +++ b/lib/python/qmk/commands.py @@ -7,7 +7,6 @@ import subprocess import shlex import shutil -from milc import cli import qmk.keymap @@ -84,6 +83,4 @@ def run(command, *args, **kwargs): safecmd = ' '.join(safecmd) command = [os.environ['SHELL'], '-c', safecmd] - cli.log.debug('Running command: %s', command) - return subprocess.run(command, *args, **kwargs) diff --git a/lib/python/qmk/constants.py b/lib/python/qmk/constants.py index 0450724df4..102111d7c4 100644 --- a/lib/python/qmk/constants.py +++ b/lib/python/qmk/constants.py @@ -9,7 +9,6 @@ QMK_FIRMWARE = Path.cwd() MAX_KEYBOARD_SUBFOLDERS = 5 # Supported processor types -ARM_PROCESSORS = 'cortex-m0', 'cortex-m0plus', 'cortex-m3', 'cortex-m4', 'MKL26Z64', 'MK20DX128', 'MK20DX256', 'STM32F042', 'STM32F072', 'STM32F103', 'STM32F303', 'STM32F401', 'STM32F411' -AVR_PROCESSORS = 'atmega16u2', 'atmega32u2', 'atmega16u4', 'atmega32u4', 'at90usb646', 'at90usb647', 'at90usb1286', 'at90usb1287', 'atmega328p', 'atmega32a', None -ALL_PROCESSORS = ARM_PROCESSORS + AVR_PROCESSORS -VUSB_PROCESSORS = 'atmega328p', 'atmega32a', 'atmega328', 'attiny85' +CHIBIOS_PROCESSORS = 'cortex-m0', 'cortex-m0plus', 'cortex-m3', 'cortex-m4', 'MKL26Z64', 'MK20DX128', 'MK20DX256', 'STM32F042', 'STM32F072', 'STM32F103', 'STM32F303', 'STM32F401', 'STM32F411' +LUFA_PROCESSORS = 'atmega16u2', 'atmega32u2', 'atmega16u4', 'atmega32u4', 'at90usb646', 'at90usb647', 'at90usb1286', 'at90usb1287', None +VUSB_PROCESSORS = 'atmega32a', 'atmega328p', 'atmega328', 'attiny85' diff --git a/lib/python/qmk/info.py b/lib/python/qmk/info.py index de7632e378..15a5c097a5 100644 --- a/lib/python/qmk/info.py +++ b/lib/python/qmk/info.py @@ -6,15 +6,22 @@ from pathlib import Path from milc import cli -from qmk.constants import ARM_PROCESSORS, AVR_PROCESSORS, VUSB_PROCESSORS +from qmk.constants import CHIBIOS_PROCESSORS, LUFA_PROCESSORS, VUSB_PROCESSORS from qmk.c_parse import find_layouts from qmk.keyboard import config_h, rules_mk +from qmk.makefile import parse_rules_mk_file from qmk.math import compute def info_json(keyboard): """Generate the info.json data for a specific keyboard. """ + cur_dir = Path('keyboards') + rules = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk') + if 'DEFAULT_FOLDER' in rules: + keyboard = rules['DEFAULT_FOLDER'] + rules = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk', rules) + info_data = { 'keyboard_name': str(keyboard), 'keyboard_folder': str(keyboard), @@ -22,7 +29,7 @@ def info_json(keyboard): 'maintainer': 'qmk', } - for layout_name, layout_json in _find_all_layouts(keyboard).items(): + for layout_name, layout_json in _find_all_layouts(keyboard, rules).items(): if not layout_name.startswith('LAYOUT_kc'): info_data['layouts'][layout_name] = layout_json @@ -88,9 +95,9 @@ def _extract_rules_mk(info_data): rules = rules_mk(info_data['keyboard_folder']) mcu = rules.get('MCU') - if mcu in ARM_PROCESSORS: + if mcu in CHIBIOS_PROCESSORS: arm_processor_rules(info_data, rules) - elif mcu in AVR_PROCESSORS: + elif mcu in LUFA_PROCESSORS + VUSB_PROCESSORS: avr_processor_rules(info_data, rules) else: cli.log.warning("%s: Unknown MCU: %s" % (info_data['keyboard_folder'], mcu)) @@ -99,22 +106,24 @@ def _extract_rules_mk(info_data): return info_data -def _find_all_layouts(keyboard): - """Looks for layout macros associated with this keyboard. - """ - layouts = {} - rules = rules_mk(keyboard) - keyboard_path = Path(rules.get('DEFAULT_FOLDER', keyboard)) - - # Pull in all layouts defined in the standard files +def _search_keyboard_h(path): current_path = Path('keyboards/') - for directory in keyboard_path.parts: + layouts = {} + for directory in path.parts: current_path = current_path / directory keyboard_h = '%s.h' % (directory,) keyboard_h_path = current_path / keyboard_h if keyboard_h_path.exists(): layouts.update(find_layouts(keyboard_h_path)) + return layouts + + +def _find_all_layouts(keyboard, rules): + """Looks for layout macros associated with this keyboard. + """ + layouts = _search_keyboard_h(Path(keyboard)) + if not layouts: # If we didn't find any layouts above we widen our search. This is error # prone which is why we want to encourage people to follow the standard above. diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py index 78510a8a78..166697ee6a 100644 --- a/lib/python/qmk/keymap.py +++ b/lib/python/qmk/keymap.py @@ -1,11 +1,18 @@ """Functions that help you work with QMK keymaps. """ from pathlib import Path +import json +import subprocess + +from pygments.lexers.c_cpp import CLexer +from pygments.token import Token +from pygments import lex from milc import cli from qmk.keyboard import rules_mk import qmk.path +import qmk.commands # The `keymap.c` template to use when a keyboard doesn't have its own DEFAULT_KEYMAP_C = """#include QMK_KEYBOARD_H @@ -22,22 +29,35 @@ __KEYMAP_GOES_HERE__ """ -def template(keyboard): - """Returns the `keymap.c` template for a keyboard. +def template(keyboard, type='c'): + """Returns the `keymap.c` or `keymap.json` template for a keyboard. If a template exists in `keyboards/<keyboard>/templates/keymap.c` that text will be used instead of `DEFAULT_KEYMAP_C`. + If a template exists in `keyboards/<keyboard>/templates/keymap.json` that + text will be used instead of an empty dictionary. + Args: keyboard The keyboard to return a template for. - """ - template_file = Path('keyboards/%s/templates/keymap.c' % keyboard) - if template_file.exists(): - return template_file.read_text() + type + 'json' for `keymap.json` and 'c' (or anything else) for `keymap.c` + """ + if type == 'json': + template_file = Path('keyboards/%s/templates/keymap.json' % keyboard) + template = {'keyboard': keyboard} + if template_file.exists(): + template.update(json.loads(template_file.read_text())) + else: + template_file = Path('keyboards/%s/templates/keymap.c' % keyboard) + if template_file.exists(): + template = template_file.read_text() + else: + template = DEFAULT_KEYMAP_C - return DEFAULT_KEYMAP_C + return template def _strip_any(keycode): @@ -57,8 +77,8 @@ def is_keymap_dir(keymap): return True -def generate(keyboard, layout, layers): - """Returns a keymap.c for the specified keyboard, layout, and layers. +def generate(keyboard, layout, layers, type='c', keymap=None): + """Returns a `keymap.c` or `keymap.json` for the specified keyboard, layout, and layers. Args: keyboard @@ -69,24 +89,32 @@ def generate(keyboard, layout, layers): layers An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode. - """ - layer_txt = [] - for layer_num, layer in enumerate(layers): - if layer_num != 0: - layer_txt[-1] = layer_txt[-1] + ',' + type + 'json' for `keymap.json` and 'c' (or anything else) for `keymap.c` + """ + new_keymap = template(keyboard, type) + if type == 'json': + new_keymap['keymap'] = keymap + new_keymap['layout'] = layout + new_keymap['layers'] = layers + else: + layer_txt = [] + for layer_num, layer in enumerate(layers): + if layer_num != 0: + layer_txt[-1] = layer_txt[-1] + ',' - layer = map(_strip_any, layer) - layer_keys = ', '.join(layer) - layer_txt.append('\t[%s] = %s(%s)' % (layer_num, layout, layer_keys)) + layer = map(_strip_any, layer) + layer_keys = ', '.join(layer) + layer_txt.append('\t[%s] = %s(%s)' % (layer_num, layout, layer_keys)) - keymap = '\n'.join(layer_txt) - keymap_c = template(keyboard) + keymap = '\n'.join(layer_txt) + new_keymap = new_keymap.replace('__KEYMAP_GOES_HERE__', keymap) - return keymap_c.replace('__KEYMAP_GOES_HERE__', keymap) + return new_keymap -def write(keyboard, keymap, layout, layers): +def write(keyboard, keymap, layout, layers, type='c'): """Generate the `keymap.c` and write it to disk. Returns the filename written to. @@ -103,12 +131,19 @@ def write(keyboard, keymap, layout, layers): layers An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode. + + type + 'json' for `keymap.json` and 'c' (or anything else) for `keymap.c` """ - keymap_c = generate(keyboard, layout, layers) - keymap_file = qmk.path.keymap(keyboard) / keymap / 'keymap.c' + keymap_content = generate(keyboard, layout, layers, type) + if type == 'json': + keymap_file = qmk.path.keymap(keyboard) / keymap / 'keymap.json' + keymap_content = json.dumps(keymap_content) + else: + keymap_file = qmk.path.keymap(keyboard) / keymap / 'keymap.c' keymap_file.parent.mkdir(parents=True, exist_ok=True) - keymap_file.write_text(keymap_c) + keymap_file.write_text(keymap_content) cli.log.info('Wrote keymap to {fg_cyan}%s', keymap_file) @@ -188,3 +223,192 @@ def list_keymaps(keyboard): names = names.union([keymap.name for keymap in cl_path.iterdir() if is_keymap_dir(keymap)]) return sorted(names) + + +def _c_preprocess(path): + """ Run a file through the C pre-processor + + Args: + path: path of the keymap.c file + + Returns: + the stdout of the pre-processor + """ + pre_processed_keymap = qmk.commands.run(['cpp', path], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) + return pre_processed_keymap.stdout + + +def _get_layers(keymap): # noqa C901 : until someone has a good idea how to simplify/split up this code + """ Find the layers in a keymap.c file. + + Args: + keymap: the content of the keymap.c file + + Returns: + a dictionary containing the parsed keymap + """ + layers = list() + opening_braces = '({[' + closing_braces = ')}]' + keymap_certainty = brace_depth = 0 + is_keymap = is_layer = is_adv_kc = False + layer = dict(name=False, layout=False, keycodes=list()) + for line in lex(keymap, CLexer()): + if line[0] is Token.Name: + if is_keymap: + # If we are inside the keymap array + # we know the keymap's name and the layout macro will come, + # followed by the keycodes + if not layer['name']: + if line[1].startswith('LAYOUT') or line[1].startswith('KEYMAP'): + # This can happen if the keymap array only has one layer, + # for macropads and such + layer['name'] = '0' + layer['layout'] = line[1] + else: + layer['name'] = line[1] + elif not layer['layout']: + layer['layout'] = line[1] + elif is_layer: + # If we are inside a layout macro, + # collect all keycodes + if line[1] == '_______': + kc = 'KC_TRNS' + elif line[1] == 'XXXXXXX': + kc = 'KC_NO' + else: + kc = line[1] + if is_adv_kc: + # If we are inside an advanced keycode + # collect everything and hope the user + # knew what he/she was doing + layer['keycodes'][-1] += kc + else: + layer['keycodes'].append(kc) + + # The keymaps array's signature: + # const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] + # + # Only if we've found all 6 keywords in this specific order + # can we know for sure that we are inside the keymaps array + elif line[1] == 'PROGMEM' and keymap_certainty == 2: + keymap_certainty = 3 + elif line[1] == 'keymaps' and keymap_certainty == 3: + keymap_certainty = 4 + elif line[1] == 'MATRIX_ROWS' and keymap_certainty == 4: + keymap_certainty = 5 + elif line[1] == 'MATRIX_COLS' and keymap_certainty == 5: + keymap_certainty = 6 + elif line[0] is Token.Keyword: + if line[1] == 'const' and keymap_certainty == 0: + keymap_certainty = 1 + elif line[0] is Token.Keyword.Type: + if line[1] == 'uint16_t' and keymap_certainty == 1: + keymap_certainty = 2 + elif line[0] is Token.Punctuation: + if line[1] in opening_braces: + brace_depth += 1 + if is_keymap: + if is_layer: + # We found the beginning of a non-basic keycode + is_adv_kc = True + layer['keycodes'][-1] += line[1] + elif line[1] == '(' and brace_depth == 2: + # We found the beginning of a layer + is_layer = True + elif line[1] == '{' and keymap_certainty == 6: + # We found the beginning of the keymaps array + is_keymap = True + elif line[1] in closing_braces: + brace_depth -= 1 + if is_keymap: + if is_adv_kc: + layer['keycodes'][-1] += line[1] + if brace_depth == 2: + # We found the end of a non-basic keycode + is_adv_kc = False + elif line[1] == ')' and brace_depth == 1: + # We found the end of a layer + is_layer = False + layers.append(layer) + layer = dict(name=False, layout=False, keycodes=list()) + elif line[1] == '}' and brace_depth == 0: + # We found the end of the keymaps array + is_keymap = False + keymap_certainty = 0 + elif is_adv_kc: + # Advanced keycodes can contain other punctuation + # e.g.: MT(MOD_LCTL | MOD_LSFT, KC_ESC) + layer['keycodes'][-1] += line[1] + + elif line[0] is Token.Literal.Number.Integer and is_keymap and not is_adv_kc: + # If the pre-processor finds the 'meaning' of the layer names, + # they will be numbers + if not layer['name']: + layer['name'] = line[1] + + else: + # We only care about + # operators and such if we + # are inside an advanced keycode + # e.g.: MT(MOD_LCTL | MOD_LSFT, KC_ESC) + if is_adv_kc: + layer['keycodes'][-1] += line[1] + + return layers + + +def parse_keymap_c(keymap_file, use_cpp=True): + """ Parse a keymap.c file. + + Currently only cares about the keymaps array. + + Args: + keymap_file: path of the keymap.c file + + use_cpp: if True, pre-process the file with the C pre-processor + + Returns: + a dictionary containing the parsed keymap + """ + if use_cpp: + keymap_file = _c_preprocess(keymap_file) + else: + keymap_file = keymap_file.read_text() + + keymap = dict() + keymap['layers'] = _get_layers(keymap_file) + return keymap + + +def c2json(keyboard, keymap, keymap_file, use_cpp=True): + """ Convert keymap.c to keymap.json + + Args: + keyboard: The name of the keyboard + + keymap: The name of the keymap + + layout: The LAYOUT macro this keymap uses. + + keymap_file: path of the keymap.c file + + use_cpp: if True, pre-process the file with the C pre-processor + + Returns: + a dictionary in keymap.json format + """ + keymap_json = parse_keymap_c(keymap_file, use_cpp) + + dirty_layers = keymap_json.pop('layers', None) + keymap_json['layers'] = list() + for layer in dirty_layers: + layer.pop('name') + layout = layer.pop('layout') + if not keymap_json.get('layout', False): + keymap_json['layout'] = layout + keymap_json['layers'].append(layer.pop('keycodes')) + + keymap_json['keyboard'] = keyboard + keymap_json['keymap'] = keymap + return keymap_json diff --git a/lib/python/qmk/tests/.gitignore b/lib/python/qmk/tests/.gitignore new file mode 100644 index 0000000000..eeb6581b87 --- /dev/null +++ b/lib/python/qmk/tests/.gitignore @@ -0,0 +1,2 @@ +# Ignore generated info.json from pytest +info.json diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index 68f8ed6047..7ac0bcbde7 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py @@ -1,10 +1,11 @@ -import subprocess +from subprocess import STDOUT, PIPE + from qmk.commands import run def check_subcommand(command, *args): cmd = ['bin/qmk', command] + list(args) - result = run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True) + result = run(cmd, stdout=PIPE, stderr=STDOUT, universal_newlines=True) return result @@ -28,6 +29,11 @@ def test_compile(): check_returncode(result) +def test_compile_json(): + result = check_subcommand('compile', '-kb', 'handwired/onekey/pytest', '-km', 'default_json') + check_returncode(result) + + def test_flash(): result = check_subcommand('flash', '-kb', 'handwired/onekey/pytest', '-km', 'default', '-n') check_returncode(result) @@ -45,8 +51,9 @@ def test_config(): def test_kle2json(): - result = check_subcommand('kle2json', 'kle.txt', '-f') + result = check_subcommand('kle2json', 'lib/python/qmk/tests/kle.txt', '-f') check_returncode(result) + assert 'Wrote out' in result.stdout def test_doctor(): @@ -152,3 +159,15 @@ def test_info_matrix_render(): assert 'LAYOUT_ortho_1x1' in result.stdout assert '│0A│' in result.stdout assert 'Matrix for "LAYOUT_ortho_1x1"' in result.stdout + + +def test_c2json(): + result = check_subcommand("c2json", "-kb", "handwired/onekey/pytest", "-km", "default", "keyboards/handwired/onekey/keymaps/default/keymap.c") + check_returncode(result) + assert result.stdout.strip() == '{"keyboard": "handwired/onekey/pytest", "documentation": "This file is a keymap.json file for handwired/onekey/pytest", "keymap": "default", "layout": "LAYOUT_ortho_1x1", "layers": [["KC_A"]]}' + + +def test_c2json_nocpp(): + result = check_subcommand("c2json", "--no-cpp", "-kb", "handwired/onekey/pytest", "-km", "default", "keyboards/handwired/onekey/keymaps/pytest_nocpp/keymap.c") + check_returncode(result) + assert result.stdout.strip() == '{"keyboard": "handwired/onekey/pytest", "documentation": "This file is a keymap.json file for handwired/onekey/pytest", "keymap": "default", "layout": "LAYOUT", "layers": [["KC_ENTER"]]}' diff --git a/lib/python/qmk/tests/test_qmk_keymap.py b/lib/python/qmk/tests/test_qmk_keymap.py index d8669e5498..7ef708e0de 100644 --- a/lib/python/qmk/tests/test_qmk_keymap.py +++ b/lib/python/qmk/tests/test_qmk_keymap.py @@ -6,14 +6,34 @@ def test_template_onekey_proton_c(): assert templ == qmk.keymap.DEFAULT_KEYMAP_C +def test_template_onekey_proton_c_json(): + templ = qmk.keymap.template('handwired/onekey/proton_c', type='json') + assert templ == {'keyboard': 'handwired/onekey/proton_c'} + + def test_template_onekey_pytest(): templ = qmk.keymap.template('handwired/onekey/pytest') assert templ == '#include QMK_KEYBOARD_H\nconst uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {__KEYMAP_GOES_HERE__};\n' +def test_template_onekey_pytest_json(): + templ = qmk.keymap.template('handwired/onekey/pytest', type='json') + assert templ == {'keyboard': 'handwired/onekey/pytest', "documentation": "This file is a keymap.json file for handwired/onekey/pytest"} + + def test_generate_onekey_pytest(): templ = qmk.keymap.generate('handwired/onekey/pytest', 'LAYOUT', [['KC_A']]) assert templ == '#include QMK_KEYBOARD_H\nconst uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {\t[0] = LAYOUT(KC_A)};\n' +def test_generate_onekey_pytest_json(): + templ = qmk.keymap.generate('handwired/onekey/pytest', 'LAYOUT', [['KC_A']], type='json', keymap='default') + assert templ == {"keyboard": "handwired/onekey/pytest", "documentation": "This file is a keymap.json file for handwired/onekey/pytest", "keymap": "default", "layout": "LAYOUT", "layers": [["KC_A"]]} + + +def test_parse_keymap_c(): + parsed_keymap_c = qmk.keymap.parse_keymap_c('keyboards/handwired/onekey/keymaps/default/keymap.c') + assert parsed_keymap_c == {'layers': [{'name': '0', 'layout': 'LAYOUT_ortho_1x1', 'keycodes': ['KC_A']}]} + + # FIXME(skullydazed): Add a test for qmk.keymap.write that mocks up an FD. |