aboutsummaryrefslogtreecommitdiff
path: root/lib/python/qmk/cli/config.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python/qmk/cli/config.py')
-rw-r--r--lib/python/qmk/cli/config.py96
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"""
3import os
4import subprocess
5
6from milc import cli
7
8
9def 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.")
18def 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