aboutsummaryrefslogtreecommitdiff
path: root/lib/python/qmk/cli
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python/qmk/cli')
-rw-r--r--lib/python/qmk/cli/__init__.py1
-rw-r--r--lib/python/qmk/cli/cformat.py10
-rwxr-xr-xlib/python/qmk/cli/info.py141
-rw-r--r--lib/python/qmk/cli/list/keymaps.py18
4 files changed, 155 insertions, 15 deletions
diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py
index 394a1353b..47f60c601 100644
--- a/lib/python/qmk/cli/__init__.py
+++ b/lib/python/qmk/cli/__init__.py
@@ -13,6 +13,7 @@ from . import docs
13from . import doctor 13from . import doctor
14from . import flash 14from . import flash
15from . import hello 15from . import hello
16from . import info
16from . import json 17from . import json
17from . import json2c 18from . import json2c
18from . import list 19from . import list
diff --git a/lib/python/qmk/cli/cformat.py b/lib/python/qmk/cli/cformat.py
index 0cd8b6192..600161c5c 100644
--- a/lib/python/qmk/cli/cformat.py
+++ b/lib/python/qmk/cli/cformat.py
@@ -4,7 +4,9 @@ import subprocess
4from shutil import which 4from shutil import which
5 5
6from milc import cli 6from milc import cli
7import qmk.path 7
8from qmk.path import normpath
9from qmk.c_parse import c_source_files
8 10
9 11
10def cformat_run(files, all_files): 12def cformat_run(files, all_files):
@@ -45,10 +47,10 @@ def cformat(cli):
45 ignores = ['tmk_core/protocol/usb_hid', 'quantum/template'] 47 ignores = ['tmk_core/protocol/usb_hid', 'quantum/template']
46 # Find the list of files to format 48 # Find the list of files to format
47 if cli.args.files: 49 if cli.args.files:
48 files.extend(qmk.path.normpath(file) for file in cli.args.files) 50 files.extend(normpath(file) for file in cli.args.files)
49 # If -a is specified 51 # If -a is specified
50 elif cli.args.all_files: 52 elif cli.args.all_files:
51 all_files = qmk.path.c_source_files(core_dirs) 53 all_files = c_source_files(core_dirs)
52 # The following statement checks each file to see if the file path is in the ignored directories. 54 # The following statement checks each file to see if the file path is in the ignored directories.
53 files.extend(file for file in all_files if not any(i in str(file) for i in ignores)) 55 files.extend(file for file in all_files if not any(i in str(file) for i in ignores))
54 # No files specified & no -a flag 56 # No files specified & no -a flag
@@ -56,7 +58,7 @@ def cformat(cli):
56 base_args = ['git', 'diff', '--name-only', cli.args.base_branch] 58 base_args = ['git', 'diff', '--name-only', cli.args.base_branch]
57 out = subprocess.run(base_args + core_dirs, check=True, stdout=subprocess.PIPE) 59 out = subprocess.run(base_args + core_dirs, check=True, stdout=subprocess.PIPE)
58 changed_files = filter(None, out.stdout.decode('UTF-8').split('\n')) 60 changed_files = filter(None, out.stdout.decode('UTF-8').split('\n'))
59 filtered_files = [qmk.path.normpath(file) for file in changed_files if not any(i in file for i in ignores)] 61 filtered_files = [normpath(file) for file in changed_files if not any(i in file for i in ignores)]
60 files.extend(file for file in filtered_files if file.exists() and file.suffix in ['.c', '.h', '.cpp']) 62 files.extend(file for file in filtered_files if file.exists() and file.suffix in ['.c', '.h', '.cpp'])
61 63
62 # Run clang-format on the files we've found 64 # Run clang-format on the files we've found
diff --git a/lib/python/qmk/cli/info.py b/lib/python/qmk/cli/info.py
new file mode 100755
index 000000000..6977673e2
--- /dev/null
+++ b/lib/python/qmk/cli/info.py
@@ -0,0 +1,141 @@
1"""Keyboard information script.
2
3Compile an info.json for a particular keyboard and pretty-print it.
4"""
5import json
6
7from milc import cli
8
9from qmk.decorators import automagic_keyboard, automagic_keymap
10from qmk.keyboard import render_layouts, render_layout
11from qmk.keymap import locate_keymap
12from qmk.info import info_json
13from qmk.path import is_keyboard
14
15ROW_LETTERS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnop'
16COL_LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijilmnopqrstuvwxyz'
17
18
19def show_keymap(info_json, title_caps=True):
20 """Render the keymap in ascii art.
21 """
22 keymap_path = locate_keymap(cli.config.info.keyboard, cli.config.info.keymap)
23
24 if keymap_path and keymap_path.suffix == '.json':
25 if title_caps:
26 cli.echo('{fg_blue}Keymap "%s"{fg_reset}:', cli.config.info.keymap)
27 else:
28 cli.echo('{fg_blue}keymap_%s{fg_reset}:', cli.config.info.keymap)
29
30 keymap_data = json.load(keymap_path.open())
31 layout_name = keymap_data['layout']
32
33 for layer_num, layer in enumerate(keymap_data['layers']):
34 if title_caps:
35 cli.echo('{fg_cyan}Layer %s{fg_reset}:', layer_num)
36 else:
37 cli.echo('{fg_cyan}layer_%s{fg_reset}:', layer_num)
38
39 print(render_layout(info_json['layouts'][layout_name]['layout'], layer))
40
41
42def show_layouts(kb_info_json, title_caps=True):
43 """Render the layouts with info.json labels.
44 """
45 for layout_name, layout_art in render_layouts(kb_info_json).items():
46 title = layout_name.title() if title_caps else layout_name
47 cli.echo('{fg_cyan}%s{fg_reset}:', title)
48 print(layout_art) # Avoid passing dirty data to cli.echo()
49
50
51def show_matrix(info_json, title_caps=True):
52 """Render the layout with matrix labels in ascii art.
53 """
54 for layout_name, layout in info_json['layouts'].items():
55 # Build our label list
56 labels = []
57 for key in layout['layout']:
58 if key['matrix']:
59 row = ROW_LETTERS[key['matrix'][0]]
60 col = COL_LETTERS[key['matrix'][1]]
61
62 labels.append(row + col)
63 else:
64 labels.append('')
65
66 # Print the header
67 if title_caps:
68 cli.echo('{fg_blue}Matrix for "%s"{fg_reset}:', layout_name)
69 else:
70 cli.echo('{fg_blue}matrix_%s{fg_reset}:', layout_name)
71
72 print(render_layout(info_json['layouts'][layout_name]['layout'], labels))
73
74
75@cli.argument('-kb', '--keyboard', help='Keyboard to show info for.')
76@cli.argument('-km', '--keymap', help='Show the layers for a JSON keymap too.')
77@cli.argument('-l', '--layouts', action='store_true', help='Render the layouts.')
78@cli.argument('-m', '--matrix', action='store_true', help='Render the layouts with matrix information.')
79@cli.argument('-f', '--format', default='friendly', arg_only=True, help='Format to display the data in (friendly, text, json) (Default: friendly).')
80@cli.subcommand('Keyboard information.')
81@automagic_keyboard
82@automagic_keymap
83def info(cli):
84 """Compile an info.json for a particular keyboard and pretty-print it.
85 """
86 # Determine our keyboard(s)
87 if not is_keyboard(cli.config.info.keyboard):
88 cli.log.error('Invalid keyboard: %s!', cli.config.info.keyboard)
89 exit(1)
90
91 # Build the info.json file
92 kb_info_json = info_json(cli.config.info.keyboard)
93
94 # Output in the requested format
95 if cli.args.format == 'json':
96 print(json.dumps(kb_info_json))
97 exit()
98
99 if cli.args.format == 'text':
100 for key in sorted(kb_info_json):
101 if key == 'layouts':
102 cli.echo('{fg_blue}layouts{fg_reset}: %s', ', '.join(sorted(kb_info_json['layouts'].keys())))
103 else:
104 cli.echo('{fg_blue}%s{fg_reset}: %s', key, kb_info_json[key])
105
106 if cli.config.info.layouts:
107 show_layouts(kb_info_json, False)
108
109 if cli.config.info.matrix:
110 show_matrix(kb_info_json, False)
111
112 if cli.config_source.info.keymap and cli.config_source.info.keymap != 'config_file':
113 show_keymap(kb_info_json, False)
114
115 elif cli.args.format == 'friendly':
116 cli.echo('{fg_blue}Keyboard Name{fg_reset}: %s', kb_info_json.get('keyboard_name', 'Unknown'))
117 cli.echo('{fg_blue}Manufacturer{fg_reset}: %s', kb_info_json.get('manufacturer', 'Unknown'))
118 if 'url' in kb_info_json:
119 cli.echo('{fg_blue}Website{fg_reset}: %s', kb_info_json['url'])
120 if kb_info_json.get('maintainer') == 'qmk':
121 cli.echo('{fg_blue}Maintainer{fg_reset}: QMK Community')
122 else:
123 cli.echo('{fg_blue}Maintainer{fg_reset}: %s', kb_info_json.get('maintainer', 'qmk'))
124 cli.echo('{fg_blue}Keyboard Folder{fg_reset}: %s', kb_info_json.get('keyboard_folder', 'Unknown'))
125 cli.echo('{fg_blue}Layouts{fg_reset}: %s', ', '.join(sorted(kb_info_json['layouts'].keys())))
126 if 'width' in kb_info_json and 'height' in kb_info_json:
127 cli.echo('{fg_blue}Size{fg_reset}: %s x %s' % (kb_info_json['width'], kb_info_json['height']))
128 cli.echo('{fg_blue}Processor{fg_reset}: %s', kb_info_json.get('processor', 'Unknown'))
129 cli.echo('{fg_blue}Bootloader{fg_reset}: %s', kb_info_json.get('bootloader', 'Unknown'))
130
131 if cli.config.info.layouts:
132 show_layouts(kb_info_json, True)
133
134 if cli.config.info.matrix:
135 show_matrix(kb_info_json, True)
136
137 if cli.config_source.info.keymap and cli.config_source.info.keymap != 'config_file':
138 show_keymap(kb_info_json, True)
139
140 else:
141 cli.log.error('Unknown format: %s', cli.args.format)
diff --git a/lib/python/qmk/cli/list/keymaps.py b/lib/python/qmk/cli/list/keymaps.py
index cec9ca022..b18289eb3 100644
--- a/lib/python/qmk/cli/list/keymaps.py
+++ b/lib/python/qmk/cli/list/keymaps.py
@@ -4,7 +4,7 @@ from milc import cli
4 4
5import qmk.keymap 5import qmk.keymap
6from qmk.decorators import automagic_keyboard 6from qmk.decorators import automagic_keyboard
7from qmk.errors import NoSuchKeyboardError 7from qmk.path import is_keyboard
8 8
9 9
10@cli.argument("-kb", "--keyboard", help="Specify keyboard name. Example: 1upkeyboards/1up60hse") 10@cli.argument("-kb", "--keyboard", help="Specify keyboard name. Example: 1upkeyboards/1up60hse")
@@ -13,13 +13,9 @@ from qmk.errors import NoSuchKeyboardError
13def list_keymaps(cli): 13def list_keymaps(cli):
14 """List the keymaps for a specific keyboard 14 """List the keymaps for a specific keyboard
15 """ 15 """
16 try: 16 if not is_keyboard(cli.config.list_keymaps.keyboard):
17 for name in qmk.keymap.list_keymaps(cli.config.list_keymaps.keyboard): 17 cli.log.error('Keyboard %s does not exist!', cli.config.list_keymaps.keyboard)
18 # We echo instead of cli.log.info to allow easier piping of this output 18 exit(1)
19 cli.echo('%s', name) 19
20 except NoSuchKeyboardError as e: 20 for name in qmk.keymap.list_keymaps(cli.config.list_keymaps.keyboard):
21 cli.echo("{fg_red}%s: %s", cli.config.list_keymaps.keyboard, e.message) 21 print(name)
22 except (FileNotFoundError, PermissionError) as e:
23 cli.echo("{fg_red}%s: %s", cli.config.list_keymaps.keyboard, e)
24 except TypeError:
25 cli.echo("{fg_red}Something went wrong. Did you specify a keyboard?")