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 | |
| 17 | Loader.register(machinery.BuiltinImporter) |
| 18 | Loader.register(machinery.FrozenImporter) |
| 19 | |
| 20 | |
| 21 | class Finder(metaclass=abc.ABCMeta): |
| 22 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame^] | 23 | """Abstract base class for import finders.""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 24 | |
| 25 | @abc.abstractmethod |
| 26 | def find_module(self, fullname:str, path:[str]=None) -> Loader: |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame^] | 27 | """Abstract method which when implemented should find a module.""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 28 | raise NotImplementedError |
| 29 | |
| 30 | Finder.register(machinery.BuiltinImporter) |
| 31 | Finder.register(machinery.FrozenImporter) |
| 32 | Finder.register(machinery.PathFinder) |
| 33 | |
| 34 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 35 | class ResourceLoader(Loader): |
| 36 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame^] | 37 | """Abstract base class for loaders which can return data from their |
| 38 | back-end storage. |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 39 | |
| 40 | This ABC represents one of the optional protocols specified by PEP 302. |
| 41 | |
| 42 | """ |
| 43 | |
| 44 | @abc.abstractmethod |
| 45 | def get_data(self, path:str) -> bytes: |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame^] | 46 | """Abstract method which when implemented should return the bytes for |
| 47 | the specified path.""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 48 | raise NotImplementedError |
| 49 | |
| 50 | |
| 51 | class InspectLoader(Loader): |
| 52 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame^] | 53 | """Abstract base class for loaders which support inspection about the |
| 54 | modules they can load. |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 55 | |
| 56 | This ABC represents one of the optional protocols specified by PEP 302. |
| 57 | |
| 58 | """ |
| 59 | |
| 60 | @abc.abstractmethod |
| 61 | def is_package(self, fullname:str) -> bool: |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame^] | 62 | """Abstract method which when implemented should return whether the |
| 63 | module is a package.""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 64 | return NotImplementedError |
| 65 | |
| 66 | @abc.abstractmethod |
| 67 | def get_code(self, fullname:str) -> types.CodeType: |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame^] | 68 | """Abstract method which when implemented should return the code object |
| 69 | for the module""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 70 | return NotImplementedError |
| 71 | |
| 72 | @abc.abstractmethod |
| 73 | def get_source(self, fullname:str) -> str: |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame^] | 74 | """Abstract method which should return the source code for the |
| 75 | module.""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 76 | return NotImplementedError |
| 77 | |
| 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 |