Skip to content

Commit

Permalink
fix: correct handling of some paths for JinjaLoaderFileSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Oct 21, 2023
1 parent 082b57b commit d9d67ed
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions mknodes/jinja/jinjaloaderfilesystem.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import io
import pathlib

import fsspec
import jinja2
Expand All @@ -12,7 +13,11 @@


class JinjaLoaderFileSystem(fsspec.AbstractFileSystem):
"""A FsSpec Filesystem implementation for jinja environment templates."""
"""A FsSpec Filesystem implementation for jinja environment templates.
This virtual file system allows to browse and access all available templates of an
environment by utilizing loader.list_templates and loader.get_source.
"""

protocol = "jinja"

Expand All @@ -24,7 +29,13 @@ def ls(self, path: str, detail: bool = True, **kwargs) -> list[str]:
if not self.env.loader:
return []
paths = self.env.loader.list_templates()
return [p for p in paths if p.startswith(path)]
if not path:
return paths
path = pathlib.Path(path).as_posix().rstrip("/")
folders = [i for i in paths if i.rsplit("/", 1)[0] == path]
if not folders:
raise FileNotFoundError(path)
return folders

def _open(self, path: str, mode="rb", **kwargs) -> io.BytesIO:
if not self.env.loader:
Expand Down

0 comments on commit d9d67ed

Please sign in to comment.