|
4 | 4 | import os |
5 | 5 | import sys |
6 | 6 | import zipfile |
| 7 | +from importlib.abc import SourceLoader |
| 8 | +from importlib.util import spec_from_file_location |
7 | 9 |
|
8 | 10 | ABS_HERE = os.path.abspath(os.path.dirname(__file__)) |
9 | | -NEW_IMPORT_SYSTEM = sys.version_info[0] >= 3 # noqa: PLR2004 |
10 | 11 |
|
11 | 12 |
|
12 | 13 | class VersionPlatformSelect: |
@@ -69,18 +70,34 @@ def _register_distutils_finder(self): |
69 | 70 | if "distlib" not in self.modules: |
70 | 71 | return |
71 | 72 |
|
| 73 | + class Resource: |
| 74 | + def __init__(self, content) -> None: |
| 75 | + self.bytes = content |
| 76 | + self.is_container = False |
| 77 | + |
72 | 78 | class DistlibFinder: |
73 | 79 | def __init__(self, path, loader) -> None: |
74 | 80 | self.path = path |
75 | 81 | self.loader = loader |
76 | 82 |
|
77 | 83 | def find(self, name): |
78 | | - class Resource: |
79 | | - def __init__(self, content) -> None: |
80 | | - self.bytes = content |
81 | | - |
82 | | - full_path = os.path.join(self.path, name) |
83 | | - return Resource(self.loader.get_data(full_path)) |
| 84 | + return Resource(self.loader.get_data(os.path.join(self.path, name))) |
| 85 | + |
| 86 | + def iterator(self, resource_name): |
| 87 | + resource = self.find(resource_name) |
| 88 | + if resource is not None: |
| 89 | + todo = [resource] |
| 90 | + while todo: |
| 91 | + resource = todo.pop(0) |
| 92 | + yield resource |
| 93 | + if resource.is_container: |
| 94 | + rname = resource.name |
| 95 | + for name in resource.resources: |
| 96 | + child = self.find(f"{rname}/{name}" if rname else name) |
| 97 | + if child.is_container: |
| 98 | + todo.append(child) |
| 99 | + else: |
| 100 | + yield child |
84 | 101 |
|
85 | 102 | from distlib.resources import register_finder # noqa: PLC0415 |
86 | 103 |
|
@@ -113,41 +130,15 @@ def locate_file(self, path): |
113 | 130 | return _VER_DISTRIBUTION_CLASS |
114 | 131 |
|
115 | 132 |
|
116 | | -if NEW_IMPORT_SYSTEM: |
117 | | - from importlib.abc import SourceLoader |
118 | | - from importlib.util import spec_from_file_location |
119 | | - |
120 | | - class VersionedFindLoad(VersionPlatformSelect, SourceLoader): |
121 | | - def find_spec(self, fullname, path, target=None): # noqa: ARG002 |
122 | | - zip_path = self.find_mod(fullname) |
123 | | - if zip_path is not None: |
124 | | - return spec_from_file_location(name=fullname, loader=self) |
125 | | - return None |
126 | | - |
127 | | - def module_repr(self, module): |
128 | | - raise NotImplementedError |
129 | | - |
130 | | -else: |
131 | | - from imp import new_module |
132 | | - |
133 | | - class VersionedFindLoad(VersionPlatformSelect): |
134 | | - def find_module(self, fullname, path=None): # noqa: ARG002 |
135 | | - return self if self.find_mod(fullname) else None |
136 | | - |
137 | | - def load_module(self, fullname): |
138 | | - filename = self.get_filename(fullname) |
139 | | - code = self.get_data(filename) |
140 | | - mod = sys.modules.setdefault(fullname, new_module(fullname)) |
141 | | - mod.__file__ = filename |
142 | | - mod.__loader__ = self |
143 | | - is_package = filename.endswith("__init__.py") |
144 | | - if is_package: |
145 | | - mod.__path__ = [os.path.dirname(filename)] |
146 | | - mod.__package__ = fullname |
147 | | - else: |
148 | | - mod.__package__ = fullname.rpartition(".")[0] |
149 | | - exec(code, mod.__dict__) # noqa: S102 |
150 | | - return mod |
| 133 | +class VersionedFindLoad(VersionPlatformSelect, SourceLoader): |
| 134 | + def find_spec(self, fullname, path, target=None): # noqa: ARG002 |
| 135 | + zip_path = self.find_mod(fullname) |
| 136 | + if zip_path is not None: |
| 137 | + return spec_from_file_location(name=fullname, loader=self) |
| 138 | + return None |
| 139 | + |
| 140 | + def module_repr(self, module): |
| 141 | + raise NotImplementedError |
151 | 142 |
|
152 | 143 |
|
153 | 144 | def run(): |
|
0 commit comments