diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/python/qmk/cli/__init__.py | 2 | ||||
| -rw-r--r-- | lib/python/qmk/cli/config.py | 116 | ||||
| -rw-r--r-- | lib/python/qmk/tests/test_cli_commands.py | 6 |
3 files changed, 1 insertions, 123 deletions
diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index 6fe769fe7..be5ba90ad 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py | |||
| @@ -140,7 +140,7 @@ from . import cformat # noqa | |||
| 140 | from . import chibios # noqa | 140 | from . import chibios # noqa |
| 141 | from . import clean # noqa | 141 | from . import clean # noqa |
| 142 | from . import compile # noqa | 142 | from . import compile # noqa |
| 143 | from . import config # noqa | 143 | from milc.subcommand import config # noqa |
| 144 | from . import docs # noqa | 144 | from . import docs # noqa |
| 145 | from . import doctor # noqa | 145 | from . import doctor # noqa |
| 146 | from . import fileformat # noqa | 146 | from . import fileformat # noqa |
diff --git a/lib/python/qmk/cli/config.py b/lib/python/qmk/cli/config.py deleted file mode 100644 index e17d8bb9b..000000000 --- a/lib/python/qmk/cli/config.py +++ /dev/null | |||
| @@ -1,116 +0,0 @@ | |||
| 1 | """Read and write configuration settings | ||
| 2 | """ | ||
| 3 | from milc import cli | ||
| 4 | |||
| 5 | |||
| 6 | def print_config(section, key): | ||
| 7 | """Print a single config setting to stdout. | ||
| 8 | """ | ||
| 9 | cli.echo('%s.%s{fg_cyan}={fg_reset}%s', section, key, cli.config[section][key]) | ||
| 10 | |||
| 11 | |||
| 12 | def show_config(): | ||
| 13 | """Print the current configuration to stdout. | ||
| 14 | """ | ||
| 15 | for section in cli.config: | ||
| 16 | for key in cli.config[section]: | ||
| 17 | print_config(section, key) | ||
| 18 | |||
| 19 | |||
| 20 | def parse_config_token(config_token): | ||
| 21 | """Split a user-supplied configuration-token into its components. | ||
| 22 | """ | ||
| 23 | section = option = value = None | ||
| 24 | |||
| 25 | if '=' in config_token and '.' not in config_token: | ||
| 26 | cli.log.error('Invalid configuration token, the key must be of the form <section>.<option>: %s', config_token) | ||
| 27 | return section, option, value | ||
| 28 | |||
| 29 | # Separate the key (<section>.<option>) from the value | ||
| 30 | if '=' in config_token: | ||
| 31 | key, value = config_token.split('=') | ||
| 32 | else: | ||
| 33 | key = config_token | ||
| 34 | |||
| 35 | # Extract the section and option from the key | ||
| 36 | if '.' in key: | ||
| 37 | section, option = key.split('.', 1) | ||
| 38 | else: | ||
| 39 | section = key | ||
| 40 | |||
| 41 | return section, option, value | ||
| 42 | |||
| 43 | |||
| 44 | def set_config(section, option, value): | ||
| 45 | """Set a config key in the running config. | ||
| 46 | """ | ||
| 47 | log_string = '%s.%s{fg_cyan}:{fg_reset} %s {fg_cyan}->{fg_reset} %s' | ||
| 48 | if cli.args.read_only: | ||
| 49 | log_string += ' {fg_red}(change not written)' | ||
| 50 | |||
| 51 | cli.echo(log_string, section, option, cli.config[section][option], value) | ||
| 52 | |||
| 53 | if not cli.args.read_only: | ||
| 54 | if value == 'None': | ||
| 55 | del cli.config[section][option] | ||
| 56 | else: | ||
| 57 | cli.config[section][option] = value | ||
| 58 | |||
| 59 | |||
| 60 | @cli.argument('-ro', '--read-only', arg_only=True, action='store_true', help='Operate in read-only mode.') | ||
| 61 | @cli.argument('configs', nargs='*', arg_only=True, help='Configuration options to read or write.') | ||
| 62 | @cli.subcommand("Read and write configuration settings.") | ||
| 63 | def config(cli): | ||
| 64 | """Read and write config settings. | ||
| 65 | |||
| 66 | This script iterates over the config_tokens supplied as argument. Each config_token has the following form: | ||
| 67 | |||
| 68 | section[.key][=value] | ||
| 69 | |||
| 70 | If only a section (EG 'compile') is supplied all keys for that section will be displayed. | ||
| 71 | |||
| 72 | If section.key is supplied the value for that single key will be displayed. | ||
| 73 | |||
| 74 | If section.key=value is supplied the value for that single key will be set. | ||
| 75 | |||
| 76 | If section.key=None is supplied the key will be deleted. | ||
| 77 | |||
| 78 | No validation is done to ensure that the supplied section.key is actually used by qmk scripts. | ||
| 79 | """ | ||
| 80 | if not cli.args.configs: | ||
| 81 | return show_config() | ||
| 82 | |||
| 83 | # Process config_tokens | ||
| 84 | save_config = False | ||
| 85 | |||
| 86 | for argument in cli.args.configs: | ||
| 87 | # Split on space in case they quoted multiple config tokens | ||
| 88 | for config_token in argument.split(' '): | ||
| 89 | section, option, value = parse_config_token(config_token) | ||
| 90 | |||
| 91 | # Validation | ||
| 92 | if option and '.' in option: | ||
| 93 | cli.log.error('Config keys may not have more than one period! "%s" is not valid.', config_token) | ||
| 94 | return False | ||
| 95 | |||
| 96 | # Do what the user wants | ||
| 97 | if section and option and value: | ||
| 98 | # Write a configuration option | ||
| 99 | set_config(section, option, value) | ||
| 100 | if not cli.args.read_only: | ||
| 101 | save_config = True | ||
| 102 | |||
| 103 | elif section and option: | ||
| 104 | # Display a single key | ||
| 105 | print_config(section, option) | ||
| 106 | |||
| 107 | elif section: | ||
| 108 | # Display an entire section | ||
| 109 | for key in cli.config[section]: | ||
| 110 | print_config(section, key) | ||
| 111 | |||
| 112 | # Ending actions | ||
| 113 | if save_config: | ||
| 114 | cli.save_config() | ||
| 115 | |||
| 116 | return True | ||
diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index d13e42d40..a7b70a7d9 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py | |||
| @@ -61,12 +61,6 @@ def test_flash_bootloaders(): | |||
| 61 | check_returncode(result, [1]) | 61 | check_returncode(result, [1]) |
| 62 | 62 | ||
| 63 | 63 | ||
| 64 | def test_config(): | ||
| 65 | result = check_subcommand('config') | ||
| 66 | check_returncode(result) | ||
| 67 | assert 'general.color' in result.stdout | ||
| 68 | |||
| 69 | |||
| 70 | def test_kle2json(): | 64 | def test_kle2json(): |
| 71 | result = check_subcommand('kle2json', 'lib/python/qmk/tests/kle.txt', '-f') | 65 | result = check_subcommand('kle2json', 'lib/python/qmk/tests/kle.txt', '-f') |
| 72 | check_returncode(result) | 66 | check_returncode(result) |
