Skip to content

Commit

Permalink
Add support for pathlib.Path to normalizeFilePath. (#784)
Browse files Browse the repository at this point in the history
* Add support for pathlib.Path to `normalizeFilePath`.

Add `pathlib.Path` to exception documentation in `normalizeFilePath`.

* Update normalizers.py

- broadened scope of `Path.resolve` for improved robustness 
- document implicit exception.
- improved error message.
- corrected typo in docstring.

---------

Co-authored-by: knutnergaard <knutnergaard@users.noreply.github.com>
  • Loading branch information
knutnergaard and knutnergaard authored Nov 22, 2024
1 parent b38e4e6 commit 8870400
Showing 1 changed file with 13 additions and 7 deletions.
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

0 comments on commit 8870400

Please sign in to comment.