Brute force update CLI tools

This commit is contained in:
Drashna Jael're
2020-11-24 09:38:19 -08:00
parent 2077e6561c
commit 2dbe99b04f
16 changed files with 376 additions and 126 deletions

View File

@@ -14,6 +14,7 @@ from . import config
from . import docs
from . import doctor
from . import flash
from . import generate
from . import hello
from . import info
from . import json

View File

@@ -44,7 +44,7 @@ def c2json(cli):
# 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'])
keymap_json = qmk.keymap.generate_json(keymap_json['keymap'], keymap_json['keyboard'], keymap_json['layout'], keymap_json['layers'])
except KeyError:
cli.log.error('Something went wrong. Try to use --no-cpp.')
sys.exit(1)

View File

@@ -0,0 +1,2 @@
from . import api
from . import docs

View File

@@ -0,0 +1,58 @@
"""This script automates the generation of the QMK API data.
"""
from pathlib import Path
from shutil import copyfile
import json
from milc import cli
from qmk.datetime import current_datetime
from qmk.info import info_json
from qmk.keyboard import list_keyboards
@cli.subcommand('Creates a new keymap for the keyboard of your choosing', hidden=False if cli.config.user.developer else True)
def generate_api(cli):
"""Generates the QMK API data.
"""
api_data_dir = Path('api_data')
v1_dir = api_data_dir / 'v1'
keyboard_list = v1_dir / 'keyboard_list.json'
keyboard_all = v1_dir / 'keyboards.json'
usb_file = v1_dir / 'usb.json'
if not api_data_dir.exists():
api_data_dir.mkdir()
kb_all = {'last_updated': current_datetime(), 'keyboards': {}}
usb_list = {'last_updated': current_datetime(), 'devices': {}}
# Generate and write keyboard specific JSON files
for keyboard_name in list_keyboards():
kb_all['keyboards'][keyboard_name] = info_json(keyboard_name)
keyboard_dir = v1_dir / 'keyboards' / keyboard_name
keyboard_info = keyboard_dir / 'info.json'
keyboard_readme = keyboard_dir / 'readme.md'
keyboard_readme_src = Path('keyboards') / keyboard_name / 'readme.md'
keyboard_dir.mkdir(parents=True, exist_ok=True)
keyboard_info.write_text(json.dumps(kb_all['keyboards'][keyboard_name]))
if keyboard_readme_src.exists():
copyfile(keyboard_readme_src, keyboard_readme)
if 'usb' in kb_all['keyboards'][keyboard_name]:
usb = kb_all['keyboards'][keyboard_name]['usb']
if usb['vid'] not in usb_list['devices']:
usb_list['devices'][usb['vid']] = {}
if usb['pid'] not in usb_list['devices'][usb['vid']]:
usb_list['devices'][usb['vid']][usb['pid']] = {}
usb_list['devices'][usb['vid']][usb['pid']][keyboard_name] = usb
# Write the global JSON files
keyboard_list.write_text(json.dumps({'last_updated': current_datetime(), 'keyboards': sorted(kb_all['keyboards'])}))
keyboard_all.write_text(json.dumps(kb_all))
usb_file.write_text(json.dumps(usb_list))

View File

@@ -0,0 +1,37 @@
"""Build QMK documentation locally
"""
import shutil
import subprocess
from pathlib import Path
from milc import cli
DOCS_PATH = Path('docs/')
BUILD_PATH = Path('.build/docs/')
@cli.subcommand('Build QMK documentation.', hidden=False if cli.config.user.developer else True)
def generate_docs(cli):
"""Invoke the docs generation process
TODO(unclaimed):
* [ ] Add a real build step... something static docs
"""
if BUILD_PATH.exists():
shutil.rmtree(BUILD_PATH)
shutil.copytree(DOCS_PATH, BUILD_PATH)
# When not verbose we want to hide all output
args = {'check': True}
if not cli.args.verbose:
args.update({'stdout': subprocess.DEVNULL, 'stderr': subprocess.STDOUT})
cli.log.info('Generating internal docs...')
# Generate internal docs
subprocess.run(['doxygen', 'Doxyfile'], **args)
subprocess.run(['moxygen', '-q', '-a', '-g', '-o', BUILD_PATH / 'internals_%s.md', 'doxygen/xml'], **args)
cli.log.info('Successfully generated internal docs to %s.', BUILD_PATH)

View File

