|
@@ -1,8 +1,5 @@
|
|
|
"""Read and write configuration settings
|
|
|
"""
|
|
|
-import os
|
|
|
-import subprocess
|
|
|
-
|
|
|
from milc import cli
|
|
|
|
|
|
|
|
@@ -12,6 +9,54 @@ def print_config(section, key):
|
|
|
cli.echo('%s.%s{fg_cyan}={fg_reset}%s', section, key, cli.config[section][key])
|
|
|
|
|
|
|
|
|
+def show_config():
|
|
|
+ """Print the current configuration to stdout.
|
|
|
+ """
|
|
|
+ for section in cli.config:
|
|
|
+ for key in cli.config[section]:
|
|
|
+ print_config(section, key)
|
|
|
+
|
|
|
+
|
|
|
+def parse_config_token(config_token):
|
|
|
+ """Split a user-supplied configuration-token into its components.
|
|
|
+ """
|
|
|
+ section = option = value = None
|
|
|
+
|
|
|
+ if '=' in config_token and '.' not in config_token:
|
|
|
+ cli.log.error('Invalid configuration token, the key must be of the form <section>.<option>: %s', config_token)
|
|
|
+ return section, option, value
|
|
|
+
|
|
|
+ # Separate the key (<section>.<option>) from the value
|
|
|
+ if '=' in config_token:
|
|
|
+ key, value = config_token.split('=')
|
|
|
+ else:
|
|
|
+ key = config_token
|
|
|
+
|
|
|
+ # Extract the section and option from the key
|
|
|
+ if '.' in key:
|
|
|
+ section, option = key.split('.', 1)
|
|
|
+ else:
|
|
|
+ section = key
|
|
|
+
|
|
|
+ return section, option, value
|
|
|
+
|
|
|
+
|
|
|
+def set_config(section, option, value):
|
|
|
+ """Set a config key in the running config.
|
|
|
+ """
|
|
|
+ log_string = '%s.%s{fg_cyan}:{fg_reset} %s {fg_cyan}->{fg_reset} %s'
|
|
|
+ if cli.args.read_only:
|
|
|
+ log_string += ' {fg_red}(change not written)'
|
|
|
+
|
|
|
+ cli.echo(log_string, section, option, cli.config[section][option], value)
|
|
|
+
|
|
|
+ if not cli.args.read_only:
|
|
|
+ if value == 'None':
|
|
|
+ del cli.config[section][option]
|
|
|
+ else:
|
|
|
+ cli.config[section][option] = value
|
|
|
+
|
|
|
+
|
|
|
@cli.argument('-ro', '--read-only', arg_only=True, action='store_true', help='Operate in read-only mode.')
|
|
|
@cli.argument('configs', nargs='*', arg_only=True, help='Configuration options to read or write.')
|
|
|
@cli.subcommand("Read and write configuration settings.")
|
|
@@ -33,12 +78,7 @@ def config(cli):
|
|
|
No validation is done to ensure that the supplied section.key is actually used by qmk scripts.
|
|
|
"""
|
|
|
if not cli.args.configs:
|
|
|
- # Walk the config tree
|
|
|
- for section in cli.config:
|
|
|
- for key in cli.config[section]:
|
|
|
- print_config(section, key)
|
|
|
-
|
|
|
- return True
|
|
|
+ return show_config()
|
|
|
|
|
|
# Process config_tokens
|
|
|
save_config = False
|
|
@@ -46,43 +86,23 @@ def config(cli):
|
|
|
for argument in cli.args.configs:
|
|
|
# Split on space in case they quoted multiple config tokens
|
|
|
for config_token in argument.split(' '):
|
|
|
- # Extract the section, config_key, and value to write from the supplied config_token.
|
|
|
- if '=' in config_token:
|
|
|
- key, value = config_token.split('=')
|
|
|
- else:
|
|
|
- key = config_token
|
|
|
- value = None
|
|
|
-
|
|
|
- if '.' in key:
|
|
|
- section, config_key = key.split('.', 1)
|
|
|
- else:
|
|
|
- section = key
|
|
|
- config_key = None
|
|
|
+ section, option, value = parse_config_token(config_token)
|
|
|
|
|
|
# Validation
|
|
|
- if config_key and '.' in config_key:
|
|
|
- cli.log.error('Config keys may not have more than one period! "%s" is not valid.', key)
|
|
|
+ if option and '.' in option:
|
|
|
+ cli.log.error('Config keys may not have more than one period! "%s" is not valid.', config_token)
|
|
|
return False
|
|
|
|
|
|
# Do what the user wants
|
|
|
- if section and config_key and value:
|
|
|
- # Write a config key
|
|
|
- log_string = '%s.%s{fg_cyan}:{fg_reset} %s {fg_cyan}->{fg_reset} %s'
|
|
|
- if cli.args.read_only:
|
|
|
- log_string += ' {fg_red}(change not written)'
|
|
|
-
|
|
|
- cli.echo(log_string, section, config_key, cli.config[section][config_key], value)
|
|
|
-
|
|
|
+ if section and option and value:
|
|
|
+ # Write a configuration option
|
|
|
+ set_config(section, option, value)
|
|
|
if not cli.args.read_only:
|
|
|
- if value == 'None':
|
|
|
- del cli.config[section][config_key]
|
|
|
- else:
|
|
|
- cli.config[section][config_key] = value
|
|
|
save_config = True
|
|
|
|
|
|
- elif section and config_key:
|
|
|
+ elif section and option:
|
|
|
# Display a single key
|
|
|
- print_config(section, config_key)
|
|
|
+ print_config(section, option)
|
|
|
|
|
|
elif section:
|
|
|
# Display an entire section
|