diff options
Diffstat (limited to 'lib/python/qmk/cli/json/keymap.py')
-rwxr-xr-x | lib/python/qmk/cli/json/keymap.py | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/lib/python/qmk/cli/json/keymap.py b/lib/python/qmk/cli/json/keymap.py index a030ab53d..c2b7dde7a 100755 --- a/lib/python/qmk/cli/json/keymap.py +++ b/lib/python/qmk/cli/json/keymap.py | |||
@@ -1,14 +1,15 @@ | |||
1 | """Generate a keymap.c from a configurator export. | 1 | """Generate a keymap.c from a configurator export. |
2 | """ | 2 | """ |
3 | import json | 3 | import json |
4 | import os | 4 | from pathlib import Path |
5 | 5 | ||
6 | from milc import cli | 6 | from milc import cli |
7 | 7 | ||
8 | import qmk.keymap | 8 | import qmk.keymap |
9 | import qmk.path | ||
9 | 10 | ||
10 | 11 | ||
11 | @cli.argument('-o', '--output', arg_only=True, help='File to write to') | 12 | @cli.argument('-o', '--output', arg_only=True, type=Path, help='File to write to') |
12 | @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") | 13 | @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") |
13 | @cli.argument('filename', arg_only=True, help='Configurator JSON file') | 14 | @cli.argument('filename', arg_only=True, help='Configurator JSON file') |
14 | @cli.subcommand('Creates a keymap.c from a QMK Configurator export.') | 15 | @cli.subcommand('Creates a keymap.c from a QMK Configurator export.') |
@@ -17,13 +18,17 @@ def json_keymap(cli): | |||
17 | 18 | ||
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 | 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 | """ |
21 | cli.args.filename = qmk.path.normpath(cli.args.filename) | ||
22 | |||
20 | # Error checking | 23 | # Error checking |
21 | if cli.args.filename == ('-'): | 24 | if not cli.args.filename.exists(): |
22 | cli.log.error('Reading from STDIN is not (yet) supported.') | 25 | cli.log.error('JSON file does not exist!') |
23 | cli.print_usage() | 26 | cli.print_usage() |
24 | exit(1) | 27 | exit(1) |
25 | if not os.path.exists(qmk.path.normpath(cli.args.filename)): | 28 | |
26 | cli.log.error('JSON file does not exist!') | 29 | if str(cli.args.filename) == '-': |
30 | # TODO(skullydazed/anyone): Read file contents from STDIN | ||
31 | cli.log.error('Reading from STDIN is not (yet) supported.') | ||
27 | cli.print_usage() | 32 | cli.print_usage() |
28 | exit(1) | 33 | exit(1) |
29 | 34 | ||
@@ -32,21 +37,17 @@ def json_keymap(cli): | |||
32 | cli.args.output = None | 37 | cli.args.output = None |
33 | 38 | ||
34 | # Parse the configurator json | 39 | # Parse the configurator json |
35 | with open(qmk.path.normpath(cli.args.filename), 'r') as fd: | 40 | with cli.args.filename.open('r') as fd: |
36 | user_keymap = json.load(fd) | 41 | user_keymap = json.load(fd) |
37 | 42 | ||
38 | # Generate the keymap | 43 | # Generate the keymap |
39 | keymap_c = qmk.keymap.generate(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers']) | 44 | keymap_c = qmk.keymap.generate(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers']) |
40 | 45 | ||
41 | if cli.args.output: | 46 | if cli.args.output: |
42 | output_dir = os.path.dirname(cli.args.output) | 47 | cli.args.output.parent.mkdir(parents=True, exist_ok=True) |
43 | 48 | if cli.args.output.exists(): | |
44 | if not os.path.exists(output_dir): | 49 | cli.args.output.replace(cli.args.output.name + '.bak') |
45 | os.makedirs(output_dir) | 50 | cli.args.output.write_text(keymap_c) |
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 | ||
51 | if not cli.args.quiet: | 52 | if not cli.args.quiet: |
52 | cli.log.info('Wrote keymap to %s.', cli.args.output) | 53 | cli.log.info('Wrote keymap to %s.', cli.args.output) |