at master 3.2 kB view raw
1"""Generate a keymap.json from a keymap.c file. 2""" 3import re 4import json 5 6from argcomplete.completers import FilesCompleter 7from milc import cli 8 9import qmk.path 10from qmk.json_encoders import InfoJSONEncoder 11from qmk.decorators import automagic_keyboard, automagic_keymap 12from qmk.keyboard import keyboard_completer, keyboard_folder 13from qmk.keymap import locate_keymap, find_keymap_from_dir, generate_json, c2json as c2json_impl 14from qmk.errors import CppError 15from qmk.commands import dump_lines 16 17 18@cli.argument('--no-cpp', arg_only=True, action='store_false', help='Do not use \'cpp\' on keymap.c') 19@cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to') 20@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") 21@cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='The keyboard\'s name') 22@cli.argument('-km', '--keymap', help='The keymap\'s name') 23@cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.c'), help='keymap.c file') 24@cli.subcommand('Creates a keymap.json from a keymap.c file.') 25@automagic_keyboard 26@automagic_keymap 27def c2json(cli): 28 """Generate a keymap.json from a keymap.c file. 29 30 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. 31 """ 32 filename = cli.args.filename 33 keyboard = cli.config.c2json.keyboard 34 keymap = cli.config.c2json.keymap 35 36 if filename: 37 if not keyboard and not keymap: 38 # fallback to inferring keyboard/keymap from path 39 (keymap, found_type) = find_keymap_from_dir(filename) 40 if found_type == 'keymap_directory': 41 keyboard = re.search(fr"keyboards/(.+)/keymaps/{keymap}/.*", filename.as_posix()).group(1) 42 43 elif keyboard and keymap: 44 if not filename: 45 # fallback to inferring keyboard/keymap from path 46 filename = locate_keymap(keyboard, keymap) 47 48 if not all((filename, keyboard, keymap)): 49 cli.log.error('You must supply keyboard and keymap, a path to a keymap.c within qmk_firmware, or absolute filename and keyboard and keymap') 50 cli.print_help() 51 return False 52 53 try: 54 keymap_json = c2json_impl(keyboard, keymap, filename, use_cpp=cli.args.no_cpp) 55 except CppError as e: 56 if cli.config.general.verbose: 57 cli.log.debug('The C pre-processor ran into a fatal error: %s', e) 58 cli.log.error('Something went wrong. Try to use --no-cpp.\nUse the CLI in verbose mode to find out more.') 59 return False 60 61 # Generate the keymap.json 62 try: 63 keymap_json = generate_json(keymap_json['keymap'], keymap_json['keyboard'], keymap_json['layout'], keymap_json['layers']) 64 except KeyError: 65 cli.log.error('Something went wrong. Try to use --no-cpp.') 66 return False 67 68 if cli.args.output: 69 keymap_lines = [json.dumps(keymap_json, cls=InfoJSONEncoder, sort_keys=True)] 70 else: 71 keymap_lines = [json.dumps(keymap_json)] 72 73 dump_lines(cli.args.output, keymap_lines, cli.args.quiet)