diff options
author | LongerHV <46924944+LongerHV@users.noreply.github.com> | 2020-12-29 20:34:48 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-29 11:34:48 -0800 |
commit | 221d8fd8669ff528bfedd01f41486f5298d960e1 (patch) | |
tree | c82fe3f0e032ce2057039b4baadbb1f91c0608ed /lib/python/qmk/cli/json2c.py | |
parent | 3300164065949e6bc9423632ccbcd0022be8074d (diff) | |
download | qmk_firmware-221d8fd8669ff528bfedd01f41486f5298d960e1.tar.gz qmk_firmware-221d8fd8669ff528bfedd01f41486f5298d960e1.zip |
[CLI] Add stdin support for json2c command (#11289)
* Implement stdin for json2c command
* Refactor
* Handle json decode error
* Add stdin support for c2json cli command
* Refactor to prevent code duplication
* Change exit(1) to return False in c2json command
* Remove unused import
Diffstat (limited to 'lib/python/qmk/cli/json2c.py')
-rwxr-xr-x | lib/python/qmk/cli/json2c.py | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/lib/python/qmk/cli/json2c.py b/lib/python/qmk/cli/json2c.py index 426078063..97d8fb0c3 100755 --- a/lib/python/qmk/cli/json2c.py +++ b/lib/python/qmk/cli/json2c.py | |||
@@ -1,6 +1,7 @@ | |||
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 sys | ||
4 | 5 | ||
5 | from milc import cli | 6 | from milc import cli |
6 | 7 | ||
@@ -17,26 +18,31 @@ def json2c(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 | """ |
20 | # Error checking | ||
21 | if cli.args.filename and cli.args.filename.name == '-': | ||
22 | # TODO(skullydazed/anyone): Read file contents from STDIN | ||
23 | cli.log.error('Reading from STDIN is not (yet) supported.') | ||
24 | cli.print_usage() | ||
25 | return False | ||
26 | 21 | ||
27 | if not cli.args.filename.exists(): | 22 | try: |
28 | cli.log.error('JSON file does not exist!') | 23 | # Parse the configurator from stdin |
29 | cli.print_usage() | 24 | if cli.args.filename and cli.args.filename.name == '-': |
25 | user_keymap = json.load(sys.stdin) | ||
26 | |||
27 | else: | ||
28 | # Error checking | ||
29 | if not cli.args.filename.exists(): | ||
30 | cli.log.error('JSON file does not exist!') | ||
31 | return False | ||
32 | |||
33 | # Parse the configurator json file | ||
34 | else: | ||
35 | user_keymap = json.loads(cli.args.filename.read_text()) | ||
36 | |||
37 | except json.decoder.JSONDecodeError as ex: | ||
38 | cli.log.error('The JSON input does not appear to be valid.') | ||
39 | cli.log.error(ex) | ||
30 | return False | 40 | return False |
31 | 41 | ||
32 | # Environment processing | 42 | # Environment processing |
33 | if cli.args.output and cli.args.output.name == '-': | 43 | if cli.args.output and cli.args.output.name == '-': |
34 | cli.args.output = None | 44 | cli.args.output = None |
35 | 45 | ||
36 | # Parse the configurator json | ||
37 | with cli.args.filename.open('r') as fd: | ||
38 | user_keymap = json.load(fd) | ||
39 | |||
40 | # Generate the keymap | 46 | # Generate the keymap |
41 | keymap_c = qmk.keymap.generate_c(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers']) | 47 | keymap_c = qmk.keymap.generate_c(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers']) |
42 | 48 | ||