path.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """Functions that help us work with files and folders.
  2. """
  3. import logging
  4. import os
  5. from qmk.errors import NoSuchKeyboardError
  6. def keymap(keyboard):
  7. """Locate the correct directory for storing a keymap.
  8. Args:
  9. keyboard
  10. The name of the keyboard. Example: clueboard/66/rev3
  11. """
  12. for directory in ['.', '..', '../..', '../../..', '../../../..', '../../../../..']:
  13. basepath = os.path.normpath(os.path.join('keyboards', keyboard, directory, 'keymaps'))
  14. if os.path.exists(basepath):
  15. return basepath
  16. logging.error('Could not find keymaps directory!')
  17. raise NoSuchKeyboardError('Could not find keymaps directory for: %s' % keyboard)
  18. def normpath(path):
  19. """Returns the fully resolved absolute path to a file.
  20. This function will return the absolute path to a file as seen from the
  21. directory the script was called from.
  22. """
  23. if path and path[0] == '/':
  24. return os.path.normpath(path)
  25. return os.path.normpath(os.path.join(os.environ['ORIG_CWD'], path))
  26. def file_lines(filename):
  27. """ Return a files content, line by line
  28. Args:
  29. filename: path to the file
  30. Returns:
  31. an list, in which each item is a line of the file
  32. """
  33. with open(filename, "r") as fd:
  34. return fd.readlines()