diff options
Diffstat (limited to 'lib/python/qmk/cli/config.py')
| -rw-r--r-- | lib/python/qmk/cli/config.py | 94 |
1 files changed, 57 insertions, 37 deletions
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 @@ | |||
| 1 | """Read and write configuration settings | 1 | """Read and write configuration settings |
| 2 | """ | 2 | """ |
| 3 | import os | ||
| 4 | import subprocess | ||
| 5 | |||
| 6 | from milc import cli | 3 | from milc import cli |
| 7 | 4 | ||
| 8 | 5 | ||
| @@ -12,6 +9,54 @@ def print_config(section, key): | |||
| 12 | cli.echo('%s.%s{fg_cyan}={fg_reset}%s', section, key, cli.config[section][key]) | 9 | cli.echo('%s.%s{fg_cyan}={fg_reset}%s', section, key, cli.config[section][key]) |
| 13 | 10 | ||
| 14 | 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 | |||
| 15 | @cli.argument('-ro', '--read-only', arg_only=True, action='store_true', help='Operate in read-only mode.') | 60 | @cli.argument('-ro', '--read-only', arg_only=True, action='store_true', help='Operate in read-only mode.') |
| 16 | @cli.argument('configs', nargs='*', arg_only=True, help='Configuration options to read or write.') | 61 | @cli.argument('configs', nargs='*', arg_only=True, help='Configuration options to read or write.') |
| 17 | @cli.subcommand("Read and write configuration settings.") | 62 | @cli.subcommand("Read and write configuration settings.") |
| @@ -33,12 +78,7 @@ def config(cli): | |||
| 33 | No validation is done to ensure that the supplied section.key is actually used by qmk scripts. | 78 | No validation is done to ensure that the supplied section.key is actually used by qmk scripts. |
| 34 | """ | 79 | """ |
| 35 | if not cli.args.configs: | 80 | if not cli.args.configs: |
| 36 | # Walk the config tree | 81 | return show_config() |
| 37 | for section in cli.config: | ||
| 38 | for key in cli.config[section]: | ||
| 39 | print_config(section, key) | ||
| 40 | |||
| 41 | return True | ||
| 42 | 82 | ||
| 43 | # Process config_tokens | 83 | # Process config_tokens |
| 44 | save_config = False | 84 | save_config = False |
| @@ -46,43 +86,23 @@ def config(cli): | |||
| 46 | for argument in cli.args.configs: | 86 | for argument in cli.args.configs: |
| 47 | # Split on space in case they quoted multiple config tokens | 87 | # Split on space in case they quoted multiple config tokens |
| 48 | for config_token in argument.split(' '): | 88 | for config_token in argument.split(' '): |
| 49 | # Extract the section, config_key, and value to write from the supplied config_token. | 89 | section, option, value = parse_config_token(config_token) |
| 50 | if '=' in config_token: | ||
| 51 | key, value = config_token.split('=') | ||
| 52 | else: | ||
| 53 | key = config_token | ||
| 54 | value = None | ||
| 55 | |||
| 56 | if '.' in key: | ||
| 57 | section, config_key = key.split('.', 1) | ||
| 58 | else: | ||
| 59 | section = key | ||
| 60 | config_key = None | ||
| 61 | 90 | ||
| 62 | # Validation | 91 | # Validation |
| 63 | if config_key and '.' in config_key: | 92 | if option and '.' in option: |
| 64 | cli.log.error('Config keys may not have more than one period! "%s" is not valid.', key) | 93 | cli.log.error('Config keys may not have more than one period! "%s" is not valid.', config_token) |
| 65 | return False | 94 | return False |
| 66 | 95 | ||
| 67 | # Do what the user wants | 96 | # Do what the user wants |
| 68 | if section and config_key and value: | 97 | if section and option and value: |
| 69 | # Write a config key | 98 | # Write a configuration option |
| 70 | log_string = '%s.%s{fg_cyan}:{fg_reset} %s {fg_cyan}->{fg_reset} %s' | 99 | set_config(section, option, value) |
| 71 | if cli.args.read_only: | ||
| 72 | log_string += ' {fg_red}(change not written)' | ||
| 73 | |||
| 74 | cli.echo(log_string, section, config_key, cli.config[section][config_key], value) | ||
| 75 | |||
| 76 | if not cli.args.read_only: | 100 | if not cli.args.read_only: |
| 77 | if value == 'None': | ||
| 78 | del cli.config[section][config_key] | ||
| 79 | else: | ||
| 80 | cli.config[section][config_key] = value | ||
| 81 | save_config = True | 101 | save_config = True |
| 82 | 102 | ||
| 83 | elif section and config_key: | 103 | elif section and option: |
| 84 | # Display a single key | 104 | # Display a single key |
| 85 | print_config(section, config_key) | 105 | print_config(section, option) |
| 86 | 106 | ||
| 87 | elif section: | 107 | elif section: |
| 88 | # Display an entire section | 108 | # Display an entire section |
