diff options
Diffstat (limited to 'lib/python/qmk')
-rwxr-xr-x | lib/python/qmk/cli/generate/api.py | 28 | ||||
-rwxr-xr-x | lib/python/qmk/cli/generate/config_h.py | 17 | ||||
-rw-r--r-- | lib/python/qmk/cli/generate/docs.py | 7 | ||||
-rwxr-xr-x | lib/python/qmk/cli/generate/rules_mk.py | 14 | ||||
-rwxr-xr-x | lib/python/qmk/cli/info.py | 16 | ||||
-rw-r--r-- | lib/python/qmk/cli/pytest.py | 3 | ||||
-rw-r--r-- | lib/python/qmk/commands.py | 2 | ||||
-rw-r--r-- | lib/python/qmk/info.py | 78 | ||||
-rw-r--r-- | lib/python/qmk/tests/test_cli_commands.py | 4 |
9 files changed, 121 insertions, 48 deletions
diff --git a/lib/python/qmk/cli/generate/api.py b/lib/python/qmk/cli/generate/api.py index 285bd90eb5..0596b3f22b 100755 --- a/lib/python/qmk/cli/generate/api.py +++ b/lib/python/qmk/cli/generate/api.py @@ -1,7 +1,7 @@ """This script automates the generation of the QMK API data. """ from pathlib import Path -from shutil import copyfile +import shutil import json from milc import cli @@ -12,28 +12,42 @@ from qmk.json_encoders import InfoJSONEncoder from qmk.json_schema import json_load from qmk.keyboard import find_readme, list_keyboards +TEMPLATE_PATH = Path('data/templates/api/') +BUILD_API_PATH = Path('.build/api_data/') + @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't write the data to disk.") +@cli.argument('-f', '--filter', arg_only=True, action='append', default=[], help="Filter the list of keyboards based on partial name matches the supplied value. May be passed multiple times.") @cli.subcommand('Creates a new keymap for the keyboard of your choosing', hidden=False if cli.config.user.developer else True) def generate_api(cli): """Generates the QMK API data. """ - api_data_dir = Path('api_data') - v1_dir = api_data_dir / 'v1' + if BUILD_API_PATH.exists(): + shutil.rmtree(BUILD_API_PATH) + + shutil.copytree(TEMPLATE_PATH, BUILD_API_PATH) + + v1_dir = BUILD_API_PATH / 'v1' keyboard_all_file = v1_dir / 'keyboards.json' # A massive JSON containing everything keyboard_list_file = v1_dir / 'keyboard_list.json' # A simple list of keyboard targets keyboard_aliases_file = v1_dir / 'keyboard_aliases.json' # A list of historical keyboard names and their new name keyboard_metadata_file = v1_dir / 'keyboard_metadata.json' # All the data configurator/via needs for initialization usb_file = v1_dir / 'usb.json' # A mapping of USB VID/PID -> keyboard target - if not api_data_dir.exists(): - api_data_dir.mkdir() + # Filter down when required + keyboard_list = list_keyboards() + if cli.args.filter: + kb_list = [] + for keyboard_name in keyboard_list: + if any(i in keyboard_name for i in cli.args.filter): + kb_list.append(keyboard_name) + keyboard_list = kb_list kb_all = {} usb_list = {} # Generate and write keyboard specific JSON files - for keyboard_name in list_keyboards(): + for keyboard_name in keyboard_list: kb_all[keyboard_name] = info_json(keyboard_name) keyboard_dir = v1_dir / 'keyboards' / keyboard_name keyboard_info = keyboard_dir / 'info.json' @@ -47,7 +61,7 @@ def generate_api(cli): cli.log.debug('Wrote file %s', keyboard_info) if keyboard_readme_src: - copyfile(keyboard_readme_src, keyboard_readme) + shutil.copyfile(keyboard_readme_src, keyboard_readme) cli.log.debug('Copied %s -> %s', keyboard_readme_src, keyboard_readme) if 'usb' in kb_all[keyboard_name]: diff --git a/lib/python/qmk/cli/generate/config_h.py b/lib/python/qmk/cli/generate/config_h.py index fdc76b23d4..20c9595ed7 100755 --- a/lib/python/qmk/cli/generate/config_h.py +++ b/lib/python/qmk/cli/generate/config_h.py @@ -5,10 +5,9 @@ from pathlib import Path from dotty_dict import dotty from milc import cli -from qmk.info import info_json -from qmk.json_schema import json_load, validate +from qmk.info import info_json, keymap_json_config +from qmk.json_schema import json_load from qmk.keyboard import keyboard_completer, keyboard_folder -from qmk.keymap import locate_keymap from qmk.commands import dump_lines from qmk.path import normpath from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE @@ -84,7 +83,7 @@ def generate_config_items(kb_info_json, config_h_lines): for config_key, info_dict in info_config_map.items(): info_key = info_dict['info_key'] - key_type = info_dict.get('value_type', 'str') + key_type = info_dict.get('value_type', 'raw') to_config = info_dict.get('to_config', True) if not to_config: @@ -112,6 +111,11 @@ def generate_config_items(kb_info_json, config_h_lines): config_h_lines.append(f'#ifndef {key}') config_h_lines.append(f'# define {key} {value}') config_h_lines.append(f'#endif // {key}') + elif key_type == 'str': + config_h_lines.append('') + config_h_lines.append(f'#ifndef {config_key}') + config_h_lines.append(f'# define {config_key} "{config_value}"') + config_h_lines.append(f'#endif // {config_key}') elif key_type == 'bcd_version': (major, minor, revision) = config_value.split('.') config_h_lines.append('') @@ -175,10 +179,7 @@ def generate_config_h(cli): """ # Determine our keyboard/keymap if cli.args.keymap: - km = locate_keymap(cli.args.keyboard, cli.args.keymap) - km_json = json_load(km) - validate(km_json, 'qmk.keymap.v1') - kb_info_json = dotty(km_json.get('config', {})) + kb_info_json = dotty(keymap_json_config(cli.args.keyboard, cli.args.keymap)) else: kb_info_json = dotty(info_json(cli.args.keyboard)) diff --git a/lib/python/qmk/cli/generate/docs.py b/lib/python/qmk/cli/generate/docs.py index 74112d834d..eb3099e138 100644 --- a/lib/python/qmk/cli/generate/docs.py +++ b/lib/python/qmk/cli/generate/docs.py @@ -10,6 +10,7 @@ DOCS_PATH = Path('docs/') BUILD_PATH = Path('.build/') BUILD_DOCS_PATH = BUILD_PATH / 'docs' DOXYGEN_PATH = BUILD_PATH / 'doxygen' +MOXYGEN_PATH = BUILD_DOCS_PATH / 'internals' @cli.subcommand('Build QMK documentation.', hidden=False if cli.config.user.developer else True) @@ -34,10 +35,10 @@ def generate_docs(cli): 'stdin': DEVNULL, } - cli.log.info('Generating internal docs...') + cli.log.info('Generating docs...') # Generate internal docs cli.run(['doxygen', 'Doxyfile'], **args) - cli.run(['moxygen', '-q', '-g', '-o', BUILD_DOCS_PATH / 'internals_%s.md', DOXYGEN_PATH / 'xml'], **args) + cli.run(['moxygen', '-q', '-g', '-o', MOXYGEN_PATH / '%s.md', DOXYGEN_PATH / 'xml'], **args) - cli.log.info('Successfully generated internal docs to %s.', BUILD_DOCS_PATH) + cli.log.info('Successfully generated docs to %s.', BUILD_DOCS_PATH) diff --git a/lib/python/qmk/cli/generate/rules_mk.py b/lib/python/qmk/cli/generate/rules_mk.py index 29a1130f99..9623d00fb5 100755 --- a/lib/python/qmk/cli/generate/rules_mk.py +++ b/lib/python/qmk/cli/generate/rules_mk.py @@ -5,10 +5,9 @@ from pathlib import Path from dotty_dict import dotty from milc import cli -from qmk.info import info_json -from qmk.json_schema import json_load, validate +from qmk.info import info_json, keymap_json_config +from qmk.json_schema import json_load from qmk.keyboard import keyboard_completer, keyboard_folder -from qmk.keymap import locate_keymap from qmk.commands import dump_lines from qmk.path import normpath from qmk.constants import GPL2_HEADER_SH_LIKE, GENERATED_HEADER_SH_LIKE @@ -21,7 +20,7 @@ def process_mapping_rule(kb_info_json, rules_key, info_dict): return None info_key = info_dict['info_key'] - key_type = info_dict.get('value_type', 'str') + key_type = info_dict.get('value_type', 'raw') try: rules_value = kb_info_json[info_key] @@ -34,6 +33,8 @@ def process_mapping_rule(kb_info_json, rules_key, info_dict): return f'{rules_key} ?= {"yes" if rules_value else "no"}' elif key_type == 'mapping': return '\n'.join([f'{key} ?= {value}' for key, value in rules_value.items()]) + elif key_type == 'str': + return f'{rules_key} ?= "{rules_value}"' return f'{rules_key} ?= {rules_value}' @@ -49,10 +50,7 @@ def generate_rules_mk(cli): """ # Determine our keyboard/keymap if cli.args.keymap: - km = locate_keymap(cli.args.keyboard, cli.args.keymap) - km_json = json_load(km) - validate(km_json, 'qmk.keymap.v1') - kb_info_json = dotty(km_json.get('config', {})) + kb_info_json = dotty(keymap_json_config(cli.args.keyboard, cli.args.keymap)) else: kb_info_json = dotty(info_json(cli.args.keyboard)) diff --git a/lib/python/qmk/cli/info.py b/lib/python/qmk/cli/info.py index 3131d4b53f..fa5729bcc9 100755 --- a/lib/python/qmk/cli/info.py +++ b/lib/python/qmk/cli/info.py @@ -11,8 +11,8 @@ from qmk.json_encoders import InfoJSONEncoder from qmk.constants import COL_LETTERS, ROW_LETTERS from qmk.decorators import automagic_keyboard, automagic_keymap from qmk.keyboard import keyboard_completer, keyboard_folder, render_layouts, render_layout, rules_mk +from qmk.info import info_json, keymap_json from qmk.keymap import locate_keymap -from qmk.info import info_json from qmk.path import is_keyboard UNICODE_SUPPORT = sys.stdout.encoding.lower().startswith('utf') @@ -135,7 +135,7 @@ def print_parsed_rules_mk(keyboard_name): @cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='Keyboard to show info for.') -@cli.argument('-km', '--keymap', help='Show the layers for a JSON keymap too.') +@cli.argument('-km', '--keymap', help='Keymap to show info for (Optional).') @cli.argument('-l', '--layouts', action='store_true', help='Render the layouts.') @cli.argument('-m', '--matrix', action='store_true', help='Render the layouts with matrix information.') @cli.argument('-f', '--format', default='friendly', arg_only=True, help='Format to display the data in (friendly, text, json) (Default: friendly).') @@ -161,8 +161,15 @@ def info(cli): print_parsed_rules_mk(cli.config.info.keyboard) return False + # default keymap stored in config file should be ignored + if cli.config_source.info.keymap == 'config_file': + cli.config_source.info.keymap = None + # Build the info.json file - kb_info_json = info_json(cli.config.info.keyboard) + if cli.config.info.keymap: + kb_info_json = keymap_json(cli.config.info.keyboard, cli.config.info.keymap) + else: + kb_info_json = info_json(cli.config.info.keyboard) # Output in the requested format if cli.args.format == 'json': @@ -178,11 +185,12 @@ def info(cli): cli.log.error('Unknown format: %s', cli.args.format) return False + # Output requested extras if cli.config.info.layouts: show_layouts(kb_info_json, title_caps) if cli.config.info.matrix: show_matrix(kb_info_json, title_caps) - if cli.config_source.info.keymap and cli.config_source.info.keymap != 'config_file': + if cli.config.info.keymap: show_keymap(kb_info_json, title_caps) diff --git a/lib/python/qmk/cli/pytest.py b/lib/python/qmk/cli/pytest.py index b7b17f0e9d..5c9c173caa 100644 --- a/lib/python/qmk/cli/pytest.py +++ b/lib/python/qmk/cli/pytest.py @@ -12,8 +12,7 @@ from milc import cli def pytest(cli): """Run several linting/testing commands. """ - nose2 = cli.run(['nose2', '-v', '-t' - 'lib/python', *cli.args.test], capture_output=False, stdin=DEVNULL) + nose2 = cli.run(['nose2', '-v', '-t', 'lib/python', *cli.args.test], capture_output=False, stdin=DEVNULL) flake8 = cli.run(['flake8', 'lib/python'], capture_output=False, stdin=DEVNULL) return flake8.returncode | nose2.returncode diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py index d0bd00beb7..9c0a5dce56 100644 --- a/lib/python/qmk/commands.py +++ b/lib/python/qmk/commands.py @@ -228,7 +228,7 @@ def dump_lines(output_file, lines, quiet=True): output_file.parent.mkdir(parents=True, exist_ok=True) if output_file.exists(): output_file.replace(output_file.parent / (output_file.name + '.bak')) - output_file.write_text(generated) + output_file.write_text(generated, encoding='utf-8') if not quiet: cli.log.info(f'Wrote {output_file.name} to {output_file}.') diff --git a/lib/python/qmk/info.py b/lib/python/qmk/info.py index fd8a3062b7..c5a7d33848 100644 --- a/lib/python/qmk/info.py +++ b/lib/python/qmk/info.py @@ -8,10 +8,11 @@ from dotty_dict import dotty from milc import cli from qmk.constants import CHIBIOS_PROCESSORS, LUFA_PROCESSORS, VUSB_PROCESSORS -from qmk.c_parse import find_layouts +from qmk.c_parse import find_layouts, parse_config_h_file from qmk.json_schema import deep_update, json_load, validate from qmk.keyboard import config_h, rules_mk -from qmk.keymap import list_keymaps +from qmk.keymap import list_keymaps, locate_keymap +from qmk.commands import parse_configurator_json from qmk.makefile import parse_rules_mk_file from qmk.math import compute @@ -68,8 +69,9 @@ def info_json(keyboard): # Merge in the data from info.json, config.h, and rules.mk info_data = merge_info_jsons(keyboard, info_data) - info_data = _extract_rules_mk(info_data) - info_data = _extract_config_h(info_data) + info_data = _process_defaults(info_data) + info_data = _extract_rules_mk(info_data, rules_mk(str(keyboard))) + info_data = _extract_config_h(info_data, config_h(str(keyboard))) # Ensure that we have matrix row and column counts info_data = _matrix_size(info_data) @@ -270,14 +272,16 @@ def _extract_split_transport(info_data, config_c): info_data['split']['transport']['protocol'] = 'i2c' - elif 'protocol' not in info_data.get('split', {}).get('transport', {}): + # Ignore transport defaults if "SPLIT_KEYBOARD" is unset + elif 'enabled' in info_data.get('split', {}): if 'split' not in info_data: info_data['split'] = {} if 'transport' not in info_data['split']: info_data['split']['transport'] = {} - info_data['split']['transport']['protocol'] = 'serial' + if 'protocol' not in info_data['split']['transport']: + info_data['split']['transport']['protocol'] = 'serial' def _extract_split_right_pins(info_data, config_c): @@ -400,18 +404,16 @@ def _extract_device_version(info_data): info_data['usb']['device_version'] = f'{major}.{minor}.{revision}' -def _extract_config_h(info_data): +def _extract_config_h(info_data, config_c): """Pull some keyboard information from existing config.h files """ - config_c = config_h(info_data['keyboard_folder']) - # Pull in data from the json map dotty_info = dotty(info_data) info_config_map = json_load(Path('data/mappings/info_config.json')) for config_key, info_dict in info_config_map.items(): info_key = info_dict['info_key'] - key_type = info_dict.get('value_type', 'str') + key_type = info_dict.get('value_type', 'raw') try: if config_key in config_c and info_dict.get('to_json', True): @@ -443,6 +445,9 @@ def _extract_config_h(info_data): elif key_type == 'int': dotty_info[info_key] = int(config_c[config_key]) + elif key_type == 'str': + dotty_info[info_key] = config_c[config_key].strip('"') + elif key_type == 'bcd_version': major = int(config_c[config_key][2:4]) minor = int(config_c[config_key][4]) @@ -469,10 +474,21 @@ def _extract_config_h(info_data): return info_data -def _extract_rules_mk(info_data): +def _process_defaults(info_data): + """Process any additional defaults based on currently discovered information + """ + defaults_map = json_load(Path('data/mappings/defaults.json')) + for default_type in defaults_map.keys(): + thing_map = defaults_map[default_type] + if default_type in info_data: + for key, value in thing_map.get(info_data[default_type], {}).items(): + info_data[key] = value + return info_data + + +def _extract_rules_mk(info_data, rules): """Pull some keyboard information from existing rules.mk files """ - rules = rules_mk(info_data['keyboard_folder']) info_data['processor'] = rules.get('MCU', info_data.get('processor', 'atmega32u4')) if info_data['processor'] in CHIBIOS_PROCESSORS: @@ -491,7 +507,7 @@ def _extract_rules_mk(info_data): for rules_key, info_dict in info_rules_map.items(): info_key = info_dict['info_key'] - key_type = info_dict.get('value_type', 'str') + key_type = info_dict.get('value_type', 'raw') try: if rules_key in rules and info_dict.get('to_json', True): @@ -523,6 +539,9 @@ def _extract_rules_mk(info_data): elif key_type == 'int': dotty_info[info_key] = int(rules[rules_key]) + elif key_type == 'str': + dotty_info[info_key] = rules[rules_key].strip('"') + else: dotty_info[info_key] = rules[rules_key] @@ -760,3 +779,36 @@ def find_info_json(keyboard): # Return a list of the info.json files that actually exist return [info_json for info_json in info_jsons if info_json.exists()] + + +def keymap_json_config(keyboard, keymap): + """Extract keymap level config + """ + keymap_folder = locate_keymap(keyboard, keymap).parent + + km_info_json = parse_configurator_json(keymap_folder / 'keymap.json') + return km_info_json.get('config', {}) + + +def keymap_json(keyboard, keymap): + """Generate the info.json data for a specific keymap. + """ + keymap_folder = locate_keymap(keyboard, keymap).parent + + # Files to scan + keymap_config = keymap_folder / 'config.h' + keymap_rules = keymap_folder / 'rules.mk' + keymap_file = keymap_folder / 'keymap.json' + + # Build the info.json file + kb_info_json = info_json(keyboard) + + # Merge in the data from keymap.json + km_info_json = keymap_json_config(keyboard, keymap) if keymap_file.exists() else {} + deep_update(kb_info_json, km_info_json) + + # Merge in the data from config.h, and rules.mk + _extract_rules_mk(kb_info_json, parse_rules_mk_file(keymap_rules)) + _extract_config_h(kb_info_json, parse_config_h_file(keymap_config)) + + return kb_info_json diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index 55e69175e6..d40d4bf573 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py @@ -244,7 +244,7 @@ def test_clean(): def test_generate_api(): - result = check_subcommand('generate-api', '--dry-run') + result = check_subcommand('generate-api', '--dry-run', '--filter', 'handwired/pytest') check_returncode(result) @@ -259,7 +259,7 @@ def test_generate_config_h(): result = check_subcommand('generate-config-h', '-kb', 'handwired/pytest/basic') check_returncode(result) assert '# define DEVICE_VER 0x0001' in result.stdout - assert '# define DESCRIPTION handwired/pytest/basic' in result.stdout + assert '# define DESCRIPTION "handwired/pytest/basic"' in result.stdout assert '# define DIODE_DIRECTION COL2ROW' in result.stdout assert '# define MANUFACTURER none' in result.stdout assert '# define PRODUCT pytest' in result.stdout |