aboutsummaryrefslogtreecommitdiff
path: root/lib/python/qmk/cli/c2json.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python/qmk/cli/c2json.py')
-rw-r--r--lib/python/qmk/cli/c2json.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/lib/python/qmk/cli/c2json.py b/lib/python/qmk/cli/c2json.py
new file mode 100644
index 000000000..0267303fd
--- /dev/null
+++ b/lib/python/qmk/cli/c2json.py
@@ -0,0 +1,62 @@
1"""Generate a keymap.json from a keymap.c file.
2"""
3import json
4import sys
5
6from milc import cli
7
8import qmk.keymap
9import qmk.path
10
11
12@cli.argument('--no-cpp', arg_only=True, action='store_false', help='Do not use \'cpp\' on keymap.c')
13@cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
14@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
15@cli.argument('-kb', '--keyboard', arg_only=True, required=True, help='The keyboard\'s name')
16@cli.argument('-km', '--keymap', arg_only=True, required=True, help='The keymap\'s name')
17@cli.argument('filename', arg_only=True, help='keymap.c file')
18@cli.subcommand('Creates a keymap.json from a keymap.c file.')
19def c2json(cli):
20 """Generate a keymap.json from a keymap.c file.
21
22 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.
23 """
24 cli.args.filename = qmk.path.normpath(cli.args.filename)
25
26 # Error checking
27 if not cli.args.filename.exists():
28 cli.log.error('C file does not exist!')
29 cli.print_usage()
30 exit(1)
31
32 if str(cli.args.filename) == '-':
33 # TODO(skullydazed/anyone): Read file contents from STDIN
34 cli.log.error('Reading from STDIN is not (yet) supported.')
35 cli.print_usage()
36 exit(1)
37
38 # Environment processing
39 if cli.args.output == ('-'):
40 cli.args.output = None
41
42 # Parse the keymap.c
43 keymap_json = qmk.keymap.c2json(cli.args.keyboard, cli.args.keymap, cli.args.filename, use_cpp=cli.args.no_cpp)
44
45 # Generate the keymap.json
46 try:
47 keymap_json = qmk.keymap.generate(keymap_json['keyboard'], keymap_json['layout'], keymap_json['layers'], type='json', keymap=keymap_json['keymap'])
48 except KeyError:
49 cli.log.error('Something went wrong. Try to use --no-cpp.')
50 sys.exit(1)
51
52 if cli.args.output:
53 cli.args.output.parent.mkdir(parents=True, exist_ok=True)
54 if cli.args.output.exists():
55 cli.args.output.replace(cli.args.output.name + '.bak')
56 cli.args.output.write_text(json.dumps(keymap_json))
57
58 if not cli.args.quiet:
59 cli.log.info('Wrote keymap to %s.', cli.args.output)
60
61 else:
62 print(json.dumps(keymap_json))