Skip to content

loaderregistry

Class info

Classes

Name Children Inherits
LoaderRegistry
jinjarope.loaderregistry
Registry which caches and builds jinja loaders.

    🛈 DocStrings

    LoaderRegistry

    Registry which caches and builds jinja loaders.

    Source code in src/jinjarope/loaderregistry.py
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    class LoaderRegistry:
        """Registry which caches and builds jinja loaders."""
    
        def __init__(self) -> None:
            self.fs_loaders: dict[str, loaders.FileSystemLoader] = {}
            self.fsspec_loaders: dict[str, fsspecloaders.FsSpecFileSystemLoader] = {}
            self.package_loaders: dict[str, loaders.PackageLoader] = {}
    
        def by_path(
            self,
            path: str | os.PathLike[str],
        ) -> loaders.FileSystemLoader | fsspecloaders.FsSpecFileSystemLoader:
            """Convenience method to get a suiting loader for given path.
    
            Return a FsSpec loader for protocol-like paths or else a FileSystem loader.
    
            Args:
                path: The path to get a loader for
            """
            if "://" in str(path):
                return self.get_fsspec_loader(str(path))
            return self.get_filesystem_loader(path)
    
        def get_fsspec_loader(self, path: str) -> fsspecloaders.FsSpecFileSystemLoader:
            """Return a FsSpec loader for given path from registry.
    
            If the loader does not exist yet, create and cache it.
    
            Args:
                path: The path to get a loader for
            """
            if path in self.fsspec_loaders:
                return self.fsspec_loaders[path]
            loader = fsspecloaders.FsSpecFileSystemLoader(path)
            self.fsspec_loaders[path] = loader
            return loader
    
        def get_filesystem_loader(
            self,
            path: str | os.PathLike[str],
        ) -> loaders.FileSystemLoader:
            """Return a FileSystem loader for given path from registry.
    
            If the loader does not exist yet, create and cache it.
    
            Args:
                path: The path to get a loader for
            """
            path = pathlib.Path(path).as_posix()
            if path in self.fs_loaders:
                return self.fs_loaders[path]
            loader = loaders.FileSystemLoader(path)
            self.fs_loaders[path] = loader
            return loader
    
        def get_package_loader(self, package: str) -> loaders.PackageLoader:
            """Return a Package loader for given (dotted) package path from registry.
    
            If the loader does not exist yet, create and cache it.
    
            Args:
                package: The package to get a loader for
            """
            if package in self.package_loaders:
                return self.package_loaders[package]
            loader = loaders.PackageLoader(package)
            self.package_loaders[package] = loader
            return loader
    
        def get_loader(
            self,
            *args: str | types.ModuleType,
            dir_paths: list[str] | None = None,
            module_paths: list[str] | None = None,
            functions: list[Callable[..., Any]] | None = None,
            static: dict[str, str] | None = None,
            fsspec_paths: bool = True,
        ) -> jinja2.BaseLoader:
            """Construct a Loader.
    
            If given a positional argument, return a loader for given argument,
            otherwise return a ChoiceLoader based on given keyword arguments.
            Loader is constructed from cached sub-loaders if existing, otherwise they are
            created (and cached).
    
            Args:
                args: What to get a loader for (can be path or module)
                dir_paths: Directory paths (either FsSpec-protocol URLs to a folder or
                           filesystem paths)
                module_paths: (dotted) package paths
                functions: A list of callables
                static: A dictionary containing a path-> template mapping
                fsspec_paths: Whether a loader for FsSpec protcol paths should be added
            """
            match args:
                case (str() as path,):
                    return self.by_path(path)
                case (types.ModuleType() as mod,):
                    return loaders.PackageLoader(mod)
                case (fn,) if callable(fn):
                    return loaders.FunctionLoader(fn)
            m_paths = iterfilters.reduce_list(module_paths or [])
            loader = loaders.ChoiceLoader([self.get_package_loader(p) for p in m_paths])
            for file in iterfilters.reduce_list(dir_paths or []):
                if "://" in file:
                    loader |= self.get_fsspec_loader(file)
                else:
                    loader |= self.get_filesystem_loader(file)
            for function in functions or []:
                loader |= loaders.FunctionLoader(function)
            if static:
                loader |= loaders.DictLoader(static)
            if fsspec_paths:
                loader |= fsspecloaders.FsSpecProtocolPathLoader()
            return loader
    

    by_path

    Convenience method to get a suiting loader for given path.

    Return a FsSpec loader for protocol-like paths or else a FileSystem loader.

    Parameters:

    Name Type Description Default
    path str | PathLike[str]

    The path to get a loader for

    required
    Source code in src/jinjarope/loaderregistry.py
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    def by_path(
        self,
        path: str | os.PathLike[str],
    ) -> loaders.FileSystemLoader | fsspecloaders.FsSpecFileSystemLoader:
        """Convenience method to get a suiting loader for given path.
    
        Return a FsSpec loader for protocol-like paths or else a FileSystem loader.
    
        Args:
            path: The path to get a loader for
        """
        if "://" in str(path):
            return self.get_fsspec_loader(str(path))
        return self.get_filesystem_loader(path)
    

    get_filesystem_loader

    get_filesystem_loader(path: str | PathLike[str]) -> FileSystemLoader
    

    Return a FileSystem loader for given path from registry.

    If the loader does not exist yet, create and cache it.

    Parameters:

    Name Type Description Default
    path str | PathLike[str]

    The path to get a loader for

    required
    Source code in src/jinjarope/loaderregistry.py
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    def get_filesystem_loader(
        self,
        path: str | os.PathLike[str],
    ) -> loaders.FileSystemLoader:
        """Return a FileSystem loader for given path from registry.
    
        If the loader does not exist yet, create and cache it.
    
        Args:
            path: The path to get a loader for
        """
        path = pathlib.Path(path).as_posix()
        if path in self.fs_loaders:
            return self.fs_loaders[path]
        loader = loaders.FileSystemLoader(path)
        self.fs_loaders[path] = loader
        return loader
    

    get_fsspec_loader

    get_fsspec_loader(path: str) -> FsSpecFileSystemLoader
    

    Return a FsSpec loader for given path from registry.

    If the loader does not exist yet, create and cache it.

    Parameters:

    Name Type Description Default
    path str

    The path to get a loader for

    required
    Source code in src/jinjarope/loaderregistry.py
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    def get_fsspec_loader(self, path: str) -> fsspecloaders.FsSpecFileSystemLoader:
        """Return a FsSpec loader for given path from registry.
    
        If the loader does not exist yet, create and cache it.
    
        Args:
            path: The path to get a loader for
        """
        if path in self.fsspec_loaders:
            return self.fsspec_loaders[path]
        loader = fsspecloaders.FsSpecFileSystemLoader(path)
        self.fsspec_loaders[path] = loader
        return loader
    

    get_loader

    get_loader(
        *args: str | ModuleType,
        dir_paths: list[str] | None = None,
        module_paths: list[str] | None = None,
        functions: list[Callable[..., Any]] | None = None,
        static: dict[str, str] | None = None,
        fsspec_paths: bool = True
    ) -> BaseLoader
    

    Construct a Loader.

    If given a positional argument, return a loader for given argument, otherwise return a ChoiceLoader based on given keyword arguments. Loader is constructed from cached sub-loaders if existing, otherwise they are created (and cached).

    Parameters:

    Name Type Description Default
    args str | ModuleType

    What to get a loader for (can be path or module)

    ()
    dir_paths list[str] | None

    Directory paths (either FsSpec-protocol URLs to a folder or filesystem paths)

    None
    module_paths list[str] | None

    (dotted) package paths

    None
    functions list[Callable[..., Any]] | None

    A list of callables

    None
    static dict[str, str] | None

    A dictionary containing a path-> template mapping

    None
    fsspec_paths bool

    Whether a loader for FsSpec protcol paths should be added

    True
    Source code in src/jinjarope/loaderregistry.py
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    def get_loader(
        self,
        *args: str | types.ModuleType,
        dir_paths: list[str] | None = None,
        module_paths: list[str] | None = None,
        functions: list[Callable[..., Any]] | None = None,
        static: dict[str, str] | None = None,
        fsspec_paths: bool = True,
    ) -> jinja2.BaseLoader:
        """Construct a Loader.
    
        If given a positional argument, return a loader for given argument,
        otherwise return a ChoiceLoader based on given keyword arguments.
        Loader is constructed from cached sub-loaders if existing, otherwise they are
        created (and cached).
    
        Args:
            args: What to get a loader for (can be path or module)
            dir_paths: Directory paths (either FsSpec-protocol URLs to a folder or
                       filesystem paths)
            module_paths: (dotted) package paths
            functions: A list of callables
            static: A dictionary containing a path-> template mapping
            fsspec_paths: Whether a loader for FsSpec protcol paths should be added
        """
        match args:
            case (str() as path,):
                return self.by_path(path)
            case (types.ModuleType() as mod,):
                return loaders.PackageLoader(mod)
            case (fn,) if callable(fn):
                return loaders.FunctionLoader(fn)
        m_paths = iterfilters.reduce_list(module_paths or [])
        loader = loaders.ChoiceLoader([self.get_package_loader(p) for p in m_paths])
        for file in iterfilters.reduce_list(dir_paths or []):
            if "://" in file:
                loader |= self.get_fsspec_loader(file)
            else:
                loader |= self.get_filesystem_loader(file)
        for function in functions or []:
            loader |= loaders.FunctionLoader(function)
        if static:
            loader |= loaders.DictLoader(static)
        if fsspec_paths:
            loader |= fsspecloaders.FsSpecProtocolPathLoader()
        return loader
    

    get_package_loader

    get_package_loader(package: str) -> PackageLoader
    

    Return a Package loader for given (dotted) package path from registry.

    If the loader does not exist yet, create and cache it.

    Parameters:

    Name Type Description Default
    package str

    The package to get a loader for

    required
    Source code in src/jinjarope/loaderregistry.py
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    def get_package_loader(self, package: str) -> loaders.PackageLoader:
        """Return a Package loader for given (dotted) package path from registry.
    
        If the loader does not exist yet, create and cache it.
    
        Args:
            package: The package to get a loader for
        """
        if package in self.package_loaders:
            return self.package_loaders[package]
        loader = loaders.PackageLoader(package)
        self.package_loaders[package] = loader
        return loader