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 |
| 4 | import abc |
| 5 | import types |
| 6 | |
| 7 | |
| 8 | class Loader(metaclass=abc.ABCMeta): |
| 9 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 10 | """Abstract base class for import loaders.""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 11 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 12 | @abc.abstractmethod |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 13 | def load_module(self, fullname:str) -> types.ModuleType: |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 14 | """Abstract method which when implemented should load a module.""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 15 | raise NotImplementedError |
| 16 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 17 | |
| 18 | class Finder(metaclass=abc.ABCMeta): |
| 19 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 20 | """Abstract base class for import finders.""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 21 | |
| 22 | @abc.abstractmethod |
| 23 | def find_module(self, fullname:str, path:[str]=None) -> Loader: |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 24 | """Abstract method which when implemented should find a module.""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 25 | raise NotImplementedError |
| 26 | |
| 27 | Finder.register(machinery.BuiltinImporter) |
| 28 | Finder.register(machinery.FrozenImporter) |
| 29 | Finder.register(machinery.PathFinder) |
| 30 | |
| 31 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 32 | class ResourceLoader(Loader): |
| 33 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 34 | """Abstract base class for loaders which can return data from their |
| 35 | back-end storage. |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 36 | |
| 37 | This ABC represents one of the optional protocols specified by PEP 302. |
| 38 | |
| 39 | """ |
| 40 | |
| 41 | @abc.abstractmethod |
| 42 | def get_data(self, path:str) -> bytes: |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 43 | """Abstract method which when implemented should return the bytes for |
| 44 | the specified path.""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 45 | raise NotImplementedError |
| 46 | |
| 47 | |
| 48 | class InspectLoader(Loader): |
| 49 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 50 | """Abstract base class for loaders which support inspection about the |
| 51 | modules they can load. |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 52 | |
| 53 | This ABC represents one of the optional protocols specified by PEP 302. |
| 54 | |
| 55 | """ |
| 56 | |
| 57 | @abc.abstractmethod |
| 58 | def is_package(self, fullname:str) -> bool: |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 59 | """Abstract method which when implemented should return whether the |
| 60 | module is a package.""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 61 | return NotImplementedError |
| 62 | |
| 63 | @abc.abstractmethod |
| 64 | def get_code(self, fullname:str) -> types.CodeType: |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 65 | """Abstract method which when implemented should return the code object |
| 66 | for the module""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 67 | return NotImplementedError |
| 68 | |
| 69 | @abc.abstractmethod |
| 70 | def get_source(self, fullname:str) -> str: |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 71 | """Abstract method which should return the source code for the |
| 72 | module.""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 73 | return NotImplementedError |
| 74 | |
Brett Cannon | a113ac5 | 2009-03-15 01:41:33 +0000 | [diff] [blame] | 75 | InspectLoader.register(machinery.BuiltinImporter) |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 76 | InspectLoader.register(machinery.FrozenImporter) |
Brett Cannon | a113ac5 | 2009-03-15 01:41:33 +0000 | [diff] [blame] | 77 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 78 | |
| 79 | class PyLoader(_bootstrap.PyLoader, InspectLoader): |
| 80 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 81 | """Abstract base class to assist in loading source code by requiring only |
| 82 | back-end storage methods to be implemented. |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 83 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 84 | The methods get_code, get_source, and load_module are implemented for the |
| 85 | user. |
| 86 | |
| 87 | """ |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 88 | |
| 89 | @abc.abstractmethod |
| 90 | def source_path(self, fullname:str) -> object: |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 91 | """Abstract method which when implemented should return the path to the |
| 92 | sourced code for the module.""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 93 | raise NotImplementedError |
| 94 | |
| 95 | |
| 96 | class PyPycLoader(_bootstrap.PyPycLoader, PyLoader): |
| 97 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 98 | """Abstract base class to assist in loading source and bytecode by |
| 99 | requiring only back-end storage methods to be implemented. |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 100 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 101 | The methods get_code, get_source, and load_module are implemented for the |
| 102 | user. |
| 103 | |
| 104 | """ |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 105 | |
| 106 | @abc.abstractmethod |
| 107 | def source_mtime(self, fullname:str) -> int: |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 108 | """Abstract method which when implemented should return the |
| 109 | modification time for the source of the module.""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 110 | raise NotImplementedError |
| 111 | |
| 112 | @abc.abstractmethod |
| 113 | def bytecode_path(self, fullname:str) -> object: |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 114 | """Abstract method which when implemented should return the path to the |
| 115 | bytecode for the module.""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 116 | raise NotImplementedError |
| 117 | |
| 118 | @abc.abstractmethod |
| 119 | def write_bytecode(self, fullname:str, bytecode:bytes): |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 120 | """Abstract method which when implemented should attempt to write the |
| 121 | bytecode for the module.""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 122 | raise NotImplementedError |