Example #1
0
    def wrapper(*args, **kwargs):
        # Check to make sure their copy of MILC supports config_source
        if not hasattr(cli, 'config_source'):
            cli.log.error("This subcommand requires a newer version of the QMK CLI. Please upgrade using `pip3 install --upgrade qmk` or your package manager.")
            exit(1)

        # Ensure that `--keymap` was not passed and that we're under `qmk_firmware`
        if cli.config_source[cli._entrypoint.__name__]['keymap'] != 'argument':
            relative_cwd = under_qmk_firmware()

            if relative_cwd and len(relative_cwd.parts) > 1:
                # If we're in `qmk_firmware/keyboards` and `keymaps` is in our path, try to find the keyboard name.
                if relative_cwd.parts[0] == 'keyboards' and 'keymaps' in relative_cwd.parts:
                    current_path = Path('/'.join(relative_cwd.parts[1:]))  # Strip 'keyboards' from the front

                    if 'keymaps' in current_path.parts and current_path.name != 'keymaps':
                        while current_path.parent.name != 'keymaps':
                            current_path = current_path.parent
                        cli.config[cli._entrypoint.__name__]['keymap'] = current_path.name
                        cli.config_source[cli._entrypoint.__name__]['keyboard'] = 'keymap_directory'

                # If we're in `qmk_firmware/layouts` guess the name from the community keymap they're in
                elif relative_cwd.parts[0] == 'layouts' and is_keymap_dir(relative_cwd):
                    cli.config[cli._entrypoint.__name__]['keymap'] = relative_cwd.name
                    cli.config_source[cli._entrypoint.__name__]['keyboard'] = 'layouts_directory'

                # If we're in `qmk_firmware/users` guess the name from the userspace they're in
                elif relative_cwd.parts[0] == 'users':
                    # Guess the keymap name based on which userspace they're in
                    cli.config[cli._entrypoint.__name__]['keymap'] = relative_cwd.parts[1]
                    cli.config_source[cli._entrypoint.__name__]['keyboard'] = 'users_directory'

        return func(*args, **kwargs)
Example #2
0
    def wrapper(*args, **kwargs):
        # Check to make sure their copy of MILC supports config_source
        if not hasattr(cli, 'config_source'):
            cli.log.error(
                "This subcommand requires a newer version of the QMK CLI. Please upgrade using `pip3 install --upgrade qmk` or your package manager."
            )
            exit(1)

        # Ensure that `--keyboard` was not passed and CWD is under `qmk_firmware/keyboards`
        if cli.config_source[
                cli._entrypoint.__name__]['keyboard'] != 'argument':
            relative_cwd = under_qmk_firmware()

            if relative_cwd and len(
                    relative_cwd.parts
            ) > 1 and relative_cwd.parts[0] == 'keyboards':
                # Attempt to extract the keyboard name from the current directory
                current_path = Path('/'.join(relative_cwd.parts[1:]))

                if 'keymaps' in current_path.parts:
                    # Strip current_path of anything after `keymaps`
                    keymap_index = len(
                        current_path.parts) - current_path.parts.index(
                            'keymaps') - 1
                    current_path = current_path.parents[keymap_index]

                if is_keyboard(current_path):
                    cli.config[cli._entrypoint.__name__]['keyboard'] = str(
                        current_path)
                    cli.config_source[cli._entrypoint.__name__][
                        'keyboard'] = 'keyboard_directory'

        return func(*args, **kwargs)
Example #3
0
def find_keyboard_from_dir():
    """Returns a keyboard name based on the user's current directory.
    """
    relative_cwd = under_qmk_firmware()

    if relative_cwd and len(
            relative_cwd.parts) > 1 and relative_cwd.parts[0] == 'keyboards':
        # Attempt to extract the keyboard name from the current directory
        current_path = Path('/'.join(relative_cwd.parts[1:]))

        if 'keymaps' in current_path.parts:
            # Strip current_path of anything after `keymaps`
            keymap_index = len(
                current_path.parts) - current_path.parts.index('keymaps') - 1
            current_path = current_path.parents[keymap_index]

        if is_keyboard(current_path):
            return str(current_path)
