blob: 2ecb8218ba4f8d51c4fe06fd3509bdc1c0765afd [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
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
19Loader.register(machinery.BuiltinImporter)
20Loader.register(machinery.FrozenImporter)
21
22
23class 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
35Finder.register(machinery.BuiltinImporter)
36Finder.register(machinery.FrozenImporter)
37Finder.register(machinery.PathFinder)
38
39
40class Importer(Finder, Loader):
41
42 """Abstract base class for importers."""
43
44
45
46class 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
60class 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
81class 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
93class 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