@@ -19,7 +19,7 @@ ROW_LETTERS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnop'
COL_LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijilmnopqrstuvwxyz'
def show_keymap(info_json, title_caps=True):
def show_keymap(kb_info_json, title_caps=True):
"""Render the keymap in ascii art.
"""
keymap_path = locate_keymap(cli.config.info.keyboard, cli.config.info.keymap)
@@ -51,10 +51,10 @@ def show_layouts(kb_info_json, title_caps=True):
print(layout_art) # Avoid passing dirty data to cli.echo()
def show_matrix(info_json, title_caps=True):
def show_matrix(kb_info_json, title_caps=True):
"""Render the layout with matrix labels in ascii art.
"""
for layout_name, layout in info_json['layouts'].items():
for layout_name, layout in kb_info_json['layouts'].items():
# Build our label list
labels = []
for key in layout['layout']:
@@ -75,51 +75,51 @@ def show_matrix(info_json, title_caps=True):
print(render_layout(kb_info_json['layouts'][layout_name]['layout'], cli.config.info.ascii, labels))
def print_friendly_output(info_json):
def print_friendly_output(kb_info_json):
"""Print the info.json in a friendly text format.
"""
cli.echo('{fg_blue}Keyboard Name{fg_reset}: %s', info_json.get('keyboard_name', 'Unknown'))
cli.echo('{fg_blue}Manufacturer{fg_reset}: %s', info_json.get('manufacturer', 'Unknown'))
if 'url' in info_json:
cli.echo('{fg_blue}Website{fg_reset}: %s', info_json.get('url', ''))
if info_json.get('maintainer', 'qmk') == 'qmk':
cli.echo('{fg_blue}Keyboard Name{fg_reset}: %s', kb_info_json.get('keyboard_name', 'Unknown'))
cli.echo('{fg_blue}Manufacturer{fg_reset}: %s', kb_info_json.get('manufacturer', 'Unknown'))
if 'url' in kb_info_json:
cli.echo('{fg_blue}Website{fg_reset}: %s', kb_info_json.get('url', ''))
if kb_info_json.get('maintainer', 'qmk') == 'qmk':
cli.echo('{fg_blue}Maintainer{fg_reset}: QMK Community')
else:
cli.echo('{fg_blue}Maintainer{fg_reset}: %s', info_json['maintainer'])
cli.echo('{fg_blue}Keyboard Folder{fg_reset}: %s', info_json.get('keyboard_folder', 'Unknown'))
cli.echo('{fg_blue}Layouts{fg_reset}: %s', ', '.join(sorted(info_json['layouts'].keys())))
if 'width' in info_json and 'height' in info_json:
cli.echo('{fg_blue}Size{fg_reset}: %s x %s' % (info_json['width'], info_json['height']))
cli.echo('{fg_blue}Processor{fg_reset}: %s', info_json.get('processor', 'Unknown'))
cli.echo('{fg_blue}Bootloader{fg_reset}: %s', info_json.get('bootloader', 'Unknown'))
cli.echo('{fg_blue}Maintainer{fg_reset}: %s', kb_info_json['maintainer'])
cli.echo('{fg_blue}Keyboard Folder{fg_reset}: %s', kb_info_json.get('keyboard_folder', 'Unknown'))
cli.echo('{fg_blue}Layouts{fg_reset}: %s', ', '.join(sorted(kb_info_json['layouts'].keys())))
if 'width' in kb_info_json and 'height' in kb_info_json:
cli.echo('{fg_blue}Size{fg_reset}: %s x %s' % (kb_info_json['width'], kb_info_json['height']))
cli.echo('{fg_blue}Processor{fg_reset}: %s', kb_info_json.get('processor', 'Unknown'))
cli.echo('{fg_blue}Bootloader{fg_reset}: %s', kb_info_json.get('bootloader', 'Unknown'))
if cli.config.info.layouts:
show_layouts(info_json, True)
show_layouts(kb_info_json, True)
if cli.config.info.matrix:
show_matrix(info_json, True)
show_matrix(kb_info_json, True)
if cli.config_source.info.keymap and cli.config_source.info.keymap != 'config_file':
show_keymap(info_json, True)
show_keymap(kb_info_json, True)
def print_text_output(info_json):
def print_text_output(kb_info_json):
"""Print the info.json in a plain text format.
"""
for key in sorted(info_json):
for key in sorted(kb_info_json):
if key == 'layouts':
cli.echo('{fg_blue}layouts{fg_reset}: %s', ', '.join(sorted(info_json['layouts'].keys())))
cli.echo('{fg_blue}layouts{fg_reset}: %s', ', '.join(sorted(kb_info_json['layouts'].keys())))
else:
cli.echo('{fg_blue}%s{fg_reset}: %s', key, info_json[key])
cli.echo('{fg_blue}%s{fg_reset}: %s', key, kb_info_json[key])
if cli.config.info.layouts:
show_layouts(info_json, False)
show_layouts(kb_info_json, False)
if cli.config.info.matrix:
show_matrix(info_json, False)
show_matrix(kb_info_json, False)
if cli.config_source.info.keymap and cli.config_source.info.keymap != 'config_file':
show_keymap(info_json, False)
show_keymap(kb_info_json, False)
@cli.argument('-kb', '--keyboard', help='Keyboard to show info for.')

View File

@@ -38,7 +38,7 @@ def json2c(cli):
user_keymap = json.load(fd)
# Generate the keymap
keymap_c = qmk.keymap.generate(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers'])
keymap_c = qmk.keymap.generate_c(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers'])
if cli.args.output:
cli.args.output.parent.mkdir(parents=True, exist_ok=True)

View File

@@ -1,28 +1,13 @@
"""List the keyboards currently defined within QMK
"""
# We avoid pathlib here because this is performance critical code.
import os
import glob
from milc import cli
BASE_PATH = os.path.join(os.getcwd(), "keyboards") + os.path.sep
KB_WILDCARD = os.path.join(BASE_PATH, "**", "rules.mk")
def find_name(path):
"""Determine the keyboard name by stripping off the base_path and rules.mk.
"""
return path.replace(BASE_PATH, "").replace(os.path.sep + "rules.mk", "")
import qmk.keyboard
@cli.subcommand("List the keyboards currently defined within QMK")
def list_keyboards(cli):
"""List the keyboards currently defined within QMK
"""
# find everywhere we have rules.mk where keymaps isn't in the path
paths = [path for path in glob.iglob(KB_WILDCARD, recursive=True) if 'keymaps' not in path]
# Extract the keyboard name from the path and print it
for keyboard_name in sorted(map(find_name, paths)):
for keyboard_name in qmk.keyboard.list_keyboards():
print(keyboard_name)