keyboard.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. """This script automates the creation of new keyboard directories using a starter template.
  2. """
  3. from datetime import date
  4. from pathlib import Path
  5. import re
  6. from qmk.commands import git_get_username
  7. import qmk.path
  8. from milc import cli
  9. from milc.questions import choice, question
  10. KEYBOARD_TYPES = ['avr', 'ps2avrgb']
  11. def keyboard_name(name):
  12. """Callable for argparse validation.
  13. """
  14. if not validate_keyboard_name(name):
  15. raise ValueError
  16. return name
  17. def validate_keyboard_name(name):
  18. """Returns True if the given keyboard name contains only lowercase a-z, 0-9 and underscore characters.
  19. """
  20. regex = re.compile(r'^[a-z0-9][a-z0-9/_]+$')
  21. return bool(regex.match(name))
  22. @cli.argument('-kb', '--keyboard', help='Specify the name for the new keyboard directory', arg_only=True, type=keyboard_name)
  23. @cli.argument('-t', '--type', help='Specify the keyboard type', arg_only=True, choices=KEYBOARD_TYPES)
  24. @cli.argument('-u', '--username', help='Specify your username (default from Git config)', arg_only=True)
  25. @cli.argument('-n', '--realname', help='Specify your real name if you want to use that. Defaults to username', arg_only=True)
  26. @cli.subcommand('Creates a new keyboard directory')
  27. def new_keyboard(cli):
  28. """Creates a new keyboard.
  29. """
  30. cli.log.info('{style_bright}Generating a new QMK keyboard directory{style_normal}')
  31. cli.echo('')
  32. # Get keyboard name
  33. new_keyboard_name = None
  34. while not new_keyboard_name:
  35. new_keyboard_name = cli.args.keyboard if cli.args.keyboard else question('Keyboard Name:')
  36. if not validate_keyboard_name(new_keyboard_name):
  37. cli.log.error('Keyboard names must contain only {fg_cyan}lowercase a-z{fg_reset}, {fg_cyan}0-9{fg_reset}, and {fg_cyan}_{fg_reset}! Please choose a different name.')
  38. # Exit if passed by arg
  39. if cli.args.keyboard:
  40. return False
  41. new_keyboard_name = None
  42. continue
  43. keyboard_path = qmk.path.keyboard(new_keyboard_name)
  44. if keyboard_path.exists():
  45. cli.log.error(f'Keyboard {{fg_cyan}}{new_keyboard_name}{{fg_reset}} already exists! Please choose a different name.')
  46. # Exit if passed by arg
  47. if cli.args.keyboard:
  48. return False
  49. new_keyboard_name = None
  50. # Get keyboard type
  51. keyboard_type = cli.args.type if cli.args.type else choice('Keyboard Type:', KEYBOARD_TYPES, default=0)
  52. # Get username
  53. user_name = None
  54. while not user_name:
  55. user_name = question('Your GitHub User Name:', default=find_user_name())
  56. if not user_name:
  57. cli.log.error('You didn\'t provide a username, and we couldn\'t find one set in your QMK or Git configs. Please try again.')
  58. # Exit if passed by arg
  59. if cli.args.username:
  60. return False
  61. real_name = None
  62. while not real_name:
  63. real_name = question('Your real name:', default=user_name)
  64. keyboard_basename = keyboard_path.name
  65. replacements = {
  66. "YEAR": str(date.today().year),
  67. "KEYBOARD": keyboard_basename,
  68. "USER_NAME": user_name,
  69. "YOUR_NAME": real_name,
  70. }
  71. template_dir = Path('data/templates')
  72. template_tree(template_dir / 'base', keyboard_path, replacements)
  73. template_tree(template_dir / keyboard_type, keyboard_path, replacements)
  74. cli.echo('')
  75. cli.log.info(f'{{fg_green}}Created a new keyboard called {{fg_cyan}}{new_keyboard_name}{{fg_green}}.{{fg_reset}}')
  76. cli.log.info(f'To start working on things, `cd` into {{fg_cyan}}{keyboard_path}{{fg_reset}},')
  77. cli.log.info('or open the directory in your preferred text editor.')
  78. def find_user_name():
  79. if cli.args.username:
  80. return cli.args.username
  81. elif cli.config.user.name:
  82. return cli.config.user.name
  83. else:
  84. return git_get_username()
  85. def template_tree(src: Path, dst: Path, replacements: dict):
  86. """Recursively copy template and replace placeholders
  87. Args:
  88. src (Path)
  89. The source folder to copy from
  90. dst (Path)
  91. The destination folder to copy to
  92. replacements (dict)
  93. a dictionary with "key":"value" pairs to replace.
  94. Raises:
  95. FileExistsError
  96. When trying to overwrite existing files
  97. """
  98. dst.mkdir(parents=True, exist_ok=True)
  99. for child in src.iterdir():
  100. if child.is_dir():
  101. template_tree(child, dst / child.name, replacements=replacements)
  102. if child.is_file():
  103. file_name = dst / (child.name % replacements)
  104. with file_name.open(mode='x') as dst_f:
  105. with child.open() as src_f:
  106. template = src_f.read()
  107. dst_f.write(template % replacements)