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 | |
| 10 | """Abstract base class for import loaders. |
| 11 | |
| 12 | See PEP 302 for details. |
| 13 | |
| 14 | """ |
| 15 | |
| 16 | def load_module(self, fullname:str) -> types.ModuleType: |
| 17 | raise NotImplementedError |
| 18 | |
| 19 | Loader.register(machinery.BuiltinImporter) |
| 20 | Loader.register(machinery.FrozenImporter) |
| 21 | |
| 22 | |
| 23 | class Finder(metaclass=abc.ABCMeta): |
| 24 | |
| 25 | """Abstract base class for import finders. |
| 26 | |
| 27 | See PEP 302 for details. |
| 28 | |
| 29 | """ |
| 30 | |
| 31 | @abc.abstractmethod |
| 32 | def find_module(self, fullname:str, path:[str]=None) -> Loader: |
| 33 | raise NotImplementedError |
| 34 | |
| 35 | Finder.register(machinery.BuiltinImporter) |
| 36 | Finder.register(machinery.FrozenImporter) |
| 37 | Finder.register(machinery.PathFinder) |
| 38 | |
| 39 | |
| 40 | class Importer(Finder, Loader): |
| 41 | |
| 42 | """Abstract base class for importers.""" |
| 43 | |
| 44 | |
| 45 | |
| 46 | class ResourceLoader(Loader): |
| 47 | |
| 48 | """Abstract base class for loaders which can return data from the back-end |
| 49 | storage. |
| 50 | |
| 51 | This ABC represents one of the optional protocols specified by PEP 302. |
| 52 | |
| 53 | """ |
| 54 | |
| 55 | @abc.abstractmethod |
| 56 | def get_data(self, path:str) -> bytes: |
| 57 | raise NotImplementedError |
| 58 | |
| 59 | |
| 60 | class InspectLoader(Loader): |
| 61 | |
| 62 | """Abstract base class for loaders which supports introspection. |
| 63 | |
| 64 | This ABC represents one of the optional protocols specified by PEP 302. |
| 65 | |
| 66 | """ |
| 67 | |
| 68 | @abc.abstractmethod |
| 69 | def is_package(self, fullname:str) -> bool: |
| 70 | return NotImplementedError |
| 71 | |
| 72 | @abc.abstractmethod |
| 73 | def get_code(self, fullname:str) -> types.CodeType: |
| 74 | return NotImplementedError |
| 75 | |
| 76 | @abc.abstractmethod |
| 77 | def get_source(self, fullname:str) -> str: |
| 78 | return NotImplementedError |
| 79 | |
| 80 | |
| 81 | class PyLoader(_bootstrap.PyLoader, InspectLoader): |
| 82 | |
| 83 | """Abstract base class that implements the core parts needed to load Python |
| 84 | source code.""" |
| 85 | |
| 86 | # load_module and get_code are implemented. |
| 87 | |
| 88 | @abc.abstractmethod |
| 89 | def source_path(self, fullname:str) -> object: |
| 90 | raise NotImplementedError |
| 91 | |
| 92 | |
| 93 | class PyPycLoader(_bootstrap.PyPycLoader, PyLoader): |
| 94 | |
| 95 | """Abstract base class that implements the core parts needed to load Python |
| 96 | source and bytecode.""" |
| 97 | |
| 98 | # Implements load_module and get_code. |
| 99 | |
| 100 | @abc.abstractmethod |
| 101 | def source_mtime(self, fullname:str) -> int: |
| 102 | raise NotImplementedError |
| 103 | |
| 104 | @abc.abstractmethod |
| 105 | def bytecode_path(self, fullname:str) -> object: |
| 106 | raise NotImplementedError |
| 107 | |
| 108 | @abc.abstractmethod |
| 109 | def write_bytecode(self, fullname:str, bytecode:bytes): |
| 110 | raise NotImplementedError |