Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for pathlib.Path to normalizeFilePath. #784

Merged
merged 9 commits into from
Nov 22, 2024
20 changes: 13 additions & 7 deletions Lib/fontParts/base/normalizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import TYPE_CHECKING, Any, Optional, Tuple, Type, Union
from collections import Counter
from fontTools.misc.fixedTools import otRound
from pathlib import Path

from fontParts.base.annotations import (
T,
Expand Down Expand Up @@ -1138,17 +1139,22 @@ def normalizeGlyphNote(value: str) -> str:
# File Path


def normalizeFilePath(value: str) -> str:
def normalizeFilePath(value: Union[str, Path]) -> str:
"""Normalize a file path.

:param value: The file path to normalize as a :class:`str`.
:return: A :class:`str` representing the noramlized file path.
:raises TypeError if `value` is not a :class:`str`.
Relative paths are resolved automatically.

:param value: The file path to normalize as a :class:`str` or :class:`pathlib.Path`.
:return: A :class:`str` representing the normalized file path.
:raises TypeError if `value` is not a :class:`str` or :class:`pathlib.Path`.
:raises FileNotFoundError: If the file path cannot be resolved because it does not exist.

"""
if not isinstance(value, str):
raise TypeError(f"File paths must be strings, not {type(value).__name__}.")
return value
if not isinstance(value, (str, Path)):
raise TypeError(
f"File paths must be strings or Path objects, not {type(value).__name__}."
)
return str(Path(value).resolve())


# Interpolation
Expand Down
Loading