diff options
Diffstat (limited to 'lib/python/qmk/cli/generate/keyboard_h.py')
-rwxr-xr-x | lib/python/qmk/cli/generate/keyboard_h.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/python/qmk/cli/generate/keyboard_h.py b/lib/python/qmk/cli/generate/keyboard_h.py new file mode 100755 index 000000000..5a2b3656e --- /dev/null +++ b/lib/python/qmk/cli/generate/keyboard_h.py | |||
@@ -0,0 +1,60 @@ | |||
1 | """Used by the make system to generate keyboard.h from info.json. | ||
2 | """ | ||
3 | from milc import cli | ||
4 | |||
5 | from qmk.decorators import automagic_keyboard, automagic_keymap | ||
6 | from qmk.info import info_json | ||
7 | from qmk.keyboard import keyboard_completer, keyboard_folder | ||
8 | from qmk.path import is_keyboard, normpath | ||
9 | |||
10 | |||
11 | def would_populate_layout_h(keyboard): | ||
12 | """Detect if a given keyboard is doing data driven layouts | ||
13 | """ | ||
14 | # Build the info.json file | ||
15 | kb_info_json = info_json(keyboard) | ||
16 | |||
17 | for layout_name in kb_info_json['layouts']: | ||
18 | if kb_info_json['layouts'][layout_name]['c_macro']: | ||
19 | continue | ||
20 | |||
21 | if 'matrix' not in kb_info_json['layouts'][layout_name]['layout'][0]: | ||
22 | cli.log.debug('%s/%s: No matrix data!', keyboard, layout_name) | ||
23 | continue | ||
24 | |||
25 | return True | ||
26 | |||
27 | return False | ||
28 | |||
29 | |||
30 | @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') | ||
31 | @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") | ||
32 | @cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, required=True, help='Keyboard to generate keyboard.h for.') | ||
33 | @cli.subcommand('Used by the make system to generate keyboard.h from info.json', hidden=True) | ||
34 | @automagic_keyboard | ||
35 | @automagic_keymap | ||
36 | def generate_keyboard_h(cli): | ||
37 | """Generates the keyboard.h file. | ||
38 | """ | ||
39 | has_layout_h = would_populate_layout_h(cli.config.generate_keyboard_h.keyboard) | ||
40 | |||
41 | # Build the layouts.h file. | ||
42 | keyboard_h_lines = ['/* This file was generated by `qmk generate-keyboard-h`. Do not edit or copy.' ' */', '', '#pragma once', '#include "quantum.h"'] | ||
43 | |||
44 | if not has_layout_h: | ||
45 | keyboard_h_lines.append('#pragma error("<keyboard>.h is only optional for data driven keyboards - kb.h == bad times")') | ||
46 | |||
47 | # Show the results | ||
48 | keyboard_h = '\n'.join(keyboard_h_lines) + '\n' | ||
49 | |||
50 | if cli.args.output: | ||
51 | cli.args.output.parent.mkdir(parents=True, exist_ok=True) | ||
52 | if cli.args.output.exists(): | ||
53 | cli.args.output.replace(cli.args.output.parent / (cli.args.output.name + '.bak')) | ||
54 | cli.args.output.write_text(keyboard_h) | ||
55 | |||
56 | if not cli.args.quiet: | ||
57 | cli.log.info('Wrote keyboard_h to %s.', cli.args.output) | ||
58 | |||
59 | else: | ||
60 | print(keyboard_h) | ||