[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
This commit is contained in:
parent
3300164065
commit
221d8fd866
4 changed files with 70 additions and 35 deletions
|
@ -1,7 +1,6 @@
|
|||
"""Generate a keymap.json from a keymap.c file.
|
||||
"""
|
||||
import json
|
||||
import sys
|
||||
|
||||
from milc import cli
|
||||
|
||||
|
@ -21,19 +20,14 @@ def c2json(cli):
|
|||
|
||||
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.
|
||||
"""
|
||||
cli.args.filename = qmk.path.normpath(cli.args.filename)
|
||||
if cli.args.filename != '-':
|
||||
cli.args.filename = qmk.path.normpath(cli.args.filename)
|
||||
|
||||
# Error checking
|
||||
if not cli.args.filename.exists():
|
||||
cli.log.error('C file does not exist!')
|
||||
cli.print_usage()
|
||||
exit(1)
|
||||
|
||||
if str(cli.args.filename) == '-':
|
||||
# TODO(skullydazed/anyone): Read file contents from STDIN
|
||||
cli.log.error('Reading from STDIN is not (yet) supported.')
|
||||
cli.print_usage()
|
||||
exit(1)
|
||||
# Error checking
|
||||
if not cli.args.filename.exists():
|
||||
cli.log.error('C file does not exist!')
|
||||
cli.print_usage()
|
||||
return False
|
||||
|
||||
# Environment processing
|
||||
if cli.args.output == ('-'):
|
||||
|
@ -47,7 +41,7 @@ def c2json(cli):
|
|||
keymap_json = qmk.keymap.generate_json(keymap_json['keymap'], keymap_json['keyboard'], keymap_json['layout'], keymap_json['layers'])
|
||||
except KeyError:
|
||||
cli.log.error('Something went wrong. Try to use --no-cpp.')
|
||||
sys.exit(1)
|
||||
return False
|
||||
|
||||
if cli.args.output:
|
||||
cli.args.output.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
|
|
@ -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'])
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue