| 
									
										
										
										
											2020-06-07 21:00:51 -04:00
										 |  |  | import zipfile | 
					
						
							|  |  |  | import pathlib | 
					
						
							|  |  |  | from . import abc | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class FileReader(abc.TraversableResources): | 
					
						
							|  |  |  |     def __init__(self, loader): | 
					
						
							|  |  |  |         self.path = pathlib.Path(loader.path).parent | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-29 16:59:22 -04:00
										 |  |  |     def resource_path(self, resource): | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         Return the file system path to prevent | 
					
						
							|  |  |  |         `resources.path()` from creating a temporary | 
					
						
							|  |  |  |         copy. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         return str(self.path.joinpath(resource)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-07 21:00:51 -04:00
										 |  |  |     def files(self): | 
					
						
							|  |  |  |         return self.path | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-29 16:59:22 -04:00
										 |  |  | class ZipReader(abc.TraversableResources): | 
					
						
							| 
									
										
										
										
											2020-06-07 21:00:51 -04:00
										 |  |  |     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() | 
					
						
							| 
									
										
										
										
											2020-06-29 16:59:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def files(self): | 
					
						
							|  |  |  |         return self.path |