style: use generic parameter for Content class
This commit is contained in:
parent
bdcb460a02
commit
cb2d41d571
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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 <EMAIL@ADDRESS>\n"
|
||||
"Language: fr\n"
|
||||
|
|
|
|||
Loading…
Reference in New Issue