diff options
author | shadchin <shadchin@yandex-team.com> | 2024-02-09 11:05:14 +0300 |
---|---|---|
committer | Alexander Smirnov <alex@ydb.tech> | 2024-02-09 19:19:09 +0300 |
commit | 3d56a7865ae67118213e530b2803309ccc0c79c9 (patch) | |
tree | ba7cf9bb0c4018ef2ed6c852bfaa1b19a312aad1 /library/python/runtime_py3/sitecustomize.pyx | |
parent | 9ab05c389791e70ee3d110bba7d3c0f944283546 (diff) | |
download | ydb-3d56a7865ae67118213e530b2803309ccc0c79c9.tar.gz |
Rework joinpath for importlib.resources
Diffstat (limited to 'library/python/runtime_py3/sitecustomize.pyx')
-rw-r--r-- | library/python/runtime_py3/sitecustomize.pyx | 75 |
1 files changed, 52 insertions, 23 deletions
diff --git a/library/python/runtime_py3/sitecustomize.pyx b/library/python/runtime_py3/sitecustomize.pyx index 64b8b909b2..25b4ccb55a 100644 --- a/library/python/runtime_py3/sitecustomize.pyx +++ b/library/python/runtime_py3/sitecustomize.pyx @@ -5,7 +5,12 @@ import re import sys import warnings -from importlib.metadata import Distribution, DistributionFinder, PackageNotFoundError, Prepared +from importlib.metadata import ( + Distribution, + DistributionFinder, + PackageNotFoundError, + Prepared, +) from importlib.resources.abc import Traversable import __res @@ -15,12 +20,12 @@ with warnings.catch_warnings(action="ignore", category=DeprecationWarning): ResourceReader.register(__res._ResfsResourceReader) -METADATA_NAME = re.compile('^Name: (.*)$', re.MULTILINE) +METADATA_NAME = re.compile("^Name: (.*)$", re.MULTILINE) -class ArcadiaResourceHandle(Traversable): - def __init__(self, key): - self.resfs_key = key +class ArcadiaResource(Traversable): + def __init__(self, resfs_key): + self.resfs_key = resfs_key def is_file(self): return True @@ -28,14 +33,14 @@ class ArcadiaResourceHandle(Traversable): def is_dir(self): return False - def open(self, mode='r', *args, **kwargs): + def open(self, mode="r", *args, **kwargs): data = __res.find(self.resfs_key.encode("utf-8")) if data is None: raise FileNotFoundError(self.resfs_key) stream = io.BytesIO(data) - if 'b' not in mode: + if "b" not in mode: stream = io.TextIOWrapper(stream, *args, **kwargs) return stream @@ -50,6 +55,9 @@ class ArcadiaResourceHandle(Traversable): def name(self): return os.path.basename(self.resfs_key) + def __repr__(self): + return f"ArcadiaResource({self.resfs_key!r})" + class ArcadiaResourceContainer(Traversable): def __init__(self, prefix): @@ -62,29 +70,50 @@ class ArcadiaResourceContainer(Traversable): return False def iterdir(self): - for key, path_without_prefix in __res.iter_keys(self.resfs_prefix.encode("utf-8")): + seen = set() + for key, path_without_prefix in __res.iter_keys( + self.resfs_prefix.encode("utf-8") + ): if b"/" in path_without_prefix: - name = path_without_prefix.decode("utf-8").split("/", maxsplit=1)[0] - yield ArcadiaResourceContainer(f"{self.resfs_prefix}{name}/") + subdir = path_without_prefix.split(b"/", maxsplit=1)[0].decode("utf-8") + if subdir not in seen: + seen.add(subdir) + yield ArcadiaResourceContainer(f"{self.resfs_prefix}{subdir}/") else: - yield ArcadiaResourceHandle(key.decode("utf-8")) + yield ArcadiaResource(key.decode("utf-8")) def open(self, *args, **kwargs): raise IsADirectoryError(self.resfs_prefix) + @staticmethod + def _flatten(compound_names): + for name in compound_names: + yield from name.split("/") + def joinpath(self, *descendants): if not descendants: return self - return ArcadiaResourceHandle(os.path.join(self.resfs_prefix, *descendants)) + names = self._flatten(descendants) + target = next(names) + for traversable in self.iterdir(): + if traversable.name == target: + if isinstance(traversable, ArcadiaResource): + return traversable + else: + return traversable.joinpath(*names) + + raise FileNotFoundError("/".join(self._flatten(descendants))) @property def name(self): return os.path.basename(self.resfs_prefix[:-1]) + def __repr__(self): + return f"ArcadiaResourceContainer({self.resfs_prefix!r})" -class ArcadiaDistribution(Distribution): +class ArcadiaDistribution(Distribution): def __init__(self, prefix): self.prefix = prefix @@ -93,17 +122,17 @@ class ArcadiaDistribution(Distribution): return pathlib.Path(self.prefix) def read_text(self, filename): - data = __res.resfs_read(f'{self.prefix}{filename}') + data = __res.resfs_read(f"{self.prefix}{filename}") if data: - return data.decode('utf-8') + return data.decode("utf-8") + read_text.__doc__ = Distribution.read_text.__doc__ def locate_file(self, path): - return f'{self.prefix}{path}' + return f"{self.prefix}{path}" class ArcadiaMetadataFinder(DistributionFinder): - prefixes = {} @classmethod @@ -116,14 +145,14 @@ class ArcadiaMetadataFinder(DistributionFinder): cls.prefixes.clear() for resource in __res.resfs_files(): - resource = resource.decode('utf-8') - if not resource.endswith('METADATA'): + resource = resource.decode("utf-8") + if not resource.endswith("METADATA"): continue - data = __res.resfs_read(resource).decode('utf-8') + data = __res.resfs_read(resource).decode("utf-8") metadata_name = METADATA_NAME.search(data) if metadata_name: metadata_name = Prepared(metadata_name.group(1)) - cls.prefixes[metadata_name.normalized] = resource[:-len('METADATA')] + cls.prefixes[metadata_name.normalized] = resource[: -len("METADATA")] @classmethod def _search_prefixes(cls, name): @@ -136,10 +165,10 @@ class ArcadiaMetadataFinder(DistributionFinder): except KeyError: raise PackageNotFoundError(name) else: - for prefix in sorted(cls.prefixes.values()): - yield prefix + yield from sorted(cls.prefixes.values()) # monkeypatch standart library import importlib.metadata + importlib.metadata.MetadataPathFinder = ArcadiaMetadataFinder |