From d804ec3c5bf8c3b1c509f1e9883b21578c40cf96 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sat, 24 Feb 2024 14:46:53 +0100 Subject: [PATCH] WIP STATE --- src/execnet/_dictimport.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/execnet/_dictimport.py b/src/execnet/_dictimport.py index 89f16789..3175c0c3 100644 --- a/src/execnet/_dictimport.py +++ b/src/execnet/_dictimport.py @@ -4,6 +4,7 @@ import base64 import json +import os import sys import types import zlib @@ -11,20 +12,40 @@ from importlib.abc import Loader from importlib.metadata import Distribution from importlib.metadata import DistributionFinder -from typing import Any from typing import IO +from typing import TYPE_CHECKING +from typing import Any from typing import Iterable from typing import Sequence +if TYPE_CHECKING: + pass + + +class DictDistribution(Distribution): + data: dict[str, str] + + def __init__(self, data: dict[str, str]) -> None: + self.data = data + + def read_text(self, filename): + return self.data[filename] + + def locate_file(self, path: str | os.PathLike[str]) -> os.PathLike[str]: + raise FileNotFoundError(path) + class DictImporter(DistributionFinder, Loader): - def __init__(self, sources: dict[str, str]): + """a limited loader/importer for distributins send via json-lines""" + + def __init__(self, sources: dict[str, str], distribution: DictDistribution): self.sources = sources + self.distribution = distribution def find_distributions( self, context: DistributionFinder.Context = DistributionFinder.Context() ) -> Iterable[Distribution]: - return [] + return [self.distribution] def find_module( self, fullname: str, path: Sequence[str | bytes] | None = None @@ -64,11 +85,12 @@ def get_source(self, name: str) -> str | None: def bootstrap( modules: dict[str, str], + distribution: dict[str, str], entry: str, args: dict[str, Any], set_argv: list[str] | None, ) -> None: - importer = DictImporter(modules) + importer = DictImporter(modules, distribution=DictDistribution(distribution)) sys.meta_path.append(importer) module, attr = entry.split(":") loaded_module = import_module(module)