~fabrixxm/pytrek

b0d988dfba1b3043fd7f26248fcdfe56c04fc4a9 — fabrixxm 4 months ago 6b4df9b
Rebuild cached entries if template is newer
4 files changed, 35 insertions(+), 5 deletions(-)

M pytrek/__init__.py
M pytrek/cache.py
M pytrek/template.py
M pytrek/types.py
M pytrek/__init__.py => pytrek/__init__.py +5 -5
@@ 143,16 143,16 @@ def write_entry(n: int, entry: Entry, site: Site, env: Environment, cfg: Config)
        imagesmtimes = max(i.mtime for i in entry.images)
    else:
        imagesmtimes = 0.
    lastmtime = max(entrymtime, imagesmtimes)
    lastmtime = max(entrymtime, imagesmtimes, cfg.templatemtime)

    # output page
    if not outputfile.exists() or outputfile.stat().st_mtime < lastmtime:
        logger.info("OUTPUT: ENTRY #%s: '%s' -> %s", n, entry.title, outputfile)
        home_href = ".." if site.baseurl == "" else site.baseurl

        template = env.get_template("entry.html.j2")
        tpl = env.get_template("entry.html.j2")
        outputfile.write_text(
            template.render(
            tpl.render(
                {
                    "home_href": home_href,
                    "site": site,


@@ 193,9 193,9 @@ def write_index(site: Site, env: Environment, cfg: Config):
    logger.info("OUTPUT: index (%d entries)", len(site.entries))
    home_href = "." if site.baseurl == "" else site.baseurl

    template = env.get_template("index.html.j2")
    tpl = env.get_template("index.html.j2")
    outputfile.write_text(
        template.render(
        tpl.render(
            {
                "home_href": home_href,
                "site": site,

M pytrek/cache.py => pytrek/cache.py +8 -0
@@ 73,6 73,11 @@ class Cache:

    @classmethod
    def ignore(cls, path: Path, ignore: bool = True):
        """
        Cache a path as "ignorable"

        This allow to skip to try to load invalid files on rebuild
        """
        cachedpath = _cache_path(path)
        ignorepath = cachedpath.parent / (cachedpath.name + ".ignore")
        if ignore:


@@ 82,6 87,9 @@ class Cache:

    @classmethod
    def ignorable(cls, path: Path) -> bool:
        """
        Check if a path is cached as ignorable
        """
        cachedpath = _cache_path(path)
        ignorepath = cachedpath.parent / (cachedpath.name + ".ignore")
        if ignorepath.exists():

M pytrek/template.py => pytrek/template.py +18 -0
@@ 42,6 42,22 @@ class AssetExtension(StandaloneTag):
        return f"{home_href}/assets/{assetname}"


def get_template_mtime(cfg: Config) -> float:
    """
    Return last modification time of anything in template paths
    """
    paths = [
        Path(__file__).parent / "themes/default/templates",     # default template
    ]
    if cfg.theme is not None:
        paths.append(cfg.theme / "templates")
    lastmtime = 0.
    for path in paths:
        for file in path.glob("**/*"):
            lastmtime = max(lastmtime, file.stat().st_mtime)
    return lastmtime


def setup(cfg: Config) -> Environment:
    loaders: list[BaseLoader] = [
        PackageLoader(package_name="pytrek", package_path="themes/default/templates"),


@@ 54,6 70,8 @@ def setup(cfg: Config) -> Environment:
        assets.append(cfg.theme / "assets")

    cfg.asset_paths = assets
    cfg.templatemtime = get_template_mtime(cfg)

    env = Environment(
        loader=ChoiceLoader(loaders), extensions=[AssetExtension], autoescape=True
    )

M pytrek/types.py => pytrek/types.py +4 -0
@@ 38,6 38,10 @@ class Config:
    # list of path to search for assets
    asset_paths: list[Path] = default_list()

    # last mtime of files in template folders
    templatemtime: float = 0.



@dataclass
class EntryImage: