path.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  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))