diff options
Diffstat (limited to 'lib/python/qmk/cli')
-rw-r--r-- | lib/python/qmk/cli/__init__.py | 1 | ||||
-rw-r--r-- | lib/python/qmk/cli/c2json.py | 62 | ||||
-rwxr-xr-x | lib/python/qmk/cli/doctor.py | 2 |
3 files changed, 64 insertions, 1 deletions
diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index 47f60c601..ba964ebbb 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py | |||
@@ -6,6 +6,7 @@ import sys | |||
6 | 6 | ||
7 | from milc import cli | 7 | from milc import cli |
8 | 8 | ||
9 | from . import c2json | ||
9 | from . import cformat | 10 | from . import cformat |
10 | from . import compile | 11 | from . import compile |
11 | from . import config | 12 | from . import config |
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)) | ||
diff --git a/lib/python/qmk/cli/doctor.py b/lib/python/qmk/cli/doctor.py index 984c308d1..7fafd5757 100755 --- a/lib/python/qmk/cli/doctor.py +++ b/lib/python/qmk/cli/doctor.py | |||
@@ -58,7 +58,7 @@ def parse_gcc_version(version): | |||
58 | return { | 58 | return { |
59 | 'major': int(m.group(1)), | 59 | 'major': int(m.group(1)), |
60 | 'minor': int(m.group(2)) if m.group(2) else 0, | 60 | 'minor': int(m.group(2)) if m.group(2) else 0, |
61 | 'patch': int(m.group(3)) if m.group(3) else 0 | 61 | 'patch': int(m.group(3)) if m.group(3) else 0, |
62 | } | 62 | } |
63 | 63 | ||
64 | 64 | ||