blob: b2bdb02c92645f79d08bb388b1aa3ef3524b0b4a [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
79class PyLoader(_bootstrap.PyLoader, InspectLoader):
80
Brett Cannon7aa21f72009-03-15 00:53:05 +000081 """Abstract base class to assist in loading source code by requiring only
82 back-end storage methods to be implemented.
Brett Cannon2a922ed2009-03-09 03:35:50 +000083
Brett Cannon7aa21f72009-03-15 00:53:05 +000084 The methods get_code, get_source, and load_module are implemented for the
85 user.
86
87 """
Brett Cannon2a922ed2009-03-09 03:35:50 +000088
89 @abc.abstractmethod
90 def source_path(self, fullname:str) -> object:
Brett Cannon7aa21f72009-03-15 00:53:05 +000091 """Abstract method which when implemented should return the path to the
92 sourced code for the module."""
Brett Cannon2a922ed2009-03-09 03:35:50 +000093 raise NotImplementedError
94
95
96class PyPycLoader(_bootstrap.PyPycLoader, PyLoader):
97
Brett Cannon7aa21f72009-03-15 00:53:05 +000098 """Abstract base class to assist in loading source and bytecode by
99 requiring only back-end storage methods to be implemented.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000100
Brett Cannon7aa21f72009-03-15 00:53:05 +0000101 The methods get_code, get_source, and load_module are implemented for the
102 user.
103
104 """
Brett Cannon2a922ed2009-03-09 03:35:50 +0000105
106 @abc.abstractmethod
107 def source_mtime(self, fullname:str) -> int:
Brett Cannon7aa21f72009-03-15 00:53:05 +0000108 """Abstract method which when implemented should return the
109 modification time for the source of the module."""
Brett Cannon2a922ed2009-03-09 03:35:50 +0000110 raise NotImplementedError
111
112 @abc.abstractmethod
113 def bytecode_path(self, fullname:str) -> object:
Brett Cannon7aa21f72009-03-15 00:53:05 +0000114 """Abstract method which when implemented should return the path to the
115 bytecode for the module."""
Brett Cannon2a922ed2009-03-09 03:35:50 +0000116 raise NotImplementedError
117
118 @abc.abstractmethod
119 def write_bytecode(self, fullname:str, bytecode:bytes):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000120 """Abstract method which when implemented should attempt to write the
121 bytecode for the module."""
Brett Cannon2a922ed2009-03-09 03:35:50 +0000122 raise NotImplementedError