aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Jinja2/py3/patches/03-fix-PackageLoader.patch
blob: b2a7691914eebf221bc2a3dc767b084af4bcb4b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
--- contrib/python/Jinja2/py3/jinja2/loaders.py	(index) 
+++ contrib/python/Jinja2/py3/jinja2/loaders.py	(working tree) 
@@ -4,6 +4,7 @@ sources. 
 import importlib.util 
 import os 
 import sys 
+import pkgutil 
 import typing as t 
 import weakref 
 import zipimport 
@@ -20,6 +21,8 @@ if t.TYPE_CHECKING: 
     from .environment import Environment 
     from .environment import Template 
  
+import __res as arcadia_res 
+ 
  
 def split_template_path(template: str) -> t.List[str]: 
     """Split a path into segments and perform a sanity check.  If it detects 
@@ -284,19 +287,22 @@ class PackageLoader(BaseLoader): 
  
         # Make sure the package exists. This also makes namespace 
         # packages work, otherwise get_loader returns None. 
-        import_module(package_name) 
+        package = import_module(package_name) 
         spec = importlib.util.find_spec(package_name) 
         assert spec is not None, "An import spec was not found for the package." 
         loader = spec.loader 
         assert loader is not None, "A loader was not found for the package." 
         self._loader = loader 
         self._archive = None 
+        self._package = package 
         template_root = None 
  
         if isinstance(loader, zipimport.zipimporter): 
             self._archive = loader.archive 
             pkgdir = next(iter(spec.submodule_search_locations))  # type: ignore 
             template_root = os.path.join(pkgdir, package_path) 
+        elif hasattr(loader, "arcadia_source_finder"): 
+            template_root = os.path.dirname(package.__file__) 
         else:
             roots: t.List[str] = []
 
@@ -329,7 +335,16 @@ class PackageLoader(BaseLoader):
         p = os.path.join(self._template_root, *split_template_path(template)) 
         up_to_date: t.Optional[t.Callable[[], bool]] 
  
-        if self._archive is None: 
+        if self._archive is None and hasattr(self, "_package"): 
+            try: 
+                source = pkgutil.get_data(self.package_name, os.path.join(self.package_path, *split_template_path(template))) 
+            except OSError: 
+                raise TemplateNotFound(template) 
+ 
+            def up_to_date() -> bool: 
+                return True 
+ 
+        elif self._archive is None: 
             # Package is a directory. 
             if not os.path.isfile(p): 
                 raise TemplateNotFound(template) 
@@ -359,7 +374,12 @@ class PackageLoader(BaseLoader):
     def list_templates(self) -> t.List[str]: 
         results: t.List[str] = [] 
  
-        if self._archive is None: 
+        if self._archive is None and hasattr(self, "_package"): 
+            prefix = os.path.join(self._template_root, self.package_path).encode() + b"/" 
+            for name in arcadia_res.resfs_files(prefix): 
+                results.append(name.removeprefix(prefix).decode()) 
+ 
+        elif self._archive is None: 
             # Package is a directory. 
             offset = len(self._template_root)