diff options
author | skullydazed <skullydazed@users.noreply.github.com> | 2019-09-22 13:25:33 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-22 13:25:33 -0700 |
commit | d569f0877155efc752994f8a21f5cf001f9d6ae6 (patch) | |
tree | eb58a3e3f916d6d938d8f05742d48919c053a579 /lib/python/qmk/cli/config.py | |
parent | 2f49cae9bcbdd94431659727ef75cfd30f557da8 (diff) | |
download | qmk_firmware-d569f0877155efc752994f8a21f5cf001f9d6ae6.tar.gz qmk_firmware-d569f0877155efc752994f8a21f5cf001f9d6ae6.zip |
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
Diffstat (limited to 'lib/python/qmk/cli/config.py')
-rw-r--r-- | lib/python/qmk/cli/config.py | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/lib/python/qmk/cli/config.py b/lib/python/qmk/cli/config.py new file mode 100644 index 000000000..d6c774e65 --- /dev/null +++ b/lib/python/qmk/cli/config.py | |||
@@ -0,0 +1,96 @@ | |||
1 | """Read and write configuration settings | ||
2 | """ | ||
3 | import os | ||
4 | import subprocess | ||
5 | |||
6 | from milc import cli | ||
7 | |||
8 | |||
9 | def print_config(section, key): | ||
10 | """Print a single config setting to stdout. | ||
11 | """ | ||
12 | cli.echo('%s.%s{fg_cyan}={fg_reset}%s', section, key, cli.config[section][key]) | ||
13 | |||
14 | |||
15 | @cli.argument('-ro', '--read-only', action='store_true', help='Operate in read-only mode.') | ||
16 | @cli.argument('configs', nargs='*', arg_only=True, help='Configuration options to read or write.') | ||
17 | @cli.subcommand("Read and write configuration settings.") | ||
18 | def config(cli): | ||
19 | """Read and write config settings. | ||
20 | |||
21 | This script iterates over the config_tokens supplied as argument. Each config_token has the following form: | ||
22 | |||
23 | section[.key][=value] | ||
24 | |||
25 | If only a section (EG 'compile') is supplied all keys for that section will be displayed. | ||
26 | |||
27 | If section.key is supplied the value for that single key will be displayed. | ||
28 | |||
29 | If section.key=value is supplied the value for that single key will be set. | ||
30 | |||
31 | If section.key=None is supplied the key will be deleted. | ||
32 | |||
33 | No validation is done to ensure that the supplied section.key is actually used by qmk scripts. | ||
34 | """ | ||
35 | if not cli.args.configs: | ||
36 | # Walk the config tree | ||
37 | for section in cli.config: | ||
38 | for key in cli.config[section]: | ||
39 | print_config(section, key) | ||
40 | |||
41 | return True | ||
42 | |||
43 | # Process config_tokens | ||
44 | save_config = False | ||
45 | |||
46 | for argument in cli.args.configs: | ||
47 | # Split on space in case they quoted multiple config tokens | ||
48 | for config_token in argument.split(' '): | ||
49 | # Extract the section, config_key, and value to write from the supplied 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 | |||
62 | # Validation | ||
63 | if config_key and '.' in config_key: | ||
64 | cli.log.error('Config keys may not have more than one period! "%s" is not valid.', key) | ||
65 | return False | ||
66 | |||
67 | # Do what the user wants | ||
68 | if section and config_key and value: | ||
69 | # Write a config key | ||
70 | log_string = '%s.%s{fg_cyan}:{fg_reset} %s {fg_cyan}->{fg_reset} %s' | ||
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: | ||
77 | if value == 'None': | ||
78 | del cli.config[section][config_key] | ||
79 | else: | ||
80 | cli.config[section][config_key] = value | ||
81 | save_config = True | ||
82 | |||
83 | elif section and config_key: | ||
84 | # Display a single key | ||
85 | print_config(section, config_key) | ||
86 | |||
87 | elif section: | ||
88 | # Display an entire section | ||
89 | for key in cli.config[section]: | ||
90 | print_config(section, key) | ||
91 | |||
92 | # Ending actions | ||
93 | if save_config: | ||
94 | cli.save_config() | ||
95 | |||
96 | return True | ||