1
"""Abstract base classes related to import."""
2
from . import _bootstrap
3
from . import machinery
8
class Loader(metaclass=abc.ABCMeta):
10
"""Abstract base class for import loaders."""
13
def load_module(self, fullname:str) -> types.ModuleType:
14
"""Abstract method which when implemented should load a module."""
15
raise NotImplementedError
18
class Finder(metaclass=abc.ABCMeta):
20
"""Abstract base class for import finders."""
23
def find_module(self, fullname:str, path:[str]=None) -> Loader:
24
"""Abstract method which when implemented should find a module."""
25
raise NotImplementedError
27
Finder.register(machinery.BuiltinImporter)
28
Finder.register(machinery.FrozenImporter)
29
Finder.register(machinery.PathFinder)
32
class ResourceLoader(Loader):
34
"""Abstract base class for loaders which can return data from their
37
This ABC represents one of the optional protocols specified by PEP 302.
42
def get_data(self, path:str) -> bytes:
43
"""Abstract method which when implemented should return the bytes for
44
the specified path."""
45
raise NotImplementedError
48
class InspectLoader(Loader):
50
"""Abstract base class for loaders which support inspection about the
51
modules they can load.
53
This ABC represents one of the optional protocols specified by PEP 302.
58
def is_package(self, fullname:str) -> bool:
59
"""Abstract method which when implemented should return whether the
60
module is a package."""
61
return NotImplementedError
64
def get_code(self, fullname:str) -> types.CodeType:
65
"""Abstract method which when implemented should return the code object
67
return NotImplementedError
70
def get_source(self, fullname:str) -> str:
71
"""Abstract method which should return the source code for the
73
return NotImplementedError
75
InspectLoader.register(machinery.BuiltinImporter)
76
InspectLoader.register(machinery.FrozenImporter)
79
class PyLoader(_bootstrap.PyLoader, InspectLoader):
81
"""Abstract base class to assist in loading source code by requiring only
82
back-end storage methods to be implemented.
84
The methods get_code, get_source, and load_module are implemented for the
90
def source_path(self, fullname:str) -> object:
91
"""Abstract method which when implemented should return the path to the
92
sourced code for the module."""
93
raise NotImplementedError
96
class PyPycLoader(_bootstrap.PyPycLoader, PyLoader):
98
"""Abstract base class to assist in loading source and bytecode by
99
requiring only back-end storage methods to be implemented.
101
The methods get_code, get_source, and load_module are implemented for the
107
def source_mtime(self, fullname:str) -> int:
108
"""Abstract method which when implemented should return the
109
modification time for the source of the module."""
110
raise NotImplementedError
113
def bytecode_path(self, fullname:str) -> object:
114
"""Abstract method which when implemented should return the path to the
115
bytecode for the module."""
116
raise NotImplementedError
119
def write_bytecode(self, fullname:str, bytecode:bytes):
120
"""Abstract method which when implemented should attempt to write the
121
bytecode for the module."""
122
raise NotImplementedError