feat: add glob method on Context
This commit is contained in:
parent
fc9a8dc461
commit
e98344e0a0
|
|
@ -20,13 +20,12 @@ class Context:
|
|||
self.__root_directory = root_directory
|
||||
self.__output_directory = root_directory / "build"
|
||||
self.__content_directory = root_directory / "content"
|
||||
self.__environment = Environment(
|
||||
loader=FileSystemLoader(searchpath=root_directory / "src"),
|
||||
)
|
||||
self.__environment = Environment(loader=FileSystemLoader(searchpath=root_directory / "src"))
|
||||
self.__translations: dict[str, GNUTranslations | NullTranslations] = {}
|
||||
self.__current_language: str | None = None
|
||||
|
||||
self.add_filters(load=self.__load, write=self.__write)
|
||||
self.add_filters(load=self.__load, write=self.__write, glob=self.__glob)
|
||||
self.add_globals(load=self.__load, write=self.__write, glob=self.__glob)
|
||||
|
||||
@property
|
||||
def current_language(self) -> str | None:
|
||||
|
|
@ -35,6 +34,9 @@ class Context:
|
|||
def add_filters(self, **filters: Any) -> None:
|
||||
self.__environment.filters.update(**filters)
|
||||
|
||||
def add_globals(self, **globals: Any) -> None:
|
||||
self.__environment.globals.update(**globals)
|
||||
|
||||
def load_extensions(self, *extensions: str) -> None:
|
||||
for extension in extensions:
|
||||
module = import_module(extension)
|
||||
|
|
@ -80,6 +82,26 @@ class Context:
|
|||
with localized_path.open("r", encoding="utf-8") as content_file:
|
||||
return Content(localized_path, content_file.read(), current_language)
|
||||
|
||||
@pass_context
|
||||
def __glob(
|
||||
self, context: JinjaContext, pattern: str, include_base_language: bool = False
|
||||
) -> Iterator[Content]:
|
||||
roots: list[Path] = []
|
||||
current_language = self.current_language
|
||||
if current_language is None:
|
||||
roots.append(self.__content_directory)
|
||||
else:
|
||||
if include_base_language:
|
||||
roots.append(self.__content_directory / _DEFAULT_LANGUAGE)
|
||||
roots.append(self.__content_directory / current_language)
|
||||
|
||||
paths: set[Path] = set()
|
||||
for root in roots:
|
||||
paths = paths | set(it.relative_to(root) for it in root.glob(pattern))
|
||||
|
||||
for path in paths:
|
||||
yield self.__load(context, path)
|
||||
|
||||
def __write(self, content: Content) -> Path:
|
||||
relative_path = content.path.relative_to(self.__content_directory)
|
||||
output_path = self.__output_directory / relative_path
|
||||
|
|
|
|||
|
|
@ -51,3 +51,32 @@ def test_write(datadir: Path) -> None:
|
|||
|
||||
with open(datadir / "build" / "output.html", encoding="utf-8") as ouput_file:
|
||||
assert ouput_file.read() == "/content.txt"
|
||||
|
||||
|
||||
def test_glob(datadir: Path) -> None:
|
||||
context = Context(datadir)
|
||||
context.render("test-glob.html", "output.html")
|
||||
|
||||
with open(datadir / "build" / "output.html", encoding="utf-8") as ouput_file:
|
||||
assert ouput_file.read() == "Otters\nCaiman\n"
|
||||
|
||||
|
||||
def test_glob_localized(datadir: Path) -> None:
|
||||
context = Context(datadir)
|
||||
context.load_translations("tests", datadir / "locale", "fr")
|
||||
context.render("test-glob.html", "output.html")
|
||||
|
||||
with open(datadir / "build" / "en" / "output.html", encoding="utf-8") as ouput_file:
|
||||
assert ouput_file.read() == "Otters\nCaimans\n"
|
||||
|
||||
with open(datadir / "build" / "fr" / "output.html", encoding="utf-8") as ouput_file:
|
||||
assert ouput_file.read() == "Loutres\n"
|
||||
|
||||
|
||||
def test_glob_localized_include_base_language(datadir: Path) -> None:
|
||||
context = Context(datadir)
|
||||
context.load_translations("tests", datadir / "locale", "fr")
|
||||
context.render("test-glob-include-base-language.html", "output.html")
|
||||
|
||||
with open(datadir / "build" / "fr" / "output.html", encoding="utf-8") as ouput_file:
|
||||
assert ouput_file.read() == "Caimans\nLoutres\n"
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
Caiman
|
||||
|
|
@ -8,7 +8,7 @@ msgid ""
|
|||
msgstr ""
|
||||
"Project-Id-Version: PROJECT VERSION\n"
|
||||
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
|
||||
"POT-Creation-Date: 2024-05-21 22:35+0200\n"
|
||||
"POT-Creation-Date: 2024-05-21 23:12+0200\n"
|
||||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
||||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
||||
"Language: fr\n"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
{% for content in glob("*.txt", include_base_language=True) | sort(attribute='path') -%}
|
||||
{{ content -}}
|
||||
{% endfor %}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
{% for content in glob("*.txt") | sort(attribute='path') -%}
|
||||
{{ content -}}
|
||||
{% endfor %}
|
||||
Loading…
Reference in New Issue