Example #4
0
def cd(cli):
    """Go to QMK Home
    """
    if not sys.stdout.isatty():
        cli.log.error(
            "This command is for interactive usage only. For non-interactive usage, 'cd $(qmk env QMK_HOME)' is more robust."
        )
        sys.exit(1)

    if not under_qmk_firmware():
        # Only do anything if the user is not under qmk_firmware already
        # in order to reduce the possibility of starting multiple shells
        cli.log.info("Spawning a subshell in your QMK_HOME directory.")
        cli.log.info("Type 'exit' to get back to the parent shell.")
        if not cli.platform.lower().startswith('windows'):
            # For Linux/Mac/etc
            # Check the user's login shell from 'passwd'
            # alternatively fall back to $SHELL env var
            # and finally to '/bin/bash'.
            import getpass
            import pwd
            shell = pwd.getpwnam(getpass.getuser()).pw_shell
            if not shell:
                shell = os.environ.get('SHELL', '/bin/bash')
            # Start the new subshell
            os.execl(shell, shell)
        else:
            # For Windows
            # Check the $SHELL env var
            # and fall back to '/usr/bin/bash'.
            qmk_env = os.environ.copy()
            # Set the prompt for the new shell
            qmk_env['MSYS2_PS1'] = qmk_env['PS1']
            # Start the new subshell
            subprocess.run([os.environ.get('SHELL', '/usr/bin/bash')],
                           env=qmk_env)
    else:
        cli.log.info("Already within qmk_firmware directory.")
Example #5
0
def find_keyboard_keymap():
    """Returns `(keyboard_name, keymap_name)` based on the user's current environment.

    This determines the keyboard and keymap name using the following precedence order:

        * Command line flags (--keyboard and --keymap)
        * Current working directory
            * `keyboards/<keyboard_name>`
            * `keyboards/<keyboard_name>/keymaps/<keymap_name>`
            * `layouts/**/<keymap_name>`
            * `users/<keymap_name>`
        * Configuration
            * cli.config.<subcommand>.keyboard
            * cli.config.<subcommand>.keymap
    """
    # Check to make sure their copy of MILC supports config_source
    if not hasattr(cli, 'config_source'):
        cli.log.error(
            "Your QMK CLI is out of date. Please upgrade using pip3 or your package manager."
        )
        exit(1)

    # State variables
    relative_cwd = under_qmk_firmware()
    keyboard_name = ""
    keymap_name = ""

    # If the keyboard or keymap are passed as arguments use that in preference to anything else
    if cli.config_source[cli._entrypoint.__name__]['keyboard'] == 'argument':
        keyboard_name = cli.config[cli._entrypoint.__name__]['keyboard']
    if cli.config_source[cli._entrypoint.__name__]['keymap'] == 'argument':
        keymap_name = cli.config[cli._entrypoint.__name__]['keymap']

    if not keyboard_name or not keymap_name:
        # If we don't have a keyboard_name and keymap_name from arguments try to derive one or both
        if relative_cwd and relative_cwd.parts and relative_cwd.parts[
                0] == 'keyboards':
            # Try to determine the keyboard and/or keymap name
            current_path = Path('/'.join(relative_cwd.parts[1:]))

            if current_path.parts[-2] == 'keymaps':
                if not keymap_name:
                    keymap_name = current_path.parts[-1]
                if not keyboard_name:
                    keyboard_name = '/'.join(current_path.parts[:-2])
            elif not keyboard_name and is_keyboard(current_path):
                keyboard_name = str(current_path)

        elif relative_cwd and relative_cwd.parts and relative_cwd.parts[
                0] == 'layouts':
            # Try to determine the keymap name from the community layout
            if is_keymap_dir(relative_cwd) and not keymap_name:
                keymap_name = relative_cwd.name

        elif relative_cwd and relative_cwd.parts and relative_cwd.parts[
                0] == 'users':
            # Try to determine the keymap name based on which userspace they're in
            if not keymap_name and len(relative_cwd.parts) > 1:
                keymap_name = relative_cwd.parts[1]

    # If we still don't have a keyboard and keymap check the config
    if not keyboard_name and cli.config[cli._entrypoint.__name__]['keyboard']:
        keyboard_name = cli.config[cli._entrypoint.__name__]['keyboard']

    if not keymap_name and cli.config[cli._entrypoint.__name__]['keymap']:
        keymap_name = cli.config[cli._entrypoint.__name__]['keymap']

    return (keyboard_name, keymap_name)