2020-10-07 02:10:19 +02:00
|
|
|
"""Generate a keymap.json from a keymap.c file.
|
|
|
|
"""
|
|
|
|
import json
|
|
|
|
|
2021-04-15 04:00:22 +02:00
|
|
|
from argcomplete.completers import FilesCompleter
|
2020-10-07 02:10:19 +02:00
|
|
|
from milc import cli
|
|
|
|
|
|
|
|
import qmk.keymap
|
|
|
|
import qmk.path
|
2021-03-25 12:38:10 +01:00
|
|
|
from qmk.json_encoders import InfoJSONEncoder
|
2021-04-15 04:00:22 +02:00
|
|
|
from qmk.keyboard import keyboard_completer, keyboard_folder
|
2021-05-27 18:42:38 +02:00
|
|
|
from qmk.errors import CppError
|
2020-10-07 02:10:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
@cli.argument('--no-cpp', arg_only=True, action='store_false', help='Do not use \'cpp\' on keymap.c')
|
|
|
|
@cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
|
|
|
|
@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
|
2021-04-15 04:00:22 +02:00
|
|
|
@cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, required=True, help='The keyboard\'s name')
|
2020-10-07 02:10:19 +02:00
|
|
|
@cli.argument('-km', '--keymap', arg_only=True, required=True, help='The keymap\'s name')
|
2021-04-15 04:00:22 +02:00
|
|
|
@cli.argument('filename', arg_only=True, completer=FilesCompleter('.c'), help='keymap.c file')
|
2020-10-07 02:10:19 +02:00
|
|
|
@cli.subcommand('Creates a keymap.json from a keymap.c file.')
|
|
|
|
def c2json(cli):
|
|
|
|
"""Generate a keymap.json from a keymap.c file.
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2020-12-29 20:34:48 +01:00
|
|
|
if cli.args.filename != '-':
|
|
|
|
cli.args.filename = qmk.path.normpath(cli.args.filename)
|
2020-10-07 02:10:19 +02:00
|
|
|
|
2020-12-29 20:34:48 +01:00
|
|
|
# Error checking
|
|
|
|
if not cli.args.filename.exists():
|
|
|
|
cli.log.error('C file does not exist!')
|
|
|
|
cli.print_usage()
|
|
|
|
return False
|
2020-10-07 02:10:19 +02:00
|
|
|
|
|
|
|
# Environment processing
|
|
|
|
if cli.args.output == ('-'):
|
|
|
|
cli.args.output = None
|
|
|
|
|
|
|
|
# Parse the keymap.c
|
2021-05-27 18:42:38 +02:00
|
|
|
try:
|
|
|
|
keymap_json = qmk.keymap.c2json(cli.args.keyboard, cli.args.keymap, cli.args.filename, use_cpp=cli.args.no_cpp)
|
|
|
|
except CppError as e:
|
|
|
|
if cli.config.general.verbose:
|
|
|
|
cli.log.debug('The C pre-processor ran into a fatal error: %s', e)
|
|
|
|
cli.log.error('Something went wrong. Try to use --no-cpp.\nUse the CLI in verbose mode to find out more.')
|
|
|
|
return False
|
2020-10-07 02:10:19 +02:00
|
|
|
|
|
|
|
# Generate the keymap.json
|
|
|
|
try:
|
2020-10-25 22:48:44 +01:00
|
|
|
keymap_json = qmk.keymap.generate_json(keymap_json['keymap'], keymap_json['keyboard'], keymap_json['layout'], keymap_json['layers'])
|
2020-10-07 02:10:19 +02:00
|
|
|
except KeyError:
|
|
|
|
cli.log.error('Something went wrong. Try to use --no-cpp.')
|
2020-12-29 20:34:48 +01:00
|
|
|
return False
|
2020-10-07 02:10:19 +02:00
|
|
|
|
|
|
|
if cli.args.output:
|
|
|
|
cli.args.output.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
if cli.args.output.exists():
|
2021-02-28 21:19:07 +01:00
|
|
|
cli.args.output.replace(cli.args.output.parent / (cli.args.output.name + '.bak'))
|
2020-12-30 19:27:37 +01:00
|
|
|
cli.args.output.write_text(json.dumps(keymap_json, cls=InfoJSONEncoder))
|
2020-10-07 02:10:19 +02:00
|
|
|
|
|
|
|
if not cli.args.quiet:
|
|
|
|
cli.log.info('Wrote keymap to %s.', cli.args.output)
|
|
|
|
|
|
|
|
else:
|
|
|
|
print(json.dumps(keymap_json))
|