Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 1 | """Abstract base classes related to import.""" |
| 2 | from . import _bootstrap |
| 3 | from . import machinery |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 4 | try: |
| 5 | import _frozen_importlib |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 6 | except ImportError as exc: |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 7 | if exc.name != '_frozen_importlib': |
| 8 | raise |
| 9 | _frozen_importlib = None |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 10 | import abc |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 11 | |
| 12 | |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 13 | def _register(abstract_cls, *classes): |
| 14 | for cls in classes: |
| 15 | abstract_cls.register(cls) |
| 16 | if _frozen_importlib is not None: |
| 17 | frozen_cls = getattr(_frozen_importlib, cls.__name__) |
| 18 | abstract_cls.register(frozen_cls) |
| 19 | |
| 20 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 21 | class Finder(metaclass=abc.ABCMeta): |
| 22 | |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 23 | """Legacy abstract base class for import finders. |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 24 | |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 25 | It may be subclassed for compatibility with legacy third party |
| 26 | reimplementations of the import system. Otherwise, finder |
| 27 | implementations should derive from the more specific MetaPathFinder |
| 28 | or PathEntryFinder ABCs. |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 29 | """ |
| 30 | |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 31 | @abc.abstractmethod |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 32 | def find_module(self, fullname, path=None): |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 33 | """An abstract method that should find a module. |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 34 | 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] | 35 | Returns a Loader object or None. |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 36 | """ |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 37 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 38 | |
| 39 | class MetaPathFinder(Finder): |
| 40 | |
| 41 | """Abstract base class for import finders on sys.meta_path.""" |
| 42 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 43 | # We don't define find_spec() here since that would break |
| 44 | # hasattr checks we do to support backward compatibility. |
| 45 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 46 | def find_module(self, fullname, path): |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 47 | """Return a loader for the module. |
| 48 | |
| 49 | If no module is found, return None. The fullname is a str and |
| 50 | the path is a list of strings or None. |
| 51 | |
Brett Cannon | 8d94229 | 2014-01-07 15:52:42 -0500 | [diff] [blame] | 52 | This method is deprecated in favor of finder.find_spec(). If find_spec() |
| 53 | exists then backwards-compatible functionality is provided for this |
| 54 | method. |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 55 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 56 | """ |
Brett Cannon | 8d94229 | 2014-01-07 15:52:42 -0500 | [diff] [blame] | 57 | if not hasattr(self, 'find_spec'): |
| 58 | return None |
| 59 | found = self.find_spec(fullname, path) |
| 60 | return found.loader if found is not None else None |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 61 | |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 62 | def invalidate_caches(self): |
| 63 | """An optional method for clearing the finder's cache, if any. |
| 64 | This method is used by importlib.invalidate_caches(). |
| 65 | """ |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 66 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 67 | _register(MetaPathFinder, machinery.BuiltinImporter, machinery.FrozenImporter, |
Nick Coghlan | ff79486 | 2012-08-02 21:45:24 +1000 | [diff] [blame] | 68 | machinery.PathFinder, machinery.WindowsRegistryFinder) |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 69 | |
| 70 | |
| 71 | class PathEntryFinder(Finder): |
| 72 | |
| 73 | """Abstract base class for path entry finders used by PathFinder.""" |
| 74 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 75 | # We don't define find_spec() here since that would break |
| 76 | # hasattr checks we do to support backward compatibility. |
| 77 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 78 | def find_loader(self, fullname): |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 79 | """Return (loader, namespace portion) for the path entry. |
| 80 | |
| 81 | The fullname is a str. The namespace portion is a sequence of |
| 82 | path entries contributing to part of a namespace package. The |
| 83 | sequence may be empty. If loader is not None, the portion will |
| 84 | be ignored. |
| 85 | |
| 86 | The portion will be discarded if another path entry finder |
| 87 | locates the module as a normal module or package. |
| 88 | |
Brett Cannon | 8d94229 | 2014-01-07 15:52:42 -0500 | [diff] [blame] | 89 | This method is deprecated in favor of finder.find_spec(). If find_spec() |
| 90 | is provided than backwards-compatible functionality is provided. |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 91 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 92 | """ |
Brett Cannon | 8d94229 | 2014-01-07 15:52:42 -0500 | [diff] [blame] | 93 | if not hasattr(self, 'find_spec'): |
| 94 | return None, [] |
| 95 | found = self.find_spec(fullname) |
| 96 | if found is not None: |
| 97 | if not found.submodule_search_locations: |
| 98 | portions = [] |
| 99 | else: |
| 100 | portions = found.submodule_search_locations |
| 101 | return found.loader, portions |
| 102 | else: |
| 103 | return None, [] |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 104 | |
Brett Cannon | f410ce8 | 2012-08-10 17:41:23 -0400 | [diff] [blame] | 105 | find_module = _bootstrap._find_module_shim |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 106 | |
| 107 | def invalidate_caches(self): |
| 108 | """An optional method for clearing the finder's cache, if any. |
| 109 | This method is used by PathFinder.invalidate_caches(). |
| 110 | """ |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 111 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 112 | _register(PathEntryFinder, machinery.FileFinder) |
| 113 | |
| 114 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 115 | class Loader(metaclass=abc.ABCMeta): |
| 116 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 117 | """Abstract base class for import loaders.""" |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 118 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 119 | def create_module(self, spec): |
| 120 | """Return a module to initialize and into which to load. |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 121 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 122 | This method should raise ImportError if anything prevents it |
| 123 | from creating a new module. It may return None to indicate |
| 124 | that the spec should create the new module. |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 125 | """ |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 126 | # By default, defer to default semantics for the new module. |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 127 | return None |
| 128 | |
| 129 | # We don't define exec_module() here since that would break |
| 130 | # hasattr checks we do to support backward compatibility. |
| 131 | |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 132 | def load_module(self, fullname): |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 133 | """Return the loaded module. |
| 134 | |
| 135 | The module must be added to sys.modules and have import-related |
| 136 | attributes set properly. The fullname is a str. |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 137 | |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 138 | ImportError is raised on failure. |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 139 | |
Brett Cannon | 8d94229 | 2014-01-07 15:52:42 -0500 | [diff] [blame] | 140 | This method is deprecated in favor of loader.exec_module(). If |
| 141 | exec_module() exists then it is used to provide a backwards-compatible |
| 142 | functionality for this method. |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 143 | |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 144 | """ |
Brett Cannon | 8d94229 | 2014-01-07 15:52:42 -0500 | [diff] [blame] | 145 | if not hasattr(self, 'exec_module'): |
| 146 | raise ImportError |
| 147 | return _bootstrap._load_module_shim(self, fullname) |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 148 | |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 149 | def module_repr(self, module): |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 150 | """Return a module's repr. |
| 151 | |
Brett Cannon | f1d7b11 | 2013-05-31 18:39:07 -0400 | [diff] [blame] | 152 | Used by the module type when the method does not raise |
| 153 | NotImplementedError. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 154 | |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 155 | This method is deprecated. |
| 156 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 157 | """ |
| 158 | # The exception will cause ModuleType.__repr__ to ignore this method. |
| 159 | raise NotImplementedError |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 160 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 161 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 162 | class ResourceLoader(Loader): |
| 163 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 164 | """Abstract base class for loaders which can return data from their |
| 165 | back-end storage. |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 166 | |
| 167 | This ABC represents one of the optional protocols specified by PEP 302. |
| 168 | |
| 169 | """ |
| 170 | |
| 171 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 172 | def get_data(self, path): |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 173 | """Abstract method which when implemented should return the bytes for |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 174 | the specified path. The path must be a str.""" |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 175 | raise IOError |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 176 | |
| 177 | |
| 178 | class InspectLoader(Loader): |
| 179 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 180 | """Abstract base class for loaders which support inspection about the |
| 181 | modules they can load. |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 182 | |
| 183 | This ABC represents one of the optional protocols specified by PEP 302. |
| 184 | |
| 185 | """ |
| 186 | |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 187 | def is_package(self, fullname): |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 188 | """Optional method which when implemented should return whether the |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 189 | module is a package. The fullname is a str. Returns a bool. |
| 190 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 191 | Raises ImportError if the module cannot be found. |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 192 | """ |
| 193 | raise ImportError |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 194 | |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 195 | def get_code(self, fullname): |
Brett Cannon | 3b62ca8 | 2013-05-27 21:11:04 -0400 | [diff] [blame] | 196 | """Method which returns the code object for the module. |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 197 | |
Brett Cannon | 3b62ca8 | 2013-05-27 21:11:04 -0400 | [diff] [blame] | 198 | The fullname is a str. Returns a types.CodeType if possible, else |
| 199 | returns None if a code object does not make sense |
| 200 | (e.g. built-in module). Raises ImportError if the module cannot be |
| 201 | found. |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 202 | """ |
Brett Cannon | 3b62ca8 | 2013-05-27 21:11:04 -0400 | [diff] [blame] | 203 | source = self.get_source(fullname) |
| 204 | if source is None: |
| 205 | return None |
| 206 | return self.source_to_code(source) |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 207 | |
| 208 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 209 | def get_source(self, fullname): |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 210 | """Abstract method which should return the source code for the |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 211 | module. The fullname is a str. Returns a str. |
| 212 | |
| 213 | Raises ImportError if the module cannot be found. |
| 214 | """ |
| 215 | raise ImportError |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 216 | |
Brett Cannon | 6eaac13 | 2014-05-09 12:28:22 -0400 | [diff] [blame] | 217 | @staticmethod |
| 218 | def source_to_code(data, path='<string>'): |
Brett Cannon | 9ffe85e | 2013-05-26 16:45:10 -0400 | [diff] [blame] | 219 | """Compile 'data' into a code object. |
| 220 | |
| 221 | The 'data' argument can be anything that compile() can handle. The'path' |
| 222 | argument should be where the data was retrieved (when applicable).""" |
| 223 | return compile(data, path, 'exec', dont_inherit=True) |
| 224 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 225 | exec_module = _bootstrap._LoaderBasics.exec_module |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 226 | load_module = _bootstrap._LoaderBasics.load_module |
| 227 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 228 | _register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter) |
Brett Cannon | a113ac5 | 2009-03-15 01:41:33 +0000 | [diff] [blame] | 229 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 230 | |
Brett Cannon | 6919427 | 2009-07-20 04:23:48 +0000 | [diff] [blame] | 231 | class ExecutionLoader(InspectLoader): |
| 232 | |
| 233 | """Abstract base class for loaders that wish to support the execution of |
| 234 | modules as scripts. |
| 235 | |
| 236 | This ABC represents one of the optional protocols specified in PEP 302. |
| 237 | |
| 238 | """ |
| 239 | |
| 240 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 241 | def get_filename(self, fullname): |
Brett Cannon | 6919427 | 2009-07-20 04:23:48 +0000 | [diff] [blame] | 242 | """Abstract method which should return the value that __file__ is to be |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 243 | set to. |
| 244 | |
| 245 | Raises ImportError if the module cannot be found. |
| 246 | """ |
| 247 | raise ImportError |
Brett Cannon | 6919427 | 2009-07-20 04:23:48 +0000 | [diff] [blame] | 248 | |
Brett Cannon | 3b62ca8 | 2013-05-27 21:11:04 -0400 | [diff] [blame] | 249 | def get_code(self, fullname): |
| 250 | """Method to return the code object for fullname. |
| 251 | |
| 252 | Should return None if not applicable (e.g. built-in module). |
| 253 | Raise ImportError if the module cannot be found. |
| 254 | """ |
| 255 | source = self.get_source(fullname) |
| 256 | if source is None: |
| 257 | return None |
| 258 | try: |
| 259 | path = self.get_filename(fullname) |
| 260 | except ImportError: |
| 261 | return self.source_to_code(source) |
| 262 | else: |
| 263 | return self.source_to_code(source, path) |
| 264 | |
Eric Snow | 7e70fa5 | 2013-10-04 20:28:52 -0600 | [diff] [blame] | 265 | _register(ExecutionLoader, machinery.ExtensionFileLoader) |
Eric Snow | 5179445 | 2013-10-03 12:08:55 -0600 | [diff] [blame] | 266 | |
Brett Cannon | 6919427 | 2009-07-20 04:23:48 +0000 | [diff] [blame] | 267 | |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 268 | class FileLoader(_bootstrap.FileLoader, ResourceLoader, ExecutionLoader): |
| 269 | |
| 270 | """Abstract base class partially implementing the ResourceLoader and |
| 271 | ExecutionLoader ABCs.""" |
| 272 | |
| 273 | _register(FileLoader, machinery.SourceFileLoader, |
Marc-Andre Lemburg | 4fe29c9 | 2012-04-25 02:31:37 +0200 | [diff] [blame] | 274 | machinery.SourcelessFileLoader) |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 275 | |
| 276 | |
Brett Cannon | 0cf9e6a | 2010-06-28 04:57:24 +0000 | [diff] [blame] | 277 | class SourceLoader(_bootstrap.SourceLoader, ResourceLoader, ExecutionLoader): |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 278 | |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 279 | """Abstract base class for loading source code (and optionally any |
| 280 | corresponding bytecode). |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 281 | |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 282 | To support loading from source code, the abstractmethods inherited from |
| 283 | ResourceLoader and ExecutionLoader need to be implemented. To also support |
| 284 | loading from bytecode, the optional methods specified directly by this ABC |
| 285 | is required. |
| 286 | |
| 287 | Inherited abstractmethods not implemented in this ABC: |
| 288 | |
| 289 | * ResourceLoader.get_data |
| 290 | * ExecutionLoader.get_filename |
| 291 | |
| 292 | """ |
| 293 | |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 294 | def path_mtime(self, path): |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 295 | """Return the (int) modification time for the path (str).""" |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 296 | if self.path_stats.__func__ is SourceLoader.path_stats: |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 297 | raise IOError |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 298 | return int(self.path_stats(path)['mtime']) |
| 299 | |
| 300 | def path_stats(self, path): |
| 301 | """Return a metadata dict for the source pointed to by the path (str). |
| 302 | Possible keys: |
| 303 | - 'mtime' (mandatory) is the numeric timestamp of last source |
| 304 | code modification; |
| 305 | - 'size' (optional) is the size in bytes of the source code. |
| 306 | """ |
| 307 | if self.path_mtime.__func__ is SourceLoader.path_mtime: |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 308 | raise IOError |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 309 | return {'mtime': self.path_mtime(path)} |
Brett Cannon | 8d18907 | 2010-08-22 20:38:47 +0000 | [diff] [blame] | 310 | |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 311 | def set_data(self, path, data): |
Brett Cannon | 8d18907 | 2010-08-22 20:38:47 +0000 | [diff] [blame] | 312 | """Write the bytes to the path (if possible). |
| 313 | |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 314 | Accepts a str path and data as bytes. |
| 315 | |
Brett Cannon | 8d18907 | 2010-08-22 20:38:47 +0000 | [diff] [blame] | 316 | Any needed intermediary directories are to be created. If for some |
| 317 | reason the file cannot be written because of permissions, fail |
| 318 | silently. |
Brett Cannon | 8d18907 | 2010-08-22 20:38:47 +0000 | [diff] [blame] | 319 | """ |
Brett Cannon | 8d18907 | 2010-08-22 20:38:47 +0000 | [diff] [blame] | 320 | |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 321 | _register(SourceLoader, machinery.SourceFileLoader) |