commands.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. """Helper functions for commands.
  2. """
  3. import json
  4. from pathlib import Path
  5. from milc import cli
  6. import qmk.keymap
  7. from qmk.path import is_keyboard, is_keymap_dir, under_qmk_firmware
  8. def create_make_command(keyboard, keymap, target=None):
  9. """Create a make compile command
  10. Args:
  11. keyboard
  12. The path of the keyboard, for example 'plank'
  13. keymap
  14. The name of the keymap, for example 'algernon'
  15. target
  16. Usually a bootloader.
  17. Returns:
  18. A command that can be run to make the specified keyboard and keymap
  19. """
  20. make_args = [keyboard, keymap]
  21. if target:
  22. make_args.append(target)
  23. return ['make', ':'.join(make_args)]
  24. def compile_configurator_json(user_keymap, bootloader=None):
  25. """Convert a configurator export JSON file into a C file
  26. Args:
  27. configurator_filename
  28. The configurator JSON export file
  29. bootloader
  30. A bootloader to flash
  31. Returns:
  32. A command to run to compile and flash the C file.
  33. """
  34. # Write the keymap C file
  35. qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers'])
  36. # Return a command that can be run to make the keymap and flash if given
  37. if bootloader is None:
  38. return create_make_command(user_keymap['keyboard'], user_keymap['keymap'])
  39. return create_make_command(user_keymap['keyboard'], user_keymap['keymap'], bootloader)
  40. def find_keyboard_keymap():
  41. """Returns `(keyboard_name, keymap_name)` based on the user's current environment.
  42. This determines the keyboard and keymap name using the following precedence order:
  43. * Command line flags (--keyboard and --keymap)
  44. * Current working directory
  45. * `keyboards/<keyboard_name>`
  46. * `keyboards/<keyboard_name>/keymaps/<keymap_name>`
  47. * `layouts/**/<keymap_name>`
  48. * `users/<keymap_name>`
  49. * Configuration
  50. * cli.config.<subcommand>.keyboard
  51. * cli.config.<subcommand>.keymap
  52. """
  53. # Check to make sure their copy of MILC supports config_source
  54. if not hasattr(cli, 'config_source'):
  55. cli.log.error("Your QMK CLI is out of date. Please upgrade using pip3 or your package manager.")
  56. exit(1)
  57. # State variables
  58. relative_cwd = under_qmk_firmware()
  59. keyboard_name = ""
  60. keymap_name = ""
  61. # If the keyboard or keymap are passed as arguments use that in preference to anything else
  62. if cli.config_source[cli._entrypoint.__name__]['keyboard'] == 'argument':
  63. keyboard_name = cli.config[cli._entrypoint.__name__]['keyboard']
  64. if cli.config_source[cli._entrypoint.__name__]['keymap'] == 'argument':
  65. keymap_name = cli.config[cli._entrypoint.__name__]['keymap']
  66. if not keyboard_name or not keymap_name:
  67. # If we don't have a keyboard_name and keymap_name from arguments try to derive one or both
  68. if relative_cwd and relative_cwd.parts and relative_cwd.parts[0] == 'keyboards':
  69. # Try to determine the keyboard and/or keymap name
  70. current_path = Path('/'.join(relative_cwd.parts[1:]))
  71. if current_path.parts[-2] == 'keymaps':
  72. if not keymap_name:
  73. keymap_name = current_path.parts[-1]
  74. if not keyboard_name:
  75. keyboard_name = '/'.join(current_path.parts[:-2])
  76. elif not keyboard_name and is_keyboard(current_path):
  77. keyboard_name = str(current_path)
  78. elif relative_cwd and relative_cwd.parts and relative_cwd.parts[0] == 'layouts':
  79. # Try to determine the keymap name from the community layout
  80. if is_keymap_dir(relative_cwd) and not keymap_name:
  81. keymap_name = relative_cwd.name
  82. elif relative_cwd and relative_cwd.parts and relative_cwd.parts[0] == 'users':
  83. # Try to determine the keymap name based on which userspace they're in
  84. if not keymap_name and len(relative_cwd.parts) > 1:
  85. keymap_name = relative_cwd.parts[1]
  86. # If we still don't have a keyboard and keymap check the config
  87. if not keyboard_name and cli.config[cli._entrypoint.__name__]['keyboard']:
  88. keyboard_name = cli.config[cli._entrypoint.__name__]['keyboard']
  89. if not keymap_name and cli.config[cli._entrypoint.__name__]['keymap']:
  90. keymap_name = cli.config[cli._entrypoint.__name__]['keymap']
  91. return (keyboard_name, keymap_name)
  92. def parse_configurator_json(configurator_file):
  93. """Open and parse a configurator json export
  94. """
  95. user_keymap = json.load(configurator_file)
  96. return user_keymap