Skip to content

Commit

Permalink
loader: Fix compatibility with TurboGears
Browse files Browse the repository at this point in the history
TurboGears currently extends FileLoader and overrides the _filename
internal method.  Make this not break by changing up our API a bit.
  • Loading branch information
jackrosenthal committed Sep 29, 2024
1 parent 45e8137 commit 40b16a1
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion kajiki/loader.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import importlib.resources
import os
from pathlib import Path
Expand Down Expand Up @@ -69,7 +71,8 @@ def __init__(
"html5": lambda **kw: XMLTemplate(mode="html5", **kw),
}

def _find_resource(self, name):
def _filename(self, name: str) -> str | Path | None:
"""Get the filename of the requested resource."""
for base in self.path:
path = Path(base) / name
if path.is_file():
Expand All @@ -78,6 +81,18 @@ def _find_resource(self, name):
msg = f"{name} not found in any of {self.path}"
raise FileNotFoundError(msg)

def _find_resource(self, name: str) -> Path:
"""Locate the loadable resource and return a Path to it."""
filename = self._filename(name)
if not filename:
msg = f"{self!r}._filename returned {filename!r}"
raise FileNotFoundError(msg)
path = Path(filename)
if not path.is_file():
msg = f"{filename} doesn't exist or isn't a file."
raise FileNotFoundError(msg)
return path

def _load(self, name, encoding="utf-8", **kwargs):
"""Load a template from file."""
from kajiki import TextTemplate, XMLTemplate
Expand Down

0 comments on commit 40b16a1

Please sign in to comment.