diff --git a/jweb/content.py b/jweb/content.py index 4bcb1b3..8b3b71e 100644 --- a/jweb/content.py +++ b/jweb/content.py @@ -1,9 +1,11 @@ from pathlib import Path -from typing import Any, Iterator +from typing import Any, Generic, Iterator, TypeVar + +TData = TypeVar("TData") -class Content: - def __init__(self, path: Path, data: Any, language: str | None = None) -> None: +class Content(Generic[TData]): + def __init__(self, path: Path, data: TData, language: str | None = None) -> None: self.__path = path self.__data = data self.__language = language @@ -17,14 +19,14 @@ class Content: return self.__language @property - def data(self) -> Any: + def data(self) -> TData: return self.__data def __str__(self) -> str: return str(self.__data) -class ContentField(Content): +class ContentField(Content[Any]): def __getitem__(self, key: Any) -> Any: return ContentField(self.path, self.data.get(key), self.language) diff --git a/jweb/context.py b/jweb/context.py index 91b418c..6c8fe6a 100644 --- a/jweb/context.py +++ b/jweb/context.py @@ -70,7 +70,7 @@ class Context: self.__render(source, self.__output_directory / output, **context) @pass_context - def __load(self, context: JinjaContext, path: str | Path) -> Content: + def __load(self, context: JinjaContext, path: str | Path) -> Content[str]: current_language = self.current_language if current_language: localized_path = self.__content_directory / current_language / path @@ -85,7 +85,7 @@ class Context: @pass_context def __glob( self, context: JinjaContext, pattern: str, include_base_language: bool = False - ) -> Iterator[Content]: + ) -> Iterator[Content[str]]: roots: list[Path] = [] current_language = self.current_language if current_language is None: @@ -102,7 +102,7 @@ class Context: for path in paths: yield self.__load(context, path) - def __write(self, content: Content) -> Path: + def __write(self, content: Content[str]) -> Path: relative_path = content.path.relative_to(self.__content_directory) output_path = self.__output_directory / relative_path output_path.parent.mkdir(parents=True, exist_ok=True) diff --git a/jweb/extensions/git.py b/jweb/extensions/git.py index f8ca5ba..ddf623d 100644 --- a/jweb/extensions/git.py +++ b/jweb/extensions/git.py @@ -1,7 +1,7 @@ from datetime import datetime from subprocess import check_output +from typing import Any -from jweb.content import Content from jweb.context import Context @@ -9,7 +9,7 @@ def load_extension(context: Context) -> None: context.add_filters(git_creation_date=_git_creation_date) -def _git_creation_date(content: Content) -> datetime: +def _git_creation_date(content: Any) -> datetime: git_dir = check_output(["git", "rev-parse", "--show-toplevel"], encoding="utf-8") git_dir = git_dir.strip() log = check_output( diff --git a/jweb/extensions/markdown.py b/jweb/extensions/markdown.py index 952df31..93171b3 100644 --- a/jweb/extensions/markdown.py +++ b/jweb/extensions/markdown.py @@ -11,7 +11,7 @@ def load_extension(context: Context) -> None: class _MarkdownDocument: - def __init__(self, content: Content) -> None: + def __init__(self, content: Content[Any]) -> None: if not isinstance(content, Content) or not isinstance(content.data, (str, bytes)): raise ValueError("markdown filter can only accept byte or string content") diff --git a/jweb/git.py b/jweb/git.py index c3a2cc3..ef6f791 100644 --- a/jweb/git.py +++ b/jweb/git.py @@ -1,10 +1,9 @@ from datetime import datetime from subprocess import check_output - -from jweb.content import Content +from typing import Any -def git_creation_date(content: Content) -> datetime: +def git_creation_date(content: Any) -> datetime: git_dir = check_output(["git", "rev-parse", "--show-toplevel"], encoding="utf-8") git_dir = git_dir.strip() log = check_output( diff --git a/tests/test_context/locale/fr/LC_MESSAGES/tests.po b/tests/test_context/locale/fr/LC_MESSAGES/tests.po index b5e464d..8af53fc 100644 --- a/tests/test_context/locale/fr/LC_MESSAGES/tests.po +++ b/tests/test_context/locale/fr/LC_MESSAGES/tests.po @@ -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 23:12+0200\n" +"POT-Creation-Date: 2024-05-21 23:32+0200\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language: fr\n"