blob: 6b00569ef17cf3a40a22e1240e251bb6615c2e61 [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
17Loader.register(machinery.BuiltinImporter)
18Loader.register(machinery.FrozenImporter)
19
20
21class Finder(metaclass=abc.ABCMeta):
22
Brett Cannon7aa21f72009-03-15 00:53:05 +000023 """Abstract base class for import finders."""
Brett Cannon2a922ed2009-03-09 03:35:50 +000024
25 @abc.abstractmethod
26 def find_module(self, fullname:str, path:[str]=None) -> Loader:
Brett Cannon7aa21f72009-03-15 00:53:05 +000027 """Abstract method which when implemented should find a module."""
Brett Cannon2a922ed2009-03-09 03:35:50 +000028 raise NotImplementedError
29
30Finder.register(machinery.BuiltinImporter)
31Finder.register(machinery.FrozenImporter)
32Finder.register(machinery.PathFinder)
33
34
Brett Cannon2a922ed2009-03-09 03:35:50 +000035class ResourceLoader(Loader):
36
Brett Cannon7aa21f72009-03-15 00:53:05 +000037 """Abstract base class for loaders which can return data from their
38 back-end storage.
Brett Cannon2a922ed2009-03-09 03:35:50 +000039
40 This ABC represents one of the optional protocols specified by PEP 302.
41
42 """
43
44 @abc.abstractmethod
45 def get_data(self, path:str) -> bytes:
Brett Cannon7aa21f72009-03-15 00:53:05 +000046 """Abstract method which when implemented should return the bytes for
47 the specified path."""
Brett Cannon2a922ed2009-03-09 03:35:50 +000048 raise NotImplementedError
49
50
51class InspectLoader(Loader):
52
Brett Cannon7aa21f72009-03-15 00:53:05 +000053 """Abstract base class for loaders which support inspection about the
54 modules they can load.
Brett Cannon2a922ed2009-03-09 03:35:50 +000055
56 This ABC represents one of the optional protocols specified by PEP 302.
57
58 """
59
60 @abc.abstractmethod
61 def is_package(self, fullname:str) -> bool:
Brett Cannon7aa21f72009-03-15 00:53:05 +000062 """Abstract method which when implemented should return whether the
63 module is a package."""
Brett Cannon2a922ed2009-03-09 03:35:50 +000064 return NotImplementedError
65
66 @abc.abstractmethod
67 def get_code(self, fullname:str) -> types.CodeType:
Brett Cannon7aa21f72009-03-15 00:53:05 +000068 """Abstract method which when implemented should return the code object
69 for the module"""
Brett Cannon2a922ed2009-03-09 03:35:50 +000070 return NotImplementedError
71
72 @abc.abstractmethod
73 def get_source(self, fullname:str) -> str:
Brett Cannon7aa21f72009-03-15 00:53:05 +000074 """Abstract method which should return the source code for the
75 module."""
Brett Cannon2a922ed2009-03-09 03:35:50 +000076 return NotImplementedError
77
78
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