]> git.donarmstrong.com Git - qmk_firmware.git/blob - lib/python/qmk/cli/json/keymap.py
QMK CLI and JSON keymap support (#6176)
[qmk_firmware.git] / lib / python / qmk / cli / json / keymap.py
1 """Generate a keymap.c from a configurator export.
2 """
3 import json
4 import os
5 import sys
6
7 from milc import cli
8
9 import qmk.keymap
10
11
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.')
15 def main(cli):
16     """Generate a keymap.c from a configurator export.
17
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.
19     """
20     # Error checking
21     if cli.args.filename == ('-'):
22         cli.log.error('Reading from STDIN is not (yet) supported.')
23         cli.print_usage()
24         exit(1)
25     if not os.path.exists(qmk.path.normpath(cli.args.filename)):
26         cli.log.error('JSON file does not exist!')
27         cli.print_usage()
28         exit(1)
29
30     # Environment processing
31     if cli.args.output == ('-'):
32         cli.args.output = None
33
34     # Parse the configurator json
35     with open(qmk.path.normpath(cli.args.filename), 'r') as fd:
36         user_keymap = json.load(fd)
37
38     # Generate the keymap
39     keymap_c = qmk.keymap.generate(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers'])
40
41     if cli.args.output:
42         output_dir = os.path.dirname(cli.args.output)
43
44         if not os.path.exists(output_dir):
45             os.makedirs(output_dir)
46
47         output_file = qmk.path.normpath(cli.args.output)
48         with open(output_file, 'w') as keymap_fd:
49             keymap_fd.write(keymap_c)
50
51         cli.log.info('Wrote keymap to %s.', cli.args.output)
52
53     else:
54         print(keymap_c)