diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2018-09-19 06:28:06 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-19 06:28:06 (GMT) |
commit | 9da3961f364da2a3ced740230b85ffb4309238d3 (patch) | |
tree | 3bdba1d723f7e24e6c27139b398a4c2866974ff4 /Lib/importlib/resources.py | |
parent | 2a9c3805ddedf282881ef7811a561c70b74f80b1 (diff) | |
download | cpython-9da3961f364da2a3ced740230b85ffb4309238d3.zip cpython-9da3961f364da2a3ced740230b85ffb4309238d3.tar.gz cpython-9da3961f364da2a3ced740230b85ffb4309238d3.tar.bz2 |
bpo-25711: Move _ZipImportResourceReader from importlib to zipimport. (GH-9406)
Diffstat (limited to 'Lib/importlib/resources.py')
-rw-r--r-- | Lib/importlib/resources.py | 84 |
1 files changed, 0 insertions, 84 deletions
diff --git a/Lib/importlib/resources.py b/Lib/importlib/resources.py index cbefdd5..fc3a1c9 100644 --- a/Lib/importlib/resources.py +++ b/Lib/importlib/resources.py @@ -257,87 +257,3 @@ def contents(package: Package) -> Iterable[str]: else: package_directory = Path(package.__spec__.origin).parent return os.listdir(package_directory) - - -# Private implementation of ResourceReader and get_resource_reader() called -# from zipimport.c. Don't use these directly! We're implementing these in -# Python because 1) it's easier, 2) zipimport may get rewritten in Python -# itself at some point, so doing this all in C would difficult and a waste of -# effort. - -class _ZipImportResourceReader(resources_abc.ResourceReader): - """Private class used to support ZipImport.get_resource_reader(). - - This class is allowed to reference all the innards and private parts of - the zipimporter. - """ - - def __init__(self, zipimporter, fullname): - self.zipimporter = zipimporter - self.fullname = fullname - - def open_resource(self, resource): - fullname_as_path = self.fullname.replace('.', '/') - path = f'{fullname_as_path}/{resource}' - try: - return BytesIO(self.zipimporter.get_data(path)) - except OSError: - raise FileNotFoundError(path) - - def resource_path(self, resource): - # All resources are in the zip 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): - # Maybe we could do better, but if we can get the data, it's a - # resource. Otherwise it isn't. - fullname_as_path = self.fullname.replace('.', '/') - path = f'{fullname_as_path}/{name}' - try: - self.zipimporter.get_data(path) - except OSError: - return False - return True - - def contents(self): - # This is a bit convoluted, because fullname will be a module path, - # but _files is a list of file names relative to the top of the - # archive's namespace. We want to compare file paths to find all the - # names of things inside the module represented by fullname. So we - # turn the module path of fullname into a file path relative to the - # top of the archive, and then we iterate through _files looking for - # names inside that "directory". - fullname_path = Path(self.zipimporter.get_filename(self.fullname)) - relative_path = fullname_path.relative_to(self.zipimporter.archive) - # Don't forget that fullname names a package, so its path will include - # __init__.py, which we want to ignore. - assert relative_path.name == '__init__.py' - package_path = relative_path.parent - subdirs_seen = set() - for filename in self.zipimporter._files: - try: - relative = Path(filename).relative_to(package_path) - except ValueError: - continue - # If the path of the file (which is relative to the top of the zip - # namespace), relative to the package given when the resource - # reader was created, has a parent, then it's a name in a - # subdirectory and thus we skip it. - parent_name = relative.parent.name - if len(parent_name) == 0: - yield relative.name - elif parent_name not in subdirs_seen: - subdirs_seen.add(parent_name) - yield parent_name - - -# Called from zipimport.c -def _zipimport_get_resource_reader(zipimporter, fullname): - try: - if not zipimporter.is_package(fullname): - return None - except ZipImportError: - return None - return _ZipImportResourceReader(zipimporter, fullname) |