blob: c912280ff1ed4c76dac201f414295abee229805a [file] [log] [blame]
Brett Cannon2a922ed2009-03-09 03:35:50 +00001"""Abstract base classes related to import."""
2from . import _bootstrap
3from . import machinery
4import abc
5import types
6
7
8class Loader(metaclass=abc.ABCMeta):
9
Brett Cannon7aa21f72009-03-15 00:53:05 +000010 """Abstract base class for import loaders."""
Brett Cannon2a922ed2009-03-09 03:35:50 +000011
Brett Cannon7aa21f72009-03-15 00:53:05 +000012 @abc.abstractmethod
Brett Cannon2a922ed2009-03-09 03:35:50 +000013 def load_module(self, fullname:str) -> types.ModuleType:
Brett Cannon7aa21f72009-03-15 00:53:05 +000014 """Abstract method which when implemented should load a module."""
Brett Cannon2a922ed2009-03-09 03:35:50 +000015 raise NotImplementedError
16
Brett Cannon2a922ed2009-03-09 03:35:50 +000017
18class Finder(metaclass=abc.ABCMeta):
19
Brett Cannon7aa21f72009-03-15 00:53:05 +000020 """Abstract base class for import finders."""
Brett Cannon2a922ed2009-03-09 03:35:50 +000021
22 @abc.abstractmethod
23 def find_module(self, fullname:str, path:[str]=None) -> Loader:
Brett Cannon7aa21f72009-03-15 00:53:05 +000024 """Abstract method which when implemented should find a module."""
Brett Cannon2a922ed2009-03-09 03:35:50 +000025 raise NotImplementedError
26
27Finder.register(machinery.BuiltinImporter)
28Finder.register(machinery.FrozenImporter)
29Finder.register(machinery.PathFinder)
30
31
Brett Cannon2a922ed2009-03-09 03:35:50 +000032class ResourceLoader(Loader):
33
Brett Cannon7aa21f72009-03-15 00:53:05 +000034 """Abstract base class for loaders which can return data from their
35 back-end storage.
Brett Cannon2a922ed2009-03-09 03:35:50 +000036
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 Cannon7aa21f72009-03-15 00:53:05 +000043 """Abstract method which when implemented should return the bytes for
44 the specified path."""
Brett Cannon2a922ed2009-03-09 03:35:50 +000045 raise NotImplementedError
46
47
48class InspectLoader(Loader):
49
Brett Cannon7aa21f72009-03-15 00:53:05 +000050 """Abstract base class for loaders which support inspection about the
51 modules they can load.
Brett Cannon2a922ed2009-03-09 03:35:50 +000052
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 Cannon7aa21f72009-03-15 00:53:05 +000059 """Abstract method which when implemented should return whether the
60 module is a package."""
Brett Cannon2a922ed2009-03-09 03:35:50 +000061 return NotImplementedError
62
63 @abc.abstractmethod
64 def get_code(self, fullname:str) -> types.CodeType:
Brett Cannon7aa21f72009-03-15 00:53:05 +000065 """Abstract method which when implemented should return the code object
66 for the module"""
Brett Cannon2a922ed2009-03-09 03:35:50 +000067 return NotImplementedError
68
69 @abc.abstractmethod
70 def get_source(self, fullname:str) -> str:
Brett Cannon7aa21f72009-03-15 00:53:05 +000071 """Abstract method which should return the source code for the
72 module."""
Brett Cannon2a922ed2009-03-09 03:35:50 +000073 return NotImplementedError
74
Brett Cannona113ac52009-03-15 01:41:33 +000075InspectLoader.register(machinery.BuiltinImporter)
Brett Cannon8d110132009-03-15 02:20:16 +000076InspectLoader.register(machinery.FrozenImporter)
Brett Cannona113ac52009-03-15 01:41:33 +000077
Brett Cannon2a922ed2009-03-09 03:35:50 +000078
Brett Cannon69194272009-07-20 04:23:48 +000079class ExecutionLoader(InspectLoader):
80
81 """Abstract base class for loaders that wish to support the execution of
82 modules as scripts.
83
84 This ABC represents one of the optional protocols specified in PEP 302.
85
86 """
87
88 @abc.abstractmethod
89 def get_filename(self, fullname:str) -> str:
90 """Abstract method which should return the value that __file__ is to be
91 set to."""
92 raise NotImplementedError
93
94
95class PyLoader(_bootstrap.PyLoader, ResourceLoader, ExecutionLoader):
Brett Cannon2a922ed2009-03-09 03:35:50 +000096
Brett Cannon7aa21f72009-03-15 00:53:05 +000097 """Abstract base class to assist in loading source code by requiring only
98 back-end storage methods to be implemented.
Brett Cannon2a922ed2009-03-09 03:35:50 +000099
Brett Cannon7aa21f72009-03-15 00:53:05 +0000100 The methods get_code, get_source, and load_module are implemented for the
101 user.
102
103 """
Brett Cannon2a922ed2009-03-09 03:35:50 +0000104
105 @abc.abstractmethod
106 def source_path(self, fullname:str) -> object:
Brett Cannon7aa21f72009-03-15 00:53:05 +0000107 """Abstract method which when implemented should return the path to the
108 sourced code for the module."""
Brett Cannon2a922ed2009-03-09 03:35:50 +0000109 raise NotImplementedError
110
111
112class PyPycLoader(_bootstrap.PyPycLoader, PyLoader):
113
Brett Cannon7aa21f72009-03-15 00:53:05 +0000114 """Abstract base class to assist in loading source and bytecode by
115 requiring only back-end storage methods to be implemented.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000116
Brett Cannon7aa21f72009-03-15 00:53:05 +0000117 The methods get_code, get_source, and load_module are implemented for the
118 user.
119
120 """
Brett Cannon2a922ed2009-03-09 03:35:50 +0000121
122 @abc.abstractmethod
123 def source_mtime(self, fullname:str) -> int:
Brett Cannon7aa21f72009-03-15 00:53:05 +0000124 """Abstract method which when implemented should return the
125 modification time for the source of the module."""
Brett Cannon2a922ed2009-03-09 03:35:50 +0000126 raise NotImplementedError
127
128 @abc.abstractmethod
129 def bytecode_path(self, fullname:str) -> object:
Brett Cannon7aa21f72009-03-15 00:53:05 +0000130 """Abstract method which when implemented should return the path to the
131 bytecode for the module."""
Brett Cannon2a922ed2009-03-09 03:35:50 +0000132 raise NotImplementedError
133
134 @abc.abstractmethod
135 def write_bytecode(self, fullname:str, bytecode:bytes):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000136 """Abstract method which when implemented should attempt to write the
137 bytecode for the module."""
Brett Cannon2a922ed2009-03-09 03:35:50 +0000138 raise NotImplementedError