2019-07-15 12:14:27 -07:00
""" Functions that help us work with files and folders.
"""
2019-09-10 08:14:25 -04:00
import logging
2019-07-15 12:14:27 -07:00
import os
2021-02-07 21:02:51 +00:00
import argparse
2020-02-17 11:42:11 -08:00
from pathlib import Path
2019-07-15 12:14:27 -07:00
2020-05-26 13:05:41 -07:00
from qmk . constants import MAX_KEYBOARD_SUBFOLDERS , QMK_FIRMWARE
2019-09-10 08:14:25 -04:00
from qmk . errors import NoSuchKeyboardError
2020-01-07 21:54:21 +01:00
2020-02-17 11:42:11 -08:00
def is_keyboard ( keyboard_name ) :
""" Returns True if `keyboard_name` is a keyboard we can compile.
"""
2020-05-27 00:43:22 -07:00
if keyboard_name :
keyboard_path = QMK_FIRMWARE / ' keyboards ' / keyboard_name
rules_mk = keyboard_path / ' rules.mk '
2021-03-24 09:26:38 -07:00
2020-05-27 00:43:22 -07:00
return rules_mk . exists ( )
2020-02-17 11:42:11 -08:00
def under_qmk_firmware ( ) :
""" Returns a Path object representing the relative path under qmk_firmware, or None.
"""
cwd = Path ( os . environ [ ' ORIG_CWD ' ] )
try :
return cwd . relative_to ( QMK_FIRMWARE )
except ValueError :
return None
2020-11-07 09:56:08 -08:00
def keyboard ( keyboard_name ) :
""" Returns the path to a keyboard ' s directory relative to the qmk root.
"""
return Path ( ' keyboards ' ) / keyboard_name
def keymap ( keyboard_name ) :
2019-07-15 12:14:27 -07:00
""" Locate the correct directory for storing a keymap.
Args:
2020-02-17 11:42:11 -08:00
2020-11-07 09:56:08 -08:00
keyboard_name
2019-07-15 12:14:27 -07:00
The name of the keyboard. Example: clueboard/66/rev3
"""
2020-11-07 09:56:08 -08:00
keyboard_folder = keyboard ( keyboard_name )
2020-02-17 11:42:11 -08:00
for i in range ( MAX_KEYBOARD_SUBFOLDERS ) :
if ( keyboard_folder / ' keymaps ' ) . exists ( ) :
return ( keyboard_folder / ' keymaps ' ) . resolve ( )
2019-07-15 12:14:27 -07:00
2020-02-17 11:42:11 -08:00
keyboard_folder = keyboard_folder . parent
2019-07-15 12:14:27 -07:00
2020-02-17 11:42:11 -08:00
logging . error ( ' Could not find the keymaps directory! ' )
2020-11-07 09:56:08 -08:00
raise NoSuchKeyboardError ( ' Could not find keymaps directory for: %s ' % keyboard_name )
2019-07-15 12:14:27 -07:00
def normpath ( path ) :
2020-02-17 11:42:11 -08:00
""" Returns a `pathlib.Path()` object for a given path.
2019-07-15 12:14:27 -07:00
2020-02-17 11:42:11 -08:00
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.
2019-07-15 12:14:27 -07:00
"""
2020-02-17 11:42:11 -08:00
path = Path ( path )
if path . is_absolute ( ) :
2020-03-13 15:47:04 -07:00
return path
2019-07-15 12:14:27 -07:00
2020-02-17 11:42:11 -08:00
return Path ( os . environ [ ' ORIG_CWD ' ] ) / path
2021-02-07 21:02:51 +00:00
class FileType ( argparse . FileType ) :
def __call__ ( self , string ) :
""" normalize and check exists
otherwise magic strings like ' - ' for stdin resolve to bad paths
"""
norm = normpath ( string )
return super ( ) . __call__ ( norm if norm . exists ( ) else string )