logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

c2json.py (3233B)


  1. """Generate a keymap.json from a keymap.c file.
  2. """
  3. import re
  4. import json
  5. from argcomplete.completers import FilesCompleter
  6. from milc import cli
  7. import qmk.path
  8. from qmk.json_encoders import InfoJSONEncoder
  9. from qmk.decorators import automagic_keyboard, automagic_keymap
  10. from qmk.keyboard import keyboard_completer, keyboard_folder
  11. from qmk.keymap import locate_keymap, find_keymap_from_dir, generate_json, c2json as c2json_impl
  12. from qmk.errors import CppError
  13. from qmk.commands import dump_lines
  14. @cli.argument('--no-cpp', arg_only=True, action='store_false', help='Do not use \'cpp\' on keymap.c')
  15. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  16. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  17. @cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='The keyboard\'s name')
  18. @cli.argument('-km', '--keymap', help='The keymap\'s name')
  19. @cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.c'), help='keymap.c file')
  20. @cli.subcommand('Creates a keymap.json from a keymap.c file.')
  21. @automagic_keyboard
  22. @automagic_keymap
  23. def c2json(cli):
  24. """Generate a keymap.json from a keymap.c file.
  25. This command uses the `qmk.keymap` module to generate a keymap.json from a keymap.c file. The generated keymap is written to stdout, or to a file if -o is provided.
  26. """
  27. filename = cli.args.filename
  28. keyboard = cli.config.c2json.keyboard
  29. keymap = cli.config.c2json.keymap
  30. if filename:
  31. if not keyboard and not keymap:
  32. # fallback to inferring keyboard/keymap from path
  33. (keymap, found_type) = find_keymap_from_dir(filename)
  34. if found_type == 'keymap_directory':
  35. keyboard = re.search(fr"keyboards/(.+)/keymaps/{keymap}/.*", filename.as_posix()).group(1)
  36. elif keyboard and keymap:
  37. if not filename:
  38. # fallback to inferring keyboard/keymap from path
  39. filename = locate_keymap(keyboard, keymap)
  40. if not all((filename, keyboard, keymap)):
  41. cli.log.error('You must supply keyboard and keymap, a path to a keymap.c within qmk_firmware, or absolute filename and keyboard and keymap')
  42. cli.print_help()
  43. return False
  44. try:
  45. keymap_json = c2json_impl(keyboard, keymap, filename, use_cpp=cli.args.no_cpp)
  46. except CppError as e:
  47. if cli.config.general.verbose:
  48. cli.log.debug('The C pre-processor ran into a fatal error: %s', e)
  49. cli.log.error('Something went wrong. Try to use --no-cpp.\nUse the CLI in verbose mode to find out more.')
  50. return False
  51. # Generate the keymap.json
  52. try:
  53. keymap_json = generate_json(keymap_json['keymap'], keymap_json['keyboard'], keymap_json['layout'], keymap_json['layers'])
  54. except KeyError:
  55. cli.log.error('Something went wrong. Try to use --no-cpp.')
  56. return False
  57. if cli.args.output:
  58. keymap_lines = [json.dumps(keymap_json, cls=InfoJSONEncoder, sort_keys=True)]
  59. else:
  60. keymap_lines = [json.dumps(keymap_json)]
  61. dump_lines(cli.args.output, keymap_lines, cli.args.quiet)