config.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """Read and write configuration settings
  2. """
  3. import os
  4. import subprocess
  5. from milc import cli
  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. @cli.argument('-ro', '--read-only', action='store_true', help='Operate in read-only mode.')
  11. @cli.argument('configs', nargs='*', arg_only=True, help='Configuration options to read or write.')
  12. @cli.subcommand("Read and write configuration settings.")
  13. def config(cli):
  14. """Read and write config settings.
  15. This script iterates over the config_tokens supplied as argument. Each config_token has the following form:
  16. section[.key][=value]
  17. If only a section (EG 'compile') is supplied all keys for that section will be displayed.
  18. If section.key is supplied the value for that single key will be displayed.
  19. If section.key=value is supplied the value for that single key will be set.
  20. If section.key=None is supplied the key will be deleted.
  21. No validation is done to ensure that the supplied section.key is actually used by qmk scripts.
  22. """
  23. if not cli.args.configs:
  24. # Walk the config tree
  25. for section in cli.config:
  26. for key in cli.config[section]:
  27. print_config(section, key)
  28. return True
  29. # Process config_tokens
  30. save_config = False
  31. for argument in cli.args.configs:
  32. # Split on space in case they quoted multiple config tokens
  33. for config_token in argument.split(' '):
  34. # Extract the section, config_key, and value to write from the supplied config_token.
  35. if '=' in config_token:
  36. key, value = config_token.split('=')
  37. else:
  38. key = config_token
  39. value = None
  40. if '.' in key:
  41. section, config_key = key.split('.', 1)
  42. else:
  43. section = key
  44. config_key = None
  45. # Validation
  46. if config_key and '.' in config_key:
  47. cli.log.error('Config keys may not have more than one period! "%s" is not valid.', key)
  48. return False
  49. # Do what the user wants
  50. if section and config_key and value:
  51. # Write a config key
  52. log_string = '%s.%s{fg_cyan}:{fg_reset} %s {fg_cyan}->{fg_reset} %s'
  53. if cli.args.read_only:
  54. log_string += ' {fg_red}(change not written)'
  55. cli.echo(log_string, section, config_key, cli.config[section][config_key], value)
  56. if not cli.args.read_only:
  57. if value == 'None':
  58. del cli.config[section][config_key]
  59. else:
  60. cli.config[section][config_key] = value
  61. save_config = True
  62. elif section and config_key:
  63. # Display a single key
  64. print_config(section, config_key)
  65. elif section:
  66. # Display an entire section
  67. for key in cli.config[section]:
  68. print_config(section, key)
  69. # Ending actions
  70. if save_config:
  71. cli.save_config()
  72. return True