You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.7 KiB

  1. """Generate a keymap.c from a configurator export.
  2. """
  3. import json
  4. from argcomplete.completers import FilesCompleter
  5. from milc import cli
  6. import qmk.keymap
  7. import qmk.path
  8. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  9. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  10. @cli.argument('filename', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
  11. @cli.subcommand('Creates a keymap.c from a QMK Configurator export.')
  12. def json2c(cli):
  13. """Generate a keymap.c from a configurator export.
  14. This command uses the `qmk.keymap` module to generate a keymap.c from a configurator export. The generated keymap is written to stdout, or to a file if -o is provided.
  15. """
  16. try:
  17. # Parse the configurator from json file (or stdin)
  18. user_keymap = json.load(cli.args.filename)
  19. except json.decoder.JSONDecodeError as ex:
  20. cli.log.error('The JSON input does not appear to be valid.')
  21. cli.log.error(ex)
  22. return False
  23. # Environment processing
  24. if cli.args.output and cli.args.output.name == '-':
  25. cli.args.output = None
  26. # Generate the keymap
  27. keymap_c = qmk.keymap.generate_c(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers'])
  28. if cli.args.output:
  29. cli.args.output.parent.mkdir(parents=True, exist_ok=True)
  30. if cli.args.output.exists():
  31. cli.args.output.replace(cli.args.output.parent / (cli.args.output.name + '.bak'))
  32. cli.args.output.write_text(keymap_c)
  33. if not cli.args.quiet:
  34. cli.log.info('Wrote keymap to %s.', cli.args.output)
  35. else:
  36. print(keymap_c)