qmk-firmware/lib/python/qmk/path.py
Erovia fbfd5312b9
CLI: Validate JSON keymap input (#16261)
* Fix schema validator

It should use the passed schema.

* Add required attributes to keymap schema

* Rework subcommands to validate the JSON keymaps

The 'compile', 'flash' and 'json2c' subcommands were reworked to add
JSON keymap validation so error is reported for non-JSON and
non-compliant-JSON inputs.

* Fix required fields in keymap schema

* Add tests

* Fix compiling keymaps directly from keymap directory

* Schema should not require version for now.
2022-02-28 20:02:39 +00:00

83 lines
2.3 KiB
Python

"""Functions that help us work with files and folders.
"""
import logging
import os
import argparse
from pathlib import Path
from qmk.constants import MAX_KEYBOARD_SUBFOLDERS, QMK_FIRMWARE
from qmk.errors import NoSuchKeyboardError
def is_keyboard(keyboard_name):
"""Returns True if `keyboard_name` is a keyboard we can compile.
"""
if keyboard_name:
keyboard_path = QMK_FIRMWARE / 'keyboards' / keyboard_name
rules_mk = keyboard_path / 'rules.mk'
return rules_mk.exists()
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
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):
"""Locate the correct directory for storing a keymap.
Args:
keyboard_name
The name of the keyboard. Example: clueboard/66/rev3
"""
keyboard_folder = keyboard(keyboard_name)
for _ in range(MAX_KEYBOARD_SUBFOLDERS):
if (keyboard_folder / 'keymaps').exists():
return (keyboard_folder / 'keymaps').resolve()
keyboard_folder = keyboard_folder.parent
logging.error('Could not find the keymaps directory!')
raise NoSuchKeyboardError('Could not find keymaps directory for: %s' % keyboard_name)
def normpath(path):
"""Returns a `pathlib.Path()` object for a given path.
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.
"""
path = Path(path)
if path.is_absolute():
return path
return Path(os.environ['ORIG_CWD']) / path
class FileType(argparse.FileType):
def __init__(self, encoding='UTF-8'):
# Use UTF8 by default for stdin
return super().__init__(encoding=encoding)
def __call__(self, string):
"""normalize and check exists
otherwise magic strings like '-' for stdin resolve to bad paths
"""
norm = normpath(string)
return norm if norm.exists() else super().__call__(string)