Skip to content

Commit

Permalink
Merge pull request #1398 from googlefonts/better-errors-unknown-filet…
Browse files Browse the repository at this point in the history
…ypes

[fontra-copy] Better exceptions when trying to use unknown file types
  • Loading branch information
justvanrossum authored May 27, 2024
2 parents f3b67c3 + d792770 commit 86035e4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/fontra/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
logger = logging.getLogger(__name__)


class UnknownFileType(Exception):
pass


def getFileSystemBackend(path: PathLike) -> ReadableFontBackend:
return _getFileSystemBackend(path, False)

Expand All @@ -27,7 +31,12 @@ def _getFileSystemBackend(path: PathLike, create: bool) -> WritableFontBackend:
logger.info(f"{logVerb} project {path.name}...")
fileType = path.suffix.lstrip(".").lower()
backendEntryPoints = entry_points(group="fontra.filesystem.backends")
entryPoint = backendEntryPoints[fileType]
try:
entryPoint = backendEntryPoints[fileType]
except KeyError:
raise UnknownFileType(
f"Can't find backend for files with extension '.{fileType}'"
)
backendClass = entryPoint.load()

if create:
Expand Down
3 changes: 2 additions & 1 deletion src/fontra/backends/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ async def mainAsync() -> None:
glyphNames.extend(args.glyph_names_file.read().split())

sourcePath = pathlib.Path(args.source)
assert sourcePath.exists()
if not sourcePath.exists():
raise FileNotFoundError(sourcePath)
destPath = pathlib.Path(args.destination)
if args:
if destPath.is_dir():
Expand Down
9 changes: 8 additions & 1 deletion test-py/test_backends_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
from test_backends_designspace import fileNamesFromDir

from fontra.backends import getFileSystemBackend, newFileSystemBackend
from fontra.backends import UnknownFileType, getFileSystemBackend, newFileSystemBackend
from fontra.backends.copy import copyFont

mutatorDSPath = (
Expand Down Expand Up @@ -51,3 +51,10 @@ def test_fontra_copy(tmpdir):
"MutatorCopy_LightWide.ufo",
"MutatorCopy_Regular.ufo",
] == fileNamesFromDir(tmpdir)


def test_newFileSystemBackend_unknown_filetype():
with pytest.raises(
UnknownFileType, match="Can't find backend for files with extension"
):
_ = newFileSystemBackend("test.someunknownextension")

0 comments on commit 86035e4

Please sign in to comment.