mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 02:15:10 +00:00 
			
		
		
		
	 843c277656
			
		
	
	
		843c277656
		
			
		
	
	
	
	
		
			
			* Provide native .files support on SourceFileLoader. * Add native importlib.resources.files() support to zipimporter. Remove fallback support. * make regen-all * 📜🤖 Added by blurb_it. * Move 'files' into the ResourceReader so it can carry the relevant module name context. * Create 'importlib.readers' module and add FileReader to it. * Add zip reader and rely on it for a TraversableResources object on zipimporter. * Remove TraversableAdapter, no longer needed. * Update blurb. * Replace backslashes with forward slashes. * Incorporate changes from importlib_metadata 2.0, finalizing the interface for extension via get_resource_reader. Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
		
			
				
	
	
		
			30 lines
		
	
	
	
		
			874 B
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			30 lines
		
	
	
	
		
			874 B
		
	
	
	
		
			Python
		
	
	
	
	
	
| import zipfile
 | |
| import pathlib
 | |
| from . import abc
 | |
| 
 | |
| 
 | |
| class FileReader(abc.TraversableResources):
 | |
|     def __init__(self, loader):
 | |
|         self.path = pathlib.Path(loader.path).parent
 | |
| 
 | |
|     def files(self):
 | |
|         return self.path
 | |
| 
 | |
| 
 | |
| class ZipReader(FileReader):
 | |
|     def __init__(self, loader, module):
 | |
|         _, _, name = module.rpartition('.')
 | |
|         prefix = loader.prefix.replace('\\', '/') + name + '/'
 | |
|         self.path = zipfile.Path(loader.archive, prefix)
 | |
| 
 | |
|     def open_resource(self, resource):
 | |
|         try:
 | |
|             return super().open_resource(resource)
 | |
|         except KeyError as exc:
 | |
|             raise FileNotFoundError(exc.args[0])
 | |
| 
 | |
|     def is_resource(self, path):
 | |
|         # workaround for `zipfile.Path.is_file` returning true
 | |
|         # for non-existent paths.
 | |
|         target = self.files().joinpath(path)
 | |
|         return target.is_file() and target.exists()
 |