Skip to content

Support generic Traversable in FileSystemLoader #2035

Open
@LecrisUT

Description

I have been looking at the implementations of FileSystemLoader and PackageLoader to see if any of them can support MultiplexedPath or more general Traversable.

The idea is that we have a project for which we place them jinja template directory such that it is effectively a namespace package that is shared with external plugin projects using entry-points. As such the base template directory is no longer a pathlib.Path type, but instead it is an imporlib.resources.MultiplexedPath.

There are two possible ways to support this.

Extend PackageLoader and use importlib.resources.as_file()

This would copy and extract all files that are under foo.bar package and put them in the native filesystem structure under a temporary directory. The major downside for this is that it can have a heavy overhead of copying and deleting files

Extend FileSystemLoader to accept arbitrary Traversable

This approach is more involved, but more efficient. Basically if the code relies only on the Traversable interface (e.g. pathlib.Path("foo") / "subfolder/template.j2") than it would be safe to simply use whatever interface the actual Traversableimplemented to navigate. If the relevant refactoring for this is contained within theFileSystemLoader`, than the refactoring is not that bad, and I could write a simple implementation for this, but I'm weary if there is some nuance that I am overlooking.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

      Participants

      @LecrisUT

      Issue actions

        Support generic `Traversable` in `FileSystemLoader` · Issue #2035 · pallets/jinja