qmk_firmware/lib/python/qmk/path.py
Kenny Hoang 595232ec98 Created new_keymap.py, python version of new_keymap.sh (#6066)
* Created python version of new_keymap.sh: new_keymap.py

* Updated usage message

* Updated new_keymap.py to use python3.5+ syntax & be more similar to new_keyboard.sh

* Updated complete message

* Updated usage in argparser and removed incorrect usage_message

* Reverted the fstrings back to strings that use .format() & updated docstring convention

* Added helper to recursively cd .. until at qmk_firmware root directory

* Revert "Added helper to recursively cd .. until at qmk_firmware root directory"

This reverts commit 61a0ff3b25f91901287bec8d58eb51a1f126e2ad.

* Updated new_keymap.py to use printf-style format strings

* First draft lib/python/qmk/cli/new/keymap.py with milc

* Removed shebang & syspath appending lines

* Added optional args & resolved some cr comemnts

* Added a docstring and updated strings
2019-09-10 05:14:25 -07:00

36 lines
1.0 KiB
Python

"""Functions that help us work with files and folders.
"""
import logging
import os
from qmk.errors import NoSuchKeyboardError
def keymap(keyboard):
"""Locate the correct directory for storing a keymap.
Args:
keyboard
The name of the keyboard. Example: clueboard/66/rev3
"""
for directory in ['.', '..', '../..', '../../..', '../../../..', '../../../../..']:
basepath = os.path.normpath(os.path.join('keyboards', keyboard, directory, 'keymaps'))
if os.path.exists(basepath):
return basepath
logging.error('Could not find keymaps directory!')
raise NoSuchKeyboardError('Could not find keymaps directory for: %s' % keyboard)
def normpath(path):
"""Returns the fully resolved absolute path to a file.
This function will return the absolute path to a file as seen from the
directory the script was called from.
"""
if path and path[0] == '/':
return os.path.normpath(path)
return os.path.normpath(os.path.join(os.environ['ORIG_CWD'], path))