1 """Generate a keymap.c from a configurator export.
12 @cli.argument('-o', '--output', help='File to write to')
13 @cli.argument('filename', help='Configurator JSON file')
14 @cli.entrypoint('Create a keymap.c from a QMK Configurator export.')
16 """Generate a keymap.c from a configurator export.
18 This command uses the `qmk.keymap` module to generate a keymap.c from a configurator export. The generated keymap is written to stdout, or to a file if -o is provided.
21 if cli.args.filename == ('-'):
22 cli.log.error('Reading from STDIN is not (yet) supported.')
25 if not os.path.exists(qmk.path.normpath(cli.args.filename)):
26 cli.log.error('JSON file does not exist!')
30 # Environment processing
31 if cli.args.output == ('-'):
32 cli.args.output = None
34 # Parse the configurator json
35 with open(qmk.path.normpath(cli.args.filename), 'r') as fd:
36 user_keymap = json.load(fd)
39 keymap_c = qmk.keymap.generate(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers'])
42 output_dir = os.path.dirname(cli.args.output)
44 if not os.path.exists(output_dir):
45 os.makedirs(output_dir)
47 output_file = qmk.path.normpath(cli.args.output)
48 with open(output_file, 'w') as keymap_fd:
49 keymap_fd.write(keymap_c)
51 cli.log.info('Wrote keymap to %s.', cli.args.output)