path.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """Functions that help us work with files and folders.
  2. """
  3. import logging
  4. import os
  5. import argparse
  6. from pathlib import Path
  7. from qmk.constants import MAX_KEYBOARD_SUBFOLDERS, QMK_FIRMWARE
  8. from qmk.errors import NoSuchKeyboardError
  9. def is_keyboard(keyboard_name):
  10. """Returns True if `keyboard_name` is a keyboard we can compile.
  11. """
  12. if keyboard_name:
  13. keyboard_path = QMK_FIRMWARE / 'keyboards' / keyboard_name
  14. rules_mk = keyboard_path / 'rules.mk'
  15. return rules_mk.exists()
  16. def under_qmk_firmware():
  17. """Returns a Path object representing the relative path under qmk_firmware, or None.
  18. """
  19. cwd = Path(os.environ['ORIG_CWD'])
  20. try:
  21. return cwd.relative_to(QMK_FIRMWARE)
  22. except ValueError:
  23. return None
  24. def keyboard(keyboard_name):
  25. """Returns the path to a keyboard's directory relative to the qmk root.
  26. """
  27. return Path('keyboards') / keyboard_name
  28. def keymaps(keyboard_name):
  29. """Returns all of the `keymaps/` directories for a given keyboard.
  30. Args:
  31. keyboard_name
  32. The name of the keyboard. Example: clueboard/66/rev3
  33. """
  34. keyboard_folder = keyboard(keyboard_name)
  35. found_dirs = []
  36. for _ in range(MAX_KEYBOARD_SUBFOLDERS):
  37. if (keyboard_folder / 'keymaps').exists():
  38. found_dirs.append((keyboard_folder / 'keymaps').resolve())
  39. keyboard_folder = keyboard_folder.parent
  40. if len(found_dirs) > 0:
  41. return found_dirs
  42. logging.error('Could not find the keymaps directory!')
  43. raise NoSuchKeyboardError('Could not find keymaps directory for: %s' % keyboard_name)
  44. def keymap(keyboard_name, keymap_name):
  45. """Locate the directory of a given keymap.
  46. Args:
  47. keyboard_name
  48. The name of the keyboard. Example: clueboard/66/rev3
  49. keymap_name
  50. The name of the keymap. Example: default
  51. """
  52. for keymap_dir in keymaps(keyboard_name):
  53. if (keymap_dir / keymap_name).exists():
  54. return (keymap_dir / keymap_name).resolve()
  55. def normpath(path):
  56. """Returns a `pathlib.Path()` object for a given path.
  57. This will use the path to a file as seen from the directory the script was called from. You should use this to normalize filenames supplied from the command line.
  58. """
  59. path = Path(path)
  60. if path.is_absolute():
  61. return path
  62. return Path(os.environ['ORIG_CWD']) / path
  63. class FileType(argparse.FileType):
  64. def __init__(self, *args, **kwargs):
  65. # Use UTF8 by default for stdin
  66. if 'encoding' not in kwargs:
  67. kwargs['encoding'] = 'UTF-8'
  68. return super().__init__(*args, **kwargs)
  69. def __call__(self, string):
  70. """normalize and check exists
  71. otherwise magic strings like '-' for stdin resolve to bad paths
  72. """
  73. norm = normpath(string)
  74. return norm if norm.exists() else super().__call__(string)