Merge upstream QMK Firmware at '0.12.52~1'

This commit is contained in:
Drashna Jael're
2021-06-29 12:23:03 -07:00
415 changed files with 18692 additions and 7301 deletions

View File

@@ -1,12 +1,27 @@
"""Functions for working with config.h files.
"""
from pathlib import Path
import re
from milc import cli
from qmk.comment_remover import comment_remover
default_key_entry = {'x': -1, 'y': 0, 'w': 1}
single_comment_regex = re.compile(r' */[/*].*$')
multi_comment_regex = re.compile(r'/\*(.|\n)*?\*/', re.MULTILINE)
def strip_line_comment(string):
"""Removes comments from a single line string.
"""
return single_comment_regex.sub('', string)
def strip_multiline_comment(string):
"""Removes comments from a single line string.
"""
return multi_comment_regex.sub('', string)
def c_source_files(dir_names):
@@ -31,7 +46,7 @@ def find_layouts(file):
parsed_layouts = {}
# Search the file for LAYOUT macros and aliases
file_contents = file.read_text()
file_contents = file.read_text(encoding='utf-8')
file_contents = comment_remover(file_contents)
file_contents = file_contents.replace('\\\n', '')
@@ -52,8 +67,11 @@ def find_layouts(file):
layout = layout.strip()
parsed_layout = [_default_key(key) for key in layout.split(',')]
for key in parsed_layout:
key['matrix'] = matrix_locations.get(key['label'])
for i, key in enumerate(parsed_layout):
if 'label' not in key:
cli.log.error('Invalid LAYOUT macro in %s: Empty parameter name in macro %s at pos %s.', file, macro_name, i)
elif key['label'] in matrix_locations:
key['matrix'] = matrix_locations[key['label']]
parsed_layouts[macro_name] = {
'key_count': len(parsed_layout),
@@ -69,12 +87,7 @@ def find_layouts(file):
except ValueError:
continue
# Populate our aliases
for alias, text in aliases.items():
if text in parsed_layouts and 'KEYMAP' not in alias:
parsed_layouts[alias] = parsed_layouts[text]
return parsed_layouts
return parsed_layouts, aliases
def parse_config_h_file(config_h_file, config_h=None):
@@ -86,14 +99,12 @@ def parse_config_h_file(config_h_file, config_h=None):
config_h_file = Path(config_h_file)
if config_h_file.exists():
config_h_text = config_h_file.read_text()
config_h_text = config_h_file.read_text(encoding='utf-8')
config_h_text = config_h_text.replace('\\\n', '')
config_h_text = strip_multiline_comment(config_h_text)
for linenum, line in enumerate(config_h_text.split('\n')):
line = line.strip()
if '//' in line:
line = line[:line.index('//')].strip()
line = strip_line_comment(line).strip()
if not line:
continue
@@ -156,6 +167,6 @@ def _parse_matrix_locations(matrix, file, macro_name):
row = row.replace('{', '').replace('}', '')
for col_num, identifier in enumerate(row.split(',')):
if identifier != 'KC_NO':
matrix_locations[identifier] = (row_num, col_num)
matrix_locations[identifier] = [row_num, col_num]
return matrix_locations

View File

@@ -2,31 +2,159 @@
We list each subcommand here explicitly because all the reliable ways of searching for modules are slow and delay startup.
"""
import os
import shlex
import sys
from importlib.util import find_spec
from pathlib import Path
from subprocess import run
from milc import cli
from milc import cli, __VERSION__
from milc.questions import yesno
from . import c2json
from . import cformat
from . import chibios
from . import clean
from . import compile
from . import config
from . import docs
from . import doctor
from . import flash
from . import generate
from . import hello
from . import info
from . import json
from . import json2c
from . import lint
from . import list
from . import kle2json
from . import new
from . import pyformat
from . import pytest
if sys.version_info[0] != 3 or sys.version_info[1] < 6:
cli.log.error('Your Python is too old! Please upgrade to Python 3.6 or later.')
def _run_cmd(*command):
"""Run a command in a subshell.
"""
if 'windows' in cli.platform.lower():
safecmd = map(shlex.quote, command)
safecmd = ' '.join(safecmd)
command = [os.environ['SHELL'], '-c', safecmd]
return run(command)
def _find_broken_requirements(requirements):
""" Check if the modules in the given requirements.txt are available.
Args:
requirements
The path to a requirements.txt file
Returns a list of modules that couldn't be imported
"""
with Path(requirements).open() as fd:
broken_modules = []
for line in fd.readlines():
line = line.strip().replace('<', '=').replace('>', '=')
if len(line) == 0 or line[0] == '#' or line.startswith('-r'):
continue
if '#' in line:
line = line.split('#')[0]
module_name = line.split('=')[0] if '=' in line else line
module_import = module_name.replace('-', '_')
# Not every module is importable by its own name.
if module_name == "pep8-naming":
module_import = "pep8ext_naming"
if not find_spec(module_import):
broken_modules.append(module_name)
return broken_modules
def _broken_module_imports(requirements):
"""Make sure we can import all the python modules.
"""
broken_modules = _find_broken_requirements(requirements)
for module in broken_modules:
print('Could not find module %s!' % module)
if broken_modules:
return True
return False
# Make sure our python is new enough
#
# Supported version information
#
# Based on the OSes we support these are the minimum python version available by default.
# Last update: 2021 Jan 02
#
# Arch: 3.9
# Debian: 3.7
# Fedora 31: 3.7
# Fedora 32: 3.8
# Fedora 33: 3.9
# FreeBSD: 3.7
# Gentoo: 3.7
# macOS: 3.9 (from homebrew)
# msys2: 3.8
# Slackware: 3.7
# solus: 3.7
# void: 3.9
if sys.version_info[0] != 3 or sys.version_info[1] < 7:
print('Error: Your Python is too old! Please upgrade to Python 3.7 or later.')
exit(127)
milc_version = __VERSION__.split('.')
if int(milc_version[0]) < 2 and int(milc_version[1]) < 3:
requirements = Path('requirements.txt').resolve()
print(f'Your MILC library is too old! Please upgrade: python3 -m pip install -U -r {str(requirements)}')
exit(127)
# Check to make sure we have all our dependencies
msg_install = 'Please run `python3 -m pip install -r %s` to install required python dependencies.'
if _broken_module_imports('requirements.txt'):
if yesno('Would you like to install the required Python modules?'):
_run_cmd(sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt')
else:
print()
print(msg_install % (str(Path('requirements.txt').resolve()),))
print()
exit(1)
if cli.config.user.developer:
args = sys.argv[1:]
while args and args[0][0] == '-':
del args[0]
if not args or args[0] != 'config':
if _broken_module_imports('requirements-dev.txt'):
if yesno('Would you like to install the required developer Python modules?'):
_run_cmd(sys.executable, '-m', 'pip', 'install', '-r', 'requirements-dev.txt')
elif yesno('Would you like to disable developer mode?'):
_run_cmd(sys.argv[0], 'config', 'user.developer=None')
else:
print()
print(msg_install % (str(Path('requirements-dev.txt').resolve()),))
print('You can also turn off developer mode: qmk config user.developer=None')
print()
exit(1)
# Import our subcommands
from . import bux # noqa
from . import c2json # noqa
from . import cformat # noqa
from . import chibios # noqa
from . import clean # noqa
from . import compile # noqa
from milc.subcommand import config # noqa
from . import docs # noqa
from . import doctor # noqa
from . import fileformat # noqa
from . import flash # noqa
from . import format # noqa
from . import generate # noqa
from . import hello # noqa
from . import info # noqa
from . import json2c # noqa
from . import lint # noqa
from . import list # noqa
from . import kle2json # noqa
from . import multibuild # noqa
from . import new # noqa
from . import pyformat # noqa
from . import pytest # noqa

49
lib/python/qmk/cli/bux.py Executable file
View File

@@ -0,0 +1,49 @@
"""QMK Bux
World domination secret weapon.
"""
from milc import cli
from milc.subcommand import config
@cli.subcommand('QMK Bux miner.', hidden=True)
def bux(cli):
"""QMK bux
"""
if not cli.config.user.bux:
bux = 0
else:
bux = cli.config.user.bux
cli.args.read_only = False
config.set_config('user', 'bux', bux + 1)
cli.save_config()
buck = """
@@BBBBBBBBBBBBBBBBBBBBK `vP8#####BE2~ x###g_ `S###q n##} -j#Bl. vBBBBBBBBBBBBBBBBBBBB@@
@B `:!: ^#@#]- `!t@@&. 7@@B@#^ _Q@Q@@R y@@l:P@#1' `!!_ B@
@B r@@@B g@@| ` N@@u 7@@iv@@u *#@z"@@R y@@&@@Q- l@@@D B@
@B !#@B ^#@#x- I@B@@&' 7@@i "B@Q@@r _@@R y@@l.k#@W: `:@@D B@
@B B@B `v3g#####B0N#d. v##x 'ckk: -##A u##i `lB#I_ @@D B@
@B B@B @@D B@
@B B@B `._":!!!=~^*|)r^~:' @@D B@
@B ~*~ `,=)]}y2tjIIfKfKfaPsffsWsUyx~. **! B@
@B .*r***r= _*]yzKsqKUfz22IAA3HzzUjtktzHWsHsIz]. B@
@B )v` , !1- -rysHHUzUzo2jzoI22ztzkyykt2zjzUzIa3qPsl' !r*****` B@
@B :} @` .j `xzqdAfzKWsj2kkcycczqAsk2zHbg&ER5q55SNN5U~ !RBB#d`c#1 f#\BQ&v B@
@B _y ]# ,c vUWNWWPsfsssN9WyccnckAfUfWb0DR0&R5RRRddq2_ `@D`jr@2U@#c3@1@Qc- B@
@B !7! .r]` }AE0RdRqNd9dNR9fUIzzosPqqAddNNdER9EE9dPy! BQ!zy@iU@.Q@@y@8x- B@
@B :****>. '7adddDdR&gRNdRbd&dNNbbRdNdd5NdRRD0RSf}- .k0&EW`xR .8Q=NRRx B@
@B =**-rx*r}r~}" ;n2jkzsf3N3zsKsP5dddRddddRddNNqPzy\" '~****" B@
@B :!!~!;=~r>:*_ `:^vxikylulKfHkyjzzozoIoklix|^!-` B@
@B ```'-_""::::!:_-.`` B@
@B `- .` B@
@B r@= In source we trust @H B@
@B r@= @H B@
@B -g@= `}&###E7 W#g. :#Q n####~ R###8k ;#& `##.7#8-`R#z t@H B@
@B r@= 8@R=-=R@g R@@#:!@@ 2@&!:` 8@1=@@!*@B `@@- v@#8@y @H B@
@B r@= :@@- _@@_R@fB#}@@ 2@@@# 8@@#@Q.*@B `@@- y@@N @H B@
@B `. g@9=_~D@g R@}`&@@@ 2@&__` 8@u_Q@2!@@^-x@@` Y@QD@z .` B@
@@BBBBBBBBBBBBBBBBBBB_ `c8@@@81` S#] `N#B l####v D###BA. vg@@#0~ i#&' 5#K RBBBBBBBBBBBBBBBBBB@@
""" # noqa: Do not care about the ASCII art
print(f"{buck}\nYou've been blessed by the QMK gods!\nYou have {cli.config.user.bux} QMK bux.")

View File

@@ -2,18 +2,22 @@
"""
import json
from argcomplete.completers import FilesCompleter
from milc import cli
import qmk.keymap
import qmk.path
from qmk.json_encoders import InfoJSONEncoder
from qmk.keyboard import keyboard_completer, keyboard_folder
from qmk.errors import CppError
@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('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, 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.argument('filename', arg_only=True, completer=FilesCompleter('.c'), 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.
@@ -34,7 +38,13 @@ def c2json(cli):
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)
try:
keymap_json = qmk.keymap.c2json(cli.args.keyboard, cli.args.keymap, cli.args.filename, use_cpp=cli.args.no_cpp)
except CppError as e:
if cli.config.general.verbose:
cli.log.debug('The C pre-processor ran into a fatal error: %s', e)
cli.log.error('Something went wrong. Try to use --no-cpp.\nUse the CLI in verbose mode to find out more.')
return False
# Generate the keymap.json
try:
@@ -46,8 +56,8 @@ def c2json(cli):
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))
cli.args.output.replace(cli.args.output.parent / (cli.args.output.name + '.bak'))
cli.args.output.write_text(json.dumps(keymap_json, cls=InfoJSONEncoder))
if not cli.args.quiet:
cli.log.info('Wrote keymap to %s.', cli.args.output)

View File

@@ -1,65 +1,137 @@
"""Format C code according to QMK's style.
"""
import subprocess
from os import path
from shutil import which
from subprocess import CalledProcessError, DEVNULL, Popen, PIPE
from argcomplete.completers import FilesCompleter
from milc import cli
from qmk.path import normpath
from qmk.c_parse import c_source_files
c_file_suffixes = ('c', 'h', 'cpp')
core_dirs = ('drivers', 'quantum', 'tests', 'tmk_core', 'platforms')
ignored = ('tmk_core/protocol/usb_hid', 'quantum/template', 'platforms/chibios')
def cformat_run(files, all_files):
def find_clang_format():
"""Returns the path to clang-format.
"""
for clang_version in range(20, 6, -1):
binary = f'clang-format-{clang_version}'
if which(binary):
return binary
return 'clang-format'
def find_diffs(files):
"""Run clang-format and diff it against a file.
"""
found_diffs = False
for file in files:
cli.log.debug('Checking for changes in %s', file)
clang_format = Popen([find_clang_format(), file], stdout=PIPE, stderr=PIPE, universal_newlines=True)
diff = cli.run(['diff', '-u', f'--label=a/{file}', f'--label=b/{file}', str(file), '-'], stdin=clang_format.stdout, capture_output=True)
if diff.returncode != 0:
print(diff.stdout)
found_diffs = True
return found_diffs
def cformat_run(files):
"""Spawn clang-format subprocess with proper arguments
"""
# Determine which version of clang-format to use
clang_format = ['clang-format', '-i']
for clang_version in [10, 9, 8, 7]:
binary = 'clang-format-%d' % clang_version
if which(binary):
clang_format[0] = binary
break
try:
if not files:
cli.log.warn('No changes detected. Use "qmk cformat -a" to format all files')
return False
subprocess.run(clang_format + [file for file in files], check=True)
cli.log.info('Successfully formatted the C code.')
clang_format = [find_clang_format(), '-i']
except subprocess.CalledProcessError:
try:
cli.run([*clang_format, *map(str, files)], check=True, capture_output=False, stdin=DEVNULL)
cli.log.info('Successfully formatted the C code.')
return True
except CalledProcessError as e:
cli.log.error('Error formatting C code!')
cli.log.debug('%s exited with returncode %s', e.cmd, e.returncode)
cli.log.debug('STDOUT:')
cli.log.debug(e.stdout)
cli.log.debug('STDERR:')
cli.log.debug(e.stderr)
return False
@cli.argument('-a', '--all-files', arg_only=True, action='store_true', help='Format all core files.')
def filter_files(files, core_only=False):
"""Yield only files to be formatted and skip the rest
"""
if core_only:
# Filter non-core files
for index, file in enumerate(files):
# The following statement checks each file to see if the file path is
# - in the core directories
# - not in the ignored directories
if not any(i in str(file) for i in core_dirs) or any(i in str(file) for i in ignored):
files[index] = None
cli.log.debug("Skipping non-core file %s, as '--core-only' is used.", file)
for file in files:
if file and file.name.split('.')[-1] in c_file_suffixes:
yield file
else:
cli.log.debug('Skipping file %s', file)
@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Flag only, don't automatically format.")
@cli.argument('-b', '--base-branch', default='origin/master', help='Branch to compare to diffs to.')
@cli.argument('files', nargs='*', arg_only=True, help='Filename(s) to format.')
@cli.argument('-a', '--all-files', arg_only=True, action='store_true', help='Format all core files.')
@cli.argument('--core-only', arg_only=True, action='store_true', help='Format core files only.')
@cli.argument('files', nargs='*', arg_only=True, type=normpath, completer=FilesCompleter('.c'), help='Filename(s) to format.')
@cli.subcommand("Format C code according to QMK's style.", hidden=False if cli.config.user.developer else True)
def cformat(cli):
"""Format C code according to QMK's style.
"""
# Empty array for files
files = []
# Core directories for formatting
core_dirs = ['drivers', 'quantum', 'tests', 'tmk_core', 'platforms']
ignores = ['tmk_core/protocol/usb_hid', 'quantum/template', 'platforms/chibios']
# Find the list of files to format
if cli.args.files:
files.extend(normpath(file) for file in cli.args.files)
files = list(filter_files(cli.args.files, cli.args.core_only))
if not files:
cli.log.error('No C files in filelist: %s', ', '.join(map(str, cli.args.files)))
exit(0)
if cli.args.all_files:
cli.log.warning('Filenames passed with -a, only formatting: %s', ','.join(map(str, files)))
# If -a is specified
elif cli.args.all_files:
all_files = c_source_files(core_dirs)
# The following statement checks each file to see if the file path is in the ignored directories.
files.extend(file for file in all_files if not any(i in str(file) for i in ignores))
# No files specified & no -a flag
files = list(filter_files(all_files, True))
else:
base_args = ['git', 'diff', '--name-only', cli.args.base_branch]
out = subprocess.run(base_args + core_dirs, check=True, stdout=subprocess.PIPE)
changed_files = filter(None, out.stdout.decode('UTF-8').split('\n'))
filtered_files = [normpath(file) for file in changed_files if not any(i in file for i in ignores)]
files.extend(file for file in filtered_files if file.exists() and file.suffix in ['.c', '.h', '.cpp'])
git_diff_cmd = ['git', 'diff', '--name-only', cli.args.base_branch, *core_dirs]
git_diff = cli.run(git_diff_cmd, stdin=DEVNULL)
if git_diff.returncode != 0:
cli.log.error("Error running %s", git_diff_cmd)
print(git_diff.stderr)
return git_diff.returncode
files = []
for file in git_diff.stdout.strip().split('\n'):
if not any([file.startswith(ignore) for ignore in ignored]):
if path.exists(file) and file.split('.')[-1] in c_file_suffixes:
files.append(file)
# Sanity check
if not files:
cli.log.error('No changed files detected. Use "qmk cformat -a" to format all core files')
return False
# Run clang-format on the files we've found
cformat_run(files, cli.args.all_files)
if cli.args.dry_run:
return not find_diffs(files)
else:
return cformat_run(files)

