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.

53 lines
1.8 KiB

  1. """Generate a keymap.c from a configurator export.
  2. """
  3. import json
  4. from milc import cli
  5. import qmk.keymap
  6. import qmk.path
  7. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  8. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  9. @cli.argument('filename', type=qmk.path.normpath, arg_only=True, help='Configurator JSON file')
  10. @cli.subcommand('Creates a keymap.c from a QMK Configurator export.')
  11. def json2c(cli):
  12. """Generate a keymap.c from a configurator export.
  13. 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.
  14. """
  15. # Error checking
  16. if cli.args.filename and cli.args.filename.name == '-':
  17. # TODO(skullydazed/anyone): Read file contents from STDIN
  18. cli.log.error('Reading from STDIN is not (yet) supported.')
  19. cli.print_usage()
  20. return False
  21. if not cli.args.filename.exists():
  22. cli.log.error('JSON file does not exist!')
  23. cli.print_usage()
  24. return False
  25. # Environment processing
  26. if cli.args.output and cli.args.output.name == '-':
  27. cli.args.output = None
  28. # Parse the configurator json
  29. with cli.args.filename.open('r') as fd:
  30. user_keymap = json.load(fd)
  31. # Generate the keymap
  32. keymap_c = qmk.keymap.generate_c(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers'])
  33. if cli.args.output:
  34. cli.args.output.parent.mkdir(parents=True, exist_ok=True)
  35. if cli.args.output.exists():
  36. cli.args.output.replace(cli.args.output.name + '.bak')
  37. cli.args.output.write_text(keymap_c)
  38. if not cli.args.quiet:
  39. cli.log.info('Wrote keymap to %s.', cli.args.output)
  40. else:
  41. print(keymap_c)