2009-03-09 00:35:50 -03:00
|
|
|
"""Abstract base classes related to import."""
|
|
|
|
from . import _bootstrap
|
|
|
|
from . import machinery
|
|
|
|
import abc
|
|
|
|
import types
|
|
|
|
|
|
|
|
|
|
|
|
class Loader(metaclass=abc.ABCMeta):
|
|
|
|
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract base class for import loaders."""
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2009-03-14 21:53:05 -03:00
|
|
|
@abc.abstractmethod
|
2009-03-09 00:35:50 -03:00
|
|
|
def load_module(self, fullname:str) -> types.ModuleType:
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract method which when implemented should load a module."""
|
2009-03-09 00:35:50 -03:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
class Finder(metaclass=abc.ABCMeta):
|
|
|
|
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract base class for import finders."""
|
2009-03-09 00:35:50 -03:00
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def find_module(self, fullname:str, path:[str]=None) -> Loader:
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract method which when implemented should find a module."""
|
2009-03-09 00:35:50 -03:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
Finder.register(machinery.BuiltinImporter)
|
|
|
|
Finder.register(machinery.FrozenImporter)
|
|
|
|
Finder.register(machinery.PathFinder)
|
|
|
|
|
|
|
|
|
|
|
|
class ResourceLoader(Loader):
|
|
|
|
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract base class for loaders which can return data from their
|
|
|
|
back-end storage.
|
2009-03-09 00:35:50 -03:00
|
|
|
|
|
|
|
This ABC represents one of the optional protocols specified by PEP 302.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def get_data(self, path:str) -> bytes:
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract method which when implemented should return the bytes for
|
|
|
|
the specified path."""
|
2009-03-09 00:35:50 -03:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
class InspectLoader(Loader):
|
|
|
|
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract base class for loaders which support inspection about the
|
|
|
|
modules they can load.
|
2009-03-09 00:35:50 -03:00
|
|
|
|
|
|
|
This ABC represents one of the optional protocols specified by PEP 302.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def is_package(self, fullname:str) -> bool:
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract method which when implemented should return whether the
|
|
|
|
module is a package."""
|
2009-03-09 00:35:50 -03:00
|
|
|
return NotImplementedError
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def get_code(self, fullname:str) -> types.CodeType:
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract method which when implemented should return the code object
|
|
|
|
for the module"""
|
2009-03-09 00:35:50 -03:00
|
|
|
return NotImplementedError
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def get_source(self, fullname:str) -> str:
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract method which should return the source code for the
|
|
|
|
module."""
|
2009-03-09 00:35:50 -03:00
|
|
|
return NotImplementedError
|
|
|
|
|
2009-03-14 22:41:33 -03:00
|
|
|
InspectLoader.register(machinery.BuiltinImporter)
|
2009-03-14 23:20:16 -03:00
|
|
|
InspectLoader.register(machinery.FrozenImporter)
|
2009-03-14 22:41:33 -03:00
|
|
|
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2009-07-20 01:23:48 -03:00
|
|
|
class ExecutionLoader(InspectLoader):
|
|
|
|
|
|
|
|
"""Abstract base class for loaders that wish to support the execution of
|
|
|
|
modules as scripts.
|
|
|
|
|
|
|
|
This ABC represents one of the optional protocols specified in PEP 302.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def get_filename(self, fullname:str) -> str:
|
|
|
|
"""Abstract method which should return the value that __file__ is to be
|
|
|
|
set to."""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
class PyLoader(_bootstrap.PyLoader, ResourceLoader, ExecutionLoader):
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract base class to assist in loading source code by requiring only
|
|
|
|
back-end storage methods to be implemented.
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2009-03-14 21:53:05 -03:00
|
|
|
The methods get_code, get_source, and load_module are implemented for the
|
|
|
|
user.
|
|
|
|
|
|
|
|
"""
|
2009-03-09 00:35:50 -03:00
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def source_path(self, fullname:str) -> object:
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract method which when implemented should return the path to the
|
|
|
|
sourced code for the module."""
|
2009-03-09 00:35:50 -03:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
class PyPycLoader(_bootstrap.PyPycLoader, PyLoader):
|
|
|
|
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract base class to assist in loading source and bytecode by
|
|
|
|
requiring only back-end storage methods to be implemented.
|
|
|
|
|
|
|
|
The methods get_code, get_source, and load_module are implemented for the
|
|
|
|
user.
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2009-03-14 21:53:05 -03:00
|
|
|
"""
|
2009-03-09 00:35:50 -03:00
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def source_mtime(self, fullname:str) -> int:
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract method which when implemented should return the
|
|
|
|
modification time for the source of the module."""
|
2009-03-09 00:35:50 -03:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def bytecode_path(self, fullname:str) -> object:
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract method which when implemented should return the path to the
|
|
|
|
bytecode for the module."""
|
2009-03-09 00:35:50 -03:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def write_bytecode(self, fullname:str, bytecode:bytes):
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract method which when implemented should attempt to write the
|
|
|
|
bytecode for the module."""
|
2009-03-09 00:35:50 -03:00
|
|
|
raise NotImplementedError
|