diff options
| author | Erovia <Erovia@users.noreply.github.com> | 2020-10-07 01:10:19 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-06 17:10:19 -0700 |
| commit | 058737f116b53116726f32175205b46e22396f86 (patch) | |
| tree | d84cecc2d1716d93b56b078a3f86eff14db13415 /lib/python/qmk/cli/c2json.py | |
| parent | c9a06965c991a84ac76014d9791e439f88dfb957 (diff) | |
| download | qmk_firmware-058737f116b53116726f32175205b46e22396f86.tar.gz qmk_firmware-058737f116b53116726f32175205b46e22396f86.zip | |
[CLI] Add c2json (#8817)
* Basic keymap parsing finally works
* Add 'keymap.json' creation to the qmk.keymap module
* Add tests and fix formatting
* Fix/exclude flake8 errors
* Convert keymap.c to valid keymap.json
* Fix some errors
* Add tests
* Finalize keymap.json creation, add json template
* Add docs
* Move pygments to the standard requirements
* Add support for nameless layers, fix tests
* Fix things after rebase
* Add missing 'keymap' value.
* Fix missing layer numbers from advanced keycodes
Buckwich noticed that if the advanced keycode / layer toggling key
contains a number, it goes missing.
Now we properly handle them.
Thx for noticing!
* Apply suggestions from code review
* fixup tests
Co-authored-by: Zach White <skullydazed@drpepper.org>
Co-authored-by: skullY <skullydazed@gmail.com>
Diffstat (limited to 'lib/python/qmk/cli/c2json.py')
| -rw-r--r-- | lib/python/qmk/cli/c2json.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/lib/python/qmk/cli/c2json.py b/lib/python/qmk/cli/c2json.py new file mode 100644 index 000000000..0267303fd --- /dev/null +++ b/lib/python/qmk/cli/c2json.py | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | """Generate a keymap.json from a keymap.c file. | ||
| 2 | """ | ||
| 3 | import json | ||
| 4 | import sys | ||
| 5 | |||
| 6 | from milc import cli | ||
| 7 | |||
| 8 | import qmk.keymap | ||
| 9 | import qmk.path | ||
| 10 | |||
| 11 | |||
| 12 | @cli.argument('--no-cpp', arg_only=True, action='store_false', help='Do not use \'cpp\' on keymap.c') | ||
| 13 | @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to') | ||
| 14 | @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") | ||
| 15 | @cli.argument('-kb', '--keyboard', arg_only=True, required=True, help='The keyboard\'s name') | ||
| 16 | @cli.argument('-km', '--keymap', arg_only=True, required=True, help='The keymap\'s name') | ||
| 17 | @cli.argument('filename', arg_only=True, help='keymap.c file') | ||
| 18 | @cli.subcommand('Creates a keymap.json from a keymap.c file.') | ||
| 19 | def c2json(cli): | ||
| 20 | """Generate a keymap.json from a keymap.c file. | ||
| 21 | |||
| 22 | 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. | ||
| 23 | """ | ||
| 24 | cli.args.filename = qmk.path.normpath(cli.args.filename) | ||
| 25 | |||
| 26 | # Error checking | ||
| 27 | if not cli.args.filename.exists(): | ||
| 28 | cli.log.error('C file does not exist!') | ||
| 29 | cli.print_usage() | ||
| 30 | exit(1) | ||
| 31 | |||
| 32 | if str(cli.args.filename) == '-': | ||
| 33 | # TODO(skullydazed/anyone): Read file contents from STDIN | ||
| 34 | cli.log.error('Reading from STDIN is not (yet) supported.') | ||
| 35 | cli.print_usage() | ||
| 36 | exit(1) | ||
| 37 | |||
| 38 | # Environment processing | ||
| 39 | if cli.args.output == ('-'): | ||
| 40 | cli.args.output = None | ||
| 41 | |||
| 42 | # Parse the keymap.c | ||
| 43 | keymap_json = qmk.keymap.c2json(cli.args.keyboard, cli.args.keymap, cli.args.filename, use_cpp=cli.args.no_cpp) | ||
| 44 | |||
| 45 | # Generate the keymap.json | ||
| 46 | try: | ||
| 47 | keymap_json = qmk.keymap.generate(keymap_json['keyboard'], keymap_json['layout'], keymap_json['layers'], type='json', keymap=keymap_json['keymap']) | ||
| 48 | except KeyError: | ||
| 49 | cli.log.error('Something went wrong. Try to use --no-cpp.') | ||
| 50 | sys.exit(1) | ||
| 51 | |||
| 52 | if cli.args.output: | ||
| 53 | cli.args.output.parent.mkdir(parents=True, exist_ok=True) | ||
| 54 | if cli.args.output.exists(): | ||
| 55 | cli.args.output.replace(cli.args.output.name + '.bak') | ||
| 56 | cli.args.output.write_text(json.dumps(keymap_json)) | ||
| 57 | |||
| 58 | if not cli.args.quiet: | ||
| 59 | cli.log.info('Wrote keymap to %s.', cli.args.output) | ||
| 60 | |||
| 61 | else: | ||
| 62 | print(json.dumps(keymap_json)) | ||
