Merge pull request #1113 from pallets/namespace-loader
Before adoption: this commit came in with the history the project adopted, so no landing records it.
PackageLoader understands namespace packages
authored by David Lord · committed by GitHub · parents 28f12c0, d2e0e78 · Browse files · 2 files·+43 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+43 added−10 removed
- other:+43 added−10 removed
Provenance
- Commit
9176fbd- Tree
ef4fdd2- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
28f12c0- Parent 2
d2e0e78
2 files changed+43 added−10 removed
modified:jinja2/loaders.py+39 added-8 removed
10 unmodified lines11 import os12 import pkgutil13 import sys14 import weakref15+from importlib import import_module16 from types import ModuleType17 from os import path18 from hashlib import sha119 from jinja2.exceptions import TemplateNotFound211 unmodified lines231 zip/egg files (less common) are supported. The Python API for232 introspecting data in packages is too limited to support other233 installation methods the way this loader requires.234 235+ There is limited support for :pep:`420` namespace packages. The236+ template directory is assumed to only be in one namespace237+ contributor. Zip files contributing to a namespace are not238+ supported.239+240 .. versionchanged:: 2.11.0241 No longer uses ``setuptools`` as a dependency.242+243+ .. versionchanged:: 2.11.0244+ Limited PEP 420 namespace package support.245 """246 247 def __init__(self, package_name, package_path="templates", encoding="utf-8"):248 if package_path == os.path.curdir:249 package_path = ""250 elif package_path[:2] == os.path.curdir + os.path.sep:251 package_path = package_path[2:]252 244- package_path = os.path.normpath(package_path)245-246- self.package_name = package_name253+ package_path = os.path.normpath(package_path).rstrip(os.path.sep)254 self.package_path = package_path255+ self.package_name = package_name256 self.encoding = encoding257 258+ # Make sure the package exists. This also makes namespace259+ # packages work, otherwise get_loader returns None.260+ import_module(package_name)250- self._loader = pkgutil.get_loader(package_name)261+ self._loader = loader = pkgutil.get_loader(package_name)262+263 # Zip loader's archive attribute points at the zip.252- self._archive = getattr(self._loader, "archive", None)253- self._template_root = os.path.join(254- os.path.dirname(self._loader.get_filename(package_name)), package_path255- ).rstrip(os.path.sep)264+ self._archive = getattr(loader, "archive", None)265+ self._template_root = None266 267+ if hasattr(loader, "get_filename"):268+ # A standard directory package, or a zip package.269+ self._template_root = os.path.join(270+ os.path.dirname(loader.get_filename(package_name)), package_path271+ )272+ elif hasattr(loader, "_path"):273+ # A namespace package, limited support. Find the first274+ # contributor with the template directory.275+ for root in loader._path:276+ root = os.path.join(root, package_path)277+278+ if os.path.isdir(root):279+ self._template_root = root280+ break281+282+ if self._template_root is None:283+ raise ValueError(284+ "The %r package was not installed in a way that"285+ " PackageLoader understands." % package_name286+ )287+288 def get_source(self, environment, template):289 p = os.path.join(self._template_root, *split_template_path(template))290 291 if self._archive is None:276 unmodified linesmodified:CHANGES.rst+4 added-2 removed
53 unmodified lines54 - In async environments, the ``loop`` attributes ``length`` and55 ``revindex`` work for async iterators. :pr:`1101`56 - In async environments, values from attribute/property access will57 be awaited if needed. :pr:`1101`58-- ``PackageLoader`` doesn't depend on setuptools or pkg_resources.59- :issue:`970`58+- :class:`~loader.PackageLoader` doesn't depend on setuptools or59+ pkg_resources. :issue:`970`60+- ``PackageLoader`` has limited support for :pep:`420` namespace61+ packages. :issue:`1097`62 - Support :class:`os.PathLike` objects in63 :class:`~loader.FileSystemLoader` and :class:`~loader.ModuleLoader`.64 :issue:`870`65 - :class:`~nativetypes.NativeTemplate` correctly handles quotes664 unmodified lines