diff options
author | QMK Bot <hello@qmk.fm> | 2020-12-29 19:35:24 +0000 |
---|---|---|
committer | QMK Bot <hello@qmk.fm> | 2020-12-29 19:35:24 +0000 |
commit | 7b7763469bb8cc0091d02f1f5a5e77d70d7666cd (patch) | |
tree | 8480a7cb9573d22d7ecbcd28c9d0cb49b5ac803b /lib/python/qmk/cli/json2c.py | |
parent | 9748b6b847d9c184db6efd660ee8b343a4bf7485 (diff) | |
parent | 221d8fd8669ff528bfedd01f41486f5298d960e1 (diff) |
Merge remote-tracking branch 'origin/master' into develop
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 426078063c..97d8fb0c33 100755 --- a/lib/python/qmk/cli/json2c.py +++ b/lib/python/qmk/cli/json2c.py @@ -1,6 +1,7 @@ """Generate a keymap.c from a configurator export. """ import json +import sys from milc import cli @@ -17,26 +18,31 @@ def json2c(cli): 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. """ - # Error checking - if cli.args.filename and cli.args.filename.name == '-': - # TODO(skullydazed/anyone): Read file contents from STDIN - cli.log.error('Reading from STDIN is not (yet) supported.') - cli.print_usage() - return False - if not cli.args.filename.exists(): - cli.log.error('JSON file does not exist!') - cli.print_usage() + try: + # Parse the configurator from stdin + if cli.args.filename and cli.args.filename.name == '-': + user_keymap = json.load(sys.stdin) + + else: + # Error checking + if not cli.args.filename.exists(): + cli.log.error('JSON file does not exist!') + return False + + # Parse the configurator json file + else: + user_keymap = json.loads(cli.args.filename.read_text()) + + except json.decoder.JSONDecodeError as ex: + cli.log.error('The JSON input does not appear to be valid.') + cli.log.error(ex) return False # Environment processing if cli.args.output and cli.args.output.name == '-': cli.args.output = None - # Parse the configurator json - with cli.args.filename.open('r') as fd: - user_keymap = json.load(fd) - # Generate the keymap keymap_c = qmk.keymap.generate_c(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers']) |