From a25dd58bc56b0c4010673723ac44eaff914979bb Mon Sep 17 00:00:00 2001 From: skullydazed Date: Mon, 15 Jul 2019 12:14:27 -0700 Subject: QMK CLI and JSON keymap support (#6176) * Script to generate keymap.c from JSON file. * Support for keymap.json * Add a warning about the keymap.c getting overwritten. * Fix keymap generating * Install the python deps * Flesh out more of the python environment * Remove defunct json2keymap * Style everything with yapf * Polish up python support * Hide json keymap.c into the .build dir * Polish up qmk-compile-json * Make milc work with positional arguments * Fix a couple small things * Fix some errors and make the CLI more understandable * Make the qmk wrapper more robust * Add basic QMK Doctor * Clean up docstrings and flesh them out as needed * remove unused compile_firmware() function --- lib/python/milc.py | 716 +++++++++++++++++++++++++++++++++ lib/python/qmk/__init__.py | 0 lib/python/qmk/cli/compile/__init__.py | 0 lib/python/qmk/cli/compile/json.py | 44 ++ lib/python/qmk/cli/doctor.py | 47 +++ lib/python/qmk/cli/hello.py | 13 + lib/python/qmk/cli/json/__init__.py | 0 lib/python/qmk/cli/json/keymap.py | 54 +++ lib/python/qmk/errors.py | 6 + lib/python/qmk/keymap.py | 100 +++++ lib/python/qmk/path.py | 32 ++ 11 files changed, 1012 insertions(+) create mode 100644 lib/python/milc.py create mode 100644 lib/python/qmk/__init__.py create mode 100644 lib/python/qmk/cli/compile/__init__.py create mode 100755 lib/python/qmk/cli/compile/json.py create mode 100755 lib/python/qmk/cli/doctor.py create mode 100755 lib/python/qmk/cli/hello.py create mode 100644 lib/python/qmk/cli/json/__init__.py create mode 100755 lib/python/qmk/cli/json/keymap.py create mode 100644 lib/python/qmk/errors.py create mode 100644 lib/python/qmk/keymap.py create mode 100644 lib/python/qmk/path.py (limited to 'lib/python') diff --git a/lib/python/milc.py b/lib/python/milc.py new file mode 100644 index 0000000000..6e82edf8b1 --- /dev/null +++ b/lib/python/milc.py @@ -0,0 +1,716 @@ +#!/usr/bin/env python3 +# coding=utf-8 +"""MILC - A CLI Framework + +PYTHON_ARGCOMPLETE_OK + +MILC is an opinionated framework for writing CLI apps. It optimizes for the +most common unix tool pattern- small tools that are run from the command +line but generally do not feature any user interaction while they run. + +For more details see the MILC documentation: + + +""" +from __future__ import division, print_function, unicode_literals +import argparse +import logging +import os +import re +import sys +from decimal import Decimal +from tempfile import NamedTemporaryFile +from time import sleep + +try: + from ConfigParser import RawConfigParser +except ImportError: + from configparser import RawConfigParser + +try: + import thread + import threading +except ImportError: + thread = None + +import argcomplete +import colorama + +# Log Level Representations +EMOJI_LOGLEVELS = { + 'CRITICAL': '{bg_red}{fg_white}¬_¬{style_reset_all}', + 'ERROR': '{fg_red}☒{style_reset_all}', + 'WARNING': '{fg_yellow}⚠{style_reset_all}', + 'INFO': '{fg_blue}ℹ{style_reset_all}', + 'DEBUG': '{fg_cyan}☐{style_reset_all}', + 'NOTSET': '{style_reset_all}¯\\_(o_o)_/¯' +} +EMOJI_LOGLEVELS['FATAL'] = EMOJI_LOGLEVELS['CRITICAL'] +EMOJI_LOGLEVELS['WARN'] = EMOJI_LOGLEVELS['WARNING'] + +# ANSI Color setup +# Regex was gratefully borrowed from kfir on stackoverflow: +# https://stackoverflow.com/a/45448194 +ansi_regex = r'\x1b(' \ + r'(\[\??\d+[hl])|' \ + r'([=<>a-kzNM78])|' \ + r'([\(\)][a-b0-2])|' \ + r'(\[\d{0,2}[ma-dgkjqi])|' \ + r'(\[\d+;\d+[hfy]?)|' \ + r'(\[;?[hf])|' \ + r'(#[3-68])|' \ + r'([01356]n)|' \ + r'(O[mlnp-z]?)|' \ + r'(/Z)|' \ + r'(\d+)|' \ + r'(\[\?\d;\d0c)|' \ + r'(\d;\dR))' +ansi_escape = re.compile(ansi_regex, flags=re.IGNORECASE) +ansi_styles = ( + ('fg', colorama.ansi.AnsiFore()), + ('bg', colorama.ansi.AnsiBack()), + ('style', colorama.ansi.AnsiStyle()), +) +ansi_colors = {} + +for prefix, obj in ansi_styles: + for color in [x for x in obj.__dict__ if not x.startswith('_')]: + ansi_colors[prefix + '_' + color.lower()] = getattr(obj, color) + + +def format_ansi(text): + """Return a copy of text with certain strings replaced with ansi. + """ + # Avoid .format() so we don't have to worry about the log content + for color in ansi_colors: + text = text.replace('{%s}' % color, ansi_colors[color]) + return text + ansi_colors['style_reset_all'] + + +class ANSIFormatter(logging.Formatter): + """A log formatter that inserts ANSI color. + """ + + def format(self, record): + msg = super(ANSIFormatter, self).format(record) + return format_ansi(msg) + + +class ANSIEmojiLoglevelFormatter(ANSIFormatter): + """A log formatter that makes the loglevel an emoji. + """ + + def format(self, record): + record.levelname = EMOJI_LOGLEVELS[record.levelname].format(**ansi_colors) + return super(ANSIEmojiLoglevelFormatter, self).format(record) + + +class ANSIStrippingFormatter(ANSIFormatter): + """A log formatter that strips ANSI. + """ + + def format(self, record): + msg = super(ANSIStrippingFormatter, self).format(record) + return ansi_escape.sub('', msg) + + +class Configuration(object): + """Represents the running configuration. + + This class never raises IndexError, instead it will return None if a + section or option does not yet exist. + """ + + def __contains__(self, key): + return self._config.__contains__(key) + + def __iter__(self): + return self._config.__iter__() + + def __len__(self): + return self._config.__len__() + + def __repr__(self): + return self._config.__repr__() + + def keys(self): + return self._config.keys() + + def items(self): + return self._config.items() + + def values(self): + return self._config.values() + + def __init__(self, *args, **kwargs): + self._config = {} + self.default_container = ConfigurationOption + + def __getitem__(self, key): + """Returns a config section, creating it if it doesn't exist yet. + """ + if key not in self._config: + self.__dict__[key] = self._config[key] = ConfigurationOption() + + return self._config[key] + + def __setitem__(self, key, value): + self.__dict__[key] = value + self._config[key] = value + + def __delitem__(self, key): + if key in self.__dict__ and key[0] != '_': + del self.__dict__[key] + del self._config[key] + + +class ConfigurationOption(Configuration): + def __init__(self, *args, **kwargs): + super(ConfigurationOption, self).__init__(*args, **kwargs) + self.default_container = dict + + def __getitem__(self, key): + """Returns a config section, creating it if it doesn't exist yet. + """ + if key not in self._config: + self.__dict__[key] = self._config[key] = None + + return self._config[key] + + +def handle_store_boolean(self, *args, **kwargs): + """Does the add_argument for action='store_boolean'. + """ + kwargs['add_dest'] = False + disabled_args = None + disabled_kwargs = kwargs.copy() + disabled_kwargs['action'] = 'store_false' + disabled_kwargs['help'] = 'Disable ' + kwargs['help'] + kwargs['action'] = 'store_true' + kwargs['help'] = 'Enable ' + kwargs['help'] + + for flag in args: + if flag[:2] == '--': + disabled_args = ('--no-' + flag[2:],) + break + + self.add_argument(*args, **kwargs) + self.add_argument(*disabled_args, **disabled_kwargs) + + return (args, kwargs, disabled_args, disabled_kwargs) + + +class SubparserWrapper(object): + """Wrap subparsers so we can populate the normal and the shadow parser. + """ + + def __init__(self, cli, submodule, subparser): + self.cli = cli + self.submodule = submodule + self.subparser = subparser + + for attr in dir(subparser): + if not hasattr(self, attr): + setattr(self, attr, getattr(subparser, attr)) + + def completer(self, completer): + """Add an arpcomplete completer to this subcommand. + """ + self.subparser.completer = completer + + def add_argument(self, *args, **kwargs): + if kwargs.get('add_dest', True): + kwargs['dest'] = self.submodule + '_' + self.cli.get_argument_name(*args, **kwargs) + if 'add_dest' in kwargs: + del kwargs['add_dest'] + + if 'action' in kwargs and kwargs['action'] == 'store_boolean': + return handle_store_boolean(self, *args, **kwargs) + + self.cli.acquire_lock() + self.subparser.add_argument(*args, **kwargs) + + if 'default' in kwargs: + del kwargs['default'] + if 'action' in kwargs and kwargs['action'] == 'store_false': + kwargs['action'] == 'store_true' + self.cli.subcommands_default[self.submodule].add_argument(*args, **kwargs) + self.cli.release_lock() + + +class MILC(object): + """MILC - An Opinionated Batteries Included Framework + """ + + def __init__(self): + """Initialize the MILC object. + """ + # Setup a lock for thread safety + self._lock = threading.RLock() if thread else None + + # Define some basic info + self.acquire_lock() + self._description = None + self._entrypoint = None + self._inside_context_manager = False + self.ansi = ansi_colors + self.config = Configuration() + self.config_file = None + self.prog_name = sys.argv[0][:-3] if sys.argv[0].endswith('.py') else sys.argv[0] + self.version = os.environ.get('QMK_VERSION', 'unknown') + self.release_lock() + + # Initialize all the things + self.initialize_argparse() + self.initialize_logging() + + @property + def description(self): + return self._description + + @description.setter + def description(self, value): + self._description = self._arg_parser.description = self._arg_defaults.description = value + + def echo(self, text, *args, **kwargs): + """Print colorized text to stdout, as long as stdout is a tty. + + ANSI color strings (such as {fg-blue}) will be converted into ANSI + escape sequences, and the ANSI reset sequence will be added to all + strings. + + If *args or **kwargs are passed they will be used to %-format the strings. + """ + if args and kwargs: + raise RuntimeError('You can only specify *args or **kwargs, not both!') + + if sys.stdout.isatty(): + args = args or kwargs + text = format_ansi(text) + + print(text % args) + + def initialize_argparse(self): + """Prepare to process arguments from sys.argv. + """ + kwargs = { + 'fromfile_prefix_chars': '@', + 'conflict_handler': 'resolve', + } + + self.acquire_lock() + self.subcommands = {} + self.subcommands_default = {} + self._subparsers = None + self._subparsers_default = None + self.argwarn = argcomplete.warn + self.args = None + self._arg_defaults = argparse.ArgumentParser(**kwargs) + self._arg_parser = argparse.ArgumentParser(**kwargs) + self.set_defaults = self._arg_parser.set_defaults + self.print_usage = self._arg_parser.print_usage + self.print_help = self._arg_parser.print_help + self.release_lock() + + def completer(self, completer): + """Add an arpcomplete completer to this subcommand. + """ + self._arg_parser.completer = completer + + def add_argument(self, *args, **kwargs): + """Wrapper to add arguments to both the main and the shadow argparser. + """ + if kwargs.get('add_dest', True) and args[0][0] == '-': + kwargs['dest'] = 'general_' + self.get_argument_name(*args, **kwargs) + if 'add_dest' in kwargs: + del kwargs['add_dest'] + + if 'action' in kwargs and kwargs['action'] == 'store_boolean': + return handle_store_boolean(self, *args, **kwargs) + + self.acquire_lock() + self._arg_parser.add_argument(*args, **kwargs) + + # Populate the shadow parser + if 'default' in kwargs: + del kwargs['default'] + if 'action' in kwargs and kwargs['action'] == 'store_false': + kwargs['action'] == 'store_true' + self._arg_defaults.add_argument(*args, **kwargs) + self.release_lock() + + def initialize_logging(self): + """Prepare the defaults for the logging infrastructure. + """ + self.acquire_lock() + self.log_file = None + self.log_file_mode = 'a' + self.log_file_handler = None + self.log_print = True + self.log_print_to = sys.stderr + self.log_print_level = logging.INFO + self.log_file_level = logging.DEBUG + self.log_level = logging.INFO + self.log = logging.getLogger(self.__class__.__name__) + self.log.setLevel(logging.DEBUG) + logging.root.setLevel(logging.DEBUG) + self.release_lock() + + self.add_argument('-V', '--version', version=self.version, action='version', help='Display the version and exit') + self.add_argument('-v', '--verbose', action='store_true', help='Make the logging more verbose') + self.add_argument('--datetime-fmt', default='%Y-%m-%d %H:%M:%S', help='Format string for datetimes') + self.add_argument('--log-fmt', default='%(levelname)s %(message)s', help='Format string for printed log output') + self.add_argument('--log-file-fmt', default='[%(levelname)s] [%(asctime)s] [file:%(pathname)s] [line:%(lineno)d] %(message)s', help='Format string for log file.') + self.add_argument('--log-file', help='File to write log messages to') + self.add_argument('--color', action='store_boolean', default=True, help='color in output') + self.add_argument('-c', '--config-file', help='The config file to read and/or write') + self.add_argument('--save-config', action='store_true', help='Save the running configuration to the config file') + + def add_subparsers(self, title='Sub-commands', **kwargs): + if self._inside_context_manager: + raise RuntimeError('You must run this before the with statement!') + + self.acquire_lock() + self._subparsers_default = self._arg_defaults.add_subparsers(title=title, dest='subparsers', **kwargs) + self._subparsers = self._arg_parser.add_subparsers(title=title, dest='subparsers', **kwargs) + self.release_lock() + + def acquire_lock(self): + """Acquire the MILC lock for exclusive access to properties. + """ + if self._lock: + self._lock.acquire() + + def release_lock(self): + """Release the MILC lock. + """ + if self._lock: + self._lock.release() + + def find_config_file(self): + """Locate the config file. + """ + if self.config_file: + return self.config_file + + if self.args and self.args.general_config_file: + return self.args.general_config_file + + return os.path.abspath(os.path.expanduser('~/.%s.ini' % self.prog_name)) + + def get_argument_name(self, *args, **kwargs): + """Takes argparse arguments and returns the dest name. + """ + try: + return self._arg_parser._get_optional_kwargs(*args, **kwargs)['dest'] + except ValueError: + return self._arg_parser._get_positional_kwargs(*args, **kwargs)['dest'] + + def argument(self, *args, **kwargs): + """Decorator to call self.add_argument or self..add_argument. + """ + if self._inside_context_manager: + raise RuntimeError('You must run this before the with statement!') + + def argument_function(handler): + if handler is self._entrypoint: + self.add_argument(*args, **kwargs) + + elif handler.__name__ in self.subcommands: + self.subcommands[handler.__name__].add_argument(*args, **kwargs) + + else: + raise RuntimeError('Decorated function is not entrypoint or subcommand!') + + return handler + + return argument_function + + def arg_passed(self, arg): + """Returns True if arg was passed on the command line. + """ + return self.args_passed[arg] in (None, False) + + def parse_args(self): + """Parse the CLI args. + """ + if self.args: + self.log.debug('Warning: Arguments have already been parsed, ignoring duplicate attempt!') + return + + argcomplete.autocomplete(self._arg_parser) + + self.acquire_lock() + self.args = self._arg_parser.parse_args() + self.args_passed = self._arg_defaults.parse_args() + + if 'entrypoint' in self.args: + self._entrypoint = self.args.entrypoint + + if self.args.general_config_file: + self.config_file = self.args.general_config_file + + self.release_lock() + + def read_config(self): + """Parse the configuration file and determine the runtime configuration. + """ + self.acquire_lock() + self.config_file = self.find_config_file() + + if self.config_file and os.path.exists(self.config_file): + config = RawConfigParser(self.config) + config.read(self.config_file) + + # Iterate over the config file options and write them into self.config + for section in config.sections(): + for option in config.options(section): + value = config.get(section, option) + + # Coerce values into useful datatypes + if value.lower() in ['1', 'yes', 'true', 'on']: + value = True + elif value.lower() in ['0', 'no', 'false', 'none', 'off']: + value = False + elif value.replace('.', '').isdigit(): + if '.' in value: + value = Decimal(value) + else: + value = int(value) + + self.config[section][option] = value + + # Fold the CLI args into self.config + for argument in vars(self.args): + if argument in ('subparsers', 'entrypoint'): + continue + + if '_' not in argument: + continue + + section, option = argument.split('_', 1) + if hasattr(self.args_passed, argument): + self.config[section][option] = getattr(self.args, argument) + else: + if option not in self.config[section]: + self.config[section][option] = getattr(self.args, argument) + + self.release_lock() + + def save_config(self): + """Save the current configuration to the config file. + """ + self.log.debug("Saving config file to '%s'", self.config_file) + + if not self.config_file: + self.log.warning('%s.config_file file not set, not saving config!', self.__class__.__name__) + return + + self.acquire_lock() + + config = RawConfigParser() + for section_name, section in self.config._config.items(): + config.add_section(section_name) + for option_name, value in section.items(): + if section_name == 'general': + if option_name in ['save_config']: + continue + config.set(section_name, option_name, str(value)) + + with NamedTemporaryFile(mode='w', dir=os.path.dirname(self.config_file), delete=False) as tmpfile: + config.write(tmpfile) + + # Move the new config file into place atomically + if os.path.getsize(tmpfile.name) > 0: + os.rename(tmpfile.name, self.config_file) + else: + self.log.warning('Config file saving failed, not replacing %s with %s.', self.config_file, tmpfile.name) + + self.release_lock() + + def __call__(self): + """Execute the entrypoint function. + """ + if not self._inside_context_manager: + # If they didn't use the context manager use it ourselves + with self: + self.__call__() + return + + if not self._entrypoint: + raise RuntimeError('No entrypoint provided!') + + return self._entrypoint(self) + + def entrypoint(self, description): + """Set the entrypoint for when no subcommand is provided. + """ + if self._inside_context_manager: + raise RuntimeError('You must run this before cli()!') + + self.acquire_lock() + self.description = description + self.release_lock() + + def entrypoint_func(handler): + self.acquire_lock() + self._entrypoint = handler + self.release_lock() + + return handler + + return entrypoint_func + + def add_subcommand(self, handler, description, name=None, **kwargs): + """Register a subcommand. + + If name is not provided we use `handler.__name__`. + """ + if self._inside_context_manager: + raise RuntimeError('You must run this before the with statement!') + + if self._subparsers is None: + self.add_subparsers() + + if not name: + name = handler.__name__ + + self.acquire_lock() + kwargs['help'] = description + self.subcommands_default[name] = self._subparsers_default.add_parser(name, **kwargs) + self.subcommands[name] = SubparserWrapper(self, name, self._subparsers.add_parser(name, **kwargs)) + self.subcommands[name].set_defaults(entrypoint=handler) + + if name not in self.__dict__: + self.__dict__[name] = self.subcommands[name] + else: + self.log.debug("Could not add subcommand '%s' to attributes, key already exists!", name) + + self.release_lock() + + return handler + + def subcommand(self, description, **kwargs): + """Decorator to register a subcommand. + """ + + def subcommand_function(handler): + return self.add_subcommand(handler, description, **kwargs) + + return subcommand_function + + def setup_logging(self): + """Called by __enter__() to setup the logging configuration. + """ + if len(logging.root.handlers) != 0: + # This is not a design decision. This is what I'm doing for now until I can examine and think about this situation in more detail. + raise RuntimeError('MILC should be the only system installing root log handlers!') + + self.acquire_lock() + + if self.config['general']['verbose']: + self.log_print_level = logging.DEBUG + + self.log_file = self.config['general']['log_file'] or self.log_file + self.log_file_format = self.config['general']['log_file_fmt'] + self.log_file_format = ANSIStrippingFormatter(self.config['general']['log_file_fmt'], self.config['general']['datetime_fmt']) + self.log_format = self.config['general']['log_fmt'] + + if self.config.general.color: + self.log_format = ANSIEmojiLoglevelFormatter(self.args.general_log_fmt, self.config.general.datetime_fmt) + else: + self.log_format = ANSIStrippingFormatter(self.args.general_log_fmt, self.config.general.datetime_fmt) + + if self.log_file: + self.log_file_handler = logging.FileHandler(self.log_file, self.log_file_mode) + self.log_file_handler.setLevel(self.log_file_level) + self.log_file_handler.setFormatter(self.log_file_format) + logging.root.addHandler(self.log_file_handler) + + if self.log_print: + self.log_print_handler = logging.StreamHandler(self.log_print_to) + self.log_print_handler.setLevel(self.log_print_level) + self.log_print_handler.setFormatter(self.log_format) + logging.root.addHandler(self.log_print_handler) + + self.release_lock() + + def __enter__(self): + if self._inside_context_manager: + self.log.debug('Warning: context manager was entered again. This usually means that self.__call__() was called before the with statement. You probably do not want to do that.') + return + + self.acquire_lock() + self._inside_context_manager = True + self.release_lock() + + colorama.init() + self.parse_args() + self.read_config() + self.setup_logging() + + if self.config.general.save_config: + self.save_config() + + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.acquire_lock() + self._inside_context_manager = False + self.release_lock() + + if exc_type is not None and not isinstance(SystemExit(), exc_type): + print(exc_type) + logging.exception(exc_val) + exit(255) + + +cli = MILC() + +if __name__ == '__main__': + + @cli.argument('-c', '--comma', help='comma in output', default=True, action='store_boolean') + @cli.entrypoint('My useful CLI tool with subcommands.') + def main(cli): + comma = ',' if cli.config.general.comma else '' + cli.log.info('{bg_green}{fg_red}Hello%s World!', comma) + + @cli.argument('-n', '--name', help='Name to greet', default='World') + @cli.subcommand('Description of hello subcommand here.') + def hello(cli): + comma = ',' if cli.config.general.comma else '' + cli.log.info('{fg_blue}Hello%s %s!', comma, cli.config.hello.name) + + def goodbye(cli): + comma = ',' if cli.config.general.comma else '' + cli.log.info('{bg_red}Goodbye%s %s!', comma, cli.config.goodbye.name) + + @cli.argument('-n', '--name', help='Name to greet', default='World') + @cli.subcommand('Think a bit before greeting the user.') + def thinking(cli): + comma = ',' if cli.config.general.comma else '' + spinner = cli.spinner(text='Just a moment...', spinner='earth') + spinner.start() + sleep(2) + spinner.stop() + + with cli.spinner(text='Almost there!', spinner='moon'): + sleep(2) + + cli.log.info('{fg_cyan}Hello%s %s!', comma, cli.config.thinking.name) + + @cli.subcommand('Show off our ANSI colors.') + def pride(cli): + cli.echo('{bg_red} ') + cli.echo('{bg_lightred_ex} ') + cli.echo('{bg_lightyellow_ex} ') + cli.echo('{bg_green} ') + cli.echo('{bg_blue} ') + cli.echo('{bg_magenta} ') + + # You can register subcommands using decorators as seen above, or using functions like like this: + cli.add_subcommand(goodbye, 'This will show up in --help output.') + cli.goodbye.add_argument('-n', '--name', help='Name to bid farewell to', default='World') + + cli() # Automatically picks between main(), hello() and goodbye() + print(sorted(ansi_colors.keys())) diff --git a/lib/python/qmk/__init__.py b/lib/python/qmk/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/python/qmk/cli/compile/__init__.py b/lib/python/qmk/cli/compile/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/python/qmk/cli/compile/json.py b/lib/python/qmk/cli/compile/json.py new file mode 100755 index 0000000000..89c16b2063 --- /dev/null +++ b/lib/python/qmk/cli/compile/json.py @@ -0,0 +1,44 @@ +"""Create a keymap directory from a configurator export. +""" +import json +import os +import sys +import subprocess + +from milc import cli + +import qmk.keymap +import qmk.path + + +@cli.argument('filename', help='Configurator JSON export') +@cli.entrypoint('Compile a QMK Configurator export.') +def main(cli): + """Compile a QMK Configurator export. + + This command creates a new keymap from a configurator export, overwriting an existing keymap if one exists. + + FIXME(skullydazed): add code to check and warn if the keymap already exists + """ + # Error checking + if cli.args.filename == ('-'): + cli.log.error('Reading from STDIN is not (yet) supported.') + exit(1) + if not os.path.exists(qmk.path.normpath(cli.args.filename)): + cli.log.error('JSON file does not exist!') + exit(1) + + # Parse the configurator json + with open(qmk.path.normpath(cli.args.filename), 'r') as fd: + user_keymap = json.load(fd) + + # Generate the keymap + keymap_path = qmk.path.keymap(user_keymap['keyboard']) + cli.log.info('Creating {fg_cyan}%s{style_reset_all} keymap in {fg_cyan}%s', user_keymap['keymap'], keymap_path) + qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers']) + cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) + + # Compile the keymap + command = ['make', ':'.join((user_keymap['keyboard'], user_keymap['keymap']))] + cli.log.info('Compiling keymap with {fg_cyan}%s\n\n', ' '.join(command)) + subprocess.run(command) diff --git a/lib/python/qmk/cli/doctor.py b/lib/python/qmk/cli/doctor.py new file mode 100755 index 0000000000..9ce765a4b5 --- /dev/null +++ b/lib/python/qmk/cli/doctor.py @@ -0,0 +1,47 @@ +"""QMK Python Doctor + +Check up for QMK environment. +""" +import shutil +import platform +import os + +from milc import cli + + +@cli.entrypoint('Basic QMK environment checks') +def main(cli): + """Basic QMK environment checks. + + This is currently very simple, it just checks that all the expected binaries are on your system. + + TODO(unclaimed): + * [ ] Run the binaries to make sure they work + * [ ] Compile a trivial program with each compiler + * [ ] Check for udev entries on linux + """ + + binaries = ['dfu-programmer', 'avrdude', 'dfu-util', 'avr-gcc', 'arm-none-eabi-gcc'] + + cli.log.info('QMK Doctor is Checking your environment') + + ok = True + for binary in binaries: + res = shutil.which(binary) + if res is None: + cli.log.error('{fg_red}QMK can\'t find ' + binary + ' in your path') + ok = False + + OS = platform.system() + if OS == "Darwin": + cli.log.info("Detected {fg_cyan}macOS") + elif OS == "Linux": + cli.log.info("Detected {fg_cyan}linux") + test = 'systemctl list-unit-files | grep enabled | grep -i ModemManager' + if os.system(test) == 0: + cli.log.warn("{bg_yellow}Detected modem manager. Please disable it if you are using Pro Micros") + else: + cli.log.info("Assuming {fg_cyan}Windows") + + if ok: + cli.log.info('{fg_green}QMK is ready to go') diff --git a/lib/python/qmk/cli/hello.py b/lib/python/qmk/cli/hello.py new file mode 100755 index 0000000000..bc0cb6de18 --- /dev/null +++ b/lib/python/qmk/cli/hello.py @@ -0,0 +1,13 @@ +"""QMK Python Hello World + +This is an example QMK CLI script. +""" +from milc import cli + + +@cli.argument('-n', '--name', default='World', help='Name to greet.') +@cli.entrypoint('QMK Hello World.') +def main(cli): + """Log a friendly greeting. + """ + cli.log.info('Hello, %s!', cli.config.general.name) diff --git a/lib/python/qmk/cli/json/__init__.py b/lib/python/qmk/cli/json/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/python/qmk/cli/json/keymap.py b/lib/python/qmk/cli/json/keymap.py new file mode 100755 index 0000000000..35fc8f9c0e --- /dev/null +++ b/lib/python/qmk/cli/json/keymap.py @@ -0,0 +1,54 @@ +"""Generate a keymap.c from a configurator export. +""" +import json +import os +import sys + +from milc import cli + +import qmk.keymap + + +@cli.argument('-o', '--output', help='File to write to') +@cli.argument('filename', help='Configurator JSON file') +@cli.entrypoint('Create a keymap.c from a QMK Configurator export.') +def main(cli): + """Generate a keymap.c from a configurator export. + + This command uses the `qmk.keymap` module to generate a keymap.c from a configurator export. The generated keymap is written to stdout, or to a file if -o is provided. + """ + # Error checking + if cli.args.filename == ('-'): + cli.log.error('Reading from STDIN is not (yet) supported.') + cli.print_usage() + exit(1) + if not os.path.exists(qmk.path.normpath(cli.args.filename)): + cli.log.error('JSON file does not exist!') + cli.print_usage() + exit(1) + + # Environment processing + if cli.args.output == ('-'): + cli.args.output = None + + # Parse the configurator json + with open(qmk.path.normpath(cli.args.filename), 'r') as fd: + user_keymap = json.load(fd) + + # Generate the keymap + keymap_c = qmk.keymap.generate(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers']) + + if cli.args.output: + output_dir = os.path.dirname(cli.args.output) + + if not os.path.exists(output_dir): + os.makedirs(output_dir) + + output_file = qmk.path.normpath(cli.args.output) + with open(output_file, 'w') as keymap_fd: + keymap_fd.write(keymap_c) + + cli.log.info('Wrote keymap to %s.', cli.args.output) + + else: + print(keymap_c) diff --git a/lib/python/qmk/errors.py b/lib/python/qmk/errors.py new file mode 100644 index 0000000000..f9bf5b9af9 --- /dev/null +++ b/lib/python/qmk/errors.py @@ -0,0 +1,6 @@ +class NoSuchKeyboardError(Exception): + """Raised when we can't find a keyboard/keymap directory. + """ + + def __init__(self, message): + self.message = message diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py new file mode 100644 index 0000000000..6eccab788a --- /dev/null +++ b/lib/python/qmk/keymap.py @@ -0,0 +1,100 @@ +"""Functions that help you work with QMK keymaps. +""" +import json +import logging +import os +from traceback import format_exc + +import qmk.path +from qmk.errors import NoSuchKeyboardError + +# The `keymap.c` template to use when a keyboard doesn't have its own +DEFAULT_KEYMAP_C = """#include QMK_KEYBOARD_H + +/* THIS FILE WAS GENERATED! + * + * This file was generated by qmk-compile-json. You may or may not want to + * edit it directly. + */ + +const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { +__KEYMAP_GOES_HERE__ +}; +""" + + +def template(keyboard): + """Returns the `keymap.c` template for a keyboard. + + If a template exists in `keyboards//templates/keymap.c` that + text will be used instead of `DEFAULT_KEYMAP_C`. + + Args: + keyboard + The keyboard to return a template for. + """ + template_name = 'keyboards/%s/templates/keymap.c' % keyboard + + if os.path.exists(template_name): + with open(template_name, 'r') as fd: + return fd.read() + + return DEFAULT_KEYMAP_C + + +def generate(keyboard, layout, layers): + """Returns a keymap.c for the specified keyboard, layout, and layers. + + Args: + keyboard + The name of the keyboard + + layout + The LAYOUT macro this keymap uses. + + layers + An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode. + """ + layer_txt = [] + for layer_num, layer in enumerate(layers): + if layer_num != 0: + layer_txt[-1] = layer_txt[-1] + ',' + layer_keys = ', '.join(layer) + layer_txt.append('\t[%s] = %s(%s)' % (layer_num, layout, layer_keys)) + + keymap = '\n'.join(layer_txt) + keymap_c = template(keyboard, keymap) + + return keymap_c.replace('__KEYMAP_GOES_HERE__', keymap) + + +def write(keyboard, keymap, layout, layers): + """Generate the `keymap.c` and write it to disk. + + Returns the filename written to. + + Args: + keyboard + The name of the keyboard + + keymap + The name of the keymap + + layout + The LAYOUT macro this keymap uses. + + layers + An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode. + """ + keymap_c = generate(keyboard, layout, layers) + keymap_path = qmk.path.keymap(keyboard) + keymap_dir = os.path.join(keymap_path, keymap) + keymap_file = os.path.join(keymap_dir, 'keymap.c') + + if not os.path.exists(keymap_dir): + os.makedirs(keymap_dir) + + with open(keymap_file, 'w') as keymap_fd: + keymap_fd.write(keymap_c) + + return keymap_file diff --git a/lib/python/qmk/path.py b/lib/python/qmk/path.py new file mode 100644 index 0000000000..f2a8346a51 --- /dev/null +++ b/lib/python/qmk/path.py @@ -0,0 +1,32 @@ +"""Functions that help us work with files and folders. +""" +import os + + +def keymap(keyboard): + """Locate the correct directory for storing a keymap. + + Args: + keyboard + The name of the keyboard. Example: clueboard/66/rev3 + """ + for directory in ['.', '..', '../..', '../../..', '../../../..', '../../../../..']: + basepath = os.path.normpath(os.path.join('keyboards', keyboard, directory, 'keymaps')) + + if os.path.exists(basepath): + return basepath + + logging.error('Could not find keymaps directory!') + raise NoSuchKeyboardError('Could not find keymaps directory for: %s' % keyboard) + + +def normpath(path): + """Returns the fully resolved absolute path to a file. + + This function will return the absolute path to a file as seen from the + directory the script was called from. + """ + if path and path[0] == '/': + return os.path.normpath(path) + + return os.path.normpath(os.path.join(os.environ['ORIG_CWD'], path)) -- cgit v1.2.3 From 7d557a0514e2cef42a3d460f6cc78771b5df0a30 Mon Sep 17 00:00:00 2001 From: skullydazed Date: Mon, 15 Jul 2019 15:12:35 -0700 Subject: Fix compiling json files. (#6340) --- lib/python/qmk/cli/json/keymap.py | 12 ++++++------ lib/python/qmk/keymap.py | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'lib/python') diff --git a/lib/python/qmk/cli/json/keymap.py b/lib/python/qmk/cli/json/keymap.py index 35fc8f9c0e..e2d0b58093 100755 --- a/lib/python/qmk/cli/json/keymap.py +++ b/lib/python/qmk/cli/json/keymap.py @@ -28,8 +28,8 @@ def main(cli): exit(1) # Environment processing - if cli.args.output == ('-'): - cli.args.output = None + if cli.config.general.output == ('-'): + cli.config.general.output = None # Parse the configurator json with open(qmk.path.normpath(cli.args.filename), 'r') as fd: @@ -38,17 +38,17 @@ def main(cli): # Generate the keymap keymap_c = qmk.keymap.generate(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers']) - if cli.args.output: - output_dir = os.path.dirname(cli.args.output) + if cli.config.general.output: + output_dir = os.path.dirname(cli.config.general.output) if not os.path.exists(output_dir): os.makedirs(output_dir) - output_file = qmk.path.normpath(cli.args.output) + output_file = qmk.path.normpath(cli.config.general.output) with open(output_file, 'w') as keymap_fd: keymap_fd.write(keymap_c) - cli.log.info('Wrote keymap to %s.', cli.args.output) + cli.log.info('Wrote keymap to %s.', cli.config.general.output) else: print(keymap_c) diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py index 6eccab788a..396b53a6f5 100644 --- a/lib/python/qmk/keymap.py +++ b/lib/python/qmk/keymap.py @@ -63,7 +63,7 @@ def generate(keyboard, layout, layers): layer_txt.append('\t[%s] = %s(%s)' % (layer_num, layout, layer_keys)) keymap = '\n'.join(layer_txt) - keymap_c = template(keyboard, keymap) + keymap_c = template(keyboard) return keymap_c.replace('__KEYMAP_GOES_HERE__', keymap) -- cgit v1.2.3 From f22c5c17b6fe069bec1241262a1c27eb89d3d3af Mon Sep 17 00:00:00 2001 From: skullydazed Date: Sun, 25 Aug 2019 11:58:24 -0700 Subject: Refactor `qmk compile-json` to `qmk compile` (#6592) --- lib/python/qmk/cli/compile.py | 53 ++++++++++++++++++++++++++++++++++ lib/python/qmk/cli/compile/__init__.py | 0 lib/python/qmk/cli/compile/json.py | 44 ---------------------------- 3 files changed, 53 insertions(+), 44 deletions(-) create mode 100755 lib/python/qmk/cli/compile.py delete mode 100644 lib/python/qmk/cli/compile/__init__.py delete mode 100755 lib/python/qmk/cli/compile/json.py (limited to 'lib/python') diff --git a/lib/python/qmk/cli/compile.py b/lib/python/qmk/cli/compile.py new file mode 100755 index 0000000000..7e14ad8fbf --- /dev/null +++ b/lib/python/qmk/cli/compile.py @@ -0,0 +1,53 @@ +"""Compile a QMK Firmware. + +You can compile a keymap already in the repo or using a QMK Configurator export. +""" +import json +import os +import sys +import subprocess +from argparse import FileType + +from milc import cli + +import qmk.keymap +import qmk.path + + +@cli.argument('filename', nargs='?', 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.entrypoint('Compile a QMK Firmware.') +def main(cli): + """Compile a QMK Firmware. + + If a Configurator export is supplied this command will create a new keymap, overwriting an existing keymap if one exists. + + FIXME(skullydazed): add code to check and warn if the keymap already exists + + If --keyboard and --keymap are provided this command will build a firmware based on that. + + """ + if cli.args.filename: + # Parse the configurator json + user_keymap = json.load(cli.args.filename) + + # Generate the keymap + keymap_path = qmk.path.keymap(user_keymap['keyboard']) + cli.log.info('Creating {fg_cyan}%s{style_reset_all} keymap in {fg_cyan}%s', user_keymap['keymap'], keymap_path) + qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers']) + cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) + + # Compile the keymap + command = ['make', ':'.join((user_keymap['keyboard'], user_keymap['keymap']))] + + elif cli.config.general.keyboard and cli.config.general.keymap: + # Generate the make command for a specific keyboard/keymap. + command = ['make', ':'.join((cli.config.general.keyboard, cli.config.general.keymap))] + + else: + cli.log.error('You must supply a configurator export or both `--keyboard` and `--keymap`.') + return False + + cli.log.info('Compiling keymap with {fg_cyan}%s\n\n', ' '.join(command)) + subprocess.run(command) diff --git a/lib/python/qmk/cli/compile/__init__.py b/lib/python/qmk/cli/compile/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/lib/python/qmk/cli/compile/json.py b/lib/python/qmk/cli/compile/json.py deleted file mode 100755 index 89c16b2063..0000000000 --- a/lib/python/qmk/cli/compile/json.py +++ /dev/null @@ -1,44 +0,0 @@ -"""Create a keymap directory from a configurator export. -""" -import json -import os -import sys -import subprocess - -from milc import cli - -import qmk.keymap -import qmk.path - - -@cli.argument('filename', help='Configurator JSON export') -@cli.entrypoint('Compile a QMK Configurator export.') -def main(cli): - """Compile a QMK Configurator export. - - This command creates a new keymap from a configurator export, overwriting an existing keymap if one exists. - - FIXME(skullydazed): add code to check and warn if the keymap already exists - """ - # Error checking - if cli.args.filename == ('-'): - cli.log.error('Reading from STDIN is not (yet) supported.') - exit(1) - if not os.path.exists(qmk.path.normpath(cli.args.filename)): - cli.log.error('JSON file does not exist!') - exit(1) - - # Parse the configurator json - with open(qmk.path.normpath(cli.args.filename), 'r') as fd: - user_keymap = json.load(fd) - - # Generate the keymap - keymap_path = qmk.path.keymap(user_keymap['keyboard']) - cli.log.info('Creating {fg_cyan}%s{style_reset_all} keymap in {fg_cyan}%s', user_keymap['keymap'], keymap_path) - qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers']) - cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) - - # Compile the keymap - command = ['make', ':'.join((user_keymap['keyboard'], user_keymap['keymap']))] - cli.log.info('Compiling keymap with {fg_cyan}%s\n\n', ' '.join(command)) - subprocess.run(command) -- cgit v1.2.3 From 95477749629407e2a9e33c6ccf26ecc8b24ab07a Mon Sep 17 00:00:00 2001 From: skullY Date: Thu, 22 Aug 2019 10:18:52 -0700 Subject: CLI command to format C code --- lib/python/qmk/cli/cformat.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 lib/python/qmk/cli/cformat.py (limited to 'lib/python') diff --git a/lib/python/qmk/cli/cformat.py b/lib/python/qmk/cli/cformat.py new file mode 100644 index 0000000000..f7020f4c5f --- /dev/null +++ b/lib/python/qmk/cli/cformat.py @@ -0,0 +1,27 @@ +"""Format C code according to QMK's style. +""" +import os +import subprocess + +from milc import cli + + +@cli.entrypoint("Format C code according to QMK's style.") +def main(cli): + """Format C code according to QMK's style. + """ + clang_format = ['clang-format', '-i'] + code_files = [] + for dir in ['drivers', 'quantum', 'tests', 'tmk_core']: + for dirpath, dirnames, filenames in os.walk(dir): + if 'tmk_core/protocol/usb_hid' in dirpath: + continue + for name in filenames: + if name.endswith('.c') or name.endswith('.h') or name.endswith('.cpp'): + code_files.append(os.path.join(dirpath, name)) + + try: + subprocess.run(clang_format + code_files, check=True) + cli.log.info('Successfully formatted the C code.') + except subprocess.CalledProcessError: + cli.log.error('Error formatting C code!') -- cgit v1.2.3 From 1784d1bfac44a63bf343b6e2098f0cba81d58cb2 Mon Sep 17 00:00:00 2001 From: skullY Date: Thu, 22 Aug 2019 13:30:50 -0700 Subject: Add support for passing files at the command line --- lib/python/qmk/cli/cformat.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'lib/python') diff --git a/lib/python/qmk/cli/cformat.py b/lib/python/qmk/cli/cformat.py index f7020f4c5f..0c209247d9 100644 --- a/lib/python/qmk/cli/cformat.py +++ b/lib/python/qmk/cli/cformat.py @@ -6,22 +6,24 @@ import subprocess from milc import cli +@cli.argument('files', nargs='*', help='Filename(s) to format.') @cli.entrypoint("Format C code according to QMK's style.") def main(cli): """Format C code according to QMK's style. """ clang_format = ['clang-format', '-i'] - code_files = [] - for dir in ['drivers', 'quantum', 'tests', 'tmk_core']: - for dirpath, dirnames, filenames in os.walk(dir): - if 'tmk_core/protocol/usb_hid' in dirpath: - continue - for name in filenames: - if name.endswith('.c') or name.endswith('.h') or name.endswith('.cpp'): - code_files.append(os.path.join(dirpath, name)) + if not cli.args.files: + for dir in ['drivers', 'quantum', 'tests', 'tmk_core']: + for dirpath, dirnames, filenames in os.walk(dir): + if 'tmk_core/protocol/usb_hid' in dirpath: + continue + for name in filenames: + if name.endswith('.c') or name.endswith('.h') or name.endswith('.cpp'): + cli.args.files.append(os.path.join(dirpath, name)) try: - subprocess.run(clang_format + code_files, check=True) + subprocess.run(clang_format + cli.args.files, check=True) cli.log.info('Successfully formatted the C code.') except subprocess.CalledProcessError: cli.log.error('Error formatting C code!') + return False -- cgit v1.2.3 From 2d688ad14e727bd3437f26a53bd3d92079e5b3c2 Mon Sep 17 00:00:00 2001 From: skullY Date: Thu, 22 Aug 2019 13:33:34 -0700 Subject: readability enhancements --- lib/python/qmk/cli/cformat.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'lib/python') diff --git a/lib/python/qmk/cli/cformat.py b/lib/python/qmk/cli/cformat.py index 0c209247d9..91e650368b 100644 --- a/lib/python/qmk/cli/cformat.py +++ b/lib/python/qmk/cli/cformat.py @@ -12,18 +12,23 @@ def main(cli): """Format C code according to QMK's style. """ clang_format = ['clang-format', '-i'] + + # Find the list of files to format if not cli.args.files: for dir in ['drivers', 'quantum', 'tests', 'tmk_core']: for dirpath, dirnames, filenames in os.walk(dir): if 'tmk_core/protocol/usb_hid' in dirpath: continue + for name in filenames: if name.endswith('.c') or name.endswith('.h') or name.endswith('.cpp'): cli.args.files.append(os.path.join(dirpath, name)) + # Run clang-format on the files we've found try: subprocess.run(clang_format + cli.args.files, check=True) cli.log.info('Successfully formatted the C code.') + except subprocess.CalledProcessError: cli.log.error('Error formatting C code!') return False -- cgit v1.2.3 From 5b7a5b2a7629fbb667d23a55836dce3c6c46a203 Mon Sep 17 00:00:00 2001 From: skullY Date: Wed, 21 Aug 2019 23:40:24 -0700 Subject: Setup a python test framework --- lib/python/milc.py | 3 +-- lib/python/qmk/cli/doctor.py | 30 ++++++++++++++++++++++-------- lib/python/qmk/cli/nose2.py | 18 ++++++++++++++++++ lib/python/qmk/tests/__init__.py | 0 lib/python/qmk/tests/attrdict.py | 8 ++++++++ lib/python/qmk/tests/onekey_export.json | 6 ++++++ lib/python/qmk/tests/test_qmk_errors.py | 7 +++++++ lib/python/qmk/tests/test_qmk_keymap.py | 18 ++++++++++++++++++ lib/python/qmk/tests/test_qmk_path.py | 12 ++++++++++++ 9 files changed, 92 insertions(+), 10 deletions(-) create mode 100644 lib/python/qmk/cli/nose2.py create mode 100644 lib/python/qmk/tests/__init__.py create mode 100644 lib/python/qmk/tests/attrdict.py create mode 100644 lib/python/qmk/tests/onekey_export.json create mode 100644 lib/python/qmk/tests/test_qmk_errors.py create mode 100644 lib/python/qmk/tests/test_qmk_keymap.py create mode 100644 lib/python/qmk/tests/test_qmk_path.py (limited to 'lib/python') diff --git a/lib/python/milc.py b/lib/python/milc.py index 6e82edf8b1..c62c1b166c 100644 --- a/lib/python/milc.py +++ b/lib/python/milc.py @@ -534,8 +534,7 @@ class MILC(object): if not self._inside_context_manager: # If they didn't use the context manager use it ourselves with self: - self.__call__() - return + return self.__call__() if not self._entrypoint: raise RuntimeError('No entrypoint provided!') diff --git a/lib/python/qmk/cli/doctor.py b/lib/python/qmk/cli/doctor.py index 9ce765a4b5..c5a144363c 100755 --- a/lib/python/qmk/cli/doctor.py +++ b/lib/python/qmk/cli/doctor.py @@ -2,9 +2,11 @@ Check up for QMK environment. """ -import shutil -import platform import os +import platform +import shutil +import subprocess +from glob import glob from milc import cli @@ -16,32 +18,44 @@ def main(cli): This is currently very simple, it just checks that all the expected binaries are on your system. TODO(unclaimed): - * [ ] Run the binaries to make sure they work * [ ] Compile a trivial program with each compiler * [ ] Check for udev entries on linux """ binaries = ['dfu-programmer', 'avrdude', 'dfu-util', 'avr-gcc', 'arm-none-eabi-gcc'] + binaries += glob('bin/qmk-*') - cli.log.info('QMK Doctor is Checking your environment') + cli.log.info('QMK Doctor is checking your environment') ok = True for binary in binaries: res = shutil.which(binary) if res is None: - cli.log.error('{fg_red}QMK can\'t find ' + binary + ' in your path') + cli.log.error("{fg_red}QMK can't find %s in your path", binary) ok = False + else: + try: + subprocess.run([binary, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5, check=True) + except subprocess.CalledProcessError: + cli.log.error("{fg_red}Can't run `%s --version`", binary) + ok = False OS = platform.system() if OS == "Darwin": cli.log.info("Detected {fg_cyan}macOS") elif OS == "Linux": cli.log.info("Detected {fg_cyan}linux") - test = 'systemctl list-unit-files | grep enabled | grep -i ModemManager' - if os.system(test) == 0: - cli.log.warn("{bg_yellow}Detected modem manager. Please disable it if you are using Pro Micros") + if shutil.which('systemctl'): + test = 'systemctl list-unit-files | grep enabled | grep -i ModemManager' + if os.system(test) == 0: + cli.log.warn("{bg_yellow}Detected modem manager. Please disable it if you are using Pro Micros") + else: + cli.log.warn("Can't find systemctl to check for ModemManager.") else: cli.log.info("Assuming {fg_cyan}Windows") if ok: cli.log.info('{fg_green}QMK is ready to go') + else: + cli.log.info('{fg_yellow}Problems detected, please fix these problems before proceeding.') + # FIXME(skullydazed): Link to a document about troubleshooting, or discord or something diff --git a/lib/python/qmk/cli/nose2.py b/lib/python/qmk/cli/nose2.py new file mode 100644 index 0000000000..c6c9c67b30 --- /dev/null +++ b/lib/python/qmk/cli/nose2.py @@ -0,0 +1,18 @@ +"""QMK Python Unit Tests + +QMK script to run unit and integration tests against our python code. +""" +from milc import cli + + +@cli.entrypoint('QMK Python Unit Tests') +def main(cli): + """Use nose2 to run unittests + """ + try: + import nose2 + except ImportError: + cli.log.error('Could not import nose2! Please install it with {fg_cyan}pip3 install nose2') + return False + + nose2.discover() diff --git a/lib/python/qmk/tests/__init__.py b/lib/python/qmk/tests/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/python/qmk/tests/attrdict.py b/lib/python/qmk/tests/attrdict.py new file mode 100644 index 0000000000..a2584b9233 --- /dev/null +++ b/lib/python/qmk/tests/attrdict.py @@ -0,0 +1,8 @@ +class AttrDict(dict): + """A dictionary that can be accessed by attributes. + + This should only be used to mock objects for unit testing. Please do not use this outside of qmk.tests. + """ + def __init__(self, *args, **kwargs): + super(AttrDict, self).__init__(*args, **kwargs) + self.__dict__ = self diff --git a/lib/python/qmk/tests/onekey_export.json b/lib/python/qmk/tests/onekey_export.json new file mode 100644 index 0000000000..95f0a980fe --- /dev/null +++ b/lib/python/qmk/tests/onekey_export.json @@ -0,0 +1,6 @@ +{ + "keyboard":"handwired/onekey/pytest", + "keymap":"pytest_unittest", + "layout":"LAYOUT", + "layers":[["KC_A"]] +} diff --git a/lib/python/qmk/tests/test_qmk_errors.py b/lib/python/qmk/tests/test_qmk_errors.py new file mode 100644 index 0000000000..3f6b567137 --- /dev/null +++ b/lib/python/qmk/tests/test_qmk_errors.py @@ -0,0 +1,7 @@ +from qmk.errors import NoSuchKeyboardError + +def test_NoSuchKeyboardError(): + try: + raise(NoSuchKeyboardError("test message")) + except NoSuchKeyboardError as e: + assert e.message == 'test message' diff --git a/lib/python/qmk/tests/test_qmk_keymap.py b/lib/python/qmk/tests/test_qmk_keymap.py new file mode 100644 index 0000000000..6a565ee900 --- /dev/null +++ b/lib/python/qmk/tests/test_qmk_keymap.py @@ -0,0 +1,18 @@ +import qmk.keymap + +def test_template_onekey_proton_c(): + templ = qmk.keymap.template('handwired/onekey/proton_c') + assert templ == qmk.keymap.DEFAULT_KEYMAP_C + + +def test_template_onekey_pytest(): + templ = qmk.keymap.template('handwired/onekey/pytest') + assert templ == 'const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {__KEYMAP_GOES_HERE__};\n' + + +def test_generate_onekey_pytest(): + templ = qmk.keymap.generate('handwired/onekey/pytest', 'LAYOUT', [['KC_A']]) + assert templ == 'const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { [0] = LAYOUT(KC_A)};\n' + + +# FIXME(skullydazed): Add a test for qmk.keymap.write that mocks up an FD. diff --git a/lib/python/qmk/tests/test_qmk_path.py b/lib/python/qmk/tests/test_qmk_path.py new file mode 100644 index 0000000000..23816be7ed --- /dev/null +++ b/lib/python/qmk/tests/test_qmk_path.py @@ -0,0 +1,12 @@ +import os + +import qmk.path + +def test_keymap_onekey_pytest(): + path = qmk.path.keymap('handwired/onekey/pytest') + assert path == 'keyboards/handwired/onekey/keymaps' + + +def test_normpath(): + path = qmk.path.normpath('lib/python') + assert path == os.environ['ORIG_CWD'] + '/lib/python' -- cgit v1.2.3 From c7eede2249d22dd4fabed83043dcf4cc7408cf27 Mon Sep 17 00:00:00 2001 From: skullY Date: Wed, 21 Aug 2019 23:46:51 -0700 Subject: run yapf on the code --- lib/python/qmk/tests/attrdict.py | 1 + lib/python/qmk/tests/test_qmk_errors.py | 3 ++- lib/python/qmk/tests/test_qmk_keymap.py | 1 + lib/python/qmk/tests/test_qmk_path.py | 1 + 4 files changed, 5 insertions(+), 1 deletion(-) (limited to 'lib/python') diff --git a/lib/python/qmk/tests/attrdict.py b/lib/python/qmk/tests/attrdict.py index a2584b9233..391c75c4e1 100644 --- a/lib/python/qmk/tests/attrdict.py +++ b/lib/python/qmk/tests/attrdict.py @@ -3,6 +3,7 @@ class AttrDict(dict): This should only be used to mock objects for unit testing. Please do not use this outside of qmk.tests. """ + def __init__(self, *args, **kwargs): super(AttrDict, self).__init__(*args, **kwargs) self.__dict__ = self diff --git a/lib/python/qmk/tests/test_qmk_errors.py b/lib/python/qmk/tests/test_qmk_errors.py index 3f6b567137..1d8690b7ef 100644 --- a/lib/python/qmk/tests/test_qmk_errors.py +++ b/lib/python/qmk/tests/test_qmk_errors.py @@ -1,7 +1,8 @@ from qmk.errors import NoSuchKeyboardError + def test_NoSuchKeyboardError(): try: - raise(NoSuchKeyboardError("test message")) + raise NoSuchKeyboardError("test message") except NoSuchKeyboardError as e: assert e.message == 'test message' diff --git a/lib/python/qmk/tests/test_qmk_keymap.py b/lib/python/qmk/tests/test_qmk_keymap.py index 6a565ee900..2db625600e 100644 --- a/lib/python/qmk/tests/test_qmk_keymap.py +++ b/lib/python/qmk/tests/test_qmk_keymap.py @@ -1,5 +1,6 @@ import qmk.keymap + def test_template_onekey_proton_c(): templ = qmk.keymap.template('handwired/onekey/proton_c') assert templ == qmk.keymap.DEFAULT_KEYMAP_C diff --git a/lib/python/qmk/tests/test_qmk_path.py b/lib/python/qmk/tests/test_qmk_path.py index 23816be7ed..94dbf3a6a6 100644 --- a/lib/python/qmk/tests/test_qmk_path.py +++ b/lib/python/qmk/tests/test_qmk_path.py @@ -2,6 +2,7 @@ import os import qmk.path + def test_keymap_onekey_pytest(): path = qmk.path.keymap('handwired/onekey/pytest') assert path == 'keyboards/handwired/onekey/keymaps' -- cgit v1.2.3 From 533d6d6a464d41d23a39cecfe42d95d2e400d335 Mon Sep 17 00:00:00 2001 From: skullY Date: Thu, 22 Aug 2019 09:38:10 -0700 Subject: Make the modem manager check more pythonic --- lib/python/qmk/cli/doctor.py | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) (limited to 'lib/python') diff --git a/lib/python/qmk/cli/doctor.py b/lib/python/qmk/cli/doctor.py index c5a144363c..5a713b20f5 100755 --- a/lib/python/qmk/cli/doctor.py +++ b/lib/python/qmk/cli/doctor.py @@ -21,17 +21,17 @@ def main(cli): * [ ] Compile a trivial program with each compiler * [ ] Check for udev entries on linux """ + cli.log.info('QMK Doctor is checking your environment.') + # Make sure the basic CLI tools we need are available and can be executed. binaries = ['dfu-programmer', 'avrdude', 'dfu-util', 'avr-gcc', 'arm-none-eabi-gcc'] binaries += glob('bin/qmk-*') - - cli.log.info('QMK Doctor is checking your environment') - ok = True + for binary in binaries: res = shutil.which(binary) if res is None: - cli.log.error("{fg_red}QMK can't find %s in your path", binary) + cli.log.error("{fg_red}QMK can't find %s in your path.", binary) ok = False else: try: @@ -40,20 +40,36 @@ def main(cli): cli.log.error("{fg_red}Can't run `%s --version`", binary) ok = False + # Determine our OS and run platform specific tests OS = platform.system() + if OS == "Darwin": - cli.log.info("Detected {fg_cyan}macOS") + cli.log.info("Detected {fg_cyan}macOS.") + elif OS == "Linux": - cli.log.info("Detected {fg_cyan}linux") + cli.log.info("Detected {fg_cyan}Linux.") if shutil.which('systemctl'): - test = 'systemctl list-unit-files | grep enabled | grep -i ModemManager' - if os.system(test) == 0: - cli.log.warn("{bg_yellow}Detected modem manager. Please disable it if you are using Pro Micros") + mm_check = subprocess.run(['systemctl', 'list-unit-files'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) + if mm_check.returncode == 0: + mm = True + for line in mm_check.stdout.split('\n'): + if 'ModemManager' in line and 'enabled' in line: + mm = False + + if mm: + cli.log.warn("{bg_yellow}Detected ModemManager. Please disable it if you are using a Pro-Micro.") + + else: + cli.log.error('{bg_red}Could not run `systemctl list-unit-files`:') + cli.log.error(mm_check.stderr) + else: cli.log.warn("Can't find systemctl to check for ModemManager.") + else: - cli.log.info("Assuming {fg_cyan}Windows") + cli.log.info("Assuming {fg_cyan}Windows.") + # Report a summary of our findings to the user if ok: cli.log.info('{fg_green}QMK is ready to go') else: -- cgit v1.2.3 From deb6fa6a87b12fcdbf577837c6faeb854cd287c7 Mon Sep 17 00:00:00 2001 From: skullY Date: Thu, 22 Aug 2019 09:40:12 -0700 Subject: Add a command to format python code --- lib/python/qmk/cli/pyformat.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100755 lib/python/qmk/cli/pyformat.py (limited to 'lib/python') diff --git a/lib/python/qmk/cli/pyformat.py b/lib/python/qmk/cli/pyformat.py new file mode 100755 index 0000000000..b1f8c02b28 --- /dev/null +++ b/lib/python/qmk/cli/pyformat.py @@ -0,0 +1,16 @@ +"""Format python code according to QMK's style. +""" +from milc import cli + +import subprocess + + +@cli.entrypoint("Format python code according to QMK's style.") +def main(cli): + """Format python code according to QMK's style. + """ + 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`.') + except subprocess.CalledProcessError: + cli.log.error('Error formatting python code!') -- cgit v1.2.3 From 595232ec989499d0ee0b8db0b55f6e8ca418c786 Mon Sep 17 00:00:00 2001 From: Kenny Hoang Date: Tue, 10 Sep 2019 08:14:25 -0400 Subject: Created new_keymap.py, python version of new_keymap.sh (#6066) * Created python version of new_keymap.sh: new_keymap.py * Updated usage message * Updated new_keymap.py to use python3.5+ syntax & be more similar to new_keyboard.sh * Updated complete message * Updated usage in argparser and removed incorrect usage_message * Reverted the fstrings back to strings that use .format() & updated docstring convention * Added helper to recursively cd .. until at qmk_firmware root directory * Revert "Added helper to recursively cd .. until at qmk_firmware root directory" This reverts commit 61a0ff3b25f91901287bec8d58eb51a1f126e2ad. * Updated new_keymap.py to use printf-style format strings * First draft lib/python/qmk/cli/new/keymap.py with milc * Removed shebang & syspath appending lines * Added optional args & resolved some cr comemnts * Added a docstring and updated strings --- lib/python/qmk/cli/__init__.py | 0 lib/python/qmk/cli/new/__init__.py | 0 lib/python/qmk/cli/new/keymap.py | 41 ++++++++++++++++++++++++++++++++++++++ lib/python/qmk/path.py | 3 +++ 4 files changed, 44 insertions(+) create mode 100644 lib/python/qmk/cli/__init__.py create mode 100644 lib/python/qmk/cli/new/__init__.py create mode 100755 lib/python/qmk/cli/new/keymap.py (limited to 'lib/python') diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/python/qmk/cli/new/__init__.py b/lib/python/qmk/cli/new/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/python/qmk/cli/new/keymap.py b/lib/python/qmk/cli/new/keymap.py new file mode 100755 index 0000000000..b378e5ab43 --- /dev/null +++ b/lib/python/qmk/cli/new/keymap.py @@ -0,0 +1,41 @@ +"""This script automates the copying of the default keymap into your own keymap. +""" +import os +import shutil + +from milc import cli + + +@cli.argument('-k', '--keyboard', help='Specify keyboard name. Example: 1upkeyboards/1up60hse') +@cli.argument('-u', '--username', help='Specify any name for the new keymap directory') +@cli.entrypoint('Creates a new keymap for the keyboard of your choosing') +def main(cli): + """Creates a new keymap for the keyboard of your choosing. + """ + # ask for user input if keyboard or username was not provided in the command line + keyboard = cli.config.general.keyboard if cli.config.general.keyboard else input("Keyboard Name: ") + username = cli.config.general.username if cli.config.general.username else input("Username: ") + + # generate keymap paths + kb_path = os.path.join(os.getcwd(), "keyboards", keyboard) + keymap_path_default = os.path.join(kb_path, "keymaps/default") + keymap_path = os.path.join(kb_path, "keymaps/%s" % username) + + # check directories + if not os.path.exists(kb_path): + cli.log.error('Keyboard %s does not exist!', kb_path) + exit(1) + if not os.path.exists(keymap_path_default): + cli.log.error('Keyboard default %s does not exist!', keymap_path_default) + exit(1) + if os.path.exists(keymap_path): + cli.log.error('Keymap %s already exists!', keymap_path) + exit(1) + + # create user directory with default keymap files + shutil.copytree(keymap_path_default, keymap_path, symlinks=True) + + # end message to user + cli.log.info("%s keymap directory created in: %s\n" + + "Compile a firmware file with your new keymap by typing: \n" + + "qmk compile -kb %s -km %s", username, keymap_path, keyboard, username) diff --git a/lib/python/qmk/path.py b/lib/python/qmk/path.py index f2a8346a51..cf087265fb 100644 --- a/lib/python/qmk/path.py +++ b/lib/python/qmk/path.py @@ -1,7 +1,10 @@ """Functions that help us work with files and folders. """ +import logging import os +from qmk.errors import NoSuchKeyboardError + def keymap(keyboard): """Locate the correct directory for storing a keymap. -- cgit v1.2.3 From d569f0877155efc752994f8a21f5cf001f9d6ae6 Mon Sep 17 00:00:00 2001 From: skullydazed Date: Sun, 22 Sep 2019 13:25:33 -0700 Subject: Configuration system for CLI (#6708) * Rework how bin/qmk handles subcommands * qmk config wip * Code to show all configs * Fully working `qmk config` command * Mark some CLI arguments so they don't pollute the config file * Fleshed out config support, nicer subcommand support * sync with installable cli * pyformat * Add a test for subcommand_modules * Documentation for the `qmk config` command * split config_token on space so qmk config is more predictable * Rework how subcommands are imported * Document `arg_only` * Document deleting from CLI * Document how multiple operations work * Add cli config to the doc index * Add tests for the cli commands * Make running the tests more reliable * Be more selective about building all default keymaps * Update new-keymap to fit the new subcommand style * Add documentation about writing CLI scripts * Document new-keyboard * Update docs/cli_configuration.md Co-Authored-By: noroadsleft <18669334+noroadsleft@users.noreply.github.com> * Update docs/cli_development.md Co-Authored-By: noroadsleft <18669334+noroadsleft@users.noreply.github.com> * Update docs/cli_development.md Co-Authored-By: noroadsleft <18669334+noroadsleft@users.noreply.github.com> * Update docs/cli_development.md Co-Authored-By: noroadsleft <18669334+noroadsleft@users.noreply.github.com> * Address yan's comments. * Apply suggestions from code review suggestions from @noahfrederick Co-Authored-By: Noah Frederick * Apply suggestions from code review Co-Authored-By: Noah Frederick * Remove pip3 from the test runner --- lib/python/milc.py | 113 ++++++++++++++++++------------ lib/python/qmk/cli/__init__.py | 13 ++++ lib/python/qmk/cli/cformat.py | 6 +- lib/python/qmk/cli/compile.py | 10 +-- lib/python/qmk/cli/config.py | 96 +++++++++++++++++++++++++ lib/python/qmk/cli/doctor.py | 5 +- lib/python/qmk/cli/hello.py | 6 +- lib/python/qmk/cli/json/__init__.py | 5 ++ lib/python/qmk/cli/json/keymap.py | 20 +++--- lib/python/qmk/cli/new/__init__.py | 1 + lib/python/qmk/cli/new/keymap.py | 17 +++-- lib/python/qmk/cli/nose2.py | 18 ----- lib/python/qmk/cli/pyformat.py | 5 +- lib/python/qmk/cli/pytest.py | 20 ++++++ lib/python/qmk/path.py | 1 + lib/python/qmk/tests/test_cli_commands.py | 39 +++++++++++ 16 files changed, 280 insertions(+), 95 deletions(-) create mode 100644 lib/python/qmk/cli/config.py delete mode 100644 lib/python/qmk/cli/nose2.py create mode 100644 lib/python/qmk/cli/pytest.py create mode 100644 lib/python/qmk/tests/test_cli_commands.py (limited to 'lib/python') diff --git a/lib/python/milc.py b/lib/python/milc.py index c62c1b166c..1a29bb25c8 100644 --- a/lib/python/milc.py +++ b/lib/python/milc.py @@ -17,6 +17,7 @@ import argparse import logging import os import re +import shlex import sys from decimal import Decimal from tempfile import NamedTemporaryFile @@ -35,6 +36,10 @@ except ImportError: import argcomplete import colorama +from appdirs import user_config_dir + +# Disable logging until we can configure it how the user wants +logging.basicConfig(filename='/dev/null') # Log Level Representations EMOJI_LOGLEVELS = { @@ -47,6 +52,7 @@ EMOJI_LOGLEVELS = { } EMOJI_LOGLEVELS['FATAL'] = EMOJI_LOGLEVELS['CRITICAL'] EMOJI_LOGLEVELS['WARN'] = EMOJI_LOGLEVELS['WARNING'] +UNICODE_SUPPORT = sys.stdout.encoding.lower().startswith('utf') # ANSI Color setup # Regex was gratefully borrowed from kfir on stackoverflow: @@ -97,11 +103,12 @@ class ANSIFormatter(logging.Formatter): class ANSIEmojiLoglevelFormatter(ANSIFormatter): - """A log formatter that makes the loglevel an emoji. + """A log formatter that makes the loglevel an emoji on UTF capable terminals. """ def format(self, record): - record.levelname = EMOJI_LOGLEVELS[record.levelname].format(**ansi_colors) + if UNICODE_SUPPORT: + record.levelname = EMOJI_LOGLEVELS[record.levelname].format(**ansi_colors) return super(ANSIEmojiLoglevelFormatter, self).format(record) @@ -144,13 +151,15 @@ class Configuration(object): def __init__(self, *args, **kwargs): self._config = {} - self.default_container = ConfigurationOption + + def __getattr__(self, key): + return self.__getitem__(key) def __getitem__(self, key): """Returns a config section, creating it if it doesn't exist yet. """ if key not in self._config: - self.__dict__[key] = self._config[key] = ConfigurationOption() + self.__dict__[key] = self._config[key] = ConfigurationSection(self) return self._config[key] @@ -161,30 +170,34 @@ class Configuration(object): def __delitem__(self, key): if key in self.__dict__ and key[0] != '_': del self.__dict__[key] - del self._config[key] + if key in self._config: + del self._config[key] -class ConfigurationOption(Configuration): - def __init__(self, *args, **kwargs): - super(ConfigurationOption, self).__init__(*args, **kwargs) - self.default_container = dict +class ConfigurationSection(Configuration): + def __init__(self, parent, *args, **kwargs): + super(ConfigurationSection, self).__init__(*args, **kwargs) + self.parent = parent def __getitem__(self, key): - """Returns a config section, creating it if it doesn't exist yet. + """Returns a config value, pulling from the `user` section as a fallback. """ - if key not in self._config: - self.__dict__[key] = self._config[key] = None + if key in self._config: + return self._config[key] - return self._config[key] + elif key in self.parent.user: + return self.parent.user[key] + + return None def handle_store_boolean(self, *args, **kwargs): """Does the add_argument for action='store_boolean'. """ - kwargs['add_dest'] = False disabled_args = None disabled_kwargs = kwargs.copy() disabled_kwargs['action'] = 'store_false' + disabled_kwargs['dest'] = self.get_argument_name(*args, **kwargs) disabled_kwargs['help'] = 'Disable ' + kwargs['help'] kwargs['action'] = 'store_true' kwargs['help'] = 'Enable ' + kwargs['help'] @@ -219,11 +232,6 @@ class SubparserWrapper(object): self.subparser.completer = completer def add_argument(self, *args, **kwargs): - if kwargs.get('add_dest', True): - kwargs['dest'] = self.submodule + '_' + self.cli.get_argument_name(*args, **kwargs) - if 'add_dest' in kwargs: - del kwargs['add_dest'] - if 'action' in kwargs and kwargs['action'] == 'store_boolean': return handle_store_boolean(self, *args, **kwargs) @@ -254,12 +262,16 @@ class MILC(object): self._entrypoint = None self._inside_context_manager = False self.ansi = ansi_colors + self.arg_only = [] self.config = Configuration() self.config_file = None - self.prog_name = sys.argv[0][:-3] if sys.argv[0].endswith('.py') else sys.argv[0] self.version = os.environ.get('QMK_VERSION', 'unknown') self.release_lock() + # Figure out our program name + self.prog_name = sys.argv[0][:-3] if sys.argv[0].endswith('.py') else sys.argv[0] + self.prog_name = self.prog_name.split('/')[-1] + # Initialize all the things self.initialize_argparse() self.initialize_logging() @@ -273,7 +285,7 @@ class MILC(object): self._description = self._arg_parser.description = self._arg_defaults.description = value def echo(self, text, *args, **kwargs): - """Print colorized text to stdout, as long as stdout is a tty. + """Print colorized text to stdout. ANSI color strings (such as {fg-blue}) will be converted into ANSI escape sequences, and the ANSI reset sequence will be added to all @@ -284,11 +296,10 @@ class MILC(object): if args and kwargs: raise RuntimeError('You can only specify *args or **kwargs, not both!') - if sys.stdout.isatty(): - args = args or kwargs - text = format_ansi(text) + args = args or kwargs + text = format_ansi(text) - print(text % args) + print(text % args) def initialize_argparse(self): """Prepare to process arguments from sys.argv. @@ -313,21 +324,21 @@ class MILC(object): self.release_lock() def completer(self, completer): - """Add an arpcomplete completer to this subcommand. + """Add an argcomplete completer to this subcommand. """ self._arg_parser.completer = completer def add_argument(self, *args, **kwargs): """Wrapper to add arguments to both the main and the shadow argparser. """ + if 'action' in kwargs and kwargs['action'] == 'store_boolean': + return handle_store_boolean(self, *args, **kwargs) + if kwargs.get('add_dest', True) and args[0][0] == '-': kwargs['dest'] = 'general_' + self.get_argument_name(*args, **kwargs) if 'add_dest' in kwargs: del kwargs['add_dest'] - if 'action' in kwargs and kwargs['action'] == 'store_boolean': - return handle_store_boolean(self, *args, **kwargs) - self.acquire_lock() self._arg_parser.add_argument(*args, **kwargs) @@ -396,7 +407,7 @@ class MILC(object): if self.args and self.args.general_config_file: return self.args.general_config_file - return os.path.abspath(os.path.expanduser('~/.%s.ini' % self.prog_name)) + return os.path.join(user_config_dir(appname='qmk', appauthor='QMK'), '%s.ini' % self.prog_name) def get_argument_name(self, *args, **kwargs): """Takes argparse arguments and returns the dest name. @@ -413,6 +424,11 @@ class MILC(object): raise RuntimeError('You must run this before the with statement!') def argument_function(handler): + if 'arg_only' in kwargs and kwargs['arg_only']: + arg_name = self.get_argument_name(*args, **kwargs) + self.arg_only.append(arg_name) + del kwargs['arg_only'] + if handler is self._entrypoint: self.add_argument(*args, **kwargs) @@ -485,15 +501,20 @@ class MILC(object): if argument in ('subparsers', 'entrypoint'): continue - if '_' not in argument: - continue - - section, option = argument.split('_', 1) - if hasattr(self.args_passed, argument): - self.config[section][option] = getattr(self.args, argument) + if '_' in argument: + section, option = argument.split('_', 1) else: - if option not in self.config[section]: - self.config[section][option] = getattr(self.args, argument) + section = self._entrypoint.__name__ + option = argument + + if option not in self.arg_only: + if hasattr(self.args_passed, argument): + arg_value = getattr(self.args, argument) + if arg_value: + self.config[section][option] = arg_value + else: + if option not in self.config[section]: + self.config[section][option] = getattr(self.args, argument) self.release_lock() @@ -509,6 +530,8 @@ class MILC(object): self.acquire_lock() config = RawConfigParser() + config_dir = os.path.dirname(self.config_file) + for section_name, section in self.config._config.items(): config.add_section(section_name) for option_name, value in section.items(): @@ -517,7 +540,10 @@ class MILC(object): continue config.set(section_name, option_name, str(value)) - with NamedTemporaryFile(mode='w', dir=os.path.dirname(self.config_file), delete=False) as tmpfile: + if not os.path.exists(config_dir): + os.makedirs(config_dir) + + with NamedTemporaryFile(mode='w', dir=config_dir, delete=False) as tmpfile: config.write(tmpfile) # Move the new config file into place atomically @@ -527,6 +553,7 @@ class MILC(object): self.log.warning('Config file saving failed, not replacing %s with %s.', self.config_file, tmpfile.name) self.release_lock() + cli.log.info('Wrote configuration to %s', shlex.quote(self.config_file)) def __call__(self): """Execute the entrypoint function. @@ -602,8 +629,8 @@ class MILC(object): """Called by __enter__() to setup the logging configuration. """ if len(logging.root.handlers) != 0: - # This is not a design decision. This is what I'm doing for now until I can examine and think about this situation in more detail. - raise RuntimeError('MILC should be the only system installing root log handlers!') + # MILC is the only thing that should have root log handlers + logging.root.handlers = [] self.acquire_lock() @@ -648,8 +675,9 @@ class MILC(object): self.read_config() self.setup_logging() - if self.config.general.save_config: + if 'save_config' in self.config.general and self.config.general.save_config: self.save_config() + exit(0) return self @@ -712,4 +740,3 @@ if __name__ == '__main__': cli.goodbye.add_argument('-n', '--name', help='Name to bid farewell to', default='World') cli() # Automatically picks between main(), hello() and goodbye() - print(sorted(ansi_colors.keys())) diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index e69de29bb2..fb4e0ecb46 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py @@ -0,0 +1,13 @@ +"""QMK CLI Subcommands + +We list each subcommand here explicitly because all the reliable ways of searching for modules are slow and delay startup. +""" +from . import cformat +from . import compile +from . import config +from . import doctor +from . import hello +from . import json +from . import new +from . import pyformat +from . import pytest diff --git a/lib/python/qmk/cli/cformat.py b/lib/python/qmk/cli/cformat.py index 91e650368b..d2382bdbde 100644 --- a/lib/python/qmk/cli/cformat.py +++ b/lib/python/qmk/cli/cformat.py @@ -6,9 +6,9 @@ import subprocess from milc import cli -@cli.argument('files', nargs='*', help='Filename(s) to format.') -@cli.entrypoint("Format C code according to QMK's style.") -def main(cli): +@cli.argument('files', nargs='*', arg_only=True, help='Filename(s) to format.') +@cli.subcommand("Format C code according to QMK's style.") +def cformat(cli): """Format C code according to QMK's style. """ clang_format = ['clang-format', '-i'] diff --git a/lib/python/qmk/cli/compile.py b/lib/python/qmk/cli/compile.py index 7e14ad8fbf..6646891b30 100755 --- a/lib/python/qmk/cli/compile.py +++ b/lib/python/qmk/cli/compile.py @@ -14,11 +14,11 @@ import qmk.keymap import qmk.path -@cli.argument('filename', nargs='?', type=FileType('r'), help='The configurator export to compile') +@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.entrypoint('Compile a QMK Firmware.') -def main(cli): +@cli.subcommand('Compile a QMK Firmware.') +def compile(cli): """Compile a QMK Firmware. If a Configurator export is supplied this command will create a new keymap, overwriting an existing keymap if one exists. @@ -41,9 +41,9 @@ def main(cli): # Compile the keymap command = ['make', ':'.join((user_keymap['keyboard'], user_keymap['keymap']))] - elif cli.config.general.keyboard and cli.config.general.keymap: + elif cli.config.compile.keyboard and cli.config.compile.keymap: # Generate the make command for a specific keyboard/keymap. - command = ['make', ':'.join((cli.config.general.keyboard, cli.config.general.keymap))] + command = ['make', ':'.join((cli.config.compile.keyboard, cli.config.compile.keymap))] else: cli.log.error('You must supply a configurator export or both `--keyboard` and `--keymap`.') diff --git a/lib/python/qmk/cli/config.py b/lib/python/qmk/cli/config.py new file mode 100644 index 0000000000..d6c774e651 --- /dev/null +++ b/lib/python/qmk/cli/config.py @@ -0,0 +1,96 @@ +"""Read and write configuration settings +""" +import os +import subprocess + +from milc import cli + + +def print_config(section, key): + """Print a single config setting to stdout. + """ + cli.echo('%s.%s{fg_cyan}={fg_reset}%s', section, key, cli.config[section][key]) + + +@cli.argument('-ro', '--read-only', action='store_true', help='Operate in read-only mode.') +@cli.argument('configs', nargs='*', arg_only=True, help='Configuration options to read or write.') +@cli.subcommand("Read and write configuration settings.") +def config(cli): + """Read and write config settings. + + This script iterates over the config_tokens supplied as argument. Each config_token has the following form: + + section[.key][=value] + + If only a section (EG 'compile') is supplied all keys for that section will be displayed. + + If section.key is supplied the value for that single key will be displayed. + + If section.key=value is supplied the value for that single key will be set. + + If section.key=None is supplied the key will be deleted. + + No validation is done to ensure that the supplied section.key is actually used by qmk scripts. + """ + if not cli.args.configs: + # Walk the config tree + for section in cli.config: + for key in cli.config[section]: + print_config(section, key) + + return True + + # Process config_tokens + save_config = False + + for argument in cli.args.configs: + # Split on space in case they quoted multiple config tokens + for config_token in argument.split(' '): + # Extract the section, config_key, and value to write from the supplied config_token. + if '=' in config_token: + key, value = config_token.split('=') + else: + key = config_token + value = None + + if '.' in key: + section, config_key = key.split('.', 1) + else: + section = key + config_key = None + + # Validation + if config_key and '.' in config_key: + cli.log.error('Config keys may not have more than one period! "%s" is not valid.', key) + return False + + # Do what the user wants + if section and config_key and value: + # Write a config key + log_string = '%s.%s{fg_cyan}:{fg_reset} %s {fg_cyan}->{fg_reset} %s' + if cli.args.read_only: + log_string += ' {fg_red}(change not written)' + + cli.echo(log_string, section, config_key, cli.config[section][config_key], value) + + if not cli.args.read_only: + if value == 'None': + del cli.config[section][config_key] + else: + cli.config[section][config_key] = value + save_config = True + + elif section and config_key: + # Display a single key + print_config(section, config_key) + + elif section: + # Display an entire section + for key in cli.config[section]: + print_config(section, key) + + # Ending actions + if save_config: + cli.save_config() + + return True diff --git a/lib/python/qmk/cli/doctor.py b/lib/python/qmk/cli/doctor.py index 5a713b20f5..3474422a89 100755 --- a/lib/python/qmk/cli/doctor.py +++ b/lib/python/qmk/cli/doctor.py @@ -11,8 +11,8 @@ from glob import glob from milc import cli -@cli.entrypoint('Basic QMK environment checks') -def main(cli): +@cli.subcommand('Basic QMK environment checks') +def doctor(cli): """Basic QMK environment checks. This is currently very simple, it just checks that all the expected binaries are on your system. @@ -36,6 +36,7 @@ def main(cli): else: try: subprocess.run([binary, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5, check=True) + cli.log.info('Found {fg_cyan}%s', binary) except subprocess.CalledProcessError: cli.log.error("{fg_red}Can't run `%s --version`", binary) ok = False diff --git a/lib/python/qmk/cli/hello.py b/lib/python/qmk/cli/hello.py index bc0cb6de18..bee28c3013 100755 --- a/lib/python/qmk/cli/hello.py +++ b/lib/python/qmk/cli/hello.py @@ -6,8 +6,8 @@ from milc import cli @cli.argument('-n', '--name', default='World', help='Name to greet.') -@cli.entrypoint('QMK Hello World.') -def main(cli): +@cli.subcommand('QMK Hello World.') +def hello(cli): """Log a friendly greeting. """ - cli.log.info('Hello, %s!', cli.config.general.name) + cli.log.info('Hello, %s!', cli.config.hello.name) diff --git a/lib/python/qmk/cli/json/__init__.py b/lib/python/qmk/cli/json/__init__.py index e69de29bb2..f4ebfc45b4 100644 --- a/lib/python/qmk/cli/json/__init__.py +++ b/lib/python/qmk/cli/json/__init__.py @@ -0,0 +1,5 @@ +"""QMK CLI JSON Subcommands + +We list each subcommand here explicitly because all the reliable ways of searching for modules are slow and delay startup. +""" +from . import keymap diff --git a/lib/python/qmk/cli/json/keymap.py b/lib/python/qmk/cli/json/keymap.py index e2d0b58093..a65acd6197 100755 --- a/lib/python/qmk/cli/json/keymap.py +++ b/lib/python/qmk/cli/json/keymap.py @@ -9,10 +9,10 @@ from milc import cli import qmk.keymap -@cli.argument('-o', '--output', help='File to write to') -@cli.argument('filename', help='Configurator JSON file') -@cli.entrypoint('Create a keymap.c from a QMK Configurator export.') -def main(cli): +@cli.argument('-o', '--output', arg_only=True, help='File to write to') +@cli.argument('filename', arg_only=True, help='Configurator JSON file') +@cli.subcommand('Create a keymap.c from a QMK Configurator export.') +def json_keymap(cli): """Generate a keymap.c from a configurator export. This command uses the `qmk.keymap` module to generate a keymap.c from a configurator export. The generated keymap is written to stdout, or to a file if -o is provided. @@ -28,8 +28,8 @@ def main(cli): exit(1) # Environment processing - if cli.config.general.output == ('-'): - cli.config.general.output = None + if cli.args.output == ('-'): + cli.args.output = None # Parse the configurator json with open(qmk.path.normpath(cli.args.filename), 'r') as fd: @@ -38,17 +38,17 @@ def main(cli): # Generate the keymap keymap_c = qmk.keymap.generate(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers']) - if cli.config.general.output: - output_dir = os.path.dirname(cli.config.general.output) + if cli.args.output: + output_dir = os.path.dirname(cli.args.output) if not os.path.exists(output_dir): os.makedirs(output_dir) - output_file = qmk.path.normpath(cli.config.general.output) + output_file = qmk.path.normpath(cli.args.output) with open(output_file, 'w') as keymap_fd: keymap_fd.write(keymap_c) - cli.log.info('Wrote keymap to %s.', cli.config.general.output) + cli.log.info('Wrote keymap to %s.', cli.args.output) else: print(keymap_c) diff --git a/lib/python/qmk/cli/new/__init__.py b/lib/python/qmk/cli/new/__init__.py index e69de29bb2..c6a26939b8 100644 --- a/lib/python/qmk/cli/new/__init__.py +++ b/lib/python/qmk/cli/new/__init__.py @@ -0,0 +1 @@ +from . import keymap diff --git a/lib/python/qmk/cli/new/keymap.py b/lib/python/qmk/cli/new/keymap.py index b378e5ab43..5efb81c93f 100755 --- a/lib/python/qmk/cli/new/keymap.py +++ b/lib/python/qmk/cli/new/keymap.py @@ -6,15 +6,15 @@ import shutil from milc import cli -@cli.argument('-k', '--keyboard', help='Specify keyboard name. Example: 1upkeyboards/1up60hse') -@cli.argument('-u', '--username', help='Specify any name for the new keymap directory') -@cli.entrypoint('Creates a new keymap for the keyboard of your choosing') -def main(cli): +@cli.argument('-kb', '--keyboard', 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') +def new_keymap(cli): """Creates a new keymap for the keyboard of your choosing. """ # ask for user input if keyboard or username was not provided in the command line - keyboard = cli.config.general.keyboard if cli.config.general.keyboard else input("Keyboard Name: ") - username = cli.config.general.username if cli.config.general.username else input("Username: ") + keyboard = cli.config.new_keymap.keyboard if cli.config.new_keymap.keyboard else input("Keyboard Name: ") + keymap = cli.config.new_keymap.keymap if cli.config.new_keymap.keymap else input("Keymap Name: ") # generate keymap paths kb_path = os.path.join(os.getcwd(), "keyboards", keyboard) @@ -36,6 +36,5 @@ def main(cli): shutil.copytree(keymap_path_default, keymap_path, symlinks=True) # end message to user - cli.log.info("%s keymap directory created in: %s\n" + - "Compile a firmware file with your new keymap by typing: \n" + - "qmk compile -kb %s -km %s", username, keymap_path, keyboard, username) + cli.log.info("%s keymap directory created in: %s", username, keymap_path) + cli.log.info("Compile a firmware with your new keymap by typing: \n" + "qmk compile -kb %s -km %s", keyboard, username) diff --git a/lib/python/qmk/cli/nose2.py b/lib/python/qmk/cli/nose2.py deleted file mode 100644 index c6c9c67b30..0000000000 --- a/lib/python/qmk/cli/nose2.py +++ /dev/null @@ -1,18 +0,0 @@ -"""QMK Python Unit Tests - -QMK script to run unit and integration tests against our python code. -""" -from milc import cli - - -@cli.entrypoint('QMK Python Unit Tests') -def main(cli): - """Use nose2 to run unittests - """ - try: - import nose2 - except ImportError: - cli.log.error('Could not import nose2! Please install it with {fg_cyan}pip3 install nose2') - return False - - nose2.discover() diff --git a/lib/python/qmk/cli/pyformat.py b/lib/python/qmk/cli/pyformat.py index b1f8c02b28..a53ba40c0a 100755 --- a/lib/python/qmk/cli/pyformat.py +++ b/lib/python/qmk/cli/pyformat.py @@ -5,12 +5,13 @@ from milc import cli import subprocess -@cli.entrypoint("Format python code according to QMK's style.") -def main(cli): +@cli.subcommand("Format python code according to QMK's style.") +def pyformat(cli): """Format python code according to QMK's style. """ 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`.') + except subprocess.CalledProcessError: cli.log.error('Error formatting python code!') diff --git a/lib/python/qmk/cli/pytest.py b/lib/python/qmk/cli/pytest.py new file mode 100644 index 0000000000..14613e1d96 --- /dev/null +++ b/lib/python/qmk/cli/pytest.py @@ -0,0 +1,20 @@ +"""QMK Python Unit Tests + +QMK script to run unit and integration tests against our python code. +""" +import sys +from milc import cli + + +@cli.subcommand('QMK Python Unit Tests') +def pytest(cli): + """Use nose2 to run unittests + """ + try: + import nose2 + + except ImportError: + cli.log.error('Could not import nose2! Please install it with {fg_cyan}pip3 install nose2') + return False + + nose2.discover(argv=['nose2', '-v']) diff --git a/lib/python/qmk/path.py b/lib/python/qmk/path.py index cf087265fb..2149625cc6 100644 --- a/lib/python/qmk/path.py +++ b/lib/python/qmk/path.py @@ -2,6 +2,7 @@ """ import logging import os +from pkgutil import walk_packages from qmk.errors import NoSuchKeyboardError diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py new file mode 100644 index 0000000000..2fc6e0f723 --- /dev/null +++ b/lib/python/qmk/tests/test_cli_commands.py @@ -0,0 +1,39 @@ +import subprocess + + +def check_subcommand(command, *args): + cmd = ['bin/qmk', command] + list(args) + return subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) + + +def test_cformat(): + assert check_subcommand('cformat', 'tmk_core/common/backlight.c').returncode == 0 + + +def test_compile(): + assert check_subcommand('compile', '-kb', 'handwired/onekey/pytest', '-km', 'default').returncode == 0 + + +def test_config(): + result = check_subcommand('config') + assert result.returncode == 0 + assert 'general.color' in result.stdout + + +def test_doctor(): + result = check_subcommand('doctor') + assert result.returncode == 0 + assert 'QMK Doctor is checking your environment.' in result.stderr + assert 'QMK is ready to go' in result.stderr + + +def test_hello(): + result = check_subcommand('hello') + assert result.returncode == 0 + assert 'Hello,' in result.stderr + + +def test_pyformat(): + result = check_subcommand('pyformat') + assert result.returncode == 0 + assert 'Successfully formatted the python code' in result.stderr -- cgit v1.2.3 From 9067dc817aefb7fe43d23a995604555642c6c897 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Tue, 1 Oct 2019 20:56:16 -0400 Subject: Fix qmk doctor 'bytes-like object is required' on linux MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes the following issue related to encoding on linux systems. Add `universal_newlines=True` to subprocess. ☒ a bytes-like object is required, not 'str' Traceback (most recent call last): File "/usr/local/lib/python3.7/site-packages/milc.py", line 564, in __call__ return self.__call__() File "/usr/local/lib/python3.7/site-packages/milc.py", line 569, in __call__ return self._entrypoint(self) File "$HOME/qmk_firmware/lib/python/qmk/cli/doctor.py", line 56, in doctor for line in mm_check.stdout.split('\n'): TypeError: a bytes-like object is required, not 'str' --- lib/python/qmk/cli/doctor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/python') diff --git a/lib/python/qmk/cli/doctor.py b/lib/python/qmk/cli/doctor.py index 3474422a89..2b6a03e443 100755 --- a/lib/python/qmk/cli/doctor.py +++ b/lib/python/qmk/cli/doctor.py @@ -50,7 +50,7 @@ def doctor(cli): elif OS == "Linux": cli.log.info("Detected {fg_cyan}Linux.") if shutil.which('systemctl'): - mm_check = subprocess.run(['systemctl', 'list-unit-files'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) + mm_check = subprocess.run(['systemctl', 'list-unit-files'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10, universal_newlines=True) if mm_check.returncode == 0: mm = True for line in mm_check.stdout.split('\n'): -- cgit v1.2.3 From 78f01eef2e4586f59b56703d0a4f1a57a1af40a1 Mon Sep 17 00:00:00 2001 From: "St. John Johnson" Date: Sat, 5 Oct 2019 23:41:15 -0700 Subject: Use `keymap` instead of `username` variable for `qmk new_keymap` (#6885) Username is not defined and this causes `qmk new_keymap` to error. This appears to have originated from a partial update in https://github.com/qmk/qmk_firmware/pull/6708/files#diff-d5208bcbc79aa428556a743b6ff41086. This change completes the migration from `username` to `keymap` --- lib/python/qmk/cli/new/keymap.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/python') diff --git a/lib/python/qmk/cli/new/keymap.py b/lib/python/qmk/cli/new/keymap.py index 5efb81c93f..96525e28e1 100755 --- a/lib/python/qmk/cli/new/keymap.py +++ b/lib/python/qmk/cli/new/keymap.py @@ -12,14 +12,14 @@ from milc import cli def new_keymap(cli): """Creates a new keymap for the keyboard of your choosing. """ - # ask for user input if keyboard or username was not provided in the command line + # ask for user input if keyboard or keymap was not provided in the command line keyboard = cli.config.new_keymap.keyboard if cli.config.new_keymap.keyboard else input("Keyboard Name: ") keymap = cli.config.new_keymap.keymap if cli.config.new_keymap.keymap else input("Keymap Name: ") # generate keymap paths kb_path = os.path.join(os.getcwd(), "keyboards", keyboard) keymap_path_default = os.path.join(kb_path, "keymaps/default") - keymap_path = os.path.join(kb_path, "keymaps/%s" % username) + keymap_path = os.path.join(kb_path, "keymaps/%s" % keymap) # check directories if not os.path.exists(kb_path): @@ -36,5 +36,5 @@ def new_keymap(cli): shutil.copytree(keymap_path_default, keymap_path, symlinks=True) # end message to user - cli.log.info("%s keymap directory created in: %s", username, keymap_path) - cli.log.info("Compile a firmware with your new keymap by typing: \n" + "qmk compile -kb %s -km %s", keyboard, username) + cli.log.info("%s keymap directory created in: %s", keymap, keymap_path) + cli.log.info("Compile a firmware with your new keymap by typing: \n" + "qmk compile -kb %s -km %s", keyboard, keymap) -- cgit v1.2.3 From f04e58dad6f56cdbd5d369c9e00405dcdb47c8ea Mon Sep 17 00:00:00 2001 From: Dan McClain Date: Mon, 7 Oct 2019 14:32:30 -0400 Subject: [CLI] Add `qmk list_keyboards` (#6927) `list_keyboards` replicates the `make list-keyboards` by globbing for all paths that include `rules.mk` and then removing the paths that include `keymaps`. This basis of this cli command could be reused in the future as a util, but is not done so here since this would be the only place that would use it currently Resolves #6911 --- lib/python/qmk/cli/__init__.py | 1 + lib/python/qmk/cli/list/__init__.py | 1 + lib/python/qmk/cli/list/keyboards.py | 26 ++++++++++++++++++++++++++ lib/python/qmk/tests/test_cli_commands.py | 8 ++++++++ 4 files changed, 36 insertions(+) create mode 100644 lib/python/qmk/cli/list/__init__.py create mode 100644 lib/python/qmk/cli/list/keyboards.py (limited to 'lib/python') diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index fb4e0ecb46..e982a75fc8 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py @@ -8,6 +8,7 @@ from . import config from . import doctor from . import hello from . import json +from . import list from . import new from . import pyformat from . import pytest diff --git a/lib/python/qmk/cli/list/__init__.py b/lib/python/qmk/cli/list/__init__.py new file mode 100644 index 0000000000..c36ba69548 --- /dev/null +++ b/lib/python/qmk/cli/list/__init__.py @@ -0,0 +1 @@ +from . import keyboards diff --git a/lib/python/qmk/cli/list/keyboards.py b/lib/python/qmk/cli/list/keyboards.py new file mode 100644 index 0000000000..53a7af75c6 --- /dev/null +++ b/lib/python/qmk/cli/list/keyboards.py @@ -0,0 +1,26 @@ +"""List the keyboards currently defined within QMK +""" +import os +import re +import glob + +from milc import cli + +@cli.subcommand("List the keyboards currently defined within QMK") +def list_keyboards(cli): + """List the keyboards currently defined within QMK + """ + + base_path = os.path.join(os.getcwd(), "keyboards") + os.path.sep + kb_path_wildcard = os.path.join(base_path, "**", "rules.mk") + + # find everywhere we have rules.mk where keymaps isn't in the path + paths = [path for path in glob.iglob(kb_path_wildcard, recursive=True) if 'keymaps' not in path] + + # strip the keyboard directory path prefix and rules.mk suffix and alphabetize + find_name = lambda path: path.replace(base_path, "").replace(os.path.sep + "rules.mk", "") + names = sorted(map(find_name, paths)) + + for name in names: + # We echo instead of cli.log.info to allow easier piping of this output + cli.echo(name) diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index 2fc6e0f723..c9d632517b 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py @@ -37,3 +37,11 @@ def test_pyformat(): result = check_subcommand('pyformat') assert result.returncode == 0 assert 'Successfully formatted the python code' in result.stderr + + +def test_list_keyboards(): + result = check_subcommand('list_keyboards') + assert result.returncode == 0 + # 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 -- cgit v1.2.3 From 2707652c9877e5fc7dd67670f8e14962c31baf42 Mon Sep 17 00:00:00 2001 From: fauxpark Date: Wed, 9 Oct 2019 05:06:26 +1100 Subject: [Docs] CLI command to serve docs locally (#6956) * CLI command to serve docs locally * Document it * Default port * Use `with` and subclass `SimpleHTTPRequestHandler` to set working dir * Apply suggestions from code review Co-Authored-By: skullydazed * Update docs/cli.md --- lib/python/qmk/cli/__init__.py | 1 + lib/python/qmk/cli/docs.py | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 lib/python/qmk/cli/docs.py (limited to 'lib/python') diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index e982a75fc8..e41cc3dcb2 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py @@ -5,6 +5,7 @@ We list each subcommand here explicitly because all the reliable ways of searchi from . import cformat from . import compile from . import config +from . import docs from . import doctor from . import hello from . import json diff --git a/lib/python/qmk/cli/docs.py b/lib/python/qmk/cli/docs.py new file mode 100644 index 0000000000..a0888ec388 --- /dev/null +++ b/lib/python/qmk/cli/docs.py @@ -0,0 +1,22 @@ +"""Serve QMK documentation locally +""" +import http.server + +from milc import cli + + +class DocsHandler(http.server.SimpleHTTPRequestHandler): + def __init__(self, *args, **kwargs): + super().__init__(*args, directory='docs', **kwargs) + + +@cli.argument('-p', '--port', default=8936, type=int, help='Port number to use.') +@cli.subcommand('Run a local webserver for QMK documentation.') +def docs(cli): + """Spin up a local HTTPServer instance for the QMK docs. + """ + with http.server.HTTPServer(('', cli.config.docs.port), DocsHandler) as httpd: + cli.log.info("Serving QMK docs at http://localhost:%d/", cli.config.docs.port) + cli.log.info("Press Control+C to exit.") + + httpd.serve_forever() -- cgit v1.2.3 From 076d8babbbd762f9a316a26144d966238b9b71cc Mon Sep 17 00:00:00 2001 From: fauxpark Date: Sat, 12 Oct 2019 15:41:58 +1100 Subject: [CLI] `qmk docs` graceful shutdown on Ctrl+C (#6989) --- lib/python/qmk/cli/docs.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'lib/python') diff --git a/lib/python/qmk/cli/docs.py b/lib/python/qmk/cli/docs.py index a0888ec388..b419891396 100644 --- a/lib/python/qmk/cli/docs.py +++ b/lib/python/qmk/cli/docs.py @@ -19,4 +19,9 @@ def docs(cli): cli.log.info("Serving QMK docs at http://localhost:%d/", cli.config.docs.port) cli.log.info("Press Control+C to exit.") - httpd.serve_forever() + try: + httpd.serve_forever() + except KeyboardInterrupt: + cli.log.info("Stopping HTTP server...") + finally: + httpd.shutdown() -- cgit v1.2.3 From f64d9b06215bb08d7f77aeba126c0804fffd0064 Mon Sep 17 00:00:00 2001 From: Harry Wada Date: Sun, 20 Oct 2019 09:33:58 -0500 Subject: Fix detection of ModemManager (#7076) --- lib/python/qmk/cli/doctor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/python') diff --git a/lib/python/qmk/cli/doctor.py b/lib/python/qmk/cli/doctor.py index 2b6a03e443..309de0c671 100755 --- a/lib/python/qmk/cli/doctor.py +++ b/lib/python/qmk/cli/doctor.py @@ -52,10 +52,10 @@ def doctor(cli): if shutil.which('systemctl'): mm_check = subprocess.run(['systemctl', 'list-unit-files'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10, universal_newlines=True) if mm_check.returncode == 0: - mm = True + mm = False for line in mm_check.stdout.split('\n'): if 'ModemManager' in line and 'enabled' in line: - mm = False + mm = True if mm: cli.log.warn("{bg_yellow}Detected ModemManager. Please disable it if you are using a Pro-Micro.") -- cgit v1.2.3 From a5a31a5fc0f14f4f66cf362ee85747be159e364d Mon Sep 17 00:00:00 2001 From: Erovia Date: Sun, 13 Oct 2019 20:23:11 +0200 Subject: MILC: Use dashes instead of underscores for subcommands The subcommand functions' name follows the Python convention of using snake case, but looks odd on the command line. Fix it by converting underscores to dashes, eg.: list_keyboards -> list-keyboards. --- lib/python/milc.py | 7 ++++--- lib/python/qmk/tests/test_cli_commands.py | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'lib/python') diff --git a/lib/python/milc.py b/lib/python/milc.py index 1a29bb25c8..7b130bdea6 100644 --- a/lib/python/milc.py +++ b/lib/python/milc.py @@ -429,11 +429,12 @@ class MILC(object): self.arg_only.append(arg_name) del kwargs['arg_only'] + name = handler.__name__.replace("_", "-") if handler is self._entrypoint: self.add_argument(*args, **kwargs) - elif handler.__name__ in self.subcommands: - self.subcommands[handler.__name__].add_argument(*args, **kwargs) + elif name in self.subcommands: + self.subcommands[name].add_argument(*args, **kwargs) else: raise RuntimeError('Decorated function is not entrypoint or subcommand!') @@ -599,7 +600,7 @@ class MILC(object): self.add_subparsers() if not name: - name = handler.__name__ + name = handler.__name__.replace("_", "-") self.acquire_lock() kwargs['help'] = description diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index c9d632517b..9a9dc4b958 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py @@ -40,7 +40,7 @@ def test_pyformat(): def test_list_keyboards(): - result = check_subcommand('list_keyboards') + result = check_subcommand('list-keyboards') assert result.returncode == 0 # check to see if a known keyboard is returned # this will fail if handwired/onekey/pytest is removed -- cgit v1.2.3 From e0e26957d43018998c405783a2609b99f0e098a7 Mon Sep 17 00:00:00 2001 From: "St. John Johnson" Date: Mon, 28 Oct 2019 18:24:36 -0700 Subject: Fix the CLI docs (#6979) - Sort the commands alphabetically - Add missing `json_keymap` - Correct underscore to dash --- lib/python/qmk/cli/json/keymap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/python') diff --git a/lib/python/qmk/cli/json/keymap.py b/lib/python/qmk/cli/json/keymap.py index a65acd6197..207ac278ca 100755 --- a/lib/python/qmk/cli/json/keymap.py +++ b/lib/python/qmk/cli/json/keymap.py @@ -11,7 +11,7 @@ import qmk.keymap @cli.argument('-o', '--output', arg_only=True, help='File to write to') @cli.argument('filename', arg_only=True, help='Configurator JSON file') -@cli.subcommand('Create a keymap.c from a QMK Configurator export.') +@cli.subcommand('Creates a keymap.c from a QMK Configurator export.') def json_keymap(cli): """Generate a keymap.c from a configurator export. -- cgit v1.2.3 From 5421ba11dedd9912967b1fa5ed1f664687221143 Mon Sep 17 00:00:00 2001 From: skullY Date: Tue, 12 Nov 2019 17:17:12 -0800 Subject: Add support for newer versions of clang-format, if installed --- lib/python/qmk/cli/cformat.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'lib/python') diff --git a/lib/python/qmk/cli/cformat.py b/lib/python/qmk/cli/cformat.py index d2382bdbde..17ca91b3b5 100644 --- a/lib/python/qmk/cli/cformat.py +++ b/lib/python/qmk/cli/cformat.py @@ -2,6 +2,7 @@ """ import os import subprocess +from shutil import which from milc import cli @@ -11,10 +12,18 @@ from milc import cli def cformat(cli): """Format C code according to QMK's style. """ + # 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 # Find the list of files to format - if not cli.args.files: + if cli.args.files: + cli.args.files = [os.path.join(os.environ['ORIG_CWD'], file) for file in cli.args.files] + else: for dir in ['drivers', 'quantum', 'tests', 'tmk_core']: for dirpath, dirnames, filenames in os.walk(dir): if 'tmk_core/protocol/usb_hid' in dirpath: -- cgit v1.2.3 From d1b6c11b7f4cc24c50d2b640a94925acf6450da6 Mon Sep 17 00:00:00 2001 From: skullY Date: Tue, 12 Nov 2019 17:21:33 -0800 Subject: When checking program returncodes treat both 0 and 1 as installed --- lib/python/qmk/cli/doctor.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'lib/python') diff --git a/lib/python/qmk/cli/doctor.py b/lib/python/qmk/cli/doctor.py index 309de0c671..c2723bfcbb 100755 --- a/lib/python/qmk/cli/doctor.py +++ b/lib/python/qmk/cli/doctor.py @@ -24,8 +24,7 @@ def doctor(cli): cli.log.info('QMK Doctor is checking your environment.') # Make sure the basic CLI tools we need are available and can be executed. - binaries = ['dfu-programmer', 'avrdude', 'dfu-util', 'avr-gcc', 'arm-none-eabi-gcc'] - binaries += glob('bin/qmk-*') + binaries = ['dfu-programmer', 'avrdude', 'dfu-util', 'avr-gcc', 'arm-none-eabi-gcc', 'bin/qmk'] ok = True for binary in binaries: @@ -34,10 +33,10 @@ def doctor(cli): cli.log.error("{fg_red}QMK can't find %s in your path.", binary) ok = False else: - try: - subprocess.run([binary, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5, check=True) + check = subprocess.run([binary, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5) + if check.returncode in [0, 1]: cli.log.info('Found {fg_cyan}%s', binary) - except subprocess.CalledProcessError: + else: cli.log.error("{fg_red}Can't run `%s --version`", binary) ok = False -- cgit v1.2.3 From 79edb7c5942108774e667c172550a1519c5543ac Mon Sep 17 00:00:00 2001 From: skullY Date: Tue, 12 Nov 2019 17:27:08 -0800 Subject: Small CLI cleanups * yapf changes * Fix the cformat test * Make the normpath test work when run from / * `qmk config`: Mark `--read-only` as arg_only --- lib/python/qmk/cli/config.py | 2 +- lib/python/qmk/cli/list/keyboards.py | 1 + lib/python/qmk/errors.py | 1 - lib/python/qmk/tests/attrdict.py | 1 - lib/python/qmk/tests/test_cli_commands.py | 2 +- lib/python/qmk/tests/test_qmk_path.py | 2 +- 6 files changed, 4 insertions(+), 5 deletions(-) (limited to 'lib/python') diff --git a/lib/python/qmk/cli/config.py b/lib/python/qmk/cli/config.py index d6c774e651..c4ee20cba5 100644 --- a/lib/python/qmk/cli/config.py +++ b/lib/python/qmk/cli/config.py @@ -12,7 +12,7 @@ def print_config(section, key): cli.echo('%s.%s{fg_cyan}={fg_reset}%s', section, key, cli.config[section][key]) -@cli.argument('-ro', '--read-only', action='store_true', help='Operate in read-only mode.') +@cli.argument('-ro', '--read-only', arg_only=True, action='store_true', help='Operate in read-only mode.') @cli.argument('configs', nargs='*', arg_only=True, help='Configuration options to read or write.') @cli.subcommand("Read and write configuration settings.") def config(cli): diff --git a/lib/python/qmk/cli/list/keyboards.py b/lib/python/qmk/cli/list/keyboards.py index 53a7af75c6..2a29ccb146 100644 --- a/lib/python/qmk/cli/list/keyboards.py +++ b/lib/python/qmk/cli/list/keyboards.py @@ -6,6 +6,7 @@ import glob from milc import cli + @cli.subcommand("List the keyboards currently defined within QMK") def list_keyboards(cli): """List the keyboards currently defined within QMK diff --git a/lib/python/qmk/errors.py b/lib/python/qmk/errors.py index f9bf5b9af9..4a8a91556b 100644 --- a/lib/python/qmk/errors.py +++ b/lib/python/qmk/errors.py @@ -1,6 +1,5 @@ class NoSuchKeyboardError(Exception): """Raised when we can't find a keyboard/keymap directory. """ - def __init__(self, message): self.message = message diff --git a/lib/python/qmk/tests/attrdict.py b/lib/python/qmk/tests/attrdict.py index 391c75c4e1..a2584b9233 100644 --- a/lib/python/qmk/tests/attrdict.py +++ b/lib/python/qmk/tests/attrdict.py @@ -3,7 +3,6 @@ class AttrDict(dict): This should only be used to mock objects for unit testing. Please do not use this outside of qmk.tests. """ - def __init__(self, *args, **kwargs): super(AttrDict, self).__init__(*args, **kwargs) self.__dict__ = self diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index 9a9dc4b958..55b8d253f7 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py @@ -7,7 +7,7 @@ def check_subcommand(command, *args): def test_cformat(): - assert check_subcommand('cformat', 'tmk_core/common/backlight.c').returncode == 0 + assert check_subcommand('cformat', 'tmk_core/common/keyboard.c').returncode == 0 def test_compile(): diff --git a/lib/python/qmk/tests/test_qmk_path.py b/lib/python/qmk/tests/test_qmk_path.py index 94dbf3a6a6..d6961a0f65 100644 --- a/lib/python/qmk/tests/test_qmk_path.py +++ b/lib/python/qmk/tests/test_qmk_path.py @@ -10,4 +10,4 @@ def test_keymap_onekey_pytest(): def test_normpath(): path = qmk.path.normpath('lib/python') - assert path == os.environ['ORIG_CWD'] + '/lib/python' + assert path == os.path.join(os.environ['ORIG_CWD'], 'lib/python') -- cgit v1.2.3 From 00fb1bd1f0550645997b61870d7d092494265a60 Mon Sep 17 00:00:00 2001 From: skullY Date: Tue, 12 Nov 2019 17:08:55 -0800 Subject: Make generating keymap.c from JSON more reliable --- lib/python/qmk/cli/json/keymap.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'lib/python') diff --git a/lib/python/qmk/cli/json/keymap.py b/lib/python/qmk/cli/json/keymap.py index 207ac278ca..7b7553104f 100755 --- a/lib/python/qmk/cli/json/keymap.py +++ b/lib/python/qmk/cli/json/keymap.py @@ -10,6 +10,7 @@ import qmk.keymap @cli.argument('-o', '--output', arg_only=True, 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', arg_only=True, help='Configurator JSON file') @cli.subcommand('Creates a keymap.c from a QMK Configurator export.') def json_keymap(cli): @@ -48,7 +49,8 @@ def json_keymap(cli): with open(output_file, 'w') as keymap_fd: keymap_fd.write(keymap_c) - cli.log.info('Wrote keymap to %s.', cli.args.output) + if not cli.args.quiet: + cli.log.info('Wrote keymap to %s.', cli.args.output) else: print(keymap_c) -- cgit v1.2.3 From 7329c2d02d38f40a23d38f789de34057fd2acd42 Mon Sep 17 00:00:00 2001 From: Cody Bender <50554676+cfbender@users.noreply.github.com> Date: Tue, 12 Nov 2019 21:55:41 -0700 Subject: Add cli convert subcommand, from raw KLE to JSON (#6898) * Add initial pass at KLE convert * Add cli log on convert * Move kle2xy, add absolute filepath arg support * Add overwrite flag, and context sensitive conversion * Update docs/cli.md * Fix converter.py typo * Add convert unit test * Rename to kle2qmk * Rename subcommand * Rename subcommand to kle2json * Change tests to cover rename * Rename in __init__.py * Update CLI docs with new subcommand name * Fix from suggestions in PR #6898 * Help with cases of case sensitivity * Update cli.md * Use angle brackets to indicate required option * Make the output text more accurate --- lib/python/kle2xy.py | 155 ++++++++++++++++++++++++++++++ lib/python/qmk/cli/__init__.py | 1 + lib/python/qmk/cli/kle2json.py | 79 +++++++++++++++ lib/python/qmk/converter.py | 33 +++++++ lib/python/qmk/tests/kle.txt | 5 + lib/python/qmk/tests/test_cli_commands.py | 2 + 6 files changed, 275 insertions(+) create mode 100644 lib/python/kle2xy.py create mode 100755 lib/python/qmk/cli/kle2json.py create mode 100644 lib/python/qmk/converter.py create mode 100644 lib/python/qmk/tests/kle.txt (limited to 'lib/python') diff --git a/lib/python/kle2xy.py b/lib/python/kle2xy.py new file mode 100644 index 0000000000..ea16a4b5ee --- /dev/null +++ b/lib/python/kle2xy.py @@ -0,0 +1,155 @@ +""" Original code from https://github.com/skullydazed/kle2xy +""" + +import hjson +from decimal import Decimal + +class KLE2xy(list): + """Abstract interface for interacting with a KLE layout. + """ + def __init__(self, layout=None, name='', invert_y=True): + super(KLE2xy, self).__init__() + + self.name = name + self.invert_y = invert_y + self.key_width = Decimal('19.05') + self.key_skel = { + 'decal': False, + 'border_color': 'none', + 'keycap_profile': '', + 'keycap_color': 'grey', + 'label_color': 'black', + 'label_size': 3, + 'label_style': 4, + 'width': Decimal('1'), 'height': Decimal('1'), + 'x': Decimal('0'), 'y': Decimal('0') + } + self.rows = Decimal(0) + self.columns = Decimal(0) + + if layout: + self.parse_layout(layout) + + @property + def width(self): + """Returns the width of the keyboard plate. + """ + return (Decimal(self.columns) * self.key_width) + self.key_width/2 + + @property + def height(self): + """Returns the height of the keyboard plate. + """ + return (self.rows * self.key_width) + self.key_width/2 + + @property + def size(self): + """Returns the size of the keyboard plate. + """ + return (self.width, self.height) + + def attrs(self, properties): + """Parse the keyboard properties dictionary. + """ + # FIXME: Store more than just the keyboard name. + if 'name' in properties: + self.name = properties['name'] + + def parse_layout(self, layout): + # Wrap this in a dictionary so hjson will parse KLE raw data + layout = '{"layout": [' + layout + ']}' + layout = hjson.loads(layout)['layout'] + + # Initialize our state machine + current_key = self.key_skel.copy() + current_row = Decimal(0) + current_col = Decimal(0) + current_x = 0 + current_y = self.key_width / 2 + + if isinstance(layout[0], dict): + self.attrs(layout[0]) + layout = layout[1:] + + for row_num, row in enumerate(layout): + self.append([]) + + # Process the current row + for key in row: + if isinstance(key, dict): + if 'w' in key and key['w'] != Decimal(1): + current_key['width'] = Decimal(key['w']) + if 'w2' in key and 'h2' in key and key['w2'] == 1.5 and key['h2'] == 1: + # FIXME: ISO Key uses these params: {x:0.25,w:1.25,h:2,w2:1.5,h2:1,x2:-0.25} + current_key['isoenter'] = True + if 'h' in key and key['h'] != Decimal(1): + current_key['height'] = Decimal(key['h']) + if 'a' in key: + current_key['label_style'] = self.key_skel['label_style'] = int(key['a']) + if current_key['label_style'] < 0: + current_key['label_style'] = 0 + elif current_key['label_style'] > 9: + current_key['label_style'] = 9 + if 'f' in key: + font_size = int(key['f']) + if font_size > 9: + font_size = 9 + elif font_size < 1: + font_size = 1 + current_key['label_size'] = self.key_skel['label_size'] = font_size + if 'p' in key: + current_key['keycap_profile'] = self.key_skel['keycap_profile'] = key['p'] + if 'c' in key: + current_key['keycap_color'] = self.key_skel['keycap_color'] = key['c'] + if 't' in key: + # FIXME: Need to do better validation, plus figure out how to support multiple colors + if '\n' in key['t']: + key['t'] = key['t'].split('\n')[0] + if key['t'] == "0": + key['t'] = "#000000" + current_key['label_color'] = self.key_skel['label_color'] = key['t'] + if 'x' in key: + current_col += Decimal(key['x']) + current_x += Decimal(key['x']) * self.key_width + if 'y' in key: + current_row += Decimal(key['y']) + current_y += Decimal(key['y']) * self.key_width + if 'd' in key: + current_key['decal'] = True + + else: + current_key['name'] = key + current_key['row'] = current_row + current_key['column'] = current_col + + # Determine the X center + x_center = (current_key['width'] * self.key_width) / 2 + current_x += x_center + current_key['x'] = current_x + current_x += x_center + + # Determine the Y center + y_center = (current_key['height'] * self.key_width) / 2 + y_offset = y_center - (self.key_width / 2) + current_key['y'] = (current_y + y_offset) + + # Tend to our row/col count + current_col += current_key['width'] + if current_col > self.columns: + self.columns = current_col + + # Invert the y-axis if neccesary + if self.invert_y: + current_key['y'] = -current_key['y'] + + # Store this key + self[-1].append(current_key) + current_key = self.key_skel.copy() + + # Move to the next row + current_x = 0 + current_y += self.key_width + current_col = Decimal(0) + current_row += Decimal(1) + if current_row > self.rows: + self.rows = Decimal(current_row) diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index e41cc3dcb2..1b83e78c70 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py @@ -10,6 +10,7 @@ from . import doctor from . import hello from . import json from . import list +from . import kle2json from . import new from . import pyformat from . import pytest diff --git a/lib/python/qmk/cli/kle2json.py b/lib/python/qmk/cli/kle2json.py new file mode 100755 index 0000000000..22eb515dff --- /dev/null +++ b/lib/python/qmk/cli/kle2json.py @@ -0,0 +1,79 @@ +"""Convert raw KLE to JSON + +""" +import json +import os +from pathlib import Path +from argparse import FileType +from decimal import Decimal +from collections import OrderedDict + +from milc import cli +from kle2xy import KLE2xy + +from qmk.converter import kle2qmk + + +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 JSONEncoder.default(self, obj) + + +@cli.argument('filename', 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') +def kle2json(cli): + """Convert a KLE layout to QMK's layout format. + """ # If filename is a path + if cli.args.filename.startswith("/") or cli.args.filename.startswith("./"): + file_path = Path(cli.args.filename) + # Otherwise assume it is a file name + else: + file_path = Path(os.environ['ORIG_CWD'], cli.args.filename) + # Check for valid file_path for more graceful failure + if not file_path.exists(): + return cli.log.error('File {fg_cyan}%s{style_reset_all} was not found.', str(file_path)) + out_path = file_path.parent + raw_code = file_path.open().read() + # 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.', str(out_path)) + return False; + try: + # Convert KLE raw to x/y coordinates (using kle2xy package from skullydazed) + kle = KLE2xy(raw_code) + except Exception as e: + cli.log.error('Could not parse KLE raw data: %s', raw_code) + cli.log.exception(e) + # FIXME: This should be better + return cli.log.error('Could not parse KLE raw data.') + 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) + # Write our info.json + file = open(str(out_path) + "/info.json", "w") + file.write(keyboard) + file.close() + cli.log.info('Wrote out {fg_cyan}%s/info.json', str(out_path)) diff --git a/lib/python/qmk/converter.py b/lib/python/qmk/converter.py new file mode 100644 index 0000000000..bbd3531317 --- /dev/null +++ b/lib/python/qmk/converter.py @@ -0,0 +1,33 @@ +"""Functions to convert to and from QMK formats +""" +from collections import OrderedDict + + +def kle2qmk(kle): + """Convert a KLE layout to QMK's layout format. + """ + layout = [] + + for row in kle: + for key in row: + if key['decal']: + continue + + qmk_key = OrderedDict( + label="", + x=key['column'], + y=key['row'], + ) + + if key['width'] != 1: + qmk_key['w'] = key['width'] + if key['height'] != 1: + qmk_key['h'] = key['height'] + if 'name' in key and key['name']: + qmk_key['label'] = key['name'].split('\n', 1)[0] + else: + del (qmk_key['label']) + + layout.append(qmk_key) + + return layout diff --git a/lib/python/qmk/tests/kle.txt b/lib/python/qmk/tests/kle.txt new file mode 100644 index 0000000000..862a899ab9 --- /dev/null +++ b/lib/python/qmk/tests/kle.txt @@ -0,0 +1,5 @@ +["¬\n`","!\n1","\"\n2","£\n3","$\n4","%\n5","^\n6","&\n7","*\n8","(\n9",")\n0","_\n-","+\n=",{w:2},"Backspace"], +[{w:1.5},"Tab","Q","W","E","R","T","Y","U","I","O","P","{\n[","}\n]",{x:0.25,w:1.25,h:2,w2:1.5,h2:1,x2:-0.25},"Enter"], +[{w:1.75},"Caps Lock","A","S","D","F","G","H","J","K","L",":\n;","@\n'","~\n#"], +[{w:1.25},"Shift","|\n\\","Z","X","C","V","B","N","M","<\n,",">\n.","?\n/",{w:2.75},"Shift"], +[{w:1.25},"Ctrl",{w:1.25},"Win",{w:1.25},"Alt",{a:7,w:6.25},"",{a:4,w:1.25},"AltGr",{w:1.25},"Win",{w:1.25},"Menu",{w:1.25},"Ctrl"] diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index 55b8d253f7..d91af992a8 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py @@ -19,6 +19,8 @@ def test_config(): assert result.returncode == 0 assert 'general.color' in result.stdout +def test_kle2json(): + assert check_subcommand('kle2json', 'kle.txt', '-f').returncode == 0 def test_doctor(): result = check_subcommand('doctor') -- cgit v1.2.3 From a4c2a9b083d82d9e7d7fe3a68c0d51ae2280495f Mon Sep 17 00:00:00 2001 From: QMK Bot Date: Wed, 13 Nov 2019 05:24:56 +0000 Subject: format code according to conventions [skip ci] --- lib/python/kle2xy.py | 17 ++++------------- lib/python/qmk/cli/kle2json.py | 10 ++++------ lib/python/qmk/tests/test_cli_commands.py | 2 ++ 3 files changed, 10 insertions(+), 19 deletions(-) (limited to 'lib/python') diff --git a/lib/python/kle2xy.py b/lib/python/kle2xy.py index ea16a4b5ee..9291443190 100644 --- a/lib/python/kle2xy.py +++ b/lib/python/kle2xy.py @@ -4,6 +4,7 @@ import hjson from decimal import Decimal + class KLE2xy(list): """Abstract interface for interacting with a KLE layout. """ @@ -13,17 +14,7 @@ class KLE2xy(list): self.name = name self.invert_y = invert_y self.key_width = Decimal('19.05') - self.key_skel = { - 'decal': False, - 'border_color': 'none', - 'keycap_profile': '', - 'keycap_color': 'grey', - 'label_color': 'black', - 'label_size': 3, - 'label_style': 4, - 'width': Decimal('1'), 'height': Decimal('1'), - 'x': Decimal('0'), 'y': Decimal('0') - } + self.key_skel = {'decal': False, 'border_color': 'none', 'keycap_profile': '', 'keycap_color': 'grey', 'label_color': 'black', 'label_size': 3, 'label_style': 4, 'width': Decimal('1'), 'height': Decimal('1'), 'x': Decimal('0'), 'y': Decimal('0')} self.rows = Decimal(0) self.columns = Decimal(0) @@ -34,13 +25,13 @@ class KLE2xy(list): def width(self): """Returns the width of the keyboard plate. """ - return (Decimal(self.columns) * self.key_width) + self.key_width/2 + return (Decimal(self.columns) * self.key_width) + self.key_width / 2 @property def height(self): """Returns the height of the keyboard plate. """ - return (self.rows * self.key_width) + self.key_width/2 + return (self.rows * self.key_width) + self.key_width / 2 @property def size(self): diff --git a/lib/python/qmk/cli/kle2json.py b/lib/python/qmk/cli/kle2json.py index 22eb515dff..5a4e97e3ab 100755 --- a/lib/python/qmk/cli/kle2json.py +++ b/lib/python/qmk/cli/kle2json.py @@ -31,7 +31,7 @@ class CustomJSONEncoder(json.JSONEncoder): @cli.subcommand('Convert a KLE layout to a Configurator JSON') def kle2json(cli): """Convert a KLE layout to QMK's layout format. - """ # If filename is a path + """ # If filename is a path if cli.args.filename.startswith("/") or cli.args.filename.startswith("./"): file_path = Path(cli.args.filename) # Otherwise assume it is a file name @@ -45,7 +45,7 @@ def kle2json(cli): # 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.', str(out_path)) - return False; + return False try: # Convert KLE raw to x/y coordinates (using kle2xy package from skullydazed) kle = KLE2xy(raw_code) @@ -65,11 +65,9 @@ def kle2json(cli): }}, ) # Initialize keyboard with json encoded from ordered dict - keyboard = json.dumps(keyboard, indent=4, separators=( - ', ', ': '), sort_keys=False, cls=CustomJSONEncoder) + 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) + layout = json.dumps(kle2qmk(kle), separators=(', ', ':'), cls=CustomJSONEncoder) # Replace layout in keyboard json keyboard = keyboard.replace('"LAYOUT_JSON_HERE"', layout) # Write our info.json diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index d91af992a8..85d4d91af1 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py @@ -19,9 +19,11 @@ def test_config(): assert result.returncode == 0 assert 'general.color' in result.stdout + def test_kle2json(): assert check_subcommand('kle2json', 'kle.txt', '-f').returncode == 0 + def test_doctor(): result = check_subcommand('doctor') assert result.returncode == 0 -- cgit v1.2.3 From 897888db419239f013561b155de5993b1966820e Mon Sep 17 00:00:00 2001 From: jorgemanzo Date: Fri, 4 Oct 2019 23:38:34 -0700 Subject: Add CLI command for flashing a keyboard A new CLI subcommand was added, flash, which behaves very similar to the already present compile CLI comamnd, but with the added ability to target a bootloader. The command is used like so: qmk flash [-h] [-b] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]. A -kb and -km is expected, or a configurator export JSON filename. A bootloader can be specified using -bl , and if left unspecified, the target is assumed to be :flash. -bl can be used to list the available bootloaders. If -km is provided, but no -kb , then a message is printed suggesting the user to run qmk list_keyboards. --- lib/python/qmk/cli/__init__.py | 1 + lib/python/qmk/cli/compile.py | 14 +++-- lib/python/qmk/cli/flash.py | 87 +++++++++++++++++++++++++++++++ lib/python/qmk/commands.py | 57 ++++++++++++++++++++ lib/python/qmk/tests/test_cli_commands.py | 3 ++ 5 files changed, 157 insertions(+), 5 deletions(-) create mode 100644 lib/python/qmk/cli/flash.py create mode 100644 lib/python/qmk/commands.py (limited to 'lib/python') diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index 1b83e78c70..72ee38f562 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py @@ -7,6 +7,7 @@ from . import compile from . import config from . import docs from . import doctor +from . import flash from . import hello from . import json from . import list diff --git a/lib/python/qmk/cli/compile.py b/lib/python/qmk/cli/compile.py index 6646891b30..c7093d4215 100755 --- a/lib/python/qmk/cli/compile.py +++ b/lib/python/qmk/cli/compile.py @@ -9,6 +9,9 @@ import subprocess from argparse import FileType from milc import cli +from qmk.commands import create_make_command +from qmk.commands import parse_configurator_json +from qmk.commands import compile_configurator_json import qmk.keymap import qmk.path @@ -30,20 +33,21 @@ def compile(cli): """ if cli.args.filename: # Parse the configurator json - user_keymap = json.load(cli.args.filename) + user_keymap = parse_configurator_json(cli.args.filename) # Generate the keymap keymap_path = qmk.path.keymap(user_keymap['keyboard']) cli.log.info('Creating {fg_cyan}%s{style_reset_all} keymap in {fg_cyan}%s', user_keymap['keymap'], keymap_path) - qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers']) + + # Compile the keymap + command = compile_configurator_json(cli.args.filename) + cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) - # Compile the keymap - command = ['make', ':'.join((user_keymap['keyboard'], user_keymap['keymap']))] elif cli.config.compile.keyboard and cli.config.compile.keymap: # Generate the make command for a specific keyboard/keymap. - command = ['make', ':'.join((cli.config.compile.keyboard, cli.config.compile.keymap))] + command = create_make_command(cli.config.compile.keyboard, cli.config.compile.keymap) else: cli.log.error('You must supply a configurator export or both `--keyboard` and `--keymap`.') diff --git a/lib/python/qmk/cli/flash.py b/lib/python/qmk/cli/flash.py new file mode 100644 index 0000000000..8f7bb55a22 --- /dev/null +++ b/lib/python/qmk/cli/flash.py @@ -0,0 +1,87 @@ +"""Compile and flash QMK Firmware + +You can compile a keymap already in the repo or using a QMK Configurator export. +A bootloader must be specified. +""" +import os +import sys +import subprocess +from argparse import FileType + +from milc import cli +from qmk.commands import create_make_command +from qmk.commands import parse_configurator_json +from qmk.commands import compile_configurator_json + +import qmk.path + + +def print_bootloader_help(): + """Prints the available bootloaders listed in docs.qmk.fm. + """ + cli.log.info('Here are the available bootloaders:') + cli.echo('\tdfu') + cli.echo('\tdfu-ee') + cli.echo('\tdfu-split-left') + cli.echo('\tdfu-split-right') + cli.echo('\tavrdude') + cli.echo('\tBootloadHID') + cli.echo('\tdfu-util') + cli.echo('\tdfu-util-split-left') + cli.echo('\tdfu-util-split-right') + cli.echo('\tst-link-cli') + cli.echo('For more info, visit https://docs.qmk.fm/#/flashing') + +@cli.argument('-bl', '--bootloader', default='flash', help='The flash command, corresponding to qmk\'s make options of bootloaders.') +@cli.argument('filename', nargs='?', arg_only=True, help='The configurator export JSON to compile. Use this if you dont want to specify a keymap and keyboard.') +@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('-b', '--bootloaders', action='store_true', help='List the available bootloaders.') +@cli.subcommand('QMK Flash.') +def flash(cli): + """Compile and or flash QMK Firmware or keyboard/layout + + If a Configurator JSON export is supplied this command will create a new keymap. Keymap and Keyboard arguments + will be ignored. + + If no file is supplied, keymap and keyboard are expected. + + If bootloader is omitted, the one according to the rules.mk will be used. + + """ + command = [] + if cli.args.bootloaders: + # Provide usage and list bootloaders + cli.echo('usage: qmk flash [-h] [-b] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]') + print_bootloader_help() + return False + + elif cli.args.keymap and not cli.args.keyboard: + # If only a keymap was given but no keyboard, suggest listing keyboards + cli.echo('usage: qmk flash [-h] [-b] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]') + cli.log.error('run \'qmk list_keyboards\' to find out the supported keyboards') + return False + + elif cli.args.filename: + # Get keymap path to log info + user_keymap = parse_configurator_json(cli.args.filename) + keymap_path = qmk.path.keymap(user_keymap['keyboard']) + + cli.log.info('Creating {fg_cyan}%s{style_reset_all} keymap in {fg_cyan}%s', user_keymap['keymap'], keymap_path) + + # Convert the JSON into a C file and write it to disk. + command = compile_configurator_json(cli.args.filename, cli.args.bootloader) + + cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) + + elif cli.args.keyboard and cli.args.keymap: + # Generate the make command for a specific keyboard/keymap. + command = create_make_command(cli.config.flash.keyboard, cli.config.flash.keymap, cli.args.bootloader) + + else: + cli.echo('usage: qmk flash [-h] [-b] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]') + cli.log.error('You must supply a configurator export or both `--keyboard` and `--keymap`. You can also specify a bootloader with --bootloader. Use --bootloaders to list the available bootloaders.') + return False + + cli.log.info('Flashing keymap with {fg_cyan}%s\n\n', ' '.join(command)) + subprocess.run(command) \ No newline at end of file diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py new file mode 100644 index 0000000000..9fbf00f165 --- /dev/null +++ b/lib/python/qmk/commands.py @@ -0,0 +1,57 @@ +"""Functions that build make commands +""" +import json +import qmk.keymap + +def create_make_command(keyboard, keymap, target=None): + """Create a make compile command + + Args: + keyboard + The path of the keyboard, for example 'plank' + + keymap + The name of the keymap, for example 'algernon' + + target + Usually a bootloader. + + Returns: + A command that can be run to make the specified keyboard and keymap + """ + if target is None: + return ['make', ':'.join((keyboard, keymap))] + return ['make', ':'.join((keyboard, keymap, target))] + +def parse_configurator_json(configurator_filename): + """Open and parse a configurator json export + """ + file = open(configurator_filename) + user_keymap = json.load(file) + file.close() + return user_keymap + +def compile_configurator_json(configurator_filename, bootloader=None): + """Convert a configurator export JSON file into a C file + + Args: + configurator_filename + The configurator JSON export file + + bootloader + A bootloader to flash + + Returns: + A command to run to compile and flash the C file. + """ + # Parse the configurator json + user_keymap = parse_configurator_json(configurator_filename) + + # Write the keymap C file + qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers']) + + # Return a command that can be run to make the keymap and flash if given + if bootloader is None: + return create_make_command(user_keymap['keyboard'], user_keymap['keymap']) + return create_make_command(user_keymap['keyboard'], user_keymap['keymap'], bootloader) + diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index 85d4d91af1..dcab8bdae4 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py @@ -13,6 +13,9 @@ def test_cformat(): def test_compile(): assert check_subcommand('compile', '-kb', 'handwired/onekey/pytest', '-km', 'default').returncode == 0 +def test_flash(): + assert check_subcommand('flash', '-b').returncode == 1 + assert check_subcommand('flash').returncode == 1 def test_config(): result = check_subcommand('config') -- cgit v1.2.3 From 7891de7f6d64431cedbd580a3f7863533dc8be88 Mon Sep 17 00:00:00 2001 From: QMK Bot Date: Sat, 16 Nov 2019 07:10:19 +0000 Subject: format code according to conventions [skip ci] --- lib/python/qmk/cli/compile.py | 3 +-- lib/python/qmk/cli/flash.py | 3 ++- lib/python/qmk/commands.py | 4 +++- lib/python/qmk/tests/test_cli_commands.py | 2 ++ 4 files changed, 8 insertions(+), 4 deletions(-) (limited to 'lib/python') diff --git a/lib/python/qmk/cli/compile.py b/lib/python/qmk/cli/compile.py index c7093d4215..5c29800096 100755 --- a/lib/python/qmk/cli/compile.py +++ b/lib/python/qmk/cli/compile.py @@ -38,13 +38,12 @@ def compile(cli): # Generate the keymap keymap_path = qmk.path.keymap(user_keymap['keyboard']) cli.log.info('Creating {fg_cyan}%s{style_reset_all} keymap in {fg_cyan}%s', user_keymap['keymap'], keymap_path) - + # Compile the keymap command = compile_configurator_json(cli.args.filename) cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) - elif cli.config.compile.keyboard and cli.config.compile.keymap: # Generate the make command for a specific keyboard/keymap. command = create_make_command(cli.config.compile.keyboard, cli.config.compile.keymap) diff --git a/lib/python/qmk/cli/flash.py b/lib/python/qmk/cli/flash.py index 8f7bb55a22..37556aaafd 100644 --- a/lib/python/qmk/cli/flash.py +++ b/lib/python/qmk/cli/flash.py @@ -32,6 +32,7 @@ def print_bootloader_help(): cli.echo('\tst-link-cli') cli.echo('For more info, visit https://docs.qmk.fm/#/flashing') + @cli.argument('-bl', '--bootloader', default='flash', help='The flash command, corresponding to qmk\'s make options of bootloaders.') @cli.argument('filename', nargs='?', arg_only=True, help='The configurator export JSON to compile. Use this if you dont want to specify a keymap and keyboard.') @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.') @@ -84,4 +85,4 @@ def flash(cli): return False cli.log.info('Flashing keymap with {fg_cyan}%s\n\n', ' '.join(command)) - subprocess.run(command) \ No newline at end of file + subprocess.run(command) diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py index 9fbf00f165..f83a89578e 100644 --- a/lib/python/qmk/commands.py +++ b/lib/python/qmk/commands.py @@ -3,6 +3,7 @@ import json import qmk.keymap + def create_make_command(keyboard, keymap, target=None): """Create a make compile command @@ -23,6 +24,7 @@ def create_make_command(keyboard, keymap, target=None): return ['make', ':'.join((keyboard, keymap))] return ['make', ':'.join((keyboard, keymap, target))] + def parse_configurator_json(configurator_filename): """Open and parse a configurator json export """ @@ -31,6 +33,7 @@ def parse_configurator_json(configurator_filename): file.close() return user_keymap + def compile_configurator_json(configurator_filename, bootloader=None): """Convert a configurator export JSON file into a C file @@ -54,4 +57,3 @@ def compile_configurator_json(configurator_filename, bootloader=None): if bootloader is None: return create_make_command(user_keymap['keyboard'], user_keymap['keymap']) return create_make_command(user_keymap['keyboard'], user_keymap['keymap'], bootloader) - diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index dcab8bdae4..3f75cef3e1 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py @@ -13,10 +13,12 @@ def test_cformat(): def test_compile(): assert check_subcommand('compile', '-kb', 'handwired/onekey/pytest', '-km', 'default').returncode == 0 + def test_flash(): assert check_subcommand('flash', '-b').returncode == 1 assert check_subcommand('flash').returncode == 1 + def test_config(): result = check_subcommand('config') assert result.returncode == 0 -- cgit v1.2.3 From 9c58da6b121ca36f616f8a9a78dd4c2234bd5942 Mon Sep 17 00:00:00 2001 From: skullydazed Date: Mon, 18 Nov 2019 14:54:50 -0800 Subject: Improve a number of things about how MILC operates (#7344) * Pull in updates for MILC * Remove the shadow argparser * Make it easier to reason about arguments and how they're translated into the config tree * Populate self.config during init to support setting user.qmk_home for the global CLI * Remove the short argument -c so that we can unambiguously determine the config file location without doing full argument processing * Remove the --save-config option as it's a little confusing anyway * Use Pathlib for path manipulation * Fix commands with no arguments --- lib/python/milc.py | 167 ++++++++++++++++++++++++++--------------------------- 1 file changed, 82 insertions(+), 85 deletions(-) (limited to 'lib/python') diff --git a/lib/python/milc.py b/lib/python/milc.py index 7b130bdea6..e8599eff3f 100644 --- a/lib/python/milc.py +++ b/lib/python/milc.py @@ -20,6 +20,7 @@ import re import shlex import sys from decimal import Decimal +from pathlib import Path from tempfile import NamedTemporaryFile from time import sleep @@ -39,7 +40,7 @@ import colorama from appdirs import user_config_dir # Disable logging until we can configure it how the user wants -logging.basicConfig(filename='/dev/null') +logging.basicConfig(stream=os.devnull) # Log Level Representations EMOJI_LOGLEVELS = { @@ -96,7 +97,6 @@ def format_ansi(text): class ANSIFormatter(logging.Formatter): """A log formatter that inserts ANSI color. """ - def format(self, record): msg = super(ANSIFormatter, self).format(record) return format_ansi(msg) @@ -105,7 +105,6 @@ class ANSIFormatter(logging.Formatter): class ANSIEmojiLoglevelFormatter(ANSIFormatter): """A log formatter that makes the loglevel an emoji on UTF capable terminals. """ - def format(self, record): if UNICODE_SUPPORT: record.levelname = EMOJI_LOGLEVELS[record.levelname].format(**ansi_colors) @@ -115,7 +114,6 @@ class ANSIEmojiLoglevelFormatter(ANSIFormatter): class ANSIStrippingFormatter(ANSIFormatter): """A log formatter that strips ANSI. """ - def format(self, record): msg = super(ANSIStrippingFormatter, self).format(record) return ansi_escape.sub('', msg) @@ -127,7 +125,6 @@ class Configuration(object): This class never raises IndexError, instead it will return None if a section or option does not yet exist. """ - def __contains__(self, key): return self._config.__contains__(key) @@ -214,9 +211,8 @@ def handle_store_boolean(self, *args, **kwargs): class SubparserWrapper(object): - """Wrap subparsers so we can populate the normal and the shadow parser. + """Wrap subparsers so we can track what options the user passed. """ - def __init__(self, cli, submodule, subparser): self.cli = cli self.submodule = submodule @@ -232,26 +228,30 @@ class SubparserWrapper(object): self.subparser.completer = completer def add_argument(self, *args, **kwargs): + """Add an argument for this subcommand. + + This also stores the default for the argument in `self.cli.default_arguments`. + """ if 'action' in kwargs and kwargs['action'] == 'store_boolean': + # Store boolean will call us again with the enable/disable flag arguments return handle_store_boolean(self, *args, **kwargs) self.cli.acquire_lock() self.subparser.add_argument(*args, **kwargs) - - if 'default' in kwargs: - del kwargs['default'] - if 'action' in kwargs and kwargs['action'] == 'store_false': - kwargs['action'] == 'store_true' - self.cli.subcommands_default[self.submodule].add_argument(*args, **kwargs) + if self.submodule not in self.cli.default_arguments: + self.cli.default_arguments[self.submodule] = {} + self.cli.default_arguments[self.submodule][self.cli.get_argument_name(*args, **kwargs)] = kwargs.get('default') self.cli.release_lock() class MILC(object): """MILC - An Opinionated Batteries Included Framework """ - def __init__(self): """Initialize the MILC object. + + version + The version string to associate with your CLI program """ # Setup a lock for thread safety self._lock = threading.RLock() if thread else None @@ -263,9 +263,10 @@ class MILC(object): self._inside_context_manager = False self.ansi = ansi_colors self.arg_only = [] - self.config = Configuration() + self.config = None self.config_file = None - self.version = os.environ.get('QMK_VERSION', 'unknown') + self.default_arguments = {} + self.version = 'unknown' self.release_lock() # Figure out our program name @@ -273,6 +274,7 @@ class MILC(object): self.prog_name = self.prog_name.split('/')[-1] # Initialize all the things + self.read_config_file() self.initialize_argparse() self.initialize_logging() @@ -282,7 +284,7 @@ class MILC(object): @description.setter def description(self, value): - self._description = self._arg_parser.description = self._arg_defaults.description = value + self._description = self._arg_parser.description = value def echo(self, text, *args, **kwargs): """Print colorized text to stdout. @@ -311,12 +313,9 @@ class MILC(object): self.acquire_lock() self.subcommands = {} - self.subcommands_default = {} self._subparsers = None - self._subparsers_default = None self.argwarn = argcomplete.warn self.args = None - self._arg_defaults = argparse.ArgumentParser(**kwargs) self._arg_parser = argparse.ArgumentParser(**kwargs) self.set_defaults = self._arg_parser.set_defaults self.print_usage = self._arg_parser.print_usage @@ -329,25 +328,18 @@ class MILC(object): self._arg_parser.completer = completer def add_argument(self, *args, **kwargs): - """Wrapper to add arguments to both the main and the shadow argparser. + """Wrapper to add arguments and track whether they were passed on the command line. """ if 'action' in kwargs and kwargs['action'] == 'store_boolean': return handle_store_boolean(self, *args, **kwargs) - if kwargs.get('add_dest', True) and args[0][0] == '-': - kwargs['dest'] = 'general_' + self.get_argument_name(*args, **kwargs) - if 'add_dest' in kwargs: - del kwargs['add_dest'] - self.acquire_lock() + self._arg_parser.add_argument(*args, **kwargs) + if 'general' not in self.default_arguments: + self.default_arguments['general'] = {} + self.default_arguments['general'][self.get_argument_name(*args, **kwargs)] = kwargs.get('default') - # Populate the shadow parser - if 'default' in kwargs: - del kwargs['default'] - if 'action' in kwargs and kwargs['action'] == 'store_false': - kwargs['action'] == 'store_true' - self._arg_defaults.add_argument(*args, **kwargs) self.release_lock() def initialize_logging(self): @@ -374,15 +366,14 @@ class MILC(object): self.add_argument('--log-file-fmt', default='[%(levelname)s] [%(asctime)s] [file:%(pathname)s] [line:%(lineno)d] %(message)s', help='Format string for log file.') self.add_argument('--log-file', help='File to write log messages to') self.add_argument('--color', action='store_boolean', default=True, help='color in output') - self.add_argument('-c', '--config-file', help='The config file to read and/or write') - self.add_argument('--save-config', action='store_true', help='Save the running configuration to the config file') + self.add_argument('--config-file', help='The location for the configuration file') + self.arg_only.append('config_file') def add_subparsers(self, title='Sub-commands', **kwargs): if self._inside_context_manager: raise RuntimeError('You must run this before the with statement!') self.acquire_lock() - self._subparsers_default = self._arg_defaults.add_subparsers(title=title, dest='subparsers', **kwargs) self._subparsers = self._arg_parser.add_subparsers(title=title, dest='subparsers', **kwargs) self.release_lock() @@ -404,10 +395,12 @@ class MILC(object): if self.config_file: return self.config_file - if self.args and self.args.general_config_file: - return self.args.general_config_file + if '--config-file' in sys.argv: + return Path(sys.argv[sys.argv.index('--config-file') + 1]).expanduser().resolve() - return os.path.join(user_config_dir(appname='qmk', appauthor='QMK'), '%s.ini' % self.prog_name) + filedir = user_config_dir(appname='qmk', appauthor='QMK') + filename = '%s.ini' % self.prog_name + return Path(filedir) / filename def get_argument_name(self, *args, **kwargs): """Takes argparse arguments and returns the dest name. @@ -446,7 +439,7 @@ class MILC(object): def arg_passed(self, arg): """Returns True if arg was passed on the command line. """ - return self.args_passed[arg] in (None, False) + return self.default_arguments.get(arg) != self.args[arg] def parse_args(self): """Parse the CLI args. @@ -459,25 +452,22 @@ class MILC(object): self.acquire_lock() self.args = self._arg_parser.parse_args() - self.args_passed = self._arg_defaults.parse_args() if 'entrypoint' in self.args: self._entrypoint = self.args.entrypoint - if self.args.general_config_file: - self.config_file = self.args.general_config_file - self.release_lock() - def read_config(self): - """Parse the configuration file and determine the runtime configuration. + def read_config_file(self): + """Read in the configuration file and store it in self.config. """ self.acquire_lock() + self.config = Configuration() self.config_file = self.find_config_file() - if self.config_file and os.path.exists(self.config_file): + if self.config_file and self.config_file.exists(): config = RawConfigParser(self.config) - config.read(self.config_file) + config.read(str(self.config_file)) # Iterate over the config file options and write them into self.config for section in config.sections(): @@ -487,8 +477,10 @@ class MILC(object): # Coerce values into useful datatypes if value.lower() in ['1', 'yes', 'true', 'on']: value = True - elif value.lower() in ['0', 'no', 'false', 'none', 'off']: + elif value.lower() in ['0', 'no', 'false', 'off']: value = False + elif value.lower() in ['none']: + continue elif value.replace('.', '').isdigit(): if '.' in value: value = Decimal(value) @@ -497,32 +489,44 @@ class MILC(object): self.config[section][option] = value - # Fold the CLI args into self.config + self.release_lock() + + def merge_args_into_config(self): + """Merge CLI arguments into self.config to create the runtime configuration. + """ + self.acquire_lock() for argument in vars(self.args): if argument in ('subparsers', 'entrypoint'): continue - if '_' in argument: - section, option = argument.split('_', 1) - else: - section = self._entrypoint.__name__ - option = argument - - if option not in self.arg_only: - if hasattr(self.args_passed, argument): + if argument not in self.arg_only: + # Find the argument's section + if self._entrypoint.__name__ in self.default_arguments and argument in self.default_arguments[self._entrypoint.__name__]: + argument_found = True + section = self._entrypoint.__name__ + if argument in self.default_arguments['general']: + argument_found = True + section = 'general' + + if not argument_found: + raise RuntimeError('Could not find argument in `self.default_arguments`. This should be impossible!') + exit(1) + + # Merge this argument into self.config + if argument in self.default_arguments: arg_value = getattr(self.args, argument) if arg_value: - self.config[section][option] = arg_value + self.config[section][argument] = arg_value else: - if option not in self.config[section]: - self.config[section][option] = getattr(self.args, argument) + if argument not in self.config[section]: + self.config[section][argument] = getattr(self.args, argument) self.release_lock() def save_config(self): """Save the current configuration to the config file. """ - self.log.debug("Saving config file to '%s'", self.config_file) + self.log.debug("Saving config file to '%s'", str(self.config_file)) if not self.config_file: self.log.warning('%s.config_file file not set, not saving config!', self.__class__.__name__) @@ -530,31 +534,34 @@ class MILC(object): self.acquire_lock() + # Generate a sanitized version of our running configuration config = RawConfigParser() - config_dir = os.path.dirname(self.config_file) - for section_name, section in self.config._config.items(): config.add_section(section_name) for option_name, value in section.items(): if section_name == 'general': - if option_name in ['save_config']: + if option_name in ['config_file']: continue - config.set(section_name, option_name, str(value)) + if value is not None: + config.set(section_name, option_name, str(value)) - if not os.path.exists(config_dir): - os.makedirs(config_dir) + # Write out the config file + config_dir = self.config_file.parent + if not config_dir.exists(): + config_dir.mkdir(parents=True, exist_ok=True) - with NamedTemporaryFile(mode='w', dir=config_dir, delete=False) as tmpfile: + with NamedTemporaryFile(mode='w', dir=str(config_dir), delete=False) as tmpfile: config.write(tmpfile) # Move the new config file into place atomically if os.path.getsize(tmpfile.name) > 0: - os.rename(tmpfile.name, self.config_file) + os.rename(tmpfile.name, str(self.config_file)) else: - self.log.warning('Config file saving failed, not replacing %s with %s.', self.config_file, tmpfile.name) + self.log.warning('Config file saving failed, not replacing %s with %s.', str(self.config_file), tmpfile.name) + # Housekeeping self.release_lock() - cli.log.info('Wrote configuration to %s', shlex.quote(self.config_file)) + cli.log.info('Wrote configuration to %s', shlex.quote(str(self.config_file))) def __call__(self): """Execute the entrypoint function. @@ -603,16 +610,11 @@ class MILC(object): name = handler.__name__.replace("_", "-") self.acquire_lock() + kwargs['help'] = description - self.subcommands_default[name] = self._subparsers_default.add_parser(name, **kwargs) self.subcommands[name] = SubparserWrapper(self, name, self._subparsers.add_parser(name, **kwargs)) self.subcommands[name].set_defaults(entrypoint=handler) - if name not in self.__dict__: - self.__dict__[name] = self.subcommands[name] - else: - self.log.debug("Could not add subcommand '%s' to attributes, key already exists!", name) - self.release_lock() return handler @@ -620,7 +622,6 @@ class MILC(object): def subcommand(self, description, **kwargs): """Decorator to register a subcommand. """ - def subcommand_function(handler): return self.add_subcommand(handler, description, **kwargs) @@ -644,9 +645,9 @@ class MILC(object): self.log_format = self.config['general']['log_fmt'] if self.config.general.color: - self.log_format = ANSIEmojiLoglevelFormatter(self.args.general_log_fmt, self.config.general.datetime_fmt) + self.log_format = ANSIEmojiLoglevelFormatter(self.args.log_fmt, self.config.general.datetime_fmt) else: - self.log_format = ANSIStrippingFormatter(self.args.general_log_fmt, self.config.general.datetime_fmt) + self.log_format = ANSIStrippingFormatter(self.args.log_fmt, self.config.general.datetime_fmt) if self.log_file: self.log_file_handler = logging.FileHandler(self.log_file, self.log_file_mode) @@ -673,13 +674,9 @@ class MILC(object): colorama.init() self.parse_args() - self.read_config() + self.merge_args_into_config() self.setup_logging() - if 'save_config' in self.config.general and self.config.general.save_config: - self.save_config() - exit(0) - return self def __exit__(self, exc_type, exc_val, exc_tb): -- cgit v1.2.3 From f7bdc54c697ff24bec1bd0781666ac05401bafb2 Mon Sep 17 00:00:00 2001 From: skullydazed Date: Wed, 20 Nov 2019 14:54:18 -0800 Subject: Add flake8 to our test suite and fix all errors (#7379) * Add flake8 to our test suite and fix all errors * Add some documentation --- lib/python/kle2xy.py | 2 +- lib/python/qmk/cli/compile.py | 3 -- lib/python/qmk/cli/config.py | 94 ++++++++++++++++++++++-------------- lib/python/qmk/cli/doctor.py | 2 - lib/python/qmk/cli/flash.py | 10 +--- lib/python/qmk/cli/json/keymap.py | 1 - lib/python/qmk/cli/kle2json.py | 4 +- lib/python/qmk/cli/list/keyboards.py | 26 +++++----- lib/python/qmk/cli/pytest.py | 16 +++--- lib/python/qmk/keymap.py | 4 -- lib/python/qmk/path.py | 1 - 11 files changed, 80 insertions(+), 83 deletions(-) (limited to 'lib/python') diff --git a/lib/python/kle2xy.py b/lib/python/kle2xy.py index 9291443190..bff1d025b7 100644 --- a/lib/python/kle2xy.py +++ b/lib/python/kle2xy.py @@ -46,7 +46,7 @@ class KLE2xy(list): if 'name' in properties: self.name = properties['name'] - def parse_layout(self, layout): + def parse_layout(self, layout): # noqa FIXME(skullydazed): flake8 says this has a complexity of 25, it should be refactored. # Wrap this in a dictionary so hjson will parse KLE raw data layout = '{"layout": [' + layout + ']}' layout = hjson.loads(layout)['layout'] diff --git a/lib/python/qmk/cli/compile.py b/lib/python/qmk/cli/compile.py index 5c29800096..234ffb12ca 100755 --- a/lib/python/qmk/cli/compile.py +++ b/lib/python/qmk/cli/compile.py @@ -2,9 +2,6 @@ You can compile a keymap already in the repo or using a QMK Configurator export. """ -import json -import os -import sys import subprocess from argparse import FileType diff --git a/lib/python/qmk/cli/config.py b/lib/python/qmk/cli/config.py index c4ee20cba5..e17d8bb9ba 100644 --- a/lib/python/qmk/cli/config.py +++ b/lib/python/qmk/cli/config.py @@ -1,8 +1,5 @@ """Read and write configuration settings """ -import os -import subprocess - from milc import cli @@ -12,6 +9,54 @@ def print_config(section, key): cli.echo('%s.%s{fg_cyan}={fg_reset}%s', section, key, cli.config[section][key]) +def show_config(): + """Print the current configuration to stdout. + """ + for section in cli.config: + for key in cli.config[section]: + print_config(section, key) + + +def parse_config_token(config_token): + """Split a user-supplied configuration-token into its components. + """ + section = option = value = None + + if '=' in config_token and '.' not in config_token: + cli.log.error('Invalid configuration token, the key must be of the form
.