diff options
author | shadchin <shadchin@yandex-team.ru> | 2022-02-10 16:44:39 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:44:39 +0300 |
commit | e9656aae26e0358d5378e5b63dcac5c8dbe0e4d0 (patch) | |
tree | 64175d5cadab313b3e7039ebaa06c5bc3295e274 /library/python/runtime_py3/importer.pxi | |
parent | 2598ef1d0aee359b4b6d5fdd1758916d5907d04f (diff) | |
download | ydb-e9656aae26e0358d5378e5b63dcac5c8dbe0e4d0.tar.gz |
Restoring authorship annotation for <shadchin@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/python/runtime_py3/importer.pxi')
-rw-r--r-- | library/python/runtime_py3/importer.pxi | 104 |
1 files changed, 52 insertions, 52 deletions
diff --git a/library/python/runtime_py3/importer.pxi b/library/python/runtime_py3/importer.pxi index 6abbb3cf58..904f94dea2 100644 --- a/library/python/runtime_py3/importer.pxi +++ b/library/python/runtime_py3/importer.pxi @@ -333,58 +333,58 @@ class ResourceImporter(object): for m in self.arcadia_source_finder.iter_modules(self._package_prefix, prefix): yield m - def get_resource_reader(self, fullname): - try: - if not self.is_package(fullname): - return None - except ImportError: - return None - return _ResfsResourceReader(self, fullname) - - -class _ResfsResourceReader: - - def __init__(self, importer, fullname): - self.importer = importer - self.fullname = fullname - - import os - self.prefix = "{}/".format(os.path.dirname(self.importer.get_filename(self.fullname))) - - def open_resource(self, resource): - path = f'{self.prefix}{resource}' - from io import BytesIO - try: - return BytesIO(self.importer.get_data(path)) - except OSError: - raise FileNotFoundError(path) - - def resource_path(self, resource): - # All resources are in the binary file, so there is no path to the file. - # Raising FileNotFoundError tells the higher level API to extract the - # binary data and create a temporary file. - raise FileNotFoundError - - def is_resource(self, name): - path = f'{self.prefix}{name}' - try: - self.importer.get_data(path) - except OSError: - return False - return True - - def contents(self): - subdirs_seen = set() - for key in resfs_files(self.prefix): - relative = key[len(self.prefix):] - res_or_subdir, *other = relative.split(b'/') - if not other: - yield _s(res_or_subdir) - elif res_or_subdir not in subdirs_seen: - subdirs_seen.add(res_or_subdir) - yield _s(res_or_subdir) - - + def get_resource_reader(self, fullname): + try: + if not self.is_package(fullname): + return None + except ImportError: + return None + return _ResfsResourceReader(self, fullname) + + +class _ResfsResourceReader: + + def __init__(self, importer, fullname): + self.importer = importer + self.fullname = fullname + + import os + self.prefix = "{}/".format(os.path.dirname(self.importer.get_filename(self.fullname))) + + def open_resource(self, resource): + path = f'{self.prefix}{resource}' + from io import BytesIO + try: + return BytesIO(self.importer.get_data(path)) + except OSError: + raise FileNotFoundError(path) + + def resource_path(self, resource): + # All resources are in the binary file, so there is no path to the file. + # Raising FileNotFoundError tells the higher level API to extract the + # binary data and create a temporary file. + raise FileNotFoundError + + def is_resource(self, name): + path = f'{self.prefix}{name}' + try: + self.importer.get_data(path) + except OSError: + return False + return True + + def contents(self): + subdirs_seen = set() + for key in resfs_files(self.prefix): + relative = key[len(self.prefix):] + res_or_subdir, *other = relative.split(b'/') + if not other: + yield _s(res_or_subdir) + elif res_or_subdir not in subdirs_seen: + subdirs_seen.add(res_or_subdir) + yield _s(res_or_subdir) + + class BuiltinSubmoduleImporter(BuiltinImporter): @classmethod def find_spec(cls, fullname, path=None, target=None): |