Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 1 | """Abstract base classes related to import.""" |
Eric Snow | 183a941 | 2015-05-15 21:54:59 -0600 | [diff] [blame] | 2 | from . import _bootstrap |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 3 | from . import _bootstrap_external |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 4 | from . import machinery |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 5 | try: |
| 6 | import _frozen_importlib |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 7 | except ImportError as exc: |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 8 | if exc.name != '_frozen_importlib': |
| 9 | raise |
| 10 | _frozen_importlib = None |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 11 | try: |
| 12 | import _frozen_importlib_external |
Pablo Galindo | 293dd23 | 2019-11-19 21:34:03 +0000 | [diff] [blame] | 13 | except ImportError: |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 14 | _frozen_importlib_external = _bootstrap_external |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 15 | import abc |
Matthias Bussonnier | 1d4601c | 2017-02-15 18:00:32 -0800 | [diff] [blame] | 16 | import warnings |
Jason R. Coombs | 7f7e706 | 2020-05-08 19:20:26 -0400 | [diff] [blame^] | 17 | from typing import Protocol, runtime_checkable |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 18 | |
| 19 | |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 20 | def _register(abstract_cls, *classes): |
| 21 | for cls in classes: |
| 22 | abstract_cls.register(cls) |
| 23 | if _frozen_importlib is not None: |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 24 | try: |
| 25 | frozen_cls = getattr(_frozen_importlib, cls.__name__) |
| 26 | except AttributeError: |
| 27 | frozen_cls = getattr(_frozen_importlib_external, cls.__name__) |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 28 | abstract_cls.register(frozen_cls) |
| 29 | |
| 30 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 31 | class Finder(metaclass=abc.ABCMeta): |
| 32 | |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 33 | """Legacy abstract base class for import finders. |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 34 | |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 35 | It may be subclassed for compatibility with legacy third party |
| 36 | reimplementations of the import system. Otherwise, finder |
| 37 | implementations should derive from the more specific MetaPathFinder |
| 38 | or PathEntryFinder ABCs. |
Matthias Bussonnier | 1d4601c | 2017-02-15 18:00:32 -0800 | [diff] [blame] | 39 | |
| 40 | Deprecated since Python 3.3 |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 41 | """ |
| 42 | |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 43 | @abc.abstractmethod |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 44 | def find_module(self, fullname, path=None): |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 45 | """An abstract method that should find a module. |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 46 | The fullname is a str and the optional path is a str or None. |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 47 | Returns a Loader object or None. |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 48 | """ |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 49 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 50 | |
| 51 | class MetaPathFinder(Finder): |
| 52 | |
| 53 | """Abstract base class for import finders on sys.meta_path.""" |
| 54 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 55 | # We don't define find_spec() here since that would break |
| 56 | # hasattr checks we do to support backward compatibility. |
| 57 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 58 | def find_module(self, fullname, path): |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 59 | """Return a loader for the module. |
| 60 | |
| 61 | If no module is found, return None. The fullname is a str and |
| 62 | the path is a list of strings or None. |
| 63 | |
Matthias Bussonnier | 1d4601c | 2017-02-15 18:00:32 -0800 | [diff] [blame] | 64 | This method is deprecated since Python 3.4 in favor of |
| 65 | finder.find_spec(). If find_spec() exists then backwards-compatible |
| 66 | functionality is provided for this method. |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 67 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 68 | """ |
Matthias Bussonnier | 1d4601c | 2017-02-15 18:00:32 -0800 | [diff] [blame] | 69 | warnings.warn("MetaPathFinder.find_module() is deprecated since Python " |
Serhiy Storchaka | 34fd4c2 | 2018-11-05 16:20:25 +0200 | [diff] [blame] | 70 | "3.4 in favor of MetaPathFinder.find_spec() " |
Matthias Bussonnier | 1d4601c | 2017-02-15 18:00:32 -0800 | [diff] [blame] | 71 | "(available since 3.4)", |
| 72 | DeprecationWarning, |
| 73 | stacklevel=2) |
Brett Cannon | 8d94229 | 2014-01-07 15:52:42 -0500 | [diff] [blame] | 74 | if not hasattr(self, 'find_spec'): |
| 75 | return None |
| 76 | found = self.find_spec(fullname, path) |
| 77 | return found.loader if found is not None else None |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 78 | |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 79 | def invalidate_caches(self): |
| 80 | """An optional method for clearing the finder's cache, if any. |
| 81 | This method is used by importlib.invalidate_caches(). |
| 82 | """ |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 83 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 84 | _register(MetaPathFinder, machinery.BuiltinImporter, machinery.FrozenImporter, |
Nick Coghlan | ff79486 | 2012-08-02 21:45:24 +1000 | [diff] [blame] | 85 | machinery.PathFinder, machinery.WindowsRegistryFinder) |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 86 | |
| 87 | |
| 88 | class PathEntryFinder(Finder): |
| 89 | |
| 90 | """Abstract base class for path entry finders used by PathFinder.""" |
| 91 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 92 | # We don't define find_spec() here since that would break |
| 93 | # hasattr checks we do to support backward compatibility. |
| 94 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 95 | def find_loader(self, fullname): |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 96 | """Return (loader, namespace portion) for the path entry. |
| 97 | |
| 98 | The fullname is a str. The namespace portion is a sequence of |
| 99 | path entries contributing to part of a namespace package. The |
| 100 | sequence may be empty. If loader is not None, the portion will |
| 101 | be ignored. |
| 102 | |
| 103 | The portion will be discarded if another path entry finder |
| 104 | locates the module as a normal module or package. |
| 105 | |
Matthias Bussonnier | 1d4601c | 2017-02-15 18:00:32 -0800 | [diff] [blame] | 106 | This method is deprecated since Python 3.4 in favor of |
| 107 | finder.find_spec(). If find_spec() is provided than backwards-compatible |
| 108 | functionality is provided. |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 109 | """ |
Matthias Bussonnier | 1d4601c | 2017-02-15 18:00:32 -0800 | [diff] [blame] | 110 | warnings.warn("PathEntryFinder.find_loader() is deprecated since Python " |
| 111 | "3.4 in favor of PathEntryFinder.find_spec() " |
| 112 | "(available since 3.4)", |
| 113 | DeprecationWarning, |
| 114 | stacklevel=2) |
Brett Cannon | 8d94229 | 2014-01-07 15:52:42 -0500 | [diff] [blame] | 115 | if not hasattr(self, 'find_spec'): |
| 116 | return None, [] |
| 117 | found = self.find_spec(fullname) |
| 118 | if found is not None: |
| 119 | if not found.submodule_search_locations: |
| 120 | portions = [] |
| 121 | else: |
| 122 | portions = found.submodule_search_locations |
| 123 | return found.loader, portions |
| 124 | else: |
| 125 | return None, [] |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 126 | |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 127 | find_module = _bootstrap_external._find_module_shim |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 128 | |
| 129 | def invalidate_caches(self): |
| 130 | """An optional method for clearing the finder's cache, if any. |
| 131 | This method is used by PathFinder.invalidate_caches(). |
| 132 | """ |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 133 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 134 | _register(PathEntryFinder, machinery.FileFinder) |
| 135 | |
| 136 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 137 | class Loader(metaclass=abc.ABCMeta): |
| 138 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 139 | """Abstract base class for import loaders.""" |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 140 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 141 | def create_module(self, spec): |
| 142 | """Return a module to initialize and into which to load. |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 143 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 144 | This method should raise ImportError if anything prevents it |
| 145 | from creating a new module. It may return None to indicate |
| 146 | that the spec should create the new module. |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 147 | """ |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 148 | # By default, defer to default semantics for the new module. |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 149 | return None |
| 150 | |
| 151 | # We don't define exec_module() here since that would break |
| 152 | # hasattr checks we do to support backward compatibility. |
| 153 | |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 154 | def load_module(self, fullname): |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 155 | """Return the loaded module. |
| 156 | |
| 157 | The module must be added to sys.modules and have import-related |
| 158 | attributes set properly. The fullname is a str. |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 159 | |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 160 | ImportError is raised on failure. |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 161 | |
Brett Cannon | 8d94229 | 2014-01-07 15:52:42 -0500 | [diff] [blame] | 162 | This method is deprecated in favor of loader.exec_module(). If |
| 163 | exec_module() exists then it is used to provide a backwards-compatible |
| 164 | functionality for this method. |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 165 | |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 166 | """ |
Brett Cannon | 8d94229 | 2014-01-07 15:52:42 -0500 | [diff] [blame] | 167 | if not hasattr(self, 'exec_module'): |
| 168 | raise ImportError |
Eric Snow | 183a941 | 2015-05-15 21:54:59 -0600 | [diff] [blame] | 169 | return _bootstrap._load_module_shim(self, fullname) |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 170 | |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 171 | def module_repr(self, module): |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 172 | """Return a module's repr. |
| 173 | |
Brett Cannon | f1d7b11 | 2013-05-31 18:39:07 -0400 | [diff] [blame] | 174 | Used by the module type when the method does not raise |
| 175 | NotImplementedError. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 176 | |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 177 | This method is deprecated. |
| 178 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 179 | """ |
| 180 | # The exception will cause ModuleType.__repr__ to ignore this method. |
| 181 | raise NotImplementedError |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 182 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 183 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 184 | class ResourceLoader(Loader): |
| 185 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 186 | """Abstract base class for loaders which can return data from their |
| 187 | back-end storage. |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 188 | |
| 189 | This ABC represents one of the optional protocols specified by PEP 302. |
| 190 | |
| 191 | """ |
| 192 | |
| 193 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 194 | def get_data(self, path): |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 195 | """Abstract method which when implemented should return the bytes for |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 196 | the specified path. The path must be a str.""" |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 197 | raise OSError |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 198 | |
| 199 | |
| 200 | class InspectLoader(Loader): |
| 201 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 202 | """Abstract base class for loaders which support inspection about the |
| 203 | modules they can load. |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 204 | |
| 205 | This ABC represents one of the optional protocols specified by PEP 302. |
| 206 | |
| 207 | """ |
| 208 | |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 209 | def is_package(self, fullname): |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 210 | """Optional method which when implemented should return whether the |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 211 | module is a package. The fullname is a str. Returns a bool. |
| 212 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 213 | Raises ImportError if the module cannot be found. |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 214 | """ |
| 215 | raise ImportError |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 216 | |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 217 | def get_code(self, fullname): |
Brett Cannon | 3b62ca8 | 2013-05-27 21:11:04 -0400 | [diff] [blame] | 218 | """Method which returns the code object for the module. |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 219 | |
Brett Cannon | 3b62ca8 | 2013-05-27 21:11:04 -0400 | [diff] [blame] | 220 | The fullname is a str. Returns a types.CodeType if possible, else |
| 221 | returns None if a code object does not make sense |
| 222 | (e.g. built-in module). Raises ImportError if the module cannot be |
| 223 | found. |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 224 | """ |
Brett Cannon | 3b62ca8 | 2013-05-27 21:11:04 -0400 | [diff] [blame] | 225 | source = self.get_source(fullname) |
| 226 | if source is None: |
| 227 | return None |
| 228 | return self.source_to_code(source) |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 229 | |
| 230 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 231 | def get_source(self, fullname): |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 232 | """Abstract method which should return the source code for the |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 233 | module. The fullname is a str. Returns a str. |
| 234 | |
| 235 | Raises ImportError if the module cannot be found. |
| 236 | """ |
| 237 | raise ImportError |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 238 | |
Brett Cannon | 6eaac13 | 2014-05-09 12:28:22 -0400 | [diff] [blame] | 239 | @staticmethod |
| 240 | def source_to_code(data, path='<string>'): |
Brett Cannon | 9ffe85e | 2013-05-26 16:45:10 -0400 | [diff] [blame] | 241 | """Compile 'data' into a code object. |
| 242 | |
| 243 | The 'data' argument can be anything that compile() can handle. The'path' |
| 244 | argument should be where the data was retrieved (when applicable).""" |
| 245 | return compile(data, path, 'exec', dont_inherit=True) |
| 246 | |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 247 | exec_module = _bootstrap_external._LoaderBasics.exec_module |
| 248 | load_module = _bootstrap_external._LoaderBasics.load_module |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 249 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 250 | _register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter) |
Brett Cannon | a113ac5 | 2009-03-15 01:41:33 +0000 | [diff] [blame] | 251 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 252 | |
Brett Cannon | 6919427 | 2009-07-20 04:23:48 +0000 | [diff] [blame] | 253 | class ExecutionLoader(InspectLoader): |
| 254 | |
| 255 | """Abstract base class for loaders that wish to support the execution of |
| 256 | modules as scripts. |
| 257 | |
| 258 | This ABC represents one of the optional protocols specified in PEP 302. |
| 259 | |
| 260 | """ |
| 261 | |
| 262 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 263 | def get_filename(self, fullname): |
Brett Cannon | 6919427 | 2009-07-20 04:23:48 +0000 | [diff] [blame] | 264 | """Abstract method which should return the value that __file__ is to be |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 265 | set to. |
| 266 | |
| 267 | Raises ImportError if the module cannot be found. |
| 268 | """ |
| 269 | raise ImportError |
Brett Cannon | 6919427 | 2009-07-20 04:23:48 +0000 | [diff] [blame] | 270 | |
Brett Cannon | 3b62ca8 | 2013-05-27 21:11:04 -0400 | [diff] [blame] | 271 | def get_code(self, fullname): |
| 272 | """Method to return the code object for fullname. |
| 273 | |
| 274 | Should return None if not applicable (e.g. built-in module). |
| 275 | Raise ImportError if the module cannot be found. |
| 276 | """ |
| 277 | source = self.get_source(fullname) |
| 278 | if source is None: |
| 279 | return None |
| 280 | try: |
| 281 | path = self.get_filename(fullname) |
| 282 | except ImportError: |
| 283 | return self.source_to_code(source) |
| 284 | else: |
| 285 | return self.source_to_code(source, path) |
| 286 | |
Eric Snow | 7e70fa5 | 2013-10-04 20:28:52 -0600 | [diff] [blame] | 287 | _register(ExecutionLoader, machinery.ExtensionFileLoader) |
Eric Snow | 5179445 | 2013-10-03 12:08:55 -0600 | [diff] [blame] | 288 | |
Brett Cannon | 6919427 | 2009-07-20 04:23:48 +0000 | [diff] [blame] | 289 | |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 290 | class FileLoader(_bootstrap_external.FileLoader, ResourceLoader, ExecutionLoader): |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 291 | |
| 292 | """Abstract base class partially implementing the ResourceLoader and |
| 293 | ExecutionLoader ABCs.""" |
| 294 | |
| 295 | _register(FileLoader, machinery.SourceFileLoader, |
Marc-Andre Lemburg | 4fe29c9 | 2012-04-25 02:31:37 +0200 | [diff] [blame] | 296 | machinery.SourcelessFileLoader) |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 297 | |
| 298 | |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 299 | class SourceLoader(_bootstrap_external.SourceLoader, ResourceLoader, ExecutionLoader): |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 300 | |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 301 | """Abstract base class for loading source code (and optionally any |
| 302 | corresponding bytecode). |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 303 | |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 304 | To support loading from source code, the abstractmethods inherited from |
| 305 | ResourceLoader and ExecutionLoader need to be implemented. To also support |
| 306 | loading from bytecode, the optional methods specified directly by this ABC |
| 307 | is required. |
| 308 | |
| 309 | Inherited abstractmethods not implemented in this ABC: |
| 310 | |
| 311 | * ResourceLoader.get_data |
| 312 | * ExecutionLoader.get_filename |
| 313 | |
| 314 | """ |
| 315 | |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 316 | def path_mtime(self, path): |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 317 | """Return the (int) modification time for the path (str).""" |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 318 | if self.path_stats.__func__ is SourceLoader.path_stats: |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 319 | raise OSError |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 320 | return int(self.path_stats(path)['mtime']) |
| 321 | |
| 322 | def path_stats(self, path): |
| 323 | """Return a metadata dict for the source pointed to by the path (str). |
| 324 | Possible keys: |
| 325 | - 'mtime' (mandatory) is the numeric timestamp of last source |
| 326 | code modification; |
| 327 | - 'size' (optional) is the size in bytes of the source code. |
| 328 | """ |
| 329 | if self.path_mtime.__func__ is SourceLoader.path_mtime: |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 330 | raise OSError |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 331 | return {'mtime': self.path_mtime(path)} |
Brett Cannon | 8d18907 | 2010-08-22 20:38:47 +0000 | [diff] [blame] | 332 | |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 333 | def set_data(self, path, data): |
Brett Cannon | 8d18907 | 2010-08-22 20:38:47 +0000 | [diff] [blame] | 334 | """Write the bytes to the path (if possible). |
| 335 | |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 336 | Accepts a str path and data as bytes. |
| 337 | |
Brett Cannon | 8d18907 | 2010-08-22 20:38:47 +0000 | [diff] [blame] | 338 | Any needed intermediary directories are to be created. If for some |
| 339 | reason the file cannot be written because of permissions, fail |
| 340 | silently. |
Brett Cannon | 8d18907 | 2010-08-22 20:38:47 +0000 | [diff] [blame] | 341 | """ |
Brett Cannon | 8d18907 | 2010-08-22 20:38:47 +0000 | [diff] [blame] | 342 | |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 343 | _register(SourceLoader, machinery.SourceFileLoader) |
Brett Cannon | 4ac5150 | 2017-12-15 16:29:35 -0800 | [diff] [blame] | 344 | |
| 345 | |
Barry Warsaw | 5ec0fee | 2018-01-15 15:07:11 -0800 | [diff] [blame] | 346 | class ResourceReader(metaclass=abc.ABCMeta): |
Brett Cannon | 4ac5150 | 2017-12-15 16:29:35 -0800 | [diff] [blame] | 347 | |
Brett Cannon | bca4218 | 2018-01-12 15:08:59 -0800 | [diff] [blame] | 348 | """Abstract base class to provide resource-reading support. |
| 349 | |
| 350 | Loaders that support resource reading are expected to implement |
| 351 | the ``get_resource_reader(fullname)`` method and have it either return None |
| 352 | or an object compatible with this ABC. |
| 353 | """ |
Brett Cannon | 4ac5150 | 2017-12-15 16:29:35 -0800 | [diff] [blame] | 354 | |
| 355 | @abc.abstractmethod |
| 356 | def open_resource(self, resource): |
| 357 | """Return an opened, file-like object for binary reading. |
| 358 | |
| 359 | The 'resource' argument is expected to represent only a file name |
| 360 | and thus not contain any subdirectory components. |
| 361 | |
| 362 | If the resource cannot be found, FileNotFoundError is raised. |
| 363 | """ |
| 364 | raise FileNotFoundError |
| 365 | |
| 366 | @abc.abstractmethod |
| 367 | def resource_path(self, resource): |
| 368 | """Return the file system path to the specified resource. |
| 369 | |
| 370 | The 'resource' argument is expected to represent only a file name |
| 371 | and thus not contain any subdirectory components. |
| 372 | |
| 373 | If the resource does not exist on the file system, raise |
| 374 | FileNotFoundError. |
| 375 | """ |
| 376 | raise FileNotFoundError |
| 377 | |
| 378 | @abc.abstractmethod |
| 379 | def is_resource(self, name): |
| 380 | """Return True if the named 'name' is consider a resource.""" |
| 381 | raise FileNotFoundError |
| 382 | |
| 383 | @abc.abstractmethod |
| 384 | def contents(self): |
Brett Cannon | 3ab9365 | 2018-04-30 11:31:45 -0700 | [diff] [blame] | 385 | """Return an iterable of strings over the contents of the package.""" |
| 386 | return [] |
Barry Warsaw | 5ec0fee | 2018-01-15 15:07:11 -0800 | [diff] [blame] | 387 | |
| 388 | |
| 389 | _register(ResourceReader, machinery.SourceFileLoader) |
Jason R. Coombs | 7f7e706 | 2020-05-08 19:20:26 -0400 | [diff] [blame^] | 390 | |
| 391 | |
| 392 | @runtime_checkable |
| 393 | class Traversable(Protocol): |
| 394 | """ |
| 395 | An object with a subset of pathlib.Path methods suitable for |
| 396 | traversing directories and opening files. |
| 397 | """ |
| 398 | |
| 399 | @abc.abstractmethod |
| 400 | def iterdir(self): |
| 401 | """ |
| 402 | Yield Traversable objects in self |
| 403 | """ |
| 404 | |
| 405 | @abc.abstractmethod |
| 406 | def read_bytes(self): |
| 407 | """ |
| 408 | Read contents of self as bytes |
| 409 | """ |
| 410 | |
| 411 | @abc.abstractmethod |
| 412 | def read_text(self, encoding=None): |
| 413 | """ |
| 414 | Read contents of self as bytes |
| 415 | """ |
| 416 | |
| 417 | @abc.abstractmethod |
| 418 | def is_dir(self): |
| 419 | """ |
| 420 | Return True if self is a dir |
| 421 | """ |
| 422 | |
| 423 | @abc.abstractmethod |
| 424 | def is_file(self): |
| 425 | """ |
| 426 | Return True if self is a file |
| 427 | """ |
| 428 | |
| 429 | @abc.abstractmethod |
| 430 | def joinpath(self, child): |
| 431 | """ |
| 432 | Return Traversable child in self |
| 433 | """ |
| 434 | |
| 435 | @abc.abstractmethod |
| 436 | def __truediv__(self, child): |
| 437 | """ |
| 438 | Return Traversable child in self |
| 439 | """ |
| 440 | |
| 441 | @abc.abstractmethod |
| 442 | def open(self, mode='r', *args, **kwargs): |
| 443 | """ |
| 444 | mode may be 'r' or 'rb' to open as text or binary. Return a handle |
| 445 | suitable for reading (same as pathlib.Path.open). |
| 446 | |
| 447 | When opening as text, accepts encoding parameters such as those |
| 448 | accepted by io.TextIOWrapper. |
| 449 | """ |
| 450 | |
| 451 | @abc.abstractproperty |
| 452 | def name(self): |
| 453 | # type: () -> str |
| 454 | """ |
| 455 | The base name of this object without any parent references. |
| 456 | """ |
| 457 | |
| 458 | |
| 459 | class TraversableResources(ResourceReader): |
| 460 | @abc.abstractmethod |
| 461 | def files(self): |
| 462 | """Return a Traversable object for the loaded package.""" |
| 463 | |
| 464 | def open_resource(self, resource): |
| 465 | return self.files().joinpath(resource).open('rb') |
| 466 | |
| 467 | def resource_path(self, resource): |
| 468 | raise FileNotFoundError(resource) |
| 469 | |
| 470 | def is_resource(self, path): |
| 471 | return self.files().joinpath(path).isfile() |
| 472 | |
| 473 | def contents(self): |
| 474 | return (item.name for item in self.files().iterdir()) |