|
4 | 4 |
|
5 | 5 | import base64 |
6 | 6 | import json |
| 7 | +import os |
7 | 8 | import sys |
8 | 9 | import types |
9 | 10 | import zlib |
10 | 11 | from importlib import import_module |
11 | 12 | from importlib.abc import Loader |
12 | 13 | from importlib.metadata import Distribution |
13 | 14 | from importlib.metadata import DistributionFinder |
14 | | -from typing import Any |
15 | 15 | from typing import IO |
| 16 | +from typing import TYPE_CHECKING |
| 17 | +from typing import Any |
16 | 18 | from typing import Iterable |
17 | 19 | from typing import Sequence |
18 | 20 |
|
| 21 | +if TYPE_CHECKING: |
| 22 | + pass |
| 23 | + |
| 24 | + |
| 25 | +class DictDistribution(Distribution): |
| 26 | + data: dict[str, str] |
| 27 | + |
| 28 | + def __init__(self, data: dict[str, str]) -> None: |
| 29 | + self.data = data |
| 30 | + |
| 31 | + def read_text(self, filename): |
| 32 | + return self.data[filename] |
| 33 | + |
| 34 | + def locate_file(self, path: str | os.PathLike[str]) -> os.PathLike[str]: |
| 35 | + raise FileNotFoundError(path) |
| 36 | + |
19 | 37 |
|
20 | 38 | class DictImporter(DistributionFinder, Loader): |
21 | | - def __init__(self, sources: dict[str, str]): |
| 39 | + """a limited loader/importer for distributins send via json-lines""" |
| 40 | + |
| 41 | + def __init__(self, sources: dict[str, str], distribution: DictDistribution): |
22 | 42 | self.sources = sources |
| 43 | + self.distribution = distribution |
23 | 44 |
|
24 | 45 | def find_distributions( |
25 | 46 | self, context: DistributionFinder.Context = DistributionFinder.Context() |
26 | 47 | ) -> Iterable[Distribution]: |
27 | | - return [] |
| 48 | + return [self.distribution] |
28 | 49 |
|
29 | 50 | def find_module( |
30 | 51 | self, fullname: str, path: Sequence[str | bytes] | None = None |
@@ -64,11 +85,12 @@ def get_source(self, name: str) -> str | None: |
64 | 85 |
|
65 | 86 | def bootstrap( |
66 | 87 | modules: dict[str, str], |
| 88 | + distribution: dict[str, str], |
67 | 89 | entry: str, |
68 | 90 | args: dict[str, Any], |
69 | 91 | set_argv: list[str] | None, |
70 | 92 | ) -> None: |
71 | | - importer = DictImporter(modules) |
| 93 | + importer = DictImporter(modules, distribution=DictDistribution(distribution)) |
72 | 94 | sys.meta_path.append(importer) |
73 | 95 | module, attr = entry.split(":") |
74 | 96 | loaded_module = import_module(module) |
|
0 commit comments