summaryrefslogtreecommitdiff
path: root/lib/python/qmk/cli
diff options
context:
space:
mode:
authorErovia <Erovia@users.noreply.github.com>2020-10-07 01:10:19 +0100
committerDrashna Jael're <drashna@live.com>2021-01-12 22:43:31 -0800
commit5af121f8155cddc682f8298111840b525395037e (patch)
tree9ab8813bfc73364d68d964bc806479a01e008ae8 /lib/python/qmk/cli
parentae5ee61e7bbf24d3dc36c1a309d3af25a00a5444 (diff)
[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')
-rw-r--r--lib/python/qmk/cli/__init__.py1
-rw-r--r--lib/python/qmk/cli/c2json.py62
-rwxr-xr-xlib/python/qmk/cli/doctor.py2
3 files changed, 64 insertions, 1 deletions
diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py
index 47f60c601b..ba964ebbbb 100644
--- a/lib/python/qmk/cli/__init__.py
+++ b/lib/python/qmk/cli/__init__.py
@@ -6,6 +6,7 @@ import sys
from milc import cli
+from . import c2json
from . import cformat
from . import compile
from . import config
diff --git a/lib/python/qmk/cli/c2json.py b/lib/python/qmk/cli/c2json.py
new file mode 100644
index 0000000000..0267303fd2
--- /dev/null
+++ b/lib/python/qmk/cli/c2json.py
@@ -0,0 +1,62 @@
+"""Generate a keymap.json from a keymap.c file.
+"""
+import json
+import sys
+
+from milc import cli
+
+import qmk.keymap
+import qmk.path
+
+
+@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")
+@cli.argument('-kb', '--keyboard', arg_only=True, required=True, help='The keyboard\'s name')
+@cli.argument('-km', '--keymap', arg_only=True, required=True, help='The keymap\'s name')
+@cli.argument('filename', arg_only=True, help='keymap.c file')
+@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.
+ """
+ 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)
+
+ # Environment processing
+ if cli.args.output == ('-'):
+ cli.args.output = None
+
+ # Parse the keymap.c
+ keymap_json = qmk.keymap.c2json(cli.args.keyboard, cli.args.keymap, cli.args.filename, use_cpp=cli.args.no_cpp)
+
+ # Generate the keymap.json
+ try:
+ keymap_json = qmk.keymap.generate(keymap_json['keyboard'], keymap_json['layout'], keymap_json['layers'], type='json', keymap=keymap_json['keymap'])
+ except KeyError:
+ cli.log.error('Something went wrong. Try to use --no-cpp.')
+ sys.exit(1)
+
+ if cli.args.output:
+ cli.args.output.parent.mkdir(parents=True, exist_ok=True)
+ if cli.args.output.exists():
+ cli.args.output.replace(cli.args.output.name + '.bak')
+ cli.args.output.write_text(json.dumps(keymap_json))
+
+ if not cli.args.quiet:
+ cli.log.info('Wrote keymap to %s.', cli.args.output)
+
+ else:
+ print(json.dumps(keymap_json))
diff --git a/lib/python/qmk/cli/doctor.py b/lib/python/qmk/cli/doctor.py
index 984c308d1e..7fafd57575 100755
--- a/lib/python/qmk/cli/doctor.py
+++ b/lib/python/qmk/cli/doctor.py
@@ -58,7 +58,7 @@ def parse_gcc_version(version):
return {
'major': int(m.group(1)),
'minor': int(m.group(2)) if m.group(2) else 0,
- 'patch': int(m.group(3)) if m.group(3) else 0
+ 'patch': int(m.group(3)) if m.group(3) else 0,
}