View File

@@ -32,7 +32,7 @@ file_header = """\
/*
* This file was auto-generated by:
* `qmk chibios-confupdate -i {0} -r {1}`
* `qmk chibios-confmigrate -i {0} -r {1}`
*/
#pragma once
@@ -40,7 +40,7 @@ file_header = """\
def collect_defines(filepath):
with open(filepath, 'r') as f:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
define_search = re.compile(r'(?m)^#\s*define\s+(?:.*\\\r?\n)*.*$', re.MULTILINE)
value_search = re.compile(r'^#\s*define\s+(?P<name>[a-zA-Z0-9_]+(\([^\)]*\))?)\s*(?P<value>.*)', re.DOTALL)
@@ -107,10 +107,11 @@ def migrate_mcuconf_h(to_override, outfile):
print("", file=outfile)
@cli.argument('-i', '--input', type=normpath, arg_only=True, help='Specify input config file.')
@cli.argument('-r', '--reference', type=normpath, arg_only=True, help='Specify the reference file to compare against')
@cli.argument('-i', '--input', type=normpath, arg_only=True, required=True, help='Specify input config file.')
@cli.argument('-r', '--reference', type=normpath, arg_only=True, required=True, help='Specify the reference file to compare against')
@cli.argument('-o', '--overwrite', arg_only=True, action='store_true', help='Overwrites the input file during migration.')
@cli.argument('-d', '--delete', arg_only=True, action='store_true', help='If the file has no overrides, migration will delete the input file.')
@cli.argument('-f', '--force', arg_only=True, action='store_true', help='Re-migrates an already migrated file, even if it doesn\'t detect a full ChibiOS config.')
@cli.subcommand('Generates a migrated ChibiOS configuration file, as a result of comparing the input against a reference')
def chibios_confmigrate(cli):
"""Generates a usable ChibiOS replacement configuration file, based on a fully-defined conf and a reference config.
@@ -142,20 +143,20 @@ def chibios_confmigrate(cli):
eprint('--------------------------------------')
if "CHCONF_H" in input_defs["dict"] or "_CHCONF_H_" in input_defs["dict"]:
if cli.args.input.name == "chconf.h" and ("CHCONF_H" in input_defs["dict"] or "_CHCONF_H_" in input_defs["dict"] or cli.args.force):
migrate_chconf_h(to_override, outfile=sys.stdout)
if cli.args.overwrite:
with open(cli.args.input, "w") as out_file:
with open(cli.args.input, "w", encoding='utf-8') as out_file:
migrate_chconf_h(to_override, outfile=out_file)
elif "HALCONF_H" in input_defs["dict"] or "_HALCONF_H_" in input_defs["dict"]:
elif cli.args.input.name == "halconf.h" and ("HALCONF_H" in input_defs["dict"] or "_HALCONF_H_" in input_defs["dict"] or cli.args.force):
migrate_halconf_h(to_override, outfile=sys.stdout)
if cli.args.overwrite:
with open(cli.args.input, "w") as out_file:
with open(cli.args.input, "w", encoding='utf-8') as out_file:
migrate_halconf_h(to_override, outfile=out_file)
elif "MCUCONF_H" in input_defs["dict"] or "_MCUCONF_H_" in input_defs["dict"]:
elif cli.args.input.name == "mcuconf.h" and ("MCUCONF_H" in input_defs["dict"] or "_MCUCONF_H_" in input_defs["dict"] or cli.args.force):
migrate_mcuconf_h(to_override, outfile=sys.stdout)
if cli.args.overwrite:
with open(cli.args.input, "w") as out_file:
with open(cli.args.input, "w", encoding='utf-8') as out_file:
migrate_mcuconf_h(to_override, outfile=out_file)

View File

@@ -1,9 +1,9 @@
"""Clean the QMK firmware folder of build artifacts.
"""
from qmk.commands import run
from milc import cli
from subprocess import DEVNULL
import shutil
from qmk.commands import create_make_target
from milc import cli
@cli.argument('-a', '--all', arg_only=True, action='store_true', help='Remove *.hex and *.bin files in the QMK root as well.')
@@ -11,6 +11,4 @@ import shutil
def clean(cli):
"""Runs `make clean` (or `make distclean` if --all is passed)
"""
make_cmd = 'gmake' if shutil.which('gmake') else 'make'
run([make_cmd, 'distclean' if cli.args.all else 'clean'])
cli.run(create_make_target('distclean' if cli.args.all else 'clean'), capture_output=False, stdin=DEVNULL)

View File

@@ -2,17 +2,21 @@
You can compile a keymap already in the repo or using a QMK Configurator export.
"""
from argparse import FileType
from subprocess import DEVNULL
from argcomplete.completers import FilesCompleter
from milc import cli
import qmk.path
from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json
from qmk.keyboard import keyboard_completer, keyboard_folder
from qmk.keymap import keymap_completer
@cli.argument('filename', nargs='?', arg_only=True, type=FileType('r'), help='The configurator export to compile')
@cli.argument('-kb', '--keyboard', help='The keyboard to build a firmware for. Ignored when a configurator export is supplied.')
@cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Ignored when a configurator export is supplied.')
@cli.argument('filename', nargs='?', arg_only=True, type=qmk.path.FileType('r'), completer=FilesCompleter('.json'), help='The configurator export to compile')
@cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='The keyboard to build a firmware for. Ignored when a configurator export is supplied.')
@cli.argument('-km', '--keymap', completer=keymap_completer, help='The keymap to build a firmware for. Ignored when a configurator export is supplied.')
@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the make command to be run.")
@cli.argument('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs to run.")
@cli.argument('-e', '--env', arg_only=True, action='append', default=[], help="Set a variable to be passed to make. May be passed multiple times.")
@@ -29,8 +33,7 @@ def compile(cli):
"""
if cli.args.clean and not cli.args.filename and not cli.args.dry_run:
command = create_make_command(cli.config.compile.keyboard, cli.config.compile.keymap, 'clean')
# FIXME(skullydazed/anyone): Remove text=False once milc 1.0.11 has had enough time to be installed everywhere.
cli.run(command, capture_output=False, text=False)
cli.run(command, capture_output=False, stdin=DEVNULL)
# Build the environment vars
envs = {}

View File

@@ -3,12 +3,12 @@
Check out the user's QMK environment and make sure it's ready to compile.
"""
import platform
from subprocess import DEVNULL
from milc import cli
from milc.questions import yesno
from qmk import submodules
from qmk.constants import QMK_FIRMWARE
from qmk.commands import run
from qmk.os_helpers import CheckStatus, check_binaries, check_binary_versions, check_submodules, check_git_repo
@@ -31,16 +31,27 @@ def os_tests():
def os_test_linux():
"""Run the Linux specific tests.
"""
cli.log.info("Detected {fg_cyan}Linux.")
from qmk.os_helpers.linux import check_udev_rules
# Don't bother with udev on WSL, for now
if 'microsoft' in platform.uname().release.lower():
cli.log.info("Detected {fg_cyan}Linux (WSL){fg_reset}.")
return check_udev_rules()
# https://github.com/microsoft/WSL/issues/4197
if QMK_FIRMWARE.as_posix().startswith("/mnt"):
cli.log.warning("I/O performance on /mnt may be extremely slow.")
return CheckStatus.WARNING
return CheckStatus.OK
else:
cli.log.info("Detected {fg_cyan}Linux{fg_reset}.")
from qmk.os_helpers.linux import check_udev_rules
return check_udev_rules()
def os_test_macos():
"""Run the Mac specific tests.
"""
cli.log.info("Detected {fg_cyan}macOS.")
cli.log.info("Detected {fg_cyan}macOS %s{fg_reset}.", platform.mac_ver()[0])
return CheckStatus.OK
@@ -48,7 +59,8 @@ def os_test_macos():
def os_test_windows():
"""Run the Windows specific tests.
"""
cli.log.info("Detected {fg_cyan}Windows.")
win32_ver = platform.win32_ver()
cli.log.info("Detected {fg_cyan}Windows %s (%s){fg_reset}.", win32_ver[0], win32_ver[1])
return CheckStatus.OK
@@ -65,11 +77,10 @@ def doctor(cli):
* [ ] Compile a trivial program with each compiler
"""
cli.log.info('QMK Doctor is checking your environment.')
cli.log.info('QMK home: {fg_cyan}%s', QMK_FIRMWARE)
status = os_tests()
cli.log.info('QMK home: {fg_cyan}%s', QMK_FIRMWARE)
# Make sure our QMK home is a Git repo
git_ok = check_git_repo()
@@ -82,7 +93,7 @@ def doctor(cli):
if not bin_ok:
if yesno('Would you like to install dependencies?', default=True):
run(['util/qmk_install.sh'])
cli.run(['util/qmk_install.sh', '-y'], stdin=DEVNULL, capture_output=False)
bin_ok = check_binaries()
if bin_ok:
@@ -107,9 +118,9 @@ def doctor(cli):
submodules.update()
sub_ok = check_submodules()
if CheckStatus.ERROR in sub_ok:
if sub_ok == CheckStatus.ERROR:
status = CheckStatus.ERROR
elif CheckStatus.WARNING in sub_ok and status == CheckStatus.OK:
elif sub_ok == CheckStatus.WARNING and status == CheckStatus.OK:
status = CheckStatus.WARNING
# Report a summary of our findings to the user

View File

@@ -3,13 +3,15 @@
You can compile a keymap already in the repo or using a QMK Configurator export.
A bootloader must be specified.
"""
from argparse import FileType
from subprocess import DEVNULL
from argcomplete.completers import FilesCompleter
from milc import cli
import qmk.path
from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json
from qmk.keyboard import keyboard_completer, keyboard_folder
def print_bootloader_help():
@@ -30,11 +32,11 @@ def print_bootloader_help():
cli.echo('For more info, visit https://docs.qmk.fm/#/flashing')
@cli.argument('filename', nargs='?', arg_only=True, type=FileType('r'), help='The configurator export JSON to compile.')
@cli.argument('filename', nargs='?', arg_only=True, type=qmk.path.FileType('r'), completer=FilesCompleter('.json'), help='The configurator export JSON to compile.')
@cli.argument('-b', '--bootloaders', action='store_true', help='List the available bootloaders.')
@cli.argument('-bl', '--bootloader', default='flash', help='The flash command, corresponding to qmk\'s make options of bootloaders.')
@cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.')
@cli.argument('-kb', '--keyboard', help='The keyboard to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.')
@cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='The keyboard to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.')
@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the make command to be run.")
@cli.argument('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs to run.")
@cli.argument('-e', '--env', arg_only=True, action='append', default=[], help="Set a variable to be passed to make. May be passed multiple times.")
@@ -54,7 +56,7 @@ def flash(cli):
"""
if cli.args.clean and not cli.args.filename and not cli.args.dry_run:
command = create_make_command(cli.config.flash.keyboard, cli.config.flash.keymap, 'clean')
cli.run(command, capture_output=False)
cli.run(command, capture_output=False, stdin=DEVNULL)
# Build the environment vars
envs = {}
@@ -97,7 +99,7 @@ def flash(cli):
cli.log.info('Compiling keymap with {fg_cyan}%s', ' '.join(command))
if not cli.args.dry_run:
cli.echo('\n')
compile = cli.run(command, capture_output=False, text=True)
compile = cli.run(command, capture_output=False, stdin=DEVNULL)
return compile.returncode
else:

View File

@@ -0,0 +1 @@
from . import json

View File

@@ -0,0 +1,66 @@
"""JSON Formatting Script
Spits out a JSON file formatted with one of QMK's formatters.
"""
import json
from jsonschema import ValidationError
from milc import cli
from qmk.info import info_json
from qmk.json_schema import json_load, keyboard_validate
from qmk.json_encoders import InfoJSONEncoder, KeymapJSONEncoder
from qmk.path import normpath
@cli.argument('json_file', arg_only=True, type=normpath, help='JSON file to format')
@cli.argument('-f', '--format', choices=['auto', 'keyboard', 'keymap'], default='auto', arg_only=True, help='JSON formatter to use (Default: autodetect)')
@cli.subcommand('Generate an info.json file for a keyboard.', hidden=False if cli.config.user.developer else True)
def format_json(cli):
"""Format a json file.
"""
json_file = json_load(cli.args.json_file)
if cli.args.format == 'auto':
try:
keyboard_validate(json_file)
json_encoder = InfoJSONEncoder
except ValidationError as e:
cli.log.warning('File %s did not validate as a keyboard:\n\t%s', cli.args.json_file, e)
cli.log.info('Treating %s as a keymap file.', cli.args.json_file)
json_encoder = KeymapJSONEncoder
elif cli.args.format == 'keyboard':
json_encoder = InfoJSONEncoder
elif cli.args.format == 'keymap':
json_encoder = KeymapJSONEncoder
else:
# This should be impossible
cli.log.error('Unknown format: %s', cli.args.format)
return False
if json_encoder == KeymapJSONEncoder and 'layout' in json_file:
# Attempt to format the keycodes.
layout = json_file['layout']
info_data = info_json(json_file['keyboard'])
if layout in info_data.get('layout_aliases', {}):
layout = json_file['layout'] = info_data['layout_aliases'][layout]
if layout in info_data.get('layouts'):
for layer_num, layer in enumerate(json_file['layers']):
current_layer = []
last_row = 0
for keymap_key, info_key in zip(layer, info_data['layouts'][layout]['layout']):
if last_row != info_key['y']:
current_layer.append('JSON_NEWLINE')
last_row = info_key['y']
current_layer.append(keymap_key)
json_file['layers'][layer_num] = current_layer
# Display the results
print(json.dumps(json_file, cls=json_encoder))

View File

@@ -1,3 +1,9 @@
from . import api
from . import config_h
from . import dfu_header
from . import docs
from . import info_json
from . import keyboard_h
from . import layouts
from . import rgb_breathe_table
from . import rules_mk

View File

@@ -8,51 +8,80 @@ from milc import cli
from qmk.datetime import current_datetime
from qmk.info import info_json
from qmk.keyboard import list_keyboards
from qmk.json_encoders import InfoJSONEncoder
from qmk.json_schema import json_load
from qmk.keyboard import find_readme, list_keyboards
@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't write the data to disk.")
@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'
keyboard_all_file = v1_dir / 'keyboards.json' # A massive JSON containing everything
keyboard_list_file = v1_dir / 'keyboard_list.json' # A simple list of keyboard targets
keyboard_aliases_file = v1_dir / 'keyboard_aliases.json' # A list of historical keyboard names and their new name
keyboard_metadata_file = v1_dir / 'keyboard_metadata.json' # All the data configurator/via needs for initialization
usb_file = v1_dir / 'usb.json' # A mapping of USB VID/PID -> keyboard target
if not api_data_dir.exists():
api_data_dir.mkdir()
kb_all = {'last_updated': current_datetime(), 'keyboards': {}}
usb_list = {'last_updated': current_datetime(), 'devices': {}}
kb_all = {}
usb_list = {}
# Generate and write keyboard specific JSON files
for keyboard_name in list_keyboards():
kb_all['keyboards'][keyboard_name] = info_json(keyboard_name)
kb_all[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_readme_src = find_readme(keyboard_name)
keyboard_dir.mkdir(parents=True, exist_ok=True)
keyboard_info.write_text(json.dumps({'last_updated': current_datetime(), 'keyboards': {keyboard_name: kb_all['keyboards'][keyboard_name]}}))
keyboard_json = json.dumps({'last_updated': current_datetime(), 'keyboards': {keyboard_name: kb_all[keyboard_name]}})
if not cli.args.dry_run:
keyboard_info.write_text(keyboard_json)
cli.log.debug('Wrote file %s', keyboard_info)
if keyboard_readme_src.exists():
copyfile(keyboard_readme_src, keyboard_readme)
if keyboard_readme_src:
copyfile(keyboard_readme_src, keyboard_readme)
cli.log.debug('Copied %s -> %s', keyboard_readme_src, keyboard_readme)
if 'usb' in kb_all['keyboards'][keyboard_name]:
usb = kb_all['keyboards'][keyboard_name]['usb']
if 'usb' in kb_all[keyboard_name]:
usb = kb_all[keyboard_name]['usb']
if usb['vid'] not in usb_list['devices']:
usb_list['devices'][usb['vid']] = {}
if 'vid' in usb and usb['vid'] not in usb_list:
usb_list[usb['vid']] = {}
if usb['pid'] not in usb_list['devices'][usb['vid']]:
usb_list['devices'][usb['vid']][usb['pid']] = {}
if 'pid' in usb and usb['pid'] not in usb_list[usb['vid']]:
usb_list[usb['vid']][usb['pid']] = {}
usb_list['devices'][usb['vid']][usb['pid']][keyboard_name] = usb
if 'vid' in usb and 'pid' in usb:
usb_list[usb['vid']][usb['pid']][keyboard_name] = usb
# Generate data for the global files
keyboard_list = sorted(kb_all)
keyboard_aliases = json_load(Path('data/mappings/keyboard_aliases.json'))
keyboard_metadata = {
'last_updated': current_datetime(),
'keyboards': keyboard_list,
'keyboard_aliases': keyboard_aliases,
'usb': usb_list,
}
# 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))
keyboard_all_json = json.dumps({'last_updated': current_datetime(), 'keyboards': kb_all}, cls=InfoJSONEncoder)
usb_json = json.dumps({'last_updated': current_datetime(), 'usb': usb_list}, cls=InfoJSONEncoder)
keyboard_list_json = json.dumps({'last_updated': current_datetime(), 'keyboards': keyboard_list}, cls=InfoJSONEncoder)
keyboard_aliases_json = json.dumps({'last_updated': current_datetime(), 'keyboard_aliases': keyboard_aliases}, cls=InfoJSONEncoder)
keyboard_metadata_json = json.dumps(keyboard_metadata, cls=InfoJSONEncoder)
if not cli.args.dry_run:
keyboard_all_file.write_text(keyboard_all_json)
usb_file.write_text(usb_json)
keyboard_list_file.write_text(keyboard_list_json)
keyboard_aliases_file.write_text(keyboard_aliases_json)
keyboard_metadata_file.write_text(keyboard_metadata_json)

View File

@@ -0,0 +1,154 @@
"""Used by the make system to generate info_config.h from info.json.
"""
from pathlib import Path
from dotty_dict import dotty
from milc import cli
from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.info import info_json
from qmk.json_schema import json_load
from qmk.keyboard import keyboard_completer, keyboard_folder
from qmk.path import is_keyboard, normpath
def direct_pins(direct_pins):
"""Return the config.h lines that set the direct pins.
"""
rows = []
for row in direct_pins:
cols = ','.join(map(str, [col or 'NO_PIN' for col in row]))
rows.append('{' + cols + '}')
col_count = len(direct_pins[0])
row_count = len(direct_pins)
return """
#ifndef MATRIX_COLS
# define MATRIX_COLS %s
#endif // MATRIX_COLS
#ifndef MATRIX_ROWS
# define MATRIX_ROWS %s
#endif // MATRIX_ROWS
#ifndef DIRECT_PINS
# define DIRECT_PINS {%s}
#endif // DIRECT_PINS
""" % (col_count, row_count, ','.join(rows))
def pin_array(define, pins):
"""Return the config.h lines that set a pin array.
"""
pin_num = len(pins)
pin_array = ', '.join(map(str, [pin or 'NO_PIN' for pin in pins]))
return f"""
#ifndef {define}S
# define {define}S {pin_num}
#endif // {define}S
#ifndef {define}_PINS
# define {define}_PINS {{ {pin_array} }}
#endif // {define}_PINS
"""
def matrix_pins(matrix_pins):
"""Add the matrix config to the config.h.
"""
pins = []
if 'direct' in matrix_pins:
pins.append(direct_pins(matrix_pins['direct']))
if 'cols' in matrix_pins:
pins.append(pin_array('MATRIX_COL', matrix_pins['cols']))
if 'rows' in matrix_pins:
pins.append(pin_array('MATRIX_ROW', matrix_pins['rows']))
return '\n'.join(pins)
@cli.argument('-o', '--output', arg_only=True, type=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', type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate config.h for.')
@cli.subcommand('Used by the make system to generate info_config.h from info.json', hidden=True)
@automagic_keyboard
@automagic_keymap
def generate_config_h(cli):
"""Generates the info_config.h file.
"""
# Determine our keyboard(s)
if not cli.config.generate_config_h.keyboard:
cli.log.error('Missing parameter: --keyboard')
cli.subcommands['info'].print_help()
return False
if not is_keyboard(cli.config.generate_config_h.keyboard):
cli.log.error('Invalid keyboard: "%s"', cli.config.generate_config_h.keyboard)
return False
# Build the info_config.h file.
kb_info_json = dotty(info_json(cli.config.generate_config_h.keyboard))
info_config_map = json_load(Path('data/mappings/info_config.json'))
config_h_lines = ['/* This file was generated by `qmk generate-config-h`. Do not edit or copy.' ' */', '', '#pragma once']
# Iterate through the info_config map to generate basic things
for config_key, info_dict in info_config_map.items():
info_key = info_dict['info_key']
key_type = info_dict.get('value_type', 'str')
to_config = info_dict.get('to_config', True)
if not to_config:
continue
try:
config_value = kb_info_json[info_key]
except KeyError:
continue
if key_type.startswith('array'):
config_h_lines.append('')
config_h_lines.append(f'#ifndef {config_key}')
config_h_lines.append(f'# define {config_key} {{ {", ".join(map(str, config_value))} }}')
config_h_lines.append(f'#endif // {config_key}')
elif key_type == 'bool':
if config_value:
config_h_lines.append('')
config_h_lines.append(f'#ifndef {config_key}')
config_h_lines.append(f'# define {config_key}')
config_h_lines.append(f'#endif // {config_key}')
elif key_type == 'mapping':
for key, value in config_value.items():
config_h_lines.append('')
config_h_lines.append(f'#ifndef {key}')
config_h_lines.append(f'# define {key} {value}')
config_h_lines.append(f'#endif // {key}')
else:
config_h_lines.append('')
config_h_lines.append(f'#ifndef {config_key}')
config_h_lines.append(f'# define {config_key} {config_value}')
config_h_lines.append(f'#endif // {config_key}')
if 'matrix_pins' in kb_info_json:
config_h_lines.append(matrix_pins(kb_info_json['matrix_pins']))
# Show the results
config_h = '\n'.join(config_h_lines)
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.parent / (cli.args.output.name + '.bak'))
cli.args.output.write_text(config_h)
if not cli.args.quiet:
cli.log.info('Wrote info_config.h to %s.', cli.args.output)
else:
print(config_h)

View File

@@ -0,0 +1,60 @@
"""Used by the make system to generate LUFA Keyboard.h from info.json
"""
from dotty_dict import dotty
from milc import cli
from qmk.decorators import automagic_keyboard
from qmk.info import info_json
from qmk.path import is_keyboard, normpath
from qmk.keyboard import keyboard_completer
@cli.argument('-o', '--output', arg_only=True, type=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', completer=keyboard_completer, help='Keyboard to generate LUFA Keyboard.h for.')
@cli.subcommand('Used by the make system to generate LUFA Keyboard.h from info.json', hidden=True)
@automagic_keyboard
def generate_dfu_header(cli):
"""Generates the Keyboard.h file.
"""
# Determine our keyboard(s)
if not cli.config.generate_dfu_header.keyboard:
cli.log.error('Missing parameter: --keyboard')
cli.subcommands['info'].print_help()
return False
if not is_keyboard(cli.config.generate_dfu_header.keyboard):
cli.log.error('Invalid keyboard: "%s"', cli.config.generate_dfu_header.keyboard)
return False
# Build the Keyboard.h file.
kb_info_json = dotty(info_json(cli.config.generate_dfu_header.keyboard))
keyboard_h_lines = ['/* This file was generated by `qmk generate-dfu-header`. Do not edit or copy.' ' */', '', '#pragma once']
keyboard_h_lines.append(f'#define MANUFACTURER {kb_info_json["manufacturer"]}')
keyboard_h_lines.append(f'#define PRODUCT {cli.config.generate_dfu_header.keyboard} Bootloader')
# Optional
if 'qmk_lufa_bootloader.esc_output' in kb_info_json:
keyboard_h_lines.append(f'#define QMK_ESC_OUTPUT {kb_info_json["qmk_lufa_bootloader.esc_output"]}')
if 'qmk_lufa_bootloader.esc_input' in kb_info_json:
keyboard_h_lines.append(f'#define QMK_ESC_INPUT {kb_info_json["qmk_lufa_bootloader.esc_input"]}')
if 'qmk_lufa_bootloader.led' in kb_info_json:
keyboard_h_lines.append(f'#define QMK_LED {kb_info_json["qmk_lufa_bootloader.led"]}')
if 'qmk_lufa_bootloader.speaker' in kb_info_json:
keyboard_h_lines.append(f'#define QMK_SPEAKER {kb_info_json["qmk_lufa_bootloader.speaker"]}')
# Show the results
keyboard_h = '\n'.join(keyboard_h_lines)
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.parent / (cli.args.output.name + '.bak'))
cli.args.output.write_text(keyboard_h)
if not cli.args.quiet:
cli.log.info('Wrote Keyboard.h to %s.', cli.args.output)
else:
print(keyboard_h)

View File

@@ -1,8 +1,8 @@
"""Build QMK documentation locally
"""
import shutil
import subprocess
from pathlib import Path
from subprocess import DEVNULL
from milc import cli
@@ -24,14 +24,16 @@ def generate_docs(cli):
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})
args = {
'capture_output': False if cli.config.general.verbose else True,
'check': True,
'stdin': DEVNULL,
}
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.run(['doxygen', 'Doxyfile'], **args)
cli.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

@@ -0,0 +1,67 @@
"""Keyboard information script.
Compile an info.json for a particular keyboard and pretty-print it.
"""
import json
from jsonschema import Draft7Validator, validators
from milc import cli
from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.info import info_json
from qmk.json_encoders import InfoJSONEncoder
from qmk.json_schema import load_jsonschema
from qmk.keyboard import keyboard_completer, keyboard_folder
from qmk.path import is_keyboard
def pruning_validator(validator_class):
"""Extends Draft7Validator to remove properties that aren't specified in the schema.
"""
validate_properties = validator_class.VALIDATORS["properties"]
def remove_additional_properties(validator, properties, instance, schema):
for prop in list(instance.keys()):
if prop not in properties:
del instance[prop]
for error in validate_properties(validator, properties, instance, schema):
yield error
return validators.extend(validator_class, {"properties": remove_additional_properties})
def strip_info_json(kb_info_json):
"""Remove the API-only properties from the info.json.
"""
pruning_draft_7_validator = pruning_validator(Draft7Validator)
schema = load_jsonschema('keyboard')
validator = pruning_draft_7_validator(schema).validate
return validator(kb_info_json)
@cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='Keyboard to show info for.')
@cli.argument('-km', '--keymap', help='Show the layers for a JSON keymap too.')
@cli.subcommand('Generate an info.json file for a keyboard.', hidden=False if cli.config.user.developer else True)
@automagic_keyboard
@automagic_keymap
def generate_info_json(cli):
"""Generate an info.json file for a keyboard
"""
# Determine our keyboard(s)
if not cli.config.generate_info_json.keyboard:
cli.log.error('Missing parameter: --keyboard')
cli.subcommands['info'].print_help()
return False
if not is_keyboard(cli.config.generate_info_json.keyboard):
cli.log.error('Invalid keyboard: "%s"', cli.config.generate_info_json.keyboard)
return False
# Build the info.json file
kb_info_json = info_json(cli.config.generate_info_json.keyboard)
strip_info_json(kb_info_json)
# Display the results
print(json.dumps(kb_info_json, indent=2, cls=InfoJSONEncoder))

View File

@@ -0,0 +1,60 @@
"""Used by the make system to generate keyboard.h from info.json.
"""
from milc import cli
from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.info import info_json
from qmk.keyboard import keyboard_completer, keyboard_folder
from qmk.path import normpath
def would_populate_layout_h(keyboard):
"""Detect if a given keyboard is doing data driven layouts
"""
# Build the info.json file
kb_info_json = info_json(keyboard)
for layout_name in kb_info_json['layouts']:
if kb_info_json['layouts'][layout_name]['c_macro']:
continue
if 'matrix' not in kb_info_json['layouts'][layout_name]['layout'][0]:
cli.log.debug('%s/%s: No matrix data!', keyboard, layout_name)
continue
return True
return False
@cli.argument('-o', '--output', arg_only=True, type=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', type=keyboard_folder, completer=keyboard_completer, required=True, help='Keyboard to generate keyboard.h for.')
@cli.subcommand('Used by the make system to generate keyboard.h from info.json', hidden=True)
@automagic_keyboard
@automagic_keymap
def generate_keyboard_h(cli):
"""Generates the keyboard.h file.
"""
has_layout_h = would_populate_layout_h(cli.config.generate_keyboard_h.keyboard)
# Build the layouts.h file.
keyboard_h_lines = ['/* This file was generated by `qmk generate-keyboard-h`. Do not edit or copy.' ' */', '', '#pragma once', '#include "quantum.h"']
if not has_layout_h:
keyboard_h_lines.append('#pragma error("<keyboard>.h is only optional for data driven keyboards - kb.h == bad times")')
# Show the results
keyboard_h = '\n'.join(keyboard_h_lines) + '\n'
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.parent / (cli.args.output.name + '.bak'))
cli.args.output.write_text(keyboard_h)
if not cli.args.quiet:
cli.log.info('Wrote keyboard_h to %s.', cli.args.output)
else:
print(keyboard_h)

View File

@@ -0,0 +1,103 @@
"""Used by the make system to generate layouts.h from info.json.
"""
from milc import cli
from qmk.constants import COL_LETTERS, ROW_LETTERS
from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.info import info_json
from qmk.keyboard import keyboard_completer, keyboard_folder
from qmk.path import is_keyboard, normpath
usb_properties = {
'vid': 'VENDOR_ID',
'pid': 'PRODUCT_ID',
'device_ver': 'DEVICE_VER',
}
@cli.argument('-o', '--output', arg_only=True, type=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', type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate config.h for.')
@cli.subcommand('Used by the make system to generate layouts.h from info.json', hidden=True)
@automagic_keyboard
@automagic_keymap
def generate_layouts(cli):
"""Generates the layouts.h file.
"""
# Determine our keyboard(s)
if not cli.config.generate_layouts.keyboard:
cli.log.error('Missing parameter: --keyboard')
cli.subcommands['info'].print_help()
return False
if not is_keyboard(cli.config.generate_layouts.keyboard):
cli.log.error('Invalid keyboard: "%s"', cli.config.generate_layouts.keyboard)
return False
# Build the info.json file
kb_info_json = info_json(cli.config.generate_layouts.keyboard)
# Build the layouts.h file.
layouts_h_lines = ['/* This file was generated by `qmk generate-layouts`. Do not edit or copy.' ' */', '', '#pragma once']
if 'matrix_pins' in kb_info_json:
if 'direct' in kb_info_json['matrix_pins']:
col_num = len(kb_info_json['matrix_pins']['direct'][0])
row_num = len(kb_info_json['matrix_pins']['direct'])
elif 'cols' in kb_info_json['matrix_pins'] and 'rows' in kb_info_json['matrix_pins']:
col_num = len(kb_info_json['matrix_pins']['cols'])
row_num = len(kb_info_json['matrix_pins']['rows'])
else:
cli.log.error('%s: Invalid matrix config.', cli.config.generate_layouts.keyboard)
return False
for layout_name in kb_info_json['layouts']:
if kb_info_json['layouts'][layout_name]['c_macro']:
continue
if 'matrix' not in kb_info_json['layouts'][layout_name]['layout'][0]:
cli.log.debug('%s/%s: No matrix data!', cli.config.generate_layouts.keyboard, layout_name)
continue
layout_keys = []
layout_matrix = [['KC_NO' for i in range(col_num)] for i in range(row_num)]
for i, key in enumerate(kb_info_json['layouts'][layout_name]['layout']):
row = key['matrix'][0]
col = key['matrix'][1]
identifier = 'k%s%s' % (ROW_LETTERS[row], COL_LETTERS[col])
try:
layout_matrix[row][col] = identifier
layout_keys.append(identifier)
except IndexError:
key_name = key.get('label', identifier)
cli.log.error('Matrix data out of bounds for layout %s at index %s (%s): %s, %s', layout_name, i, key_name, row, col)
return False
layouts_h_lines.append('')
layouts_h_lines.append('#define %s(%s) {\\' % (layout_name, ', '.join(layout_keys)))
rows = ', \\\n'.join(['\t {' + ', '.join(row) + '}' for row in layout_matrix])
rows += ' \\'
layouts_h_lines.append(rows)
layouts_h_lines.append('}')
for alias, target in kb_info_json.get('layout_aliases', {}).items():
layouts_h_lines.append('')
layouts_h_lines.append('#define %s %s' % (alias, target))
# Show the results
layouts_h = '\n'.join(layouts_h_lines) + '\n'
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.parent / (cli.args.output.name + '.bak'))
cli.args.output.write_text(layouts_h)
if not cli.args.quiet:
cli.log.info('Wrote info_config.h to %s.', cli.args.output)
else:
print(layouts_h)

View File

@@ -70,7 +70,7 @@ static const int table_scale = 256 / sizeof(rgblight_effect_breathe_table);
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.replace(cli.args.output.parent / (cli.args.output.name + '.bak'))
cli.args.output.write_text(table_template)
if not cli.args.quiet:

View File

@@ -0,0 +1,97 @@
"""Used by the make system to generate a rules.mk
"""
from pathlib import Path
from dotty_dict import dotty
from milc import cli
from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.info import info_json
from qmk.json_schema import json_load
from qmk.keyboard import keyboard_completer, keyboard_folder
from qmk.path import is_keyboard, normpath
def process_mapping_rule(kb_info_json, rules_key, info_dict):
"""Return the rules.mk line(s) for a mapping rule.
"""
if not info_dict.get('to_c', True):
return None
info_key = info_dict['info_key']
key_type = info_dict.get('value_type', 'str')
try:
rules_value = kb_info_json[info_key]
except KeyError:
return None
if key_type == 'array':
return f'{rules_key} ?= {" ".join(rules_value)}'
elif key_type == 'bool':
return f'{rules_key} ?= {"on" if rules_value else "off"}'
elif key_type == 'mapping':
return '\n'.join([f'{key} ?= {value}' for key, value in rules_value.items()])
return f'{rules_key} ?= {rules_value}'
@cli.argument('-o', '--output', arg_only=True, type=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('-e', '--escape', arg_only=True, action='store_true', help="Escape spaces in quiet mode")
@cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate config.h for.')
@cli.subcommand('Used by the make system to generate info_config.h from info.json', hidden=True)
@automagic_keyboard
@automagic_keymap
def generate_rules_mk(cli):
"""Generates a rules.mk file from info.json.
"""
if not cli.config.generate_rules_mk.keyboard:
cli.log.error('Missing parameter: --keyboard')
cli.subcommands['info'].print_help()
return False
if not is_keyboard(cli.config.generate_rules_mk.keyboard):
cli.log.error('Invalid keyboard: "%s"', cli.config.generate_rules_mk.keyboard)
return False
kb_info_json = dotty(info_json(cli.config.generate_rules_mk.keyboard))
info_rules_map = json_load(Path('data/mappings/info_rules.json'))
rules_mk_lines = ['# This file was generated by `qmk generate-rules-mk`. Do not edit or copy.', '']
# Iterate through the info_rules map to generate basic rules
for rules_key, info_dict in info_rules_map.items():
new_entry = process_mapping_rule(kb_info_json, rules_key, info_dict)
if new_entry:
rules_mk_lines.append(new_entry)
# Iterate through features to enable/disable them
if 'features' in kb_info_json:
for feature, enabled in kb_info_json['features'].items():
if feature == 'bootmagic_lite' and enabled:
rules_mk_lines.append('BOOTMAGIC_ENABLE ?= lite')
else:
feature = feature.upper()
enabled = 'yes' if enabled else 'no'
rules_mk_lines.append(f'{feature}_ENABLE ?= {enabled}')
# Show the results
rules_mk = '\n'.join(rules_mk_lines) + '\n'
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.parent / (cli.args.output.name + '.bak'))
cli.args.output.write_text(rules_mk)
if cli.args.quiet:
if cli.args.escape:
print(cli.args.output.as_posix().replace(' ', '\\ '))
else:
print(cli.args.output)
else:
cli.log.info('Wrote rules.mk to %s.', cli.args.output)
else:
print(rules_mk)

View File

@@ -2,21 +2,20 @@
Compile an info.json for a particular keyboard and pretty-print it.
"""
import sys
import json
import platform
from milc import cli
from qmk.json_encoders import InfoJSONEncoder
from qmk.constants import COL_LETTERS, ROW_LETTERS
from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.keyboard import render_layouts, render_layout
from qmk.keyboard import keyboard_completer, keyboard_folder, render_layouts, render_layout, rules_mk
from qmk.keymap import locate_keymap
from qmk.info import info_json
from qmk.path import is_keyboard
platform_id = platform.platform().lower()
ROW_LETTERS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnop'
COL_LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijilmnopqrstuvwxyz'
UNICODE_SUPPORT = sys.stdout.encoding.lower().startswith('utf')
def show_keymap(kb_info_json, title_caps=True):
@@ -30,7 +29,7 @@ def show_keymap(kb_info_json, title_caps=True):
else:
cli.echo('{fg_blue}keymap_%s{fg_reset}:', cli.config.info.keymap)
keymap_data = json.load(keymap_path.open())
keymap_data = json.load(keymap_path.open(encoding='utf-8'))
layout_name = keymap_data['layout']
for layer_num, layer in enumerate(keymap_data['layers']):
@@ -58,7 +57,7 @@ def show_matrix(kb_info_json, title_caps=True):
# Build our label list
labels = []
for key in layout['layout']:
if key['matrix']:
if 'matrix' in key:
row = ROW_LETTERS[key['matrix'][0]]
col = COL_LETTERS[key['matrix'][1]]
@@ -92,6 +91,9 @@ def print_friendly_output(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 'layout_aliases' in kb_info_json:
aliases = [f'{key}={value}' for key, value in kb_info_json['layout_aliases'].items()]
cli.echo('{fg_blue}Layout aliases:{fg_reset} %s' % (', '.join(aliases),))
if cli.config.info.layouts:
show_layouts(kb_info_json, True)
@@ -122,12 +124,20 @@ def print_text_output(kb_info_json):
show_keymap(kb_info_json, False)
@cli.argument('-kb', '--keyboard', help='Keyboard to show info for.')
def print_parsed_rules_mk(keyboard_name):
rules = rules_mk(keyboard_name)
for k in sorted(rules.keys()):
print('%s = %s' % (k, rules[k]))
return
@cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='Keyboard to show info for.')
@cli.argument('-km', '--keymap', help='Show the layers for a JSON keymap too.')
@cli.argument('-l', '--layouts', action='store_true', help='Render the layouts.')
@cli.argument('-m', '--matrix', action='store_true', help='Render the layouts with matrix information.')
@cli.argument('-f', '--format', default='friendly', arg_only=True, help='Format to display the data in (friendly, text, json) (Default: friendly).')
@cli.argument('--ascii', action='store_true', default='windows' in platform_id, help='Render layout box drawings in ASCII only.')
@cli.argument('--ascii', action='store_true', default=not UNICODE_SUPPORT, help='Render layout box drawings in ASCII only.')
@cli.argument('-r', '--rules-mk', action='store_true', help='Render the parsed values of the keyboard\'s rules.mk file.')
@cli.subcommand('Keyboard information.')
@automagic_keyboard
@automagic_keymap
@@ -144,12 +154,16 @@ def info(cli):
cli.log.error('Invalid keyboard: "%s"', cli.config.info.keyboard)
return False
if bool(cli.args.rules_mk):
print_parsed_rules_mk(cli.config.info.keyboard)
return False
# Build the info.json file
kb_info_json = info_json(cli.config.info.keyboard)
# Output in the requested format
if cli.args.format == 'json':
print(json.dumps(kb_info_json))
print(json.dumps(kb_info_json, cls=InfoJSONEncoder))
elif cli.args.format == 'text':
print_text_output(kb_info_json)
elif cli.args.format == 'friendly':

View File

@@ -1,8 +1,8 @@
"""Generate a keymap.c from a configurator export.
"""
import json
import sys
from argcomplete.completers import FilesCompleter
from milc import cli
import qmk.keymap
@@ -11,7 +11,7 @@ import qmk.path
@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('filename', type=qmk.path.normpath, arg_only=True, help='Configurator JSON file')
@cli.argument('filename', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
@cli.subcommand('Creates a keymap.c from a QMK Configurator export.')
def json2c(cli):
"""Generate a keymap.c from a configurator export.
@@ -20,19 +20,8 @@ def json2c(cli):
"""
try:
# Parse the configurator from stdin
if cli.args.filename and cli.args.filename.name == '-':
user_keymap = json.load(sys.stdin)
else:
# Error checking
if not cli.args.filename.exists():
cli.log.error('JSON file does not exist!')
return False
# Parse the configurator json file
else:
user_keymap = json.loads(cli.args.filename.read_text())
# Parse the configurator from json file (or stdin)
user_keymap = json.load(cli.args.filename)
except json.decoder.JSONDecodeError as ex:
cli.log.error('The JSON input does not appear to be valid.')
@@ -49,7 +38,7 @@ def json2c(cli):
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.replace(cli.args.output.parent / (cli.args.output.name + '.bak'))
cli.args.output.write_text(keymap_c)
if not cli.args.quiet:

View File

@@ -3,28 +3,16 @@
import json
import os
from pathlib import Path
from decimal import Decimal
from collections import OrderedDict
from argcomplete.completers import FilesCompleter
from milc import cli
from kle2xy import KLE2xy
from qmk.converter import kle2qmk
from qmk.json_encoders import InfoJSONEncoder
class CustomJSONEncoder(json.JSONEncoder):
def default(self, obj):
try:
if isinstance(obj, Decimal):
if obj % 2 in (Decimal(0), Decimal(1)):
return int(obj)
return float(obj)
except TypeError:
pass
return json.JSONEncoder.default(self, obj)
@cli.argument('filename', help='The KLE raw txt to convert')
@cli.argument('filename', completer=FilesCompleter('.json'), help='The KLE raw txt to convert')
@cli.argument('-f', '--force', action='store_true', help='Flag to overwrite current info.json')
@cli.subcommand('Convert a KLE layout to a Configurator JSON', hidden=False if cli.config.user.developer else True)
def kle2json(cli):
@@ -40,7 +28,7 @@ def kle2json(cli):
cli.log.error('File {fg_cyan}%s{style_reset_all} was not found.', file_path)
return False
out_path = file_path.parent
raw_code = file_path.open().read()
raw_code = file_path.read_text(encoding='utf-8')
# Check if info.json exists, allow overwrite with force
if Path(out_path, "info.json").exists() and not cli.args.force:
cli.log.error('File {fg_cyan}%s/info.json{style_reset_all} already exists, use -f or --force to overwrite.', out_path)
@@ -52,24 +40,22 @@ def kle2json(cli):
cli.log.error('Could not parse KLE raw data: %s', raw_code)
cli.log.exception(e)
return False
keyboard = OrderedDict(
keyboard_name=kle.name,
url='',
maintainer='qmk',
width=kle.columns,
height=kle.rows,
layouts={'LAYOUT': {
'layout': 'LAYOUT_JSON_HERE'
}},
)
# Initialize keyboard with json encoded from ordered dict
keyboard = json.dumps(keyboard, indent=4, separators=(', ', ': '), sort_keys=False, cls=CustomJSONEncoder)
# Initialize layout with kle2qmk from converter module
layout = json.dumps(kle2qmk(kle), separators=(', ', ':'), cls=CustomJSONEncoder)
# Replace layout in keyboard json
keyboard = keyboard.replace('"LAYOUT_JSON_HERE"', layout)
keyboard = {
'keyboard_name': kle.name,
'url': '',
'maintainer': 'qmk',
'width': kle.columns,
'height': kle.rows,
'layouts': {
'LAYOUT': {
'layout': kle2qmk(kle)
}
},
}
# Write our info.json
file = open(out_path / "info.json", "w")
file.write(keyboard)
file.close()
keyboard = json.dumps(keyboard, indent=4, separators=(', ', ': '), sort_keys=False, cls=InfoJSONEncoder)
info_json_file = out_path / 'info.json'
info_json_file.write_text(keyboard)
cli.log.info('Wrote out {fg_cyan}%s/info.json', out_path)

View File

@@ -4,12 +4,13 @@ from milc import cli
from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.info import info_json
from qmk.keyboard import keyboard_completer
from qmk.keymap import locate_keymap
from qmk.path import is_keyboard, keyboard
@cli.argument('--strict', action='store_true', help='Treat warnings as errors.')
@cli.argument('-kb', '--keyboard', help='The keyboard to check.')
@cli.argument('-kb', '--keyboard', completer=keyboard_completer, help='The keyboard to check.')
@cli.argument('-km', '--keymap', help='The keymap to check.')
@cli.subcommand('Check keyboard and keymap for common mistakes.')
@automagic_keyboard

View File

@@ -4,18 +4,14 @@ from milc import cli
import qmk.keymap
from qmk.decorators import automagic_keyboard
from qmk.path import is_keyboard
from qmk.keyboard import keyboard_completer, keyboard_folder
@cli.argument("-kb", "--keyboard", help="Specify keyboard name. Example: 1upkeyboards/1up60hse")
@cli.argument("-kb", "--keyboard", type=keyboard_folder, completer=keyboard_completer, help="Specify keyboard name. Example: 1upkeyboards/1up60hse")
@cli.subcommand("List the keymaps for a specific keyboard")
@automagic_keyboard
def list_keymaps(cli):
"""List the keymaps for a specific keyboard
"""
if not is_keyboard(cli.config.list_keymaps.keyboard):
cli.log.error('Keyboard %s does not exist!', cli.config.list_keymaps.keyboard)
return False
for name in qmk.keymap.list_keymaps(cli.config.list_keymaps.keyboard):
print(name)

View File

@@ -0,0 +1,79 @@
"""Compile all keyboards.
This will compile everything in parallel, for testing purposes.
"""
import re
from pathlib import Path
from subprocess import DEVNULL
from milc import cli
from qmk.constants import QMK_FIRMWARE
from qmk.commands import _find_make
import qmk.keyboard
def _make_rules_mk_filter(key, value):
def _rules_mk_filter(keyboard_name):
rules_mk = qmk.keyboard.rules_mk(keyboard_name)
return True if key in rules_mk and rules_mk[key].lower() == str(value).lower() else False
return _rules_mk_filter
def _is_split(keyboard_name):
rules_mk = qmk.keyboard.rules_mk(keyboard_name)
return True if 'SPLIT_KEYBOARD' in rules_mk and rules_mk['SPLIT_KEYBOARD'].lower() == 'yes' else False
@cli.argument('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs to run.")
@cli.argument('-c', '--clean', arg_only=True, action='store_true', help="Remove object files before compiling.")
@cli.argument('-f', '--filter', arg_only=True, action='append', default=[], help="Filter the list of keyboards based on the supplied value in rules.mk. Supported format is 'SPLIT_KEYBOARD=yes'. May be passed multiple times.")
@cli.subcommand('Compile QMK Firmware for all keyboards.', hidden=False if cli.config.user.developer else True)
def multibuild(cli):
"""Compile QMK Firmware against all keyboards.
"""
make_cmd = _find_make()
if cli.args.clean:
cli.run([make_cmd, 'clean'], capture_output=False, stdin=DEVNULL)
builddir = Path(QMK_FIRMWARE) / '.build'
makefile = builddir / 'parallel_kb_builds.mk'
keyboard_list = qmk.keyboard.list_keyboards()
filter_re = re.compile(r'^(?P<key>[A-Z0-9_]+)\s*=\s*(?P<value>[^#]+)$')
for filter_txt in cli.args.filter:
f = filter_re.match(filter_txt)
if f is not None:
keyboard_list = filter(_make_rules_mk_filter(f.group('key'), f.group('value')), keyboard_list)
keyboard_list = list(sorted(keyboard_list))
if len(keyboard_list) == 0:
return
builddir.mkdir(parents=True, exist_ok=True)
with open(makefile, "w") as f:
for keyboard_name in keyboard_list:
keyboard_safe = keyboard_name.replace('/', '_')
# yapf: disable
f.write(
f"""\
all: {keyboard_safe}_binary
{keyboard_safe}_binary:
@rm -f "{QMK_FIRMWARE}/.build/failed.log.{keyboard_safe}" || true
+@$(MAKE) -C "{QMK_FIRMWARE}" -f "{QMK_FIRMWARE}/build_keyboard.mk" KEYBOARD="{keyboard_name}" KEYMAP="default" REQUIRE_PLATFORM_KEY= COLOR=true SILENT=false \\
>>"{QMK_FIRMWARE}/.build/build.log.{keyboard_safe}" 2>&1 \\
|| cp "{QMK_FIRMWARE}/.build/build.log.{keyboard_safe}" "{QMK_FIRMWARE}/.build/failed.log.{keyboard_safe}"
@{{ grep '\[ERRORS\]' "{QMK_FIRMWARE}/.build/build.log.{keyboard_safe}" >/dev/null 2>&1 && printf "Build %-64s \e[1;31m[ERRORS]\e[0m\\n" "{keyboard_name}:default" ; }} \\
|| {{ grep '\[WARNINGS\]' "{QMK_FIRMWARE}/.build/build.log.{keyboard_safe}" >/dev/null 2>&1 && printf "Build %-64s \e[1;33m[WARNINGS]\e[0m\\n" "{keyboard_name}:default" ; }} \\
|| printf "Build %-64s \e[1;32m[OK]\e[0m\\n" "{keyboard_name}:default"
@rm -f "{QMK_FIRMWARE}/.build/build.log.{keyboard_safe}" || true
"""# noqa
)
# yapf: enable
cli.run([make_cmd, '-j', str(cli.args.parallel), '-f', makefile, 'all'], capture_output=False, stdin=DEVNULL)

View File

@@ -1 +1,2 @@
from . import keyboard
from . import keymap

View File

@@ -0,0 +1,11 @@
"""This script automates the creation of keyboards.
"""
from milc import cli
@cli.subcommand('Creates a new keyboard')
def new_keyboard(cli):
"""Creates a new keyboard
"""
# TODO: replace this bodge to the existing script
cli.run(['util/new_keyboard.sh'], stdin=None, capture_output=False)

View File

@@ -5,10 +5,11 @@ from pathlib import Path
import qmk.path
from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.keyboard import keyboard_completer, keyboard_folder
from milc import cli
@cli.argument('-kb', '--keyboard', help='Specify keyboard name. Example: 1upkeyboards/1up60hse')
@cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='Specify keyboard name. Example: 1upkeyboards/1up60hse')
@cli.argument('-km', '--keymap', help='Specify the name for the new keymap directory')
@cli.subcommand('Creates a new keymap for the keyboard of your choosing')
@automagic_keyboard

View File

@@ -1,17 +1,26 @@
"""Format python code according to QMK's style.
"""
from subprocess import CalledProcessError, DEVNULL
from milc import cli
import subprocess
@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Flag only, don't automatically format.")
@cli.subcommand("Format python code according to QMK's style.", hidden=False if cli.config.user.developer else True)
def pyformat(cli):
"""Format python code according to QMK's style.
"""
edit = '--diff' if cli.args.dry_run else '--in-place'
yapf_cmd = ['yapf', '-vv', '--recursive', edit, 'bin/qmk', 'lib/python']
try:
subprocess.run(['yapf', '-vv', '-ri', 'bin/qmk', 'lib/python'], check=True)
cli.log.info('Successfully formatted the python code in `bin/qmk` and `lib/python`.')
cli.run(yapf_cmd, check=True, capture_output=False, stdin=DEVNULL)
cli.log.info('Python code in `bin/qmk` and `lib/python` is correctly formatted.')
return True
except subprocess.CalledProcessError:
cli.log.error('Error formatting python code!')
except CalledProcessError:
if cli.args.dry_run:
cli.log.error('Python code in `bin/qmk` and `lib/python` incorrectly formatted!')
else:
cli.log.error('Error formatting python code!')
return False

View File

@@ -2,7 +2,7 @@
QMK script to run unit and integration tests against our python code.
"""
import subprocess
from subprocess import DEVNULL
from milc import cli
@@ -11,6 +11,7 @@ from milc import cli
def pytest(cli):
"""Run several linting/testing commands.
"""
flake8 = subprocess.run(['flake8', 'lib/python', 'bin/qmk'])
nose2 = subprocess.run(['nose2', '-v'])
nose2 = cli.run(['nose2', '-v'], capture_output=False, stdin=DEVNULL)
flake8 = cli.run(['flake8', 'lib/python', 'bin/qmk'], capture_output=False, stdin=DEVNULL)
return flake8.returncode | nose2.returncode

View File

@@ -2,17 +2,16 @@
"""
import json
import os
import platform
import subprocess
import shlex
import shutil
from pathlib import Path
from subprocess import DEVNULL
from time import strftime
from milc import cli
import qmk.keymap
from qmk.constants import KEYBOARD_OUTPUT_PREFIX
from qmk.json_schema import json_load
time_fmt = '%Y-%m-%d-%H:%M:%S'
@@ -28,6 +27,33 @@ def _find_make():
return make_cmd
def create_make_target(target, parallel=1, **env_vars):
"""Create a make command
Args:
target
Usually a make rule, such as 'clean' or 'all'.
parallel
The number of make jobs to run in parallel
**env_vars
Environment variables to be passed to make.
Returns:
A command that can be run to make the specified keyboard and keymap
"""
env = []
make_cmd = _find_make()
for key, value in env_vars.items():
env.append(f'{key}={value}')
return [make_cmd, '-j', str(parallel), *env, target]
def create_make_command(keyboard, keymap, target=None, parallel=1, **env_vars):
"""Create a make compile command
@@ -52,17 +78,12 @@ def create_make_command(keyboard, keymap, target=None, parallel=1, **env_vars):
A command that can be run to make the specified keyboard and keymap
"""
env = []
make_args = [keyboard, keymap]
make_cmd = _find_make()
if target:
make_args.append(target)
for key, value in env_vars.items():
env.append(f'{key}={value}')
return [make_cmd, '-j', str(parallel), *env, ':'.join(make_args)]
return create_make_target(':'.join(make_args), parallel, **env_vars)
def get_git_version(repo_dir='.', check_dir='.'):
@@ -71,13 +92,13 @@ def get_git_version(repo_dir='.', check_dir='.'):
git_describe_cmd = ['git', 'describe', '--abbrev=6', '--dirty', '--always', '--tags']
if Path(check_dir).exists():
git_describe = cli.run(git_describe_cmd, cwd=repo_dir)
git_describe = cli.run(git_describe_cmd, stdin=DEVNULL, cwd=repo_dir)
if git_describe.returncode == 0:
return git_describe.stdout.strip()
else:
cli.args.warn(f'"{" ".join(git_describe_cmd)}" returned error code {git_describe.returncode}')
cli.log.warn(f'"{" ".join(git_describe_cmd)}" returned error code {git_describe.returncode}')
print(git_describe.stderr)
return strftime(time_fmt)
@@ -190,22 +211,14 @@ def parse_configurator_json(configurator_file):
"""
# FIXME(skullydazed/anyone): Add validation here
user_keymap = json.load(configurator_file)
orig_keyboard = user_keymap['keyboard']
aliases = json_load(Path('data/mappings/keyboard_aliases.json'))
if orig_keyboard in aliases:
if 'target' in aliases[orig_keyboard]:
user_keymap['keyboard'] = aliases[orig_keyboard]['target']
if 'layouts' in aliases[orig_keyboard] and user_keymap['layout'] in aliases[orig_keyboard]['layouts']:
user_keymap['layout'] = aliases[orig_keyboard]['layouts'][user_keymap['layout']]
return user_keymap
def run(command, *args, **kwargs):
"""Run a command with subprocess.run
"""
platform_id = platform.platform().lower()
if isinstance(command, str):
raise TypeError('`command` must be a non-text sequence such as list or tuple.')
if 'windows' in platform_id:
safecmd = map(str, command)
safecmd = map(shlex.quote, safecmd)
safecmd = ' '.join(safecmd)
command = [os.environ['SHELL'], '-c', safecmd]
return subprocess.run(command, *args, **kwargs)

View File

@@ -10,8 +10,8 @@ QMK_FIRMWARE = Path.cwd()
MAX_KEYBOARD_SUBFOLDERS = 5
# Supported processor types
CHIBIOS_PROCESSORS = 'cortex-m0', 'cortex-m0plus', 'cortex-m3', 'cortex-m4', 'MKL26Z64', 'MK20DX128', 'MK20DX256', 'STM32F042', 'STM32F072', 'STM32F103', 'STM32F303', 'STM32F401', 'STM32F411'
LUFA_PROCESSORS = 'atmega16u2', 'atmega32u2', 'atmega16u4', 'atmega32u4', 'at90usb646', 'at90usb647', 'at90usb1286', 'at90usb1287', None
CHIBIOS_PROCESSORS = 'cortex-m0', 'cortex-m0plus', 'cortex-m3', 'cortex-m4', 'MKL26Z64', 'MK20DX128', 'MK20DX256', 'STM32F042', 'STM32F072', 'STM32F103', 'STM32F303', 'STM32F401', 'STM32F411', 'STM32G431', 'STM32G474'
LUFA_PROCESSORS = 'at90usb162', 'atmega16u2', 'atmega32u2', 'atmega16u4', 'atmega32u4', 'at90usb646', 'at90usb647', 'at90usb1286', 'at90usb1287', None
VUSB_PROCESSORS = 'atmega32a', 'atmega328p', 'atmega328', 'attiny85'
# Common format strings
@@ -19,6 +19,17 @@ DATE_FORMAT = '%Y-%m-%d'
DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S %Z'
TIME_FORMAT = '%H:%M:%S'
# Used when generating matrix locations
COL_LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijilmnopqrstuvwxyz'
ROW_LETTERS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnop'
# Mapping between info.json and config.h keys
LED_INDICATORS = {
'caps_lock': 'LED_CAPS_LOCK_PIN',
'num_lock': 'LED_NUM_LOCK_PIN',
'scroll_lock': 'LED_SCROLL_LOCK_PIN',
}
# Constants that should match their counterparts in make
BUILD_DIR = environ.get('BUILD_DIR', '.build')
KEYBOARD_OUTPUT_PREFIX = f'{BUILD_DIR}/obj_'

View File

@@ -1,13 +1,12 @@
"""Helpful decorators that subcommands can use.
"""
import functools
from pathlib import Path
from time import monotonic
from milc import cli
from qmk.keymap import is_keymap_dir
from qmk.path import is_keyboard, under_qmk_firmware
from qmk.keyboard import find_keyboard_from_dir
from qmk.keymap import find_keymap_from_dir
def automagic_keyboard(func):
@@ -17,27 +16,13 @@ def automagic_keyboard(func):
"""
@functools.wraps(func)
def wrapper(*args, **kwargs):
# Check to make sure their copy of MILC supports config_source
if not hasattr(cli, 'config_source'):
cli.log.error("This subcommand requires a newer version of the QMK CLI. Please upgrade using `pip3 install --upgrade qmk` or your package manager.")
exit(1)
# Ensure that `--keyboard` was not passed and CWD is under `qmk_firmware/keyboards`
if cli.config_source[cli._entrypoint.__name__]['keyboard'] != 'argument':
relative_cwd = under_qmk_firmware()
keyboard = find_keyboard_from_dir()
if relative_cwd and len(relative_cwd.parts) > 1 and relative_cwd.parts[0] == 'keyboards':
# Attempt to extract the keyboard name from the current directory
current_path = Path('/'.join(relative_cwd.parts[1:]))
if 'keymaps' in current_path.parts:
# Strip current_path of anything after `keymaps`
keymap_index = len(current_path.parts) - current_path.parts.index('keymaps') - 1
current_path = current_path.parents[keymap_index]
if is_keyboard(current_path):
cli.config[cli._entrypoint.__name__]['keyboard'] = str(current_path)
cli.config_source[cli._entrypoint.__name__]['keyboard'] = 'keyboard_directory'
if keyboard:
cli.config[cli._entrypoint.__name__]['keyboard'] = keyboard
cli.config_source[cli._entrypoint.__name__]['keyboard'] = 'keyboard_directory'
return func(*args, **kwargs)
@@ -51,36 +36,13 @@ def automagic_keymap(func):
"""
@functools.wraps(func)
def wrapper(*args, **kwargs):
# Check to make sure their copy of MILC supports config_source
if not hasattr(cli, 'config_source'):
cli.log.error("This subcommand requires a newer version of the QMK CLI. Please upgrade using `pip3 install --upgrade qmk` or your package manager.")
exit(1)
# Ensure that `--keymap` was not passed and that we're under `qmk_firmware`
if cli.config_source[cli._entrypoint.__name__]['keymap'] != 'argument':
relative_cwd = under_qmk_firmware()
keymap_name, keymap_type = find_keymap_from_dir()
if relative_cwd and len(relative_cwd.parts) > 1:
# If we're in `qmk_firmware/keyboards` and `keymaps` is in our path, try to find the keyboard name.
if relative_cwd.parts[0] == 'keyboards' and 'keymaps' in relative_cwd.parts:
current_path = Path('/'.join(relative_cwd.parts[1:])) # Strip 'keyboards' from the front
if 'keymaps' in current_path.parts and current_path.name != 'keymaps':
while current_path.parent.name != 'keymaps':
current_path = current_path.parent
cli.config[cli._entrypoint.__name__]['keymap'] = current_path.name
cli.config_source[cli._entrypoint.__name__]['keymap'] = 'keymap_directory'
# If we're in `qmk_firmware/layouts` guess the name from the community keymap they're in
elif relative_cwd.parts[0] == 'layouts' and is_keymap_dir(relative_cwd):
cli.config[cli._entrypoint.__name__]['keymap'] = relative_cwd.name
cli.config_source[cli._entrypoint.__name__]['keymap'] = 'layouts_directory'
# If we're in `qmk_firmware/users` guess the name from the userspace they're in
elif relative_cwd.parts[0] == 'users':
# Guess the keymap name based on which userspace they're in
cli.config[cli._entrypoint.__name__]['keymap'] = relative_cwd.parts[1]
cli.config_source[cli._entrypoint.__name__]['keymap'] = 'users_directory'
if keymap_name:
cli.config[cli._entrypoint.__name__]['keymap'] = keymap_name
cli.config_source[cli._entrypoint.__name__]['keymap'] = keymap_type
return func(*args, **kwargs)

View File

@@ -3,3 +3,10 @@ class NoSuchKeyboardError(Exception):
"""
def __init__(self, message):
self.message = message
class CppError(Exception):
"""Raised when 'cpp' cannot process a file.
"""
def __init__(self, message):
self.message = message

View File

@@ -1,18 +1,29 @@
"""Functions that help us generate and use info.json files.
"""
import json
from glob import glob
from pathlib import Path
import jsonschema
from dotty_dict import dotty
from milc import cli
from qmk.constants import CHIBIOS_PROCESSORS, LUFA_PROCESSORS, VUSB_PROCESSORS
from qmk.c_parse import find_layouts
from qmk.json_schema import deep_update, json_load, keyboard_validate, keyboard_api_validate
from qmk.keyboard import config_h, rules_mk
from qmk.keymap import list_keymaps
from qmk.makefile import parse_rules_mk_file
from qmk.math import compute
true_values = ['1', 'on', 'yes']
false_values = ['0', 'off', 'no']
def _valid_community_layout(layout):
"""Validate that a declared community list exists
"""
return (Path('layouts/default') / layout).exists()
def info_json(keyboard):
"""Generate the info.json data for a specific keyboard.
@@ -38,8 +49,14 @@ def info_json(keyboard):
info_data['keymaps'][keymap.name] = {'url': f'https://raw.githubusercontent.com/qmk/qmk_firmware/master/{keymap}/keymap.json'}
# Populate layout data
for layout_name, layout_json in _find_all_layouts(info_data, keyboard, rules).items():
layouts, aliases = _find_all_layouts(info_data, keyboard)
if aliases:
info_data['layout_aliases'] = aliases
for layout_name, layout_json in layouts.items():
if not layout_name.startswith('LAYOUT_kc'):
layout_json['c_macro'] = True
info_data['layouts'][layout_name] = layout_json
# Merge in the data from info.json, config.h, and rules.mk
@@ -47,54 +64,211 @@ def info_json(keyboard):
info_data = _extract_config_h(info_data)
info_data = _extract_rules_mk(info_data)
# Validate against the jsonschema
try:
keyboard_api_validate(info_data)
except jsonschema.ValidationError as e:
json_path = '.'.join([str(p) for p in e.absolute_path])
cli.log.error('Invalid API data: %s: %s: %s', keyboard, json_path, e.message)
exit()
# Make sure we have at least one layout
if not info_data.get('layouts'):
_log_error(info_data, 'No LAYOUTs defined! Need at least one layout defined in the keyboard.h or info.json.')
# Filter out any non-existing community layouts
for layout in info_data.get('community_layouts', []):
if not _valid_community_layout(layout):
# Ignore layout from future checks
info_data['community_layouts'].remove(layout)
_log_error(info_data, 'Claims to support a community layout that does not exist: %s' % (layout))
# Make sure we supply layout macros for the community layouts we claim to support
for layout in info_data.get('community_layouts', []):
layout_name = 'LAYOUT_' + layout
if layout_name not in info_data.get('layouts', {}) and layout_name not in info_data.get('layout_aliases', {}):
_log_error(info_data, 'Claims to support community layout %s but no %s() macro found' % (layout, layout_name))
return info_data
def _extract_config_h(info_data):
"""Pull some keyboard information from existing rules.mk files
def _extract_features(info_data, rules):
"""Find all the features enabled in rules.mk.
"""
# Special handling for bootmagic which also supports a "lite" mode.
if rules.get('BOOTMAGIC_ENABLE') == 'lite':
rules['BOOTMAGIC_LITE_ENABLE'] = 'on'
del rules['BOOTMAGIC_ENABLE']
if rules.get('BOOTMAGIC_ENABLE') == 'full':
rules['BOOTMAGIC_ENABLE'] = 'on'
# Skip non-boolean features we haven't implemented special handling for
for feature in 'HAPTIC_ENABLE', 'QWIIC_ENABLE':
if rules.get(feature):
del rules[feature]
# Process the rest of the rules as booleans
for key, value in rules.items():
if key.endswith('_ENABLE'):
key = '_'.join(key.split('_')[:-1]).lower()
value = True if value.lower() in true_values else False if value.lower() in false_values else value
if 'config_h_features' not in info_data:
info_data['config_h_features'] = {}
if 'features' not in info_data:
info_data['features'] = {}
if key in info_data['features']:
_log_warning(info_data, 'Feature %s is specified in both info.json and rules.mk, the rules.mk value wins.' % (key,))
info_data['features'][key] = value
info_data['config_h_features'][key] = value
return info_data
def _pin_name(pin):
"""Returns the proper representation for a pin.
"""
pin = pin.strip()
if not pin:
return None
elif pin.isdigit():
return int(pin)
elif pin == 'NO_PIN':
return None
elif pin[0] in 'ABCDEFGHIJK' and pin[1].isdigit():
return pin
raise ValueError(f'Invalid pin: {pin}')
def _extract_pins(pins):
"""Returns a list of pins from a comma separated string of pins.
"""
return [_pin_name(pin) for pin in pins.split(',')]
def _extract_direct_matrix(info_data, direct_pins):
"""
"""
info_data['matrix_pins'] = {}
direct_pin_array = []
while direct_pins[-1] != '}':
direct_pins = direct_pins[:-1]
for row in direct_pins.split('},{'):
if row.startswith('{'):
row = row[1:]
if row.endswith('}'):
row = row[:-1]
direct_pin_array.append([])
for pin in row.split(','):
if pin == 'NO_PIN':
pin = None
direct_pin_array[-1].append(pin)
return direct_pin_array
def _extract_matrix_info(info_data, config_c):
"""Populate the matrix information.
"""
config_c = config_h(info_data['keyboard_folder'])
row_pins = config_c.get('MATRIX_ROW_PINS', '').replace('{', '').replace('}', '').strip()
col_pins = config_c.get('MATRIX_COL_PINS', '').replace('{', '').replace('}', '').strip()
direct_pins = config_c.get('DIRECT_PINS', '').replace(' ', '')[1:-1]
info_data['diode_direction'] = config_c.get('DIODE_DIRECTION')
info_data['matrix_size'] = {
'rows': compute(config_c.get('MATRIX_ROWS', '0')),
'cols': compute(config_c.get('MATRIX_COLS', '0')),
}
info_data['matrix_pins'] = {}
if 'MATRIX_ROWS' in config_c and 'MATRIX_COLS' in config_c:
if 'matrix_size' in info_data:
_log_warning(info_data, 'Matrix size is specified in both info.json and config.h, the config.h values win.')
if row_pins:
info_data['matrix_pins']['rows'] = row_pins.split(',')
if col_pins:
info_data['matrix_pins']['cols'] = col_pins.split(',')
info_data['matrix_size'] = {
'cols': compute(config_c.get('MATRIX_COLS', '0')),
'rows': compute(config_c.get('MATRIX_ROWS', '0')),
}
if row_pins and col_pins:
if 'matrix_pins' in info_data:
_log_warning(info_data, 'Matrix pins are specified in both info.json and config.h, the config.h values win.')
info_data['matrix_pins'] = {
'cols': _extract_pins(col_pins),
'rows': _extract_pins(row_pins),
}
if direct_pins:
direct_pin_array = []
for row in direct_pins.split('},{'):
if row.startswith('{'):
row = row[1:]
if row.endswith('}'):
row = row[:-1]
if 'matrix_pins' in info_data:
_log_warning(info_data, 'Direct pins are specified in both info.json and config.h, the config.h values win.')
direct_pin_array.append([])
info_data['matrix_pins']['direct'] = _extract_direct_matrix(info_data, direct_pins)
for pin in row.split(','):
if pin == 'NO_PIN':
pin = None
return info_data
direct_pin_array[-1].append(pin)
info_data['matrix_pins']['direct'] = direct_pin_array
def _extract_config_h(info_data):
"""Pull some keyboard information from existing config.h files
"""
config_c = config_h(info_data['keyboard_folder'])
info_data['usb'] = {
'vid': config_c.get('VENDOR_ID'),
'pid': config_c.get('PRODUCT_ID'),
'device_ver': config_c.get('DEVICE_VER'),
'manufacturer': config_c.get('MANUFACTURER'),
'product': config_c.get('PRODUCT'),
}
# Pull in data from the json map
dotty_info = dotty(info_data)
info_config_map = json_load(Path('data/mappings/info_config.json'))
for config_key, info_dict in info_config_map.items():
info_key = info_dict['info_key']
key_type = info_dict.get('value_type', 'str')
try:
if config_key in config_c and info_dict.get('to_json', True):
if dotty_info.get(info_key) and info_dict.get('warn_duplicate', True):
_log_warning(info_data, '%s in config.h is overwriting %s in info.json' % (config_key, info_key))
if key_type.startswith('array'):
if '.' in key_type:
key_type, array_type = key_type.split('.', 1)
else:
array_type = None
config_value = config_c[config_key].replace('{', '').replace('}', '').strip()
if array_type == 'int':
dotty_info[info_key] = list(map(int, config_value.split(',')))
else:
dotty_info[info_key] = config_value.split(',')
elif key_type == 'bool':
dotty_info[info_key] = config_c[config_key] in true_values
elif key_type == 'hex':
dotty_info[info_key] = '0x' + config_c[config_key][2:].upper()
elif key_type == 'list':
dotty_info[info_key] = config_c[config_key].split()
elif key_type == 'int':
dotty_info[info_key] = int(config_c[config_key])
else:
dotty_info[info_key] = config_c[config_key]
except Exception as e:
_log_warning(info_data, f'{config_key}->{info_key}: {e}')
info_data.update(dotty_info)
# Pull data that easily can't be mapped in json
_extract_matrix_info(info_data, config_c)
return info_data
@@ -103,63 +277,144 @@ def _extract_rules_mk(info_data):
"""Pull some keyboard information from existing rules.mk files
"""
rules = rules_mk(info_data['keyboard_folder'])
mcu = rules.get('MCU')
info_data['processor'] = rules.get('MCU', info_data.get('processor', 'atmega32u4'))
if mcu in CHIBIOS_PROCESSORS:
return arm_processor_rules(info_data, rules)
if info_data['processor'] in CHIBIOS_PROCESSORS:
arm_processor_rules(info_data, rules)
elif mcu in LUFA_PROCESSORS + VUSB_PROCESSORS:
return avr_processor_rules(info_data, rules)
elif info_data['processor'] in LUFA_PROCESSORS + VUSB_PROCESSORS:
avr_processor_rules(info_data, rules)
msg = "Unknown MCU: " + str(mcu)
else:
cli.log.warning("%s: Unknown MCU: %s" % (info_data['keyboard_folder'], info_data['processor']))
unknown_processor_rules(info_data, rules)
_log_warning(info_data, msg)
# Pull in data from the json map
dotty_info = dotty(info_data)
info_rules_map = json_load(Path('data/mappings/info_rules.json'))
return unknown_processor_rules(info_data, rules)
for rules_key, info_dict in info_rules_map.items():
info_key = info_dict['info_key']
key_type = info_dict.get('value_type', 'str')
try:
if rules_key in rules and info_dict.get('to_json', True):
if dotty_info.get(info_key) and info_dict.get('warn_duplicate', True):
_log_warning(info_data, '%s in rules.mk is overwriting %s in info.json' % (rules_key, info_key))
if key_type.startswith('array'):
if '.' in key_type:
key_type, array_type = key_type.split('.', 1)
else:
array_type = None
rules_value = rules[rules_key].replace('{', '').replace('}', '').strip()
if array_type == 'int':
dotty_info[info_key] = list(map(int, rules_value.split(',')))
else:
dotty_info[info_key] = rules_value.split(',')
elif key_type == 'list':
dotty_info[info_key] = rules[rules_key].split()
elif key_type == 'bool':
dotty_info[info_key] = rules[rules_key] in true_values
elif key_type == 'hex':
dotty_info[info_key] = '0x' + rules[rules_key][2:].upper()
elif key_type == 'int':
dotty_info[info_key] = int(rules[rules_key])
else:
dotty_info[info_key] = rules[rules_key]
except Exception as e:
_log_warning(info_data, f'{rules_key}->{info_key}: {e}')
info_data.update(dotty_info)
# Merge in config values that can't be easily mapped
_extract_features(info_data, rules)
return info_data
def _merge_layouts(info_data, new_info_data):
"""Merge new_info_data into info_data in an intelligent way.
"""
for layout_name, layout_json in new_info_data['layouts'].items():
if layout_name in info_data['layouts']:
# Pull in layouts we have a macro for
if len(info_data['layouts'][layout_name]['layout']) != len(layout_json['layout']):
msg = '%s: %s: Number of elements in info.json does not match! info.json:%s != %s:%s'
_log_error(info_data, msg % (info_data['keyboard_folder'], layout_name, len(layout_json['layout']), layout_name, len(info_data['layouts'][layout_name]['layout'])))
else:
for i, key in enumerate(info_data['layouts'][layout_name]['layout']):
key.update(layout_json['layout'][i])
else:
# Pull in layouts that have matrix data
missing_matrix = False
for key in layout_json.get('layout', {}):
if 'matrix' not in key:
missing_matrix = True
if not missing_matrix:
if layout_name in info_data['layouts']:
# Update an existing layout with new data
for i, key in enumerate(info_data['layouts'][layout_name]['layout']):
key.update(layout_json['layout'][i])
else:
# Copy in the new layout wholesale
layout_json['c_macro'] = False
info_data['layouts'][layout_name] = layout_json
return info_data
def _search_keyboard_h(path):
current_path = Path('keyboards/')
aliases = {}
layouts = {}
for directory in path.parts:
current_path = current_path / directory
keyboard_h = '%s.h' % (directory,)
keyboard_h_path = current_path / keyboard_h
if keyboard_h_path.exists():
layouts.update(find_layouts(keyboard_h_path))
new_layouts, new_aliases = find_layouts(keyboard_h_path)
layouts.update(new_layouts)
return layouts
for alias, alias_text in new_aliases.items():
if alias_text in layouts:
aliases[alias] = alias_text
return layouts, aliases
def _find_all_layouts(info_data, keyboard, rules):
def _find_all_layouts(info_data, keyboard):
"""Looks for layout macros associated with this keyboard.
"""
layouts = _search_keyboard_h(Path(keyboard))
layouts, aliases = _search_keyboard_h(Path(keyboard))
if not layouts:
# If we didn't find any layouts above we widen our search. This is error
# prone which is why we want to encourage people to follow the standard above.
_log_warning(info_data, 'Falling back to searching for KEYMAP/LAYOUT macros.')
# If we don't find any layouts from info.json or keyboard.h we widen our search. This is error prone which is why we want to encourage people to follow the standard above.
info_data['parse_warnings'].append('%s: Falling back to searching for KEYMAP/LAYOUT macros.' % (keyboard))
for file in glob('keyboards/%s/*.h' % keyboard):
if file.endswith('.h'):
these_layouts = find_layouts(file)
these_layouts, these_aliases = find_layouts(file)
if these_layouts:
layouts.update(these_layouts)
if 'LAYOUTS' in rules:
# Match these up against the supplied layouts
supported_layouts = rules['LAYOUTS'].strip().split()
for layout_name in sorted(layouts):
if not layout_name.startswith('LAYOUT_'):
continue
layout_name = layout_name[7:]
if layout_name in supported_layouts:
supported_layouts.remove(layout_name)
for alias, alias_text in these_aliases.items():
if alias_text in layouts:
aliases[alias] = alias_text
if supported_layouts:
_log_error(info_data, 'Missing LAYOUT() macro for %s' % (', '.join(supported_layouts)))
return layouts
return layouts, aliases
def _log_error(info_data, message):
@@ -180,13 +435,13 @@ def arm_processor_rules(info_data, rules):
"""Setup the default info for an ARM board.
"""
info_data['processor_type'] = 'arm'
info_data['bootloader'] = rules['BOOTLOADER'] if 'BOOTLOADER' in rules else 'unknown'
info_data['processor'] = rules['MCU'] if 'MCU' in rules else 'unknown'
info_data['protocol'] = 'ChibiOS'
if info_data['bootloader'] == 'unknown':
if 'bootloader' not in info_data:
if 'STM32' in info_data['processor']:
info_data['bootloader'] = 'stm32-dfu'
else:
info_data['bootloader'] = 'unknown'
if 'STM32' in info_data['processor']:
info_data['platform'] = 'STM32'
@@ -202,11 +457,12 @@ def avr_processor_rules(info_data, rules):
"""Setup the default info for an AVR board.
"""
info_data['processor_type'] = 'avr'
info_data['bootloader'] = rules['BOOTLOADER'] if 'BOOTLOADER' in rules else 'atmel-dfu'
info_data['platform'] = rules['ARCH'] if 'ARCH' in rules else 'unknown'
info_data['processor'] = rules['MCU'] if 'MCU' in rules else 'unknown'
info_data['protocol'] = 'V-USB' if rules.get('MCU') in VUSB_PROCESSORS else 'LUFA'
if 'bootloader' not in info_data:
info_data['bootloader'] = 'atmel-dfu'
# FIXME(fauxpark/anyone): Eventually we should detect the protocol by looking at PROTOCOL inherited from mcu_selection.mk:
# info_data['protocol'] = 'V-USB' if rules.get('PROTOCOL') == 'VUSB' else 'LUFA'
@@ -230,33 +486,42 @@ def merge_info_jsons(keyboard, info_data):
"""
for info_file in find_info_json(keyboard):
# Load and validate the JSON data
try:
with info_file.open('r') as info_fd:
new_info_data = json.load(info_fd)
except Exception as e:
_log_error(info_data, "Invalid JSON in file %s: %s: %s" % (str(info_file), e.__class__.__name__, e))
continue
new_info_data = json_load(info_file)
if not isinstance(new_info_data, dict):
_log_error(info_data, "Invalid file %s, root object should be a dictionary." % (str(info_file),))
continue
# Copy whitelisted keys into `info_data`
for key in ('keyboard_name', 'manufacturer', 'identifier', 'url', 'maintainer', 'processor', 'bootloader', 'width', 'height'):
if key in new_info_data:
info_data[key] = new_info_data[key]
try:
keyboard_validate(new_info_data)
except jsonschema.ValidationError as e:
json_path = '.'.join([str(p) for p in e.absolute_path])
cli.log.error('Not including data from file: %s', info_file)
cli.log.error('\t%s: %s', json_path, e.message)
continue
# Merge the layouts in
# Merge layout data in
if 'layout_aliases' in new_info_data:
info_data['layout_aliases'] = {**info_data.get('layout_aliases', {}), **new_info_data['layout_aliases']}
del new_info_data['layout_aliases']
for layout_name, layout in new_info_data.get('layouts', {}).items():
if layout_name in info_data.get('layout_aliases', {}):
_log_warning(info_data, f"info.json uses alias name {layout_name} instead of {info_data['layout_aliases'][layout_name]}")
layout_name = info_data['layout_aliases'][layout_name]
if layout_name in info_data['layouts']:
for new_key, existing_key in zip(layout['layout'], info_data['layouts'][layout_name]['layout']):
existing_key.update(new_key)
else:
layout['c_macro'] = False
info_data['layouts'][layout_name] = layout
# Update info_data with the new data
if 'layouts' in new_info_data:
for layout_name, json_layout in new_info_data['layouts'].items():
# Only pull in layouts we have a macro for
if layout_name in info_data['layouts']:
if info_data['layouts'][layout_name]['key_count'] != len(json_layout['layout']):
msg = '%s: Number of elements in info.json does not match! info.json:%s != %s:%s'
_log_error(info_data, msg % (layout_name, len(json_layout['layout']), layout_name, len(info_data['layouts'][layout_name]['layout'])))
else:
for i, key in enumerate(info_data['layouts'][layout_name]['layout']):
key.update(json_layout['layout'][i])
del new_info_data['layouts']
deep_update(info_data, new_info_data)
return info_data

192
lib/python/qmk/json_encoders.py Executable file
View File

@@ -0,0 +1,192 @@
"""Class that pretty-prints QMK info.json files.
"""
import json
from decimal import Decimal
newline = '\n'
class QMKJSONEncoder(json.JSONEncoder):
"""Base class for all QMK JSON encoders.
"""
container_types = (list, tuple, dict)
indentation_char = " "
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.indentation_level = 0
if not self.indent:
self.indent = 4
def encode_decimal(self, obj):
"""Encode a decimal object.
"""
if obj == int(obj): # I can't believe Decimal objects don't have .is_integer()
return int(obj)
return float(obj)
def encode_list(self, obj):
"""Encode a list-like object.
"""
if self.primitives_only(obj):
return "[" + ", ".join(self.encode(element) for element in obj) + "]"
else:
self.indentation_level += 1
output = [self.indent_str + self.encode(element) for element in obj]
self.indentation_level -= 1
return "[\n" + ",\n".join(output) + "\n" + self.indent_str + "]"
def encode(self, obj):
"""Encode keymap.json objects for QMK.
"""
if isinstance(obj, Decimal):
return self.encode_decimal(obj)
elif isinstance(obj, (list, tuple)):
return self.encode_list(obj)
elif isinstance(obj, dict):
return self.encode_dict(obj)
else:
return super().encode(obj)
def primitives_only(self, obj):
"""Returns true if the object doesn't have any container type objects (list, tuple, dict).
"""
if isinstance(obj, dict):
obj = obj.values()
return not any(isinstance(element, self.container_types) for element in obj)
@property
def indent_str(self):
return self.indentation_char * (self.indentation_level * self.indent)
class InfoJSONEncoder(QMKJSONEncoder):
"""Custom encoder to make info.json's a little nicer to work with.
"""
def encode_dict(self, obj):
"""Encode info.json dictionaries.
"""
if obj:
if self.indentation_level == 4:
# These are part of a layout, put them on a single line.
return "{ " + ", ".join(f"{self.encode(key)}: {self.encode(element)}" for key, element in sorted(obj.items())) + " }"
else:
self.indentation_level += 1
output = [self.indent_str + f"{json.dumps(key)}: {self.encode(value)}" for key, value in sorted(obj.items(), key=self.sort_dict)]
self.indentation_level -= 1
return "{\n" + ",\n".join(output) + "\n" + self.indent_str + "}"
else:
return "{}"
def sort_dict(self, key):
"""Forces layout to the back of the sort order.
"""
key = key[0]
if self.indentation_level == 1:
if key == 'manufacturer':
return '10keyboard_name'
elif key == 'keyboard_name':
return '11keyboard_name'
elif key == 'maintainer':
return '12maintainer'
elif key in ('height', 'width'):
return '40' + str(key)
elif key == 'community_layouts':
return '97community_layouts'
elif key == 'layout_aliases':
return '98layout_aliases'
elif key == 'layouts':
return '99layouts'
else:
return '50' + str(key)
return key
class KeymapJSONEncoder(QMKJSONEncoder):
"""Custom encoder to make keymap.json's a little nicer to work with.
"""
def encode_dict(self, obj):
"""Encode dictionary objects for keymap.json.
"""
if obj:
self.indentation_level += 1
output_lines = [f"{self.indent_str}{json.dumps(key)}: {self.encode(value)}" for key, value in sorted(obj.items(), key=self.sort_dict)]
output = ',\n'.join(output_lines)
self.indentation_level -= 1
return f"{{\n{output}\n{self.indent_str}}}"
else:
return "{}"
def encode_list(self, obj):
"""Encode a list-like object.
"""
if self.indentation_level == 2:
indent_level = self.indentation_level + 1
# We have a list of keycodes
layer = [[]]
for key in obj:
if key == 'JSON_NEWLINE':
layer.append([])
else:
layer[-1].append(f'"{key}"')
layer = [f"{self.indent_str*indent_level}{', '.join(row)}" for row in layer]
return f"{self.indent_str}[\n{newline.join(layer)}\n{self.indent_str*self.indentation_level}]"
elif self.primitives_only(obj):
return "[" + ", ".join(self.encode(element) for element in obj) + "]"
else:
self.indentation_level += 1
output = [self.indent_str + self.encode(element) for element in obj]
self.indentation_level -= 1
return "[\n" + ",\n".join(output) + "\n" + self.indent_str + "]"
def sort_dict(self, key):
"""Sorts the hashes in a nice way.
"""
key = key[0]
if self.indentation_level == 1:
if key == 'version':
return '00version'
elif key == 'author':
return '01author'
elif key == 'notes':
return '02notes'
elif key == 'layers':
return '98layers'
elif key == 'documentation':
return '99documentation'
else:
return '50' + str(key)
return key

View File

@@ -0,0 +1,68 @@
"""Functions that help us generate and use info.json files.
"""
import json
from collections.abc import Mapping
from pathlib import Path
import hjson
import jsonschema
from milc import cli
def json_load(json_file):
"""Load a json file from disk.
Note: file must be a Path object.
"""
try:
return hjson.load(json_file.open(encoding='utf-8'))
except json.decoder.JSONDecodeError as e:
cli.log.error('Invalid JSON encountered attempting to load {fg_cyan}%s{fg_reset}:\n\t{fg_red}%s', json_file, e)
exit(1)
def load_jsonschema(schema_name):
"""Read a jsonschema file from disk.
FIXME(skullydazed/anyone): Refactor to make this a public function.
"""
schema_path = Path(f'data/schemas/{schema_name}.jsonschema')
if not schema_path.exists():
schema_path = Path('data/schemas/false.jsonschema')
return json_load(schema_path)
def keyboard_validate(data):
"""Validates data against the keyboard jsonschema.
"""
schema = load_jsonschema('keyboard')
validator = jsonschema.Draft7Validator(schema).validate
return validator(data)
def keyboard_api_validate(data):
"""Validates data against the api_keyboard jsonschema.
"""
base = load_jsonschema('keyboard')
relative = load_jsonschema('api_keyboard')
resolver = jsonschema.RefResolver.from_schema(base)
validator = jsonschema.Draft7Validator(relative, resolver=resolver).validate
return validator(data)
def deep_update(origdict, newdict):
"""Update a dictionary in place, recursing to do a deep copy.
"""
for key, value in newdict.items():
if isinstance(value, Mapping):
origdict[key] = deep_update(origdict.get(key, {}), value)
else:
origdict[key] = value
return origdict

View File

@@ -6,7 +6,9 @@ from pathlib import Path
import os
from glob import glob
import qmk.path
from qmk.c_parse import parse_config_h_file
from qmk.json_schema import json_load
from qmk.makefile import parse_rules_mk_file
BOX_DRAWING_CHARACTERS = {
@@ -31,12 +33,71 @@ BOX_DRAWING_CHARACTERS = {
base_path = os.path.join(os.getcwd(), "keyboards") + os.path.sep
def find_keyboard_from_dir():
"""Returns a keyboard name based on the user's current directory.
"""
relative_cwd = qmk.path.under_qmk_firmware()
if relative_cwd and len(relative_cwd.parts) > 1 and relative_cwd.parts[0] == 'keyboards':
# Attempt to extract the keyboard name from the current directory
current_path = Path('/'.join(relative_cwd.parts[1:]))
if 'keymaps' in current_path.parts:
# Strip current_path of anything after `keymaps`
keymap_index = len(current_path.parts) - current_path.parts.index('keymaps') - 1
current_path = current_path.parents[keymap_index]
if qmk.path.is_keyboard(current_path):
return str(current_path)
def find_readme(keyboard):
"""Returns the readme for this keyboard.
"""
cur_dir = qmk.path.keyboard(keyboard)
keyboards_dir = Path('keyboards')
while not (cur_dir / 'readme.md').exists():
if cur_dir == keyboards_dir:
return None
cur_dir = cur_dir.parent
return cur_dir / 'readme.md'
def keyboard_folder(keyboard):
"""Returns the actual keyboard folder.
This checks aliases and DEFAULT_FOLDER to resolve the actual path for a keyboard.
"""
aliases = json_load(Path('data/mappings/keyboard_aliases.json'))
if keyboard in aliases:
keyboard = aliases[keyboard].get('target', keyboard)
rules_mk_file = Path(base_path, keyboard, 'rules.mk')
if rules_mk_file.exists():
rules_mk = parse_rules_mk_file(rules_mk_file)
keyboard = rules_mk.get('DEFAULT_FOLDER', keyboard)
if not qmk.path.is_keyboard(keyboard):
raise ValueError(f'Invalid keyboard: {keyboard}')
return keyboard
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", "")
def keyboard_completer(prefix, action, parser, parsed_args):
"""Returns a list of keyboards for tab completion.
"""
return list_keyboards()
def list_keyboards():
"""Returns a list of all keyboards.
"""
@@ -44,7 +105,16 @@ def list_keyboards():
kb_wildcard = os.path.join(base_path, "**", "rules.mk")
paths = [path for path in glob(kb_wildcard, recursive=True) if 'keymaps' not in path]
return sorted(map(_find_name, paths))
return sorted(set(map(resolve_keyboard, map(_find_name, paths))))
def resolve_keyboard(keyboard):
cur_dir = Path('keyboards')
rules = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk')
while 'DEFAULT_FOLDER' in rules and keyboard != rules['DEFAULT_FOLDER']:
keyboard = rules['DEFAULT_FOLDER']
rules = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk')
return keyboard
def config_h(keyboard):
@@ -58,8 +128,7 @@ def config_h(keyboard):
"""
config = {}
cur_dir = Path('keyboards')
rules = rules_mk(keyboard)
keyboard = Path(rules['DEFAULT_FOLDER'] if 'DEFAULT_FOLDER' in rules else keyboard)
keyboard = Path(resolve_keyboard(keyboard))
for dir in keyboard.parts:
cur_dir = cur_dir / dir
@@ -77,13 +146,10 @@ def rules_mk(keyboard):
Returns:
a dictionary representing the content of the entire rules.mk tree for a keyboard
"""
keyboard = Path(keyboard)
cur_dir = Path('keyboards')
keyboard = Path(resolve_keyboard(keyboard))
rules = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk')
if 'DEFAULT_FOLDER' in rules:
keyboard = Path(rules['DEFAULT_FOLDER'])
for i, dir in enumerate(keyboard.parts):
cur_dir = cur_dir / dir
rules = parse_rules_mk_file(cur_dir / 'rules.mk', rules)

View File

@@ -1,19 +1,19 @@
"""Functions that help you work with QMK keymaps.
"""
from pathlib import Path
import json
import subprocess
import sys
from pathlib import Path
from subprocess import DEVNULL
import argcomplete
from milc import cli
from pygments.lexers.c_cpp import CLexer
from pygments.token import Token
from pygments import lex
from milc import cli
from qmk.keyboard import rules_mk
import qmk.path
import qmk.commands
from qmk.keyboard import find_keyboard_from_dir, rules_mk
from qmk.errors import CppError
# The `keymap.c` template to use when a keyboard doesn't have its own
DEFAULT_KEYMAP_C = """#include QMK_KEYBOARD_H
@@ -42,7 +42,7 @@ def template_json(keyboard):
template_file = Path('keyboards/%s/templates/keymap.json' % keyboard)
template = {'keyboard': keyboard}
if template_file.exists():
template.update(json.loads(template_file.read_text()))
template.update(json.load(template_file.open(encoding='utf-8')))
return template
@@ -58,7 +58,7 @@ def template_c(keyboard):
"""
template_file = Path('keyboards/%s/templates/keymap.c' % keyboard)
if template_file.exists():
template = template_file.read_text()
template = template_file.read_text(encoding='utf-8')
else:
template = DEFAULT_KEYMAP_C
@@ -74,6 +74,54 @@ def _strip_any(keycode):
return keycode
def find_keymap_from_dir():
"""Returns `(keymap_name, source)` for the directory we're currently in.
"""
relative_cwd = qmk.path.under_qmk_firmware()
if relative_cwd and len(relative_cwd.parts) > 1:
# If we're in `qmk_firmware/keyboards` and `keymaps` is in our path, try to find the keyboard name.
if relative_cwd.parts[0] == 'keyboards' and 'keymaps' in relative_cwd.parts:
current_path = Path('/'.join(relative_cwd.parts[1:])) # Strip 'keyboards' from the front
if 'keymaps' in current_path.parts and current_path.name != 'keymaps':
while current_path.parent.name != 'keymaps':
current_path = current_path.parent
return current_path.name, 'keymap_directory'
# If we're in `qmk_firmware/layouts` guess the name from the community keymap they're in
elif relative_cwd.parts[0] == 'layouts' and is_keymap_dir(relative_cwd):
return relative_cwd.name, 'layouts_directory'
# If we're in `qmk_firmware/users` guess the name from the userspace they're in
elif relative_cwd.parts[0] == 'users':
# Guess the keymap name based on which userspace they're in
return relative_cwd.parts[1], 'users_directory'
return None, None
def keymap_completer(prefix, action, parser, parsed_args):
"""Returns a list of keymaps for tab completion.
"""
try:
if parsed_args.keyboard:
return list_keymaps(parsed_args.keyboard)
keyboard = find_keyboard_from_dir()
if keyboard:
return list_keymaps(keyboard)
except Exception as e:
argcomplete.warn(f'Error: {e.__class__.__name__}: {str(e)}')
return []
return []
def is_keymap_dir(keymap, c=True, json=True, additional_files=None):
"""Return True if Path object `keymap` has a keymap file inside.
@@ -313,7 +361,7 @@ def list_keymaps(keyboard, c=True, json=True, additional_files=None, fullpath=Fa
return sorted(names)
def _c_preprocess(path, stdin=None):
def _c_preprocess(path, stdin=DEVNULL):
""" Run a file through the C pre-processor
Args:
@@ -323,7 +371,12 @@ def _c_preprocess(path, stdin=None):
Returns:
the stdout of the pre-processor
"""
pre_processed_keymap = qmk.commands.run(['cpp', path] if path else ['cpp'], stdin=stdin, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
cmd = ['cpp', str(path)] if path else ['cpp']
pre_processed_keymap = cli.run(cmd, stdin=stdin)
if 'fatal error' in pre_processed_keymap.stderr:
for line in pre_processed_keymap.stderr.split('\n'):
if 'fatal error' in line:
raise (CppError(line))
return pre_processed_keymap.stdout
@@ -469,7 +522,7 @@ def parse_keymap_c(keymap_file, use_cpp=True):
if use_cpp:
keymap_file = _c_preprocess(keymap_file)
else:
keymap_file = keymap_file.read_text()
keymap_file = keymap_file.read_text(encoding='utf-8')
keymap = dict()
keymap['layers'] = _get_layers(keymap_file)

View File

@@ -3,10 +3,9 @@
from enum import Enum
import re
import shutil
import subprocess
from subprocess import DEVNULL
from milc import cli
from qmk.commands import run
from qmk import submodules
from qmk.constants import QMK_FIRMWARE
@@ -142,7 +141,7 @@ def is_executable(command):
# Make sure the command can be executed
version_arg = ESSENTIAL_BINARIES[command].get('version_arg', '--version')
check = run([command, version_arg], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=5, universal_newlines=True)
check = cli.run([command, version_arg], combined_output=True, stdin=DEVNULL, timeout=5)
ESSENTIAL_BINARIES[command]['output'] = check.stdout

View File

@@ -5,7 +5,6 @@ import shutil
from milc import cli
from qmk.constants import QMK_FIRMWARE
from qmk.commands import run
from qmk.os_helpers import CheckStatus
@@ -48,6 +47,10 @@ def check_udev_rules():
_udev_rule("03eb", "2ff3"), # ATmega16U4
_udev_rule("03eb", "2ff4"), # ATmega32U4
_udev_rule("03eb", "2ff9"), # AT90USB64
<<<<<<< HEAD
=======
_udev_rule("03eb", "2ffa"), # AT90USB162
>>>>>>> 0.12.52~1
_udev_rule("03eb", "2ffb") # AT90USB128
},
'kiibohd': {_udev_rule("1c11", "b007")},
@@ -94,7 +97,11 @@ def check_udev_rules():
# Collect all rules from the config files
for rule_file in udev_rules:
<<<<<<< HEAD
for line in rule_file.read_text().split('\n'):
=======
for line in rule_file.read_text(encoding='utf-8').split('\n'):
>>>>>>> 0.12.52~1
line = line.strip()
if not line.startswith("#") and len(line):
current_rules.add(line)
@@ -131,7 +138,11 @@ def check_modem_manager():
"""
if check_systemd():
<<<<<<< HEAD
mm_check = run(["systemctl", "--quiet", "is-active", "ModemManager.service"], timeout=10)
=======
mm_check = cli.run(["systemctl", "--quiet", "is-active", "ModemManager.service"], timeout=10)
>>>>>>> 0.12.52~1
if mm_check.returncode == 0:
return True
else:

View File

@@ -2,6 +2,7 @@
"""
import logging
import os
import argparse
from pathlib import Path
from qmk.constants import MAX_KEYBOARD_SUBFOLDERS, QMK_FIRMWARE
@@ -65,3 +66,12 @@ def normpath(path):
return path
return Path(os.environ['ORIG_CWD']) / path
class FileType(argparse.FileType):
def __call__(self, string):
"""normalize and check exists
otherwise magic strings like '-' for stdin resolve to bad paths
"""
norm = normpath(string)
return super().__call__(norm if norm.exists() else string)

View File

@@ -1,7 +1,6 @@
"""Functions for working with QMK's submodules.
"""
import subprocess
from milc import cli
def status():
@@ -18,7 +17,7 @@ def status():
status is None when the submodule doesn't exist, False when it's out of date, and True when it's current
"""
submodules = {}
git_cmd = subprocess.run(['git', 'submodule', 'status'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=30, universal_newlines=True)
git_cmd = cli.run(['git', 'submodule', 'status'], timeout=30)
for line in git_cmd.stdout.split('\n'):
if not line:
@@ -53,19 +52,19 @@ def update(submodules=None):
# Update everything
git_sync_cmd.append('--recursive')
git_update_cmd.append('--recursive')
subprocess.run(git_sync_cmd, check=True)
subprocess.run(git_update_cmd, check=True)
cli.run(git_sync_cmd, check=True)
cli.run(git_update_cmd, check=True)
else:
if isinstance(submodules, str):
# Update only a single submodule
git_sync_cmd.append(submodules)
git_update_cmd.append(submodules)
subprocess.run(git_sync_cmd, check=True)
subprocess.run(git_update_cmd, check=True)
cli.run(git_sync_cmd, check=True)
cli.run(git_update_cmd, check=True)
else:
# Update submodules in a list
for submodule in submodules:
subprocess.run(git_sync_cmd + [submodule], check=True)
subprocess.run(git_update_cmd + [submodule], check=True)
cli.run([*git_sync_cmd, submodule], check=True)
cli.run([*git_update_cmd, submodule], check=True)

View File

@@ -0,0 +1,13 @@
{
"keyboard_name": "tester",
"maintainer": "qmk",
"height": 5,
"width": 15,
"layouts": {
"LAYOUT": {
"layout": [
{ "label": "KC_A", "x": 0, "y": 0, "matrix": [0, 0] }
]
}
}
}

View File

@@ -0,0 +1,7 @@
{
"keyboard": "handwired/pytest/basic",
"keymap": "test",
"layers": [["KC_A"]],
"layout": "LAYOUT_ortho_1x1",
"version": 1
}

View File

@@ -1,24 +1,23 @@
import platform
from subprocess import DEVNULL
from subprocess import STDOUT, PIPE
from qmk.commands import run
from milc import cli
is_windows = 'windows' in platform.platform().lower()
def check_subcommand(command, *args):
cmd = ['bin/qmk', command, *args]
result = run(cmd, stdout=PIPE, stderr=STDOUT, universal_newlines=True)
result = cli.run(cmd, stdin=DEVNULL, combined_output=True)
return result
def check_subcommand_stdin(file_to_read, command, *args):
"""Pipe content of a file to a command and return output.
"""
with open(file_to_read) as my_file:
with open(file_to_read, encoding='utf-8') as my_file:
cmd = ['bin/qmk', command, *args]
result = run(cmd, stdin=my_file, stdout=PIPE, stderr=STDOUT, universal_newlines=True)
result = cli.run(cmd, stdin=my_file, combined_output=True)
return result
@@ -33,22 +32,27 @@ def check_returncode(result, expected=[0]):
def test_cformat():
result = check_subcommand('cformat', 'quantum/matrix.c')
result = check_subcommand('cformat', '-n', 'quantum/matrix.c')
check_returncode(result)
def test_cformat_all():
result = check_subcommand('cformat', '-n', '-a')
check_returncode(result, [0, 1])
def test_compile():
result = check_subcommand('compile', '-kb', 'handwired/onekey/pytest', '-km', 'default', '-n')
result = check_subcommand('compile', '-kb', 'handwired/pytest/basic', '-km', 'default', '-n')
check_returncode(result)
def test_compile_json():
result = check_subcommand('compile', '-kb', 'handwired/onekey/pytest', '-km', 'default_json')
result = check_subcommand('compile', '-kb', 'handwired/pytest/basic', '-km', 'default_json', '-n')
check_returncode(result)
def test_flash():
result = check_subcommand('flash', '-kb', 'handwired/onekey/pytest', '-km', 'default', '-n')
result = check_subcommand('flash', '-kb', 'handwired/pytest/basic', '-km', 'default', '-n')
check_returncode(result)
@@ -57,12 +61,6 @@ def test_flash_bootloaders():
check_returncode(result, [1])
def test_config():
result = check_subcommand('config')
check_returncode(result)
assert 'general.color' in result.stdout
def test_kle2json():
result = check_subcommand('kle2json', 'lib/python/qmk/tests/kle.txt', '-f')
check_returncode(result)
@@ -83,29 +81,35 @@ def test_hello():
def test_pyformat():
result = check_subcommand('pyformat')
result = check_subcommand('pyformat', '--dry-run')
check_returncode(result)
assert 'Successfully formatted the python code' in result.stdout
assert 'Python code in `bin/qmk` and `lib/python` is correctly formatted.' in result.stdout
def test_list_keyboards():
result = check_subcommand('list-keyboards')
check_returncode(result)
# check to see if a known keyboard is returned
# this will fail if handwired/onekey/pytest is removed
assert 'handwired/onekey/pytest' in result.stdout
# this will fail if handwired/pytest/basic is removed
assert 'handwired/pytest/basic' in result.stdout
def test_list_keymaps():
result = check_subcommand('list-keymaps', '-kb', 'handwired/onekey/pytest')
result = check_subcommand('list-keymaps', '-kb', 'handwired/pytest/basic')
check_returncode(result)
assert 'default' and 'test' in result.stdout
assert 'default' and 'default_json' in result.stdout
def test_list_keymaps_long():
result = check_subcommand('list-keymaps', '--keyboard', 'handwired/onekey/pytest')
result = check_subcommand('list-keymaps', '--keyboard', 'handwired/pytest/basic')
check_returncode(result)
assert 'default' and 'test' in result.stdout
assert 'default' and 'default_json' in result.stdout
def test_list_keymaps_community():
result = check_subcommand('list-keymaps', '--keyboard', 'handwired/pytest/has_community')
check_returncode(result)
assert 'test' in result.stdout
def test_list_keymaps_kb_only():
@@ -128,45 +132,45 @@ def test_list_keymaps_vendor_kb_rev():
def test_list_keymaps_no_keyboard_found():
result = check_subcommand('list-keymaps', '-kb', 'asdfghjkl')
check_returncode(result, [1])
assert 'does not exist' in result.stdout
check_returncode(result, [2])
assert 'invalid keyboard_folder value' in result.stdout
def test_json2c():
result = check_subcommand('json2c', 'keyboards/handwired/onekey/keymaps/default_json/keymap.json')
result = check_subcommand('json2c', 'keyboards/handwired/pytest/has_template/keymaps/default_json/keymap.json')
check_returncode(result)
assert result.stdout == '#include QMK_KEYBOARD_H\nconst uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {\t[0] = LAYOUT_ortho_1x1(KC_A)};\n\n'
def test_json2c_stdin():
result = check_subcommand_stdin('keyboards/handwired/onekey/keymaps/default_json/keymap.json', 'json2c', '-')
result = check_subcommand_stdin('keyboards/handwired/pytest/has_template/keymaps/default_json/keymap.json', 'json2c', '-')
check_returncode(result)
assert result.stdout == '#include QMK_KEYBOARD_H\nconst uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {\t[0] = LAYOUT_ortho_1x1(KC_A)};\n\n'
def test_info():
result = check_subcommand('info', '-kb', 'handwired/onekey/pytest')
result = check_subcommand('info', '-kb', 'handwired/pytest/basic')
check_returncode(result)
assert 'Keyboard Name: handwired/onekey/pytest' in result.stdout
assert 'Processor: STM32F303' in result.stdout
assert 'Keyboard Name: handwired/pytest/basic' in result.stdout
assert 'Processor: atmega32u4' in result.stdout
assert 'Layout:' not in result.stdout
assert 'k0' not in result.stdout
def test_info_keyboard_render():
result = check_subcommand('info', '-kb', 'handwired/onekey/pytest', '-l')
result = check_subcommand('info', '-kb', 'handwired/pytest/basic', '-l')
check_returncode(result)
assert 'Keyboard Name: handwired/onekey/pytest' in result.stdout
assert 'Processor: STM32F303' in result.stdout
assert 'Keyboard Name: handwired/pytest/basic' in result.stdout
assert 'Processor: atmega32u4' in result.stdout
assert 'Layouts:' in result.stdout
assert 'k0' in result.stdout
def test_info_keymap_render():
result = check_subcommand('info', '-kb', 'handwired/onekey/pytest', '-km', 'default_json')
result = check_subcommand('info', '-kb', 'handwired/pytest/basic', '-km', 'default_json')
check_returncode(result)
assert 'Keyboard Name: handwired/onekey/pytest' in result.stdout
assert 'Processor: STM32F303' in result.stdout
assert 'Keyboard Name: handwired/pytest/basic' in result.stdout
assert 'Processor: atmega32u4' in result.stdout
if is_windows:
assert '|A |' in result.stdout
@@ -175,10 +179,10 @@ def test_info_keymap_render():
def test_info_matrix_render():
result = check_subcommand('info', '-kb', 'handwired/onekey/pytest', '-m')
result = check_subcommand('info', '-kb', 'handwired/pytest/basic', '-m')
check_returncode(result)
assert 'Keyboard Name: handwired/onekey/pytest' in result.stdout
assert 'Processor: STM32F303' in result.stdout
assert 'Keyboard Name: handwired/pytest/basic' in result.stdout
assert 'Processor: atmega32u4' in result.stdout
assert 'LAYOUT_ortho_1x1' in result.stdout
if is_windows:
@@ -190,27 +194,27 @@ def test_info_matrix_render():
def test_c2json():
result = check_subcommand("c2json", "-kb", "handwired/onekey/pytest", "-km", "default", "keyboards/handwired/onekey/keymaps/default/keymap.c")
result = check_subcommand("c2json", "-kb", "handwired/pytest/has_template", "-km", "default", "keyboards/handwired/pytest/has_template/keymaps/default/keymap.c")
check_returncode(result)
assert result.stdout.strip() == '{"keyboard": "handwired/onekey/pytest", "documentation": "This file is a keymap.json file for handwired/onekey/pytest", "keymap": "default", "layout": "LAYOUT_ortho_1x1", "layers": [["KC_A"]]}'
assert result.stdout.strip() == '{"keyboard": "handwired/pytest/has_template", "documentation": "This file is a keymap.json file for handwired/pytest/has_template", "keymap": "default", "layout": "LAYOUT_ortho_1x1", "layers": [["KC_A"]]}'
def test_c2json_nocpp():
result = check_subcommand("c2json", "--no-cpp", "-kb", "handwired/onekey/pytest", "-km", "default", "keyboards/handwired/onekey/keymaps/pytest_nocpp/keymap.c")
result = check_subcommand("c2json", "--no-cpp", "-kb", "handwired/pytest/has_template", "-km", "default", "keyboards/handwired/pytest/has_template/keymaps/nocpp/keymap.c")
check_returncode(result)
assert result.stdout.strip() == '{"keyboard": "handwired/onekey/pytest", "documentation": "This file is a keymap.json file for handwired/onekey/pytest", "keymap": "default", "layout": "LAYOUT", "layers": [["KC_ENTER"]]}'
assert result.stdout.strip() == '{"keyboard": "handwired/pytest/has_template", "documentation": "This file is a keymap.json file for handwired/pytest/has_template", "keymap": "default", "layout": "LAYOUT", "layers": [["KC_ENTER"]]}'
def test_c2json_stdin():
result = check_subcommand_stdin("keyboards/handwired/onekey/keymaps/default/keymap.c", "c2json", "-kb", "handwired/onekey/pytest", "-km", "default", "-")
result = check_subcommand_stdin("keyboards/handwired/pytest/has_template/keymaps/default/keymap.c", "c2json", "-kb", "handwired/pytest/has_template", "-km", "default", "-")
check_returncode(result)
assert result.stdout.strip() == '{"keyboard": "handwired/onekey/pytest", "documentation": "This file is a keymap.json file for handwired/onekey/pytest", "keymap": "default", "layout": "LAYOUT_ortho_1x1", "layers": [["KC_A"]]}'
assert result.stdout.strip() == '{"keyboard": "handwired/pytest/has_template", "documentation": "This file is a keymap.json file for handwired/pytest/has_template", "keymap": "default", "layout": "LAYOUT_ortho_1x1", "layers": [["KC_A"]]}'
def test_c2json_nocpp_stdin():
result = check_subcommand_stdin("keyboards/handwired/onekey/keymaps/pytest_nocpp/keymap.c", "c2json", "--no-cpp", "-kb", "handwired/onekey/pytest", "-km", "default", "-")
result = check_subcommand_stdin("keyboards/handwired/pytest/has_template/keymaps/nocpp/keymap.c", "c2json", "--no-cpp", "-kb", "handwired/pytest/has_template", "-km", "default", "-")
check_returncode(result)
assert result.stdout.strip() == '{"keyboard": "handwired/onekey/pytest", "documentation": "This file is a keymap.json file for handwired/onekey/pytest", "keymap": "default", "layout": "LAYOUT", "layers": [["KC_ENTER"]]}'
assert result.stdout.strip() == '{"keyboard": "handwired/pytest/has_template", "documentation": "This file is a keymap.json file for handwired/pytest/has_template", "keymap": "default", "layout": "LAYOUT", "layers": [["KC_ENTER"]]}'
def test_clean():
@@ -219,8 +223,66 @@ def test_clean():
assert result.stdout.count('done') == 2
def test_generate_api():
result = check_subcommand('generate-api', '--dry-run')
check_returncode(result)
def test_generate_rgb_breathe_table():
result = check_subcommand("generate-rgb-breathe-table", "-c", "1.2", "-m", "127")
check_returncode(result)
assert 'Breathing center: 1.2' in result.stdout
assert 'Breathing max: 127' in result.stdout
def test_generate_config_h():
result = check_subcommand('generate-config-h', '-kb', 'handwired/pytest/basic')
check_returncode(result)
assert '# define DEVICE_VER 0x0001' in result.stdout
assert '# define DESCRIPTION handwired/pytest/basic' in result.stdout
assert '# define DIODE_DIRECTION COL2ROW' in result.stdout
assert '# define MANUFACTURER none' in result.stdout
assert '# define PRODUCT handwired/pytest/basic' in result.stdout
assert '# define PRODUCT_ID 0x6465' in result.stdout
assert '# define VENDOR_ID 0xFEED' in result.stdout
assert '# define MATRIX_COLS 1' in result.stdout
assert '# define MATRIX_COL_PINS { F4 }' in result.stdout
assert '# define MATRIX_ROWS 1' in result.stdout
assert '# define MATRIX_ROW_PINS { F5 }' in result.stdout
def test_generate_rules_mk():
result = check_subcommand('generate-rules-mk', '-kb', 'handwired/pytest/basic')
check_returncode(result)
assert 'BOOTLOADER ?= atmel-dfu' in result.stdout
assert 'MCU ?= atmega32u4' in result.stdout
def test_generate_layouts():
result = check_subcommand('generate-layouts', '-kb', 'handwired/pytest/basic')
check_returncode(result)
assert '#define LAYOUT_custom(k0A) {' in result.stdout
def test_format_json_keyboard():
result = check_subcommand('format-json', '--format', 'keyboard', 'lib/python/qmk/tests/minimal_info.json')
check_returncode(result)
assert result.stdout == '{\n "keyboard_name": "tester",\n "maintainer": "qmk",\n "height": 5,\n "width": 15,\n "layouts": {\n "LAYOUT": {\n "layout": [\n { "label": "KC_A", "matrix": [0, 0], "x": 0, "y": 0 }\n ]\n }\n }\n}\n'
def test_format_json_keymap():
result = check_subcommand('format-json', '--format', 'keymap', 'lib/python/qmk/tests/minimal_keymap.json')
check_returncode(result)
assert result.stdout == '{\n "version": 1,\n "keyboard": "handwired/pytest/basic",\n "keymap": "test",\n "layout": "LAYOUT_ortho_1x1",\n "layers": [\n [\n "KC_A"\n ]\n ]\n}\n'
def test_format_json_keyboard_auto():
result = check_subcommand('format-json', '--format', 'auto', 'lib/python/qmk/tests/minimal_info.json')
check_returncode(result)
assert result.stdout == '{\n "keyboard_name": "tester",\n "maintainer": "qmk",\n "height": 5,\n "width": 15,\n "layouts": {\n "LAYOUT": {\n "layout": [\n { "label": "KC_A", "matrix": [0, 0], "x": 0, "y": 0 }\n ]\n }\n }\n}\n'
def test_format_json_keymap_auto():
result = check_subcommand('format-json', '--format', 'auto', 'lib/python/qmk/tests/minimal_keymap.json')
check_returncode(result)
assert result.stdout == '{\n "keyboard": "handwired/pytest/basic",\n "keymap": "test",\n "layers": [\n ["KC_A"]\n ],\n "layout": "LAYOUT_ortho_1x1",\n "version": 1\n}\n'

View File

@@ -1,38 +1,38 @@
import qmk.keymap
def test_template_c_onekey_proton_c():
templ = qmk.keymap.template_c('handwired/onekey/proton_c')
def test_template_c_pytest_basic():
templ = qmk.keymap.template_c('handwired/pytest/basic')
assert templ == qmk.keymap.DEFAULT_KEYMAP_C
def test_template_json_onekey_proton_c():
templ = qmk.keymap.template_json('handwired/onekey/proton_c')
assert templ == {'keyboard': 'handwired/onekey/proton_c'}
def test_template_json_pytest_basic():
templ = qmk.keymap.template_json('handwired/pytest/basic')
assert templ == {'keyboard': 'handwired/pytest/basic'}
def test_template_c_onekey_pytest():
templ = qmk.keymap.template_c('handwired/onekey/pytest')
def test_template_c_pytest_has_template():
templ = qmk.keymap.template_c('handwired/pytest/has_template')
assert templ == '#include QMK_KEYBOARD_H\nconst uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {__KEYMAP_GOES_HERE__};\n'
def test_template_json_onekey_pytest():
templ = qmk.keymap.template_json('handwired/onekey/pytest')
assert templ == {'keyboard': 'handwired/onekey/pytest', "documentation": "This file is a keymap.json file for handwired/onekey/pytest"}
def test_template_json_pytest_has_template():
templ = qmk.keymap.template_json('handwired/pytest/has_template')
assert templ == {'keyboard': 'handwired/pytest/has_template', "documentation": "This file is a keymap.json file for handwired/pytest/has_template"}
def test_generate_c_onekey_pytest():
templ = qmk.keymap.generate_c('handwired/onekey/pytest', 'LAYOUT', [['KC_A']])
def test_generate_c_pytest_has_template():
templ = qmk.keymap.generate_c('handwired/pytest/has_template', 'LAYOUT', [['KC_A']])
assert templ == '#include QMK_KEYBOARD_H\nconst uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {\t[0] = LAYOUT(KC_A)};\n'
def test_generate_json_onekey_pytest():
templ = qmk.keymap.generate_json('default', 'handwired/onekey/pytest', 'LAYOUT', [['KC_A']])
assert templ == {"keyboard": "handwired/onekey/pytest", "documentation": "This file is a keymap.json file for handwired/onekey/pytest", "keymap": "default", "layout": "LAYOUT", "layers": [["KC_A"]]}
def test_generate_json_pytest_has_template():
templ = qmk.keymap.generate_json('default', 'handwired/pytest/has_template', 'LAYOUT', [['KC_A']])
assert templ == {"keyboard": "handwired/pytest/has_template", "documentation": "This file is a keymap.json file for handwired/pytest/has_template", "keymap": "default", "layout": "LAYOUT", "layers": [["KC_A"]]}
def test_parse_keymap_c():
parsed_keymap_c = qmk.keymap.parse_keymap_c('keyboards/handwired/onekey/keymaps/default/keymap.c')
parsed_keymap_c = qmk.keymap.parse_keymap_c('keyboards/handwired/pytest/basic/keymaps/default/keymap.c')
assert parsed_keymap_c == {'layers': [{'name': '0', 'layout': 'LAYOUT_ortho_1x1', 'keycodes': ['KC_A']}]}

View File

@@ -4,9 +4,9 @@ from pathlib import Path
import qmk.path
def test_keymap_onekey_pytest():
path = qmk.path.keymap('handwired/onekey/pytest')
assert path.samefile('keyboards/handwired/onekey/keymaps')
def test_keymap_pytest_basic():
path = qmk.path.keymap('handwired/pytest/basic')
assert path.samefile('keyboards/handwired/pytest/basic/keymaps')
def test_normpath():