You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

826 lines
29 KiB

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 <code@noahfrederick.com> * Apply suggestions from code review Co-Authored-By: Noah Frederick <code@noahfrederick.com> * Remove pip3 from the test runner
4 years ago
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 <code@noahfrederick.com> * Apply suggestions from code review Co-Authored-By: Noah Frederick <code@noahfrederick.com> * Remove pip3 from the test runner
4 years ago
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 <code@noahfrederick.com> * Apply suggestions from code review Co-Authored-By: Noah Frederick <code@noahfrederick.com> * Remove pip3 from the test runner
4 years ago
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 <code@noahfrederick.com> * Apply suggestions from code review Co-Authored-By: Noah Frederick <code@noahfrederick.com> * Remove pip3 from the test runner
4 years ago
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 <code@noahfrederick.com> * Apply suggestions from code review Co-Authored-By: Noah Frederick <code@noahfrederick.com> * Remove pip3 from the test runner
4 years ago
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 <code@noahfrederick.com> * Apply suggestions from code review Co-Authored-By: Noah Frederick <code@noahfrederick.com> * Remove pip3 from the test runner
4 years ago
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 <code@noahfrederick.com> * Apply suggestions from code review Co-Authored-By: Noah Frederick <code@noahfrederick.com> * Remove pip3 from the test runner
4 years ago
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 <code@noahfrederick.com> * Apply suggestions from code review Co-Authored-By: Noah Frederick <code@noahfrederick.com> * Remove pip3 from the test runner
4 years ago
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 <code@noahfrederick.com> * Apply suggestions from code review Co-Authored-By: Noah Frederick <code@noahfrederick.com> * Remove pip3 from the test runner
4 years ago
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 <code@noahfrederick.com> * Apply suggestions from code review Co-Authored-By: Noah Frederick <code@noahfrederick.com> * Remove pip3 from the test runner
4 years ago
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 <code@noahfrederick.com> * Apply suggestions from code review Co-Authored-By: Noah Frederick <code@noahfrederick.com> * Remove pip3 from the test runner
4 years ago
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 <code@noahfrederick.com> * Apply suggestions from code review Co-Authored-By: Noah Frederick <code@noahfrederick.com> * Remove pip3 from the test runner
4 years ago
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 <code@noahfrederick.com> * Apply suggestions from code review Co-Authored-By: Noah Frederick <code@noahfrederick.com> * Remove pip3 from the test runner
4 years ago
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 <code@noahfrederick.com> * Apply suggestions from code review Co-Authored-By: Noah Frederick <code@noahfrederick.com> * Remove pip3 from the test runner
4 years ago
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 <code@noahfrederick.com> * Apply suggestions from code review Co-Authored-By: Noah Frederick <code@noahfrederick.com> * Remove pip3 from the test runner
4 years ago
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 <code@noahfrederick.com> * Apply suggestions from code review Co-Authored-By: Noah Frederick <code@noahfrederick.com> * Remove pip3 from the test runner
4 years ago
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 <code@noahfrederick.com> * Apply suggestions from code review Co-Authored-By: Noah Frederick <code@noahfrederick.com> * Remove pip3 from the test runner
4 years ago
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 <code@noahfrederick.com> * Apply suggestions from code review Co-Authored-By: Noah Frederick <code@noahfrederick.com> * Remove pip3 from the test runner
4 years ago
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 <code@noahfrederick.com> * Apply suggestions from code review Co-Authored-By: Noah Frederick <code@noahfrederick.com> * Remove pip3 from the test runner
4 years ago
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 <code@noahfrederick.com> * Apply suggestions from code review Co-Authored-By: Noah Frederick <code@noahfrederick.com> * Remove pip3 from the test runner
4 years ago
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 <code@noahfrederick.com> * Apply suggestions from code review Co-Authored-By: Noah Frederick <code@noahfrederick.com> * Remove pip3 from the test runner
4 years ago
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 <code@noahfrederick.com> * Apply suggestions from code review Co-Authored-By: Noah Frederick <code@noahfrederick.com> * Remove pip3 from the test runner
4 years ago
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 <code@noahfrederick.com> * Apply suggestions from code review Co-Authored-By: Noah Frederick <code@noahfrederick.com> * Remove pip3 from the test runner
4 years ago
  1. #!/usr/bin/env python3
  2. # coding=utf-8
  3. """MILC - A CLI Framework
  4. PYTHON_ARGCOMPLETE_OK
  5. MILC is an opinionated framework for writing CLI apps. It optimizes for the
  6. most common unix tool pattern- small tools that are run from the command
  7. line but generally do not feature any user interaction while they run.
  8. For more details see the MILC documentation:
  9. <https://github.com/clueboard/milc/tree/master/docs>
  10. """
  11. from __future__ import division, print_function, unicode_literals
  12. import argparse
  13. import logging
  14. import os
  15. import re
  16. import shlex
  17. import subprocess
  18. import sys
  19. from decimal import Decimal
  20. from pathlib import Path
  21. from platform import platform
  22. from tempfile import NamedTemporaryFile
  23. from time import sleep
  24. try:
  25. from ConfigParser import RawConfigParser
  26. except ImportError:
  27. from configparser import RawConfigParser
  28. try:
  29. import thread
  30. import threading
  31. except ImportError:
  32. thread = None
  33. import argcomplete
  34. import colorama
  35. from appdirs import user_config_dir
  36. # Disable logging until we can configure it how the user wants
  37. logging.basicConfig(stream=os.devnull)
  38. # Log Level Representations
  39. EMOJI_LOGLEVELS = {
  40. 'CRITICAL': '{bg_red}{fg_white}¬_¬{style_reset_all}',
  41. 'ERROR': '{fg_red}☒{style_reset_all}',
  42. 'WARNING': '{fg_yellow}⚠{style_reset_all}',
  43. 'INFO': '{fg_blue}ℹ{style_reset_all}',
  44. 'DEBUG': '{fg_cyan}☐{style_reset_all}',
  45. 'NOTSET': '{style_reset_all}¯\\_(o_o)_/¯'
  46. }
  47. EMOJI_LOGLEVELS['FATAL'] = EMOJI_LOGLEVELS['CRITICAL']
  48. EMOJI_LOGLEVELS['WARN'] = EMOJI_LOGLEVELS['WARNING']
  49. UNICODE_SUPPORT = sys.stdout.encoding.lower().startswith('utf')
  50. # ANSI Color setup
  51. # Regex was gratefully borrowed from kfir on stackoverflow:
  52. # https://stackoverflow.com/a/45448194
  53. ansi_regex = r'\x1b(' \
  54. r'(\[\??\d+[hl])|' \
  55. r'([=<>a-kzNM78])|' \
  56. r'([\(\)][a-b0-2])|' \
  57. r'(\[\d{0,2}[ma-dgkjqi])|' \
  58. r'(\[\d+;\d+[hfy]?)|' \
  59. r'(\[;?[hf])|' \
  60. r'(#[3-68])|' \
  61. r'([01356]n)|' \
  62. r'(O[mlnp-z]?)|' \
  63. r'(/Z)|' \
  64. r'(\d+)|' \
  65. r'(\[\?\d;\d0c)|' \
  66. r'(\d;\dR))'
  67. ansi_escape = re.compile(ansi_regex, flags=re.IGNORECASE)
  68. ansi_styles = (
  69. ('fg', colorama.ansi.AnsiFore()),
  70. ('bg', colorama.ansi.AnsiBack()),
  71. ('style', colorama.ansi.AnsiStyle()),
  72. )
  73. ansi_colors = {}
  74. for prefix, obj in ansi_styles:
  75. for color in [x for x in obj.__dict__ if not x.startswith('_')]:
  76. ansi_colors[prefix + '_' + color.lower()] = getattr(obj, color)
  77. def format_ansi(text):
  78. """Return a copy of text with certain strings replaced with ansi.
  79. """
  80. # Avoid .format() so we don't have to worry about the log content
  81. for color in ansi_colors:
  82. text = text.replace('{%s}' % color, ansi_colors[color])
  83. return text + ansi_colors['style_reset_all']
  84. class ANSIFormatterMixin(object):
  85. """A log formatter mixin that inserts ANSI color.
  86. """
  87. def format(self, record):
  88. msg = super(ANSIFormatterMixin, self).format(record)
  89. return format_ansi(msg)
  90. class ANSIStrippingMixin(object):
  91. """A log formatter mixin that strips ANSI.
  92. """
  93. def format(self, record):
  94. msg = super(ANSIStrippingMixin, self).format(record)
  95. record.levelname = ansi_escape.sub('', record.levelname)
  96. return ansi_escape.sub('', msg)
  97. class EmojiLoglevelMixin(object):
  98. """A log formatter mixin that makes the loglevel an emoji on UTF capable terminals.
  99. """
  100. def format(self, record):
  101. if UNICODE_SUPPORT:
  102. record.levelname = EMOJI_LOGLEVELS[record.levelname].format(**ansi_colors)
  103. return super(EmojiLoglevelMixin, self).format(record)
  104. class ANSIFormatter(ANSIFormatterMixin, logging.Formatter):
  105. """A log formatter that colorizes output.
  106. """
  107. pass
  108. class ANSIStrippingFormatter(ANSIStrippingMixin, ANSIFormatterMixin, logging.Formatter):
  109. """A log formatter that strips ANSI
  110. """
  111. pass
  112. class ANSIEmojiLoglevelFormatter(EmojiLoglevelMixin, ANSIFormatterMixin, logging.Formatter):
  113. """A log formatter that adds Emoji and ANSI
  114. """
  115. pass
  116. class ANSIStrippingEmojiLoglevelFormatter(ANSIStrippingMixin, EmojiLoglevelMixin, ANSIFormatterMixin, logging.Formatter):
  117. """A log formatter that adds Emoji and strips ANSI
  118. """
  119. pass
  120. class Configuration(object):
  121. """Represents the running configuration.
  122. This class never raises IndexError, instead it will return None if a
  123. section or option does not yet exist.
  124. """
  125. def __contains__(self, key):
  126. return self._config.__contains__(key)
  127. def __iter__(self):
  128. return self._config.__iter__()
  129. def __len__(self):
  130. return self._config.__len__()
  131. def __repr__(self):
  132. return self._config.__repr__()
  133. def keys(self):
  134. return self._config.keys()
  135. def items(self):
  136. return self._config.items()
  137. def values(self):
  138. return self._config.values()
  139. def __init__(self, *args, **kwargs):
  140. self._config = {}
  141. def __getattr__(self, key):
  142. return self.__getitem__(key)
  143. def __getitem__(self, key):
  144. """Returns a config section, creating it if it doesn't exist yet.
  145. """
  146. if key not in self._config:
  147. self.__dict__[key] = self._config[key] = ConfigurationSection(self)
  148. return self._config[key]
  149. def __setitem__(self, key, value):
  150. self.__dict__[key] = value
  151. self._config[key] = value
  152. def __delitem__(self, key):
  153. if key in self.__dict__ and key[0] != '_':
  154. del self.__dict__[key]
  155. if key in self._config:
  156. del self._config[key]
  157. class ConfigurationSection(Configuration):
  158. def __init__(self, parent, *args, **kwargs):
  159. super(ConfigurationSection, self).__init__(*args, **kwargs)
  160. self.parent = parent
  161. def __getitem__(self, key):
  162. """Returns a config value, pulling from the `user` section as a fallback.
  163. This is called when the attribute is accessed either via the get method or through [ ] index.
  164. """
  165. if key in self._config and self._config.get(key) is not None:
  166. return self._config[key]
  167. elif key in self.parent.user:
  168. return self.parent.user[key]
  169. return None
  170. def __getattr__(self, key):
  171. """Returns the config value from the `user` section.
  172. This is called when the attribute is accessed via dot notation but does not exists.
  173. """
  174. if key in self.parent.user:
  175. return self.parent.user[key]
  176. return None
  177. def handle_store_boolean(self, *args, **kwargs):
  178. """Does the add_argument for action='store_boolean'.
  179. """
  180. disabled_args = None
  181. disabled_kwargs = kwargs.copy()
  182. disabled_kwargs['action'] = 'store_false'
  183. disabled_kwargs['dest'] = self.get_argument_name(*args, **kwargs)
  184. disabled_kwargs['help'] = 'Disable ' + kwargs['help']
  185. kwargs['action'] = 'store_true'
  186. kwargs['help'] = 'Enable ' + kwargs['help']
  187. for flag in args:
  188. if flag[:2] == '--':
  189. disabled_args = ('--no-' + flag[2:],)
  190. break
  191. self.add_argument(*args, **kwargs)
  192. self.add_argument(*disabled_args, **disabled_kwargs)
  193. return (args, kwargs, disabled_args, disabled_kwargs)
  194. class SubparserWrapper(object):
  195. """Wrap subparsers so we can track what options the user passed.
  196. """
  197. def __init__(self, cli, submodule, subparser):
  198. self.cli = cli
  199. self.submodule = submodule
  200. self.subparser = subparser
  201. for attr in dir(subparser):
  202. if not hasattr(self, attr):
  203. setattr(self, attr, getattr(subparser, attr))
  204. def completer(self, completer):
  205. """Add an arpcomplete completer to this subcommand.
  206. """
  207. self.subparser.completer = completer
  208. def add_argument(self, *args, **kwargs):
  209. """Add an argument for this subcommand.
  210. This also stores the default for the argument in `self.cli.default_arguments`.
  211. """
  212. if kwargs.get('action') == 'store_boolean':
  213. # Store boolean will call us again with the enable/disable flag arguments
  214. return handle_store_boolean(self, *args, **kwargs)
  215. self.cli.acquire_lock()
  216. argument_name = self.cli.get_argument_name(*args, **kwargs)
  217. self.subparser.add_argument(*args, **kwargs)
  218. if kwargs.get('action') == 'store_false':
  219. self.cli._config_store_false.append(argument_name)
  220. if kwargs.get('action') == 'store_true':
  221. self.cli._config_store_true.append(argument_name)
  222. if self.submodule not in self.cli.default_arguments:
  223. self.cli.default_arguments[self.submodule] = {}
  224. self.cli.default_arguments[self.submodule][argument_name] = kwargs.get('default')
  225. self.cli.release_lock()
  226. class MILC(object):
  227. """MILC - An Opinionated Batteries Included Framework
  228. """
  229. def __init__(self):
  230. """Initialize the MILC object.
  231. version
  232. The version string to associate with your CLI program
  233. """
  234. # Setup a lock for thread safety
  235. self._lock = threading.RLock() if thread else None
  236. # Define some basic info
  237. self.acquire_lock()
  238. self._config_store_true = []
  239. self._config_store_false = []
  240. self._description = None
  241. self._entrypoint = None
  242. self._inside_context_manager = False
  243. self.ansi = ansi_colors
  244. self.arg_only = {}
  245. self.config = self.config_source = None
  246. self.config_file = None
  247. self.default_arguments = {}
  248. self.version = 'unknown'
  249. self.platform = platform()
  250. # Figure out our program name
  251. self.prog_name = sys.argv[0][:-3] if sys.argv[0].endswith('.py') else sys.argv[0]
  252. self.prog_name = self.prog_name.split('/')[-1]
  253. self.release_lock()
  254. # Initialize all the things
  255. self.read_config_file()
  256. self.initialize_argparse()
  257. self.initialize_logging()
  258. @property
  259. def description(self):
  260. return self._description
  261. @description.setter
  262. def description(self, value):
  263. self._description = self._arg_parser.description = value
  264. def echo(self, text, *args, **kwargs):
  265. """Print colorized text to stdout.
  266. ANSI color strings (such as {fg-blue}) will be converted into ANSI
  267. escape sequences, and the ANSI reset sequence will be added to all
  268. strings.
  269. If *args or **kwargs are passed they will be used to %-format the strings.
  270. If `self.config.general.color` is False any ANSI escape sequences in the text will be stripped.
  271. """
  272. if args and kwargs:
  273. raise RuntimeError('You can only specify *args or **kwargs, not both!')
  274. args = args or kwargs
  275. text = format_ansi(text)
  276. if not self.config.general.color:
  277. text = ansi_escape.sub('', text)
  278. print(text % args)
  279. def run(self, command, *args, **kwargs):
  280. """Run a command with subprocess.run
  281. The *args and **kwargs arguments get passed directly to `subprocess.run`.
  282. """
  283. if isinstance(command, str):
  284. raise TypeError('`command` must be a non-text sequence such as list or tuple.')
  285. if 'windows' in self.platform.lower():
  286. safecmd = map(shlex.quote, command)
  287. safecmd = ' '.join(safecmd)
  288. command = [os.environ['SHELL'], '-c', safecmd]
  289. self.log.debug('Running command: %s', command)
  290. return subprocess.run(command, *args, **kwargs)
  291. def initialize_argparse(self):
  292. """Prepare to process arguments from sys.argv.
  293. """
  294. kwargs = {
  295. 'fromfile_prefix_chars': '@',
  296. 'conflict_handler': 'resolve',
  297. }
  298. self.acquire_lock()
  299. self.subcommands = {}
  300. self._subparsers = None
  301. self.argwarn = argcomplete.warn
  302. self.args = None
  303. self._arg_parser = argparse.ArgumentParser(**kwargs)
  304. self.set_defaults = self._arg_parser.set_defaults
  305. self.print_usage = self._arg_parser.print_usage
  306. self.print_help = self._arg_parser.print_help
  307. self.release_lock()
  308. def completer(self, completer):
  309. """Add an argcomplete completer to this subcommand.
  310. """
  311. self._arg_parser.completer = completer
  312. def add_argument(self, *args, **kwargs):
  313. """Wrapper to add arguments and track whether they were passed on the command line.
  314. """
  315. if 'action' in kwargs and kwargs['action'] == 'store_boolean':
  316. return handle_store_boolean(self, *args, **kwargs)
  317. self.acquire_lock()
  318. self._arg_parser.add_argument(*args, **kwargs)
  319. if 'general' not in self.default_arguments:
  320. self.default_arguments['general'] = {}
  321. self.default_arguments['general'][self.get_argument_name(*args, **kwargs)] = kwargs.get('default')
  322. self.release_lock()
  323. def initialize_logging(self):
  324. """Prepare the defaults for the logging infrastructure.
  325. """
  326. self.acquire_lock()
  327. self.log_file = None
  328. self.log_file_mode = 'a'
  329. self.log_file_handler = None
  330. self.log_print = True
  331. self.log_print_to = sys.stderr
  332. self.log_print_level = logging.INFO
  333. self.log_file_level = logging.DEBUG
  334. self.log_level = logging.INFO
  335. self.log = logging.getLogger(self.__class__.__name__)
  336. self.log.setLevel(logging.DEBUG)
  337. logging.root.setLevel(logging.DEBUG)
  338. self.release_lock()
  339. self.add_argument('-V', '--version', version=self.version, action='version', help='Display the version and exit')
  340. self.add_argument('-v', '--verbose', action='store_true', help='Make the logging more verbose')
  341. self.add_argument('--datetime-fmt', default='%Y-%m-%d %H:%M:%S', help='Format string for datetimes')
  342. self.add_argument('--log-fmt', default='%(levelname)s %(message)s', help='Format string for printed log output')
  343. 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.')
  344. self.add_argument('--log-file', help='File to write log messages to')
  345. self.add_argument('--color', action='store_boolean', default=True, help='color in output')
  346. self.add_argument('--config-file', help='The location for the configuration file')
  347. self.arg_only['config_file'] = ['general']
  348. def add_subparsers(self, title='Sub-commands', **kwargs):
  349. if self._inside_context_manager:
  350. raise RuntimeError('You must run this before the with statement!')
  351. self.acquire_lock()
  352. self._subparsers = self._arg_parser.add_subparsers(title=title, dest='subparsers', **kwargs)
  353. self.release_lock()
  354. def acquire_lock(self):
  355. """Acquire the MILC lock for exclusive access to properties.
  356. """
  357. if self._lock:
  358. self._lock.acquire()
  359. def release_lock(self):
  360. """Release the MILC lock.
  361. """
  362. if self._lock:
  363. self._lock.release()
  364. def find_config_file(self):
  365. """Locate the config file.
  366. """
  367. if self.config_file:
  368. return self.config_file
  369. if '--config-file' in sys.argv:
  370. return Path(sys.argv[sys.argv.index('--config-file') + 1]).expanduser().resolve()
  371. filedir = user_config_dir(appname='qmk', appauthor='QMK')
  372. filename = '%s.ini' % self.prog_name
  373. return Path(filedir) / filename
  374. def get_argument_name(self, *args, **kwargs):
  375. """Takes argparse arguments and returns the dest name.
  376. """
  377. try:
  378. return self._arg_parser._get_optional_kwargs(*args, **kwargs)['dest']
  379. except ValueError:
  380. return self._arg_parser._get_positional_kwargs(*args, **kwargs)['dest']
  381. def argument(self, *args, **kwargs):
  382. """Decorator to call self.add_argument or self.<subcommand>.add_argument.
  383. """
  384. if self._inside_context_manager:
  385. raise RuntimeError('You must run this before the with statement!')
  386. def argument_function(handler):
  387. subcommand_name = handler.__name__.replace("_", "-")
  388. if kwargs.get('arg_only'):
  389. arg_name = self.get_argument_name(*args, **kwargs)
  390. if arg_name not in self.arg_only:
  391. self.arg_only[arg_name] = []
  392. self.arg_only[arg_name].append(subcommand_name)
  393. del kwargs['arg_only']
  394. if handler is self._entrypoint:
  395. self.add_argument(*args, **kwargs)
  396. elif subcommand_name in self.subcommands:
  397. self.subcommands[subcommand_name].add_argument(*args, **kwargs)
  398. else:
  399. raise RuntimeError('Decorated function is not entrypoint or subcommand!')
  400. return handler
  401. return argument_function
  402. def arg_passed(self, arg):
  403. """Returns True if arg was passed on the command line.
  404. """
  405. return self.default_arguments.get(arg) != self.args[arg]
  406. def parse_args(self):
  407. """Parse the CLI args.
  408. """
  409. if self.args:
  410. self.log.debug('Warning: Arguments have already been parsed, ignoring duplicate attempt!')
  411. return
  412. argcomplete.autocomplete(self._arg_parser)
  413. self.acquire_lock()
  414. self.args = self._arg_parser.parse_args()
  415. if 'entrypoint' in self.args:
  416. self._entrypoint = self.args.entrypoint
  417. self.release_lock()
  418. def read_config_file(self):
  419. """Read in the configuration file and store it in self.config.
  420. """
  421. self.acquire_lock()
  422. self.config = Configuration()
  423. self.config_source = Configuration()
  424. self.config_file = self.find_config_file()
  425. if self.config_file and self.config_file.exists():
  426. config = RawConfigParser(self.config)
  427. config.read(str(self.config_file))
  428. # Iterate over the config file options and write them into self.config
  429. for section in config.sections():
  430. for option in config.options(section):
  431. value = config.get(section, option)
  432. # Coerce values into useful datatypes
  433. if value.lower() in ['1', 'yes', 'true', 'on']:
  434. value = True
  435. elif value.lower() in ['0', 'no', 'false', 'off']:
  436. value = False
  437. elif value.lower() in ['none']:
  438. continue
  439. elif value.replace('.', '').isdigit():
  440. if '.' in value:
  441. value = Decimal(value)
  442. else:
  443. value = int(value)
  444. self.config[section][option] = value
  445. self.config_source[section][option] = 'config_file'
  446. self.release_lock()
  447. def merge_args_into_config(self):
  448. """Merge CLI arguments into self.config to create the runtime configuration.
  449. """
  450. self.acquire_lock()
  451. for argument in vars(self.args):
  452. if argument in ('subparsers', 'entrypoint'):
  453. continue
  454. # Find the argument's section
  455. # Underscores in command's names are converted to dashes during initialization.
  456. # TODO(Erovia) Find a better solution
  457. entrypoint_name = self._entrypoint.__name__.replace("_", "-")
  458. if entrypoint_name in self.default_arguments and argument in self.default_arguments[entrypoint_name]:
  459. argument_found = True
  460. section = self._entrypoint.__name__
  461. if argument in self.default_arguments['general']:
  462. argument_found = True
  463. section = 'general'
  464. if not argument_found:
  465. raise RuntimeError('Could not find argument in `self.default_arguments`. This should be impossible!')
  466. exit(1)
  467. if argument not in self.arg_only or section not in self.arg_only[argument]:
  468. # Determine the arg value and source
  469. arg_value = getattr(self.args, argument)
  470. if argument in self._config_store_true and arg_value:
  471. passed_on_cmdline = True
  472. elif argument in self._config_store_false and not arg_value:
  473. passed_on_cmdline = True
  474. elif arg_value is not None:
  475. passed_on_cmdline = True
  476. else:
  477. passed_on_cmdline = False
  478. # Merge this argument into self.config
  479. if passed_on_cmdline and (argument in self.default_arguments['general'] or argument in self.default_arguments[entrypoint_name] or argument not in self.config[entrypoint_name]):
  480. self.config[section][argument] = arg_value
  481. self.config_source[section][argument] = 'argument'
  482. self.release_lock()
  483. def save_config(self):
  484. """Save the current configuration to the config file.
  485. """
  486. self.log.debug("Saving config file to '%s'", str(self.config_file))
  487. if not self.config_file:
  488. self.log.warning('%s.config_file file not set, not saving config!', self.__class__.__name__)
  489. return
  490. self.acquire_lock()
  491. # Generate a sanitized version of our running configuration
  492. config = RawConfigParser()
  493. for section_name, section in self.config._config.items():
  494. config.add_section(section_name)
  495. for option_name, value in section.items():
  496. if section_name == 'general':
  497. if option_name in ['config_file']:
  498. continue
  499. if value is not None:
  500. config.set(section_name, option_name, str(value))
  501. # Write out the config file
  502. config_dir = self.config_file.parent
  503. if not config_dir.exists():
  504. config_dir.mkdir(parents=True, exist_ok=True)
  505. with NamedTemporaryFile(mode='w', dir=str(config_dir), delete=False) as tmpfile:
  506. config.write(tmpfile)
  507. # Move the new config file into place atomically
  508. if os.path.getsize(tmpfile.name) > 0:
  509. os.replace(tmpfile.name, str(self.config_file))
  510. else:
  511. self.log.warning('Config file saving failed, not replacing %s with %s.', str(self.config_file), tmpfile.name)
  512. # Housekeeping
  513. self.release_lock()
  514. cli.log.info('Wrote configuration to %s', shlex.quote(str(self.config_file)))
  515. def __call__(self):
  516. """Execute the entrypoint function.
  517. """
  518. if not self._inside_context_manager:
  519. # If they didn't use the context manager use it ourselves
  520. with self:
  521. return self.__call__()
  522. if not self._entrypoint:
  523. raise RuntimeError('No entrypoint provided!')
  524. return self._entrypoint(self)
  525. def entrypoint(self, description):
  526. """Set the entrypoint for when no subcommand is provided.
  527. """
  528. if self._inside_context_manager:
  529. raise RuntimeError('You must run this before cli()!')
  530. self.acquire_lock()
  531. self.description = description
  532. self.release_lock()
  533. def entrypoint_func(handler):
  534. self.acquire_lock()
  535. self._entrypoint = handler
  536. self.release_lock()
  537. return handler
  538. return entrypoint_func
  539. def add_subcommand(self, handler, description, name=None, hidden=False, **kwargs):
  540. """Register a subcommand.
  541. If name is not provided we use `handler.__name__`.
  542. """
  543. if self._inside_context_manager:
  544. raise RuntimeError('You must run this before the with statement!')
  545. if self._subparsers is None:
  546. self.add_subparsers(metavar="")
  547. if not name:
  548. name = handler.__name__.replace("_", "-")
  549. self.acquire_lock()
  550. if not hidden:
  551. self._subparsers.metavar = "{%s,%s}" % (self._subparsers.metavar[1:-1], name) if self._subparsers.metavar else "{%s%s}" % (self._subparsers.metavar[1:-1], name)
  552. kwargs['help'] = description
  553. self.subcommands[name] = SubparserWrapper(self, name, self._subparsers.add_parser(name, **kwargs))
  554. self.subcommands[name].set_defaults(entrypoint=handler)
  555. self.release_lock()
  556. return handler
  557. def subcommand(self, description, hidden=False, **kwargs):
  558. """Decorator to register a subcommand.
  559. """
  560. def subcommand_function(handler):
  561. return self.add_subcommand(handler, description, hidden=hidden, **kwargs)
  562. return subcommand_function
  563. def setup_logging(self):
  564. """Called by __enter__() to setup the logging configuration.
  565. """
  566. if len(logging.root.handlers) != 0:
  567. # MILC is the only thing that should have root log handlers
  568. logging.root.handlers = []
  569. self.acquire_lock()
  570. if self.config['general']['verbose']:
  571. self.log_print_level = logging.DEBUG
  572. self.log_file = self.config['general']['log_file'] or self.log_file
  573. self.log_file_format = ANSIStrippingFormatter(self.config['general']['log_file_fmt'], self.config['general']['datetime_fmt'])
  574. self.log_format = self.config['general']['log_fmt']
  575. if self.config.general.color:
  576. self.log_format = ANSIEmojiLoglevelFormatter(self.config.general.log_fmt, self.config.general.datetime_fmt)
  577. else:
  578. self.log_format = ANSIStrippingEmojiLoglevelFormatter(self.config.general.log_fmt, self.config.general.datetime_fmt)
  579. if self.log_file:
  580. self.log_file_handler = logging.FileHandler(self.log_file, self.log_file_mode)
  581. self.log_file_handler.setLevel(self.log_file_level)
  582. self.log_file_handler.setFormatter(self.log_file_format)
  583. logging.root.addHandler(self.log_file_handler)
  584. if self.log_print:
  585. self.log_print_handler = logging.StreamHandler(self.log_print_to)
  586. self.log_print_handler.setLevel(self.log_print_level)
  587. self.log_print_handler.setFormatter(self.log_format)
  588. logging.root.addHandler(self.log_print_handler)
  589. self.release_lock()
  590. def __enter__(self):
  591. if self._inside_context_manager:
  592. 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.')
  593. return
  594. self.acquire_lock()
  595. self._inside_context_manager = True
  596. self.release_lock()
  597. colorama.init()
  598. self.parse_args()
  599. self.merge_args_into_config()
  600. self.setup_logging()
  601. return self
  602. def __exit__(self, exc_type, exc_val, exc_tb):
  603. self.acquire_lock()
  604. self._inside_context_manager = False
  605. self.release_lock()
  606. if exc_type is not None and not isinstance(SystemExit(), exc_type):
  607. print(exc_type)
  608. logging.exception(exc_val)
  609. exit(255)
  610. cli = MILC()
  611. if __name__ == '__main__':
  612. @cli.argument('-c', '--comma', help='comma in output', default=True, action='store_boolean')
  613. @cli.entrypoint('My useful CLI tool with subcommands.')
  614. def main(cli):
  615. comma = ',' if cli.config.general.comma else ''
  616. cli.log.info('{bg_green}{fg_red}Hello%s World!', comma)
  617. @cli.argument('-n', '--name', help='Name to greet', default='World')
  618. @cli.subcommand('Description of hello subcommand here.')
  619. def hello(cli):
  620. comma = ',' if cli.config.general.comma else ''
  621. cli.log.info('{fg_blue}Hello%s %s!', comma, cli.config.hello.name)
  622. def goodbye(cli):
  623. comma = ',' if cli.config.general.comma else ''
  624. cli.log.info('{bg_red}Goodbye%s %s!', comma, cli.config.goodbye.name)
  625. @cli.argument('-n', '--name', help='Name to greet', default='World')
  626. @cli.subcommand('Think a bit before greeting the user.')
  627. def thinking(cli):
  628. comma = ',' if cli.config.general.comma else ''
  629. spinner = cli.spinner(text='Just a moment...', spinner='earth')
  630. spinner.start()
  631. sleep(2)
  632. spinner.stop()
  633. with cli.spinner(text='Almost there!', spinner='moon'):
  634. sleep(2)
  635. cli.log.info('{fg_cyan}Hello%s %s!', comma, cli.config.thinking.name)
  636. @cli.subcommand('Show off our ANSI colors.')
  637. def pride(cli):
  638. cli.echo('{bg_red} ')
  639. cli.echo('{bg_lightred_ex} ')
  640. cli.echo('{bg_lightyellow_ex} ')
  641. cli.echo('{bg_green} ')
  642. cli.echo('{bg_blue} ')
  643. cli.echo('{bg_magenta} ')
  644. # You can register subcommands using decorators as seen above, or using functions like like this:
  645. cli.add_subcommand(goodbye, 'This will show up in --help output.')
  646. cli.goodbye.add_argument('-n', '--name', help='Name to bid farewell to', default='World')
  647. cli() # Automatically picks between main(), hello() and goodbye()