2009-03-09 00:35:50 -03:00
|
|
|
"""Abstract base classes related to import."""
|
2015-05-02 22:15:18 -03:00
|
|
|
from . import _bootstrap_external
|
2009-03-09 00:35:50 -03:00
|
|
|
from . import machinery
|
2012-04-22 20:58:33 -03:00
|
|
|
try:
|
|
|
|
import _frozen_importlib
|
2013-07-04 18:43:24 -03:00
|
|
|
except ImportError as exc:
|
2012-04-22 20:58:33 -03:00
|
|
|
if exc.name != '_frozen_importlib':
|
|
|
|
raise
|
|
|
|
_frozen_importlib = None
|
2015-05-02 22:15:18 -03:00
|
|
|
try:
|
|
|
|
import _frozen_importlib_external
|
2019-11-19 17:34:03 -04:00
|
|
|
except ImportError:
|
2015-05-02 22:15:18 -03:00
|
|
|
_frozen_importlib_external = _bootstrap_external
|
2020-06-17 18:15:59 -03:00
|
|
|
from ._abc import Loader
|
2009-03-09 00:35:50 -03:00
|
|
|
import abc
|
2017-02-15 22:00:32 -04:00
|
|
|
import warnings
|
2020-05-08 20:20:26 -03:00
|
|
|
from typing import Protocol, runtime_checkable
|
2009-03-09 00:35:50 -03:00
|
|
|
|
|
|
|
|
2012-04-22 20:58:33 -03:00
|
|
|
def _register(abstract_cls, *classes):
|
|
|
|
for cls in classes:
|
|
|
|
abstract_cls.register(cls)
|
|
|
|
if _frozen_importlib is not None:
|
2015-05-02 22:15:18 -03:00
|
|
|
try:
|
|
|
|
frozen_cls = getattr(_frozen_importlib, cls.__name__)
|
|
|
|
except AttributeError:
|
|
|
|
frozen_cls = getattr(_frozen_importlib_external, cls.__name__)
|
2012-04-22 20:58:33 -03:00
|
|
|
abstract_cls.register(frozen_cls)
|
|
|
|
|
|
|
|
|
2012-08-02 08:26:03 -03:00
|
|
|
class Finder(metaclass=abc.ABCMeta):
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2012-08-10 13:21:12 -03:00
|
|
|
"""Legacy abstract base class for import finders.
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2012-08-10 13:21:12 -03:00
|
|
|
It may be subclassed for compatibility with legacy third party
|
|
|
|
reimplementations of the import system. Otherwise, finder
|
|
|
|
implementations should derive from the more specific MetaPathFinder
|
|
|
|
or PathEntryFinder ABCs.
|
2017-02-15 22:00:32 -04:00
|
|
|
|
|
|
|
Deprecated since Python 3.3
|
2012-08-02 08:26:03 -03:00
|
|
|
"""
|
|
|
|
|
2012-08-10 13:21:12 -03:00
|
|
|
@abc.abstractmethod
|
2012-08-02 08:26:03 -03:00
|
|
|
def find_module(self, fullname, path=None):
|
2012-08-10 13:21:12 -03:00
|
|
|
"""An abstract method that should find a module.
|
2012-08-02 08:26:03 -03:00
|
|
|
The fullname is a str and the optional path is a str or None.
|
2013-04-09 17:59:39 -03:00
|
|
|
Returns a Loader object or None.
|
2012-08-02 08:26:03 -03:00
|
|
|
"""
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2012-08-02 08:26:03 -03:00
|
|
|
|
|
|
|
class MetaPathFinder(Finder):
|
|
|
|
|
|
|
|
"""Abstract base class for import finders on sys.meta_path."""
|
|
|
|
|
2013-11-22 12:05:39 -04:00
|
|
|
# We don't define find_spec() here since that would break
|
|
|
|
# hasattr checks we do to support backward compatibility.
|
|
|
|
|
2012-08-02 08:26:03 -03:00
|
|
|
def find_module(self, fullname, path):
|
2013-11-22 12:05:39 -04:00
|
|
|
"""Return a loader for the module.
|
|
|
|
|
|
|
|
If no module is found, return None. The fullname is a str and
|
|
|
|
the path is a list of strings or None.
|
|
|
|
|
2017-02-15 22:00:32 -04:00
|
|
|
This method is deprecated since Python 3.4 in favor of
|
|
|
|
finder.find_spec(). If find_spec() exists then backwards-compatible
|
|
|
|
functionality is provided for this method.
|
2014-01-06 23:49:04 -04:00
|
|
|
|
2012-08-02 08:26:03 -03:00
|
|
|
"""
|
2017-02-15 22:00:32 -04:00
|
|
|
warnings.warn("MetaPathFinder.find_module() is deprecated since Python "
|
2018-11-05 10:20:25 -04:00
|
|
|
"3.4 in favor of MetaPathFinder.find_spec() "
|
2017-02-15 22:00:32 -04:00
|
|
|
"(available since 3.4)",
|
|
|
|
DeprecationWarning,
|
|
|
|
stacklevel=2)
|
2014-01-07 16:52:42 -04:00
|
|
|
if not hasattr(self, 'find_spec'):
|
|
|
|
return None
|
|
|
|
found = self.find_spec(fullname, path)
|
|
|
|
return found.loader if found is not None else None
|
2012-07-29 17:36:17 -03:00
|
|
|
|
2012-08-10 13:21:12 -03:00
|
|
|
def invalidate_caches(self):
|
|
|
|
"""An optional method for clearing the finder's cache, if any.
|
|
|
|
This method is used by importlib.invalidate_caches().
|
|
|
|
"""
|
|
|
|
|
2012-08-02 08:26:03 -03:00
|
|
|
_register(MetaPathFinder, machinery.BuiltinImporter, machinery.FrozenImporter,
|
2012-08-02 08:45:24 -03:00
|
|
|
machinery.PathFinder, machinery.WindowsRegistryFinder)
|
2009-03-09 00:35:50 -03:00
|
|
|
|
|
|
|
|
2012-08-02 08:26:03 -03:00
|
|
|
class PathEntryFinder(Finder):
|
|
|
|
|
|
|
|
"""Abstract base class for path entry finders used by PathFinder."""
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2013-11-22 12:05:39 -04:00
|
|
|
# We don't define find_spec() here since that would break
|
|
|
|
# hasattr checks we do to support backward compatibility.
|
|
|
|
|
2012-07-29 17:36:17 -03:00
|
|
|
def find_loader(self, fullname):
|
2013-11-22 12:05:39 -04:00
|
|
|
"""Return (loader, namespace portion) for the path entry.
|
|
|
|
|
|
|
|
The fullname is a str. The namespace portion is a sequence of
|
|
|
|
path entries contributing to part of a namespace package. The
|
|
|
|
sequence may be empty. If loader is not None, the portion will
|
|
|
|
be ignored.
|
|
|
|
|
|
|
|
The portion will be discarded if another path entry finder
|
|
|
|
locates the module as a normal module or package.
|
|
|
|
|
2017-02-15 22:00:32 -04:00
|
|
|
This method is deprecated since Python 3.4 in favor of
|
|
|
|
finder.find_spec(). If find_spec() is provided than backwards-compatible
|
|
|
|
functionality is provided.
|
2012-07-29 17:36:17 -03:00
|
|
|
"""
|
2017-02-15 22:00:32 -04:00
|
|
|
warnings.warn("PathEntryFinder.find_loader() is deprecated since Python "
|
|
|
|
"3.4 in favor of PathEntryFinder.find_spec() "
|
|
|
|
"(available since 3.4)",
|
|
|
|
DeprecationWarning,
|
|
|
|
stacklevel=2)
|
2014-01-07 16:52:42 -04:00
|
|
|
if not hasattr(self, 'find_spec'):
|
|
|
|
return None, []
|
|
|
|
found = self.find_spec(fullname)
|
|
|
|
if found is not None:
|
|
|
|
if not found.submodule_search_locations:
|
|
|
|
portions = []
|
|
|
|
else:
|
|
|
|
portions = found.submodule_search_locations
|
|
|
|
return found.loader, portions
|
|
|
|
else:
|
|
|
|
return None, []
|
2012-07-29 17:36:17 -03:00
|
|
|
|
2015-05-02 22:15:18 -03:00
|
|
|
find_module = _bootstrap_external._find_module_shim
|
2012-08-10 13:21:12 -03:00
|
|
|
|
|
|
|
def invalidate_caches(self):
|
|
|
|
"""An optional method for clearing the finder's cache, if any.
|
|
|
|
This method is used by PathFinder.invalidate_caches().
|
|
|
|
"""
|
|
|
|
|
2012-08-02 08:26:03 -03:00
|
|
|
_register(PathEntryFinder, machinery.FileFinder)
|
|
|
|
|
|
|
|
|
2009-03-09 00:35:50 -03:00
|
|
|
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
|
2011-01-12 22:31:25 -04:00
|
|
|
def get_data(self, path):
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract method which when implemented should return the bytes for
|
2011-01-13 15:08:04 -04:00
|
|
|
the specified path. The path must be a str."""
|
2017-04-16 04:46:38 -03:00
|
|
|
raise OSError
|
2009-03-09 00:35:50 -03:00
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2011-01-12 22:31:25 -04:00
|
|
|
def is_package(self, fullname):
|
2013-11-22 12:05:39 -04:00
|
|
|
"""Optional method which when implemented should return whether the
|
2013-04-09 17:59:39 -03:00
|
|
|
module is a package. The fullname is a str. Returns a bool.
|
|
|
|
|
2013-11-22 12:05:39 -04:00
|
|
|
Raises ImportError if the module cannot be found.
|
2013-04-09 17:59:39 -03:00
|
|
|
"""
|
|
|
|
raise ImportError
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2011-01-12 22:31:25 -04:00
|
|
|
def get_code(self, fullname):
|
2013-05-27 22:11:04 -03:00
|
|
|
"""Method which returns the code object for the module.
|
2013-04-09 17:59:39 -03:00
|
|
|
|
2013-05-27 22:11:04 -03:00
|
|
|
The fullname is a str. Returns a types.CodeType if possible, else
|
|
|
|
returns None if a code object does not make sense
|
|
|
|
(e.g. built-in module). Raises ImportError if the module cannot be
|
|
|
|
found.
|
2013-04-09 17:59:39 -03:00
|
|
|
"""
|
2013-05-27 22:11:04 -03:00
|
|
|
source = self.get_source(fullname)
|
|
|
|
if source is None:
|
|
|
|
return None
|
|
|
|
return self.source_to_code(source)
|
2009-03-09 00:35:50 -03:00
|
|
|
|
|
|
|
@abc.abstractmethod
|
2011-01-12 22:31:25 -04:00
|
|
|
def get_source(self, fullname):
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract method which should return the source code for the
|
2013-04-09 17:59:39 -03:00
|
|
|
module. The fullname is a str. Returns a str.
|
|
|
|
|
|
|
|
Raises ImportError if the module cannot be found.
|
|
|
|
"""
|
|
|
|
raise ImportError
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2014-05-09 13:28:22 -03:00
|
|
|
@staticmethod
|
|
|
|
def source_to_code(data, path='<string>'):
|
2013-05-26 17:45:10 -03:00
|
|
|
"""Compile 'data' into a code object.
|
|
|
|
|
|
|
|
The 'data' argument can be anything that compile() can handle. The'path'
|
|
|
|
argument should be where the data was retrieved (when applicable)."""
|
|
|
|
return compile(data, path, 'exec', dont_inherit=True)
|
|
|
|
|
2015-05-02 22:15:18 -03:00
|
|
|
exec_module = _bootstrap_external._LoaderBasics.exec_module
|
|
|
|
load_module = _bootstrap_external._LoaderBasics.load_module
|
2013-05-31 19:56:47 -03:00
|
|
|
|
2013-11-22 12:05:39 -04:00
|
|
|
_register(InspectLoader, machinery.BuiltinImporter, 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
|
2011-01-12 22:31:25 -04:00
|
|
|
def get_filename(self, fullname):
|
2009-07-20 01:23:48 -03:00
|
|
|
"""Abstract method which should return the value that __file__ is to be
|
2013-04-09 17:59:39 -03:00
|
|
|
set to.
|
|
|
|
|
|
|
|
Raises ImportError if the module cannot be found.
|
|
|
|
"""
|
|
|
|
raise ImportError
|
2009-07-20 01:23:48 -03:00
|
|
|
|
2013-05-27 22:11:04 -03:00
|
|
|
def get_code(self, fullname):
|
|
|
|
"""Method to return the code object for fullname.
|
|
|
|
|
|
|
|
Should return None if not applicable (e.g. built-in module).
|
|
|
|
Raise ImportError if the module cannot be found.
|
|
|
|
"""
|
|
|
|
source = self.get_source(fullname)
|
|
|
|
if source is None:
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
path = self.get_filename(fullname)
|
|
|
|
except ImportError:
|
|
|
|
return self.source_to_code(source)
|
|
|
|
else:
|
|
|
|
return self.source_to_code(source, path)
|
|
|
|
|
2013-10-04 23:28:52 -03:00
|
|
|
_register(ExecutionLoader, machinery.ExtensionFileLoader)
|
2013-10-03 15:08:55 -03:00
|
|
|
|
2009-07-20 01:23:48 -03:00
|
|
|
|
2015-05-02 22:15:18 -03:00
|
|
|
class FileLoader(_bootstrap_external.FileLoader, ResourceLoader, ExecutionLoader):
|
2012-04-22 20:58:33 -03:00
|
|
|
|
|
|
|
"""Abstract base class partially implementing the ResourceLoader and
|
|
|
|
ExecutionLoader ABCs."""
|
|
|
|
|
|
|
|
_register(FileLoader, machinery.SourceFileLoader,
|
2012-04-24 21:31:37 -03:00
|
|
|
machinery.SourcelessFileLoader)
|
2012-04-22 20:58:33 -03:00
|
|
|
|
|
|
|
|
2015-05-02 22:15:18 -03:00
|
|
|
class SourceLoader(_bootstrap_external.SourceLoader, ResourceLoader, ExecutionLoader):
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2010-06-27 20:57:46 -03:00
|
|
|
"""Abstract base class for loading source code (and optionally any
|
|
|
|
corresponding bytecode).
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2010-06-27 20:57:46 -03:00
|
|
|
To support loading from source code, the abstractmethods inherited from
|
|
|
|
ResourceLoader and ExecutionLoader need to be implemented. To also support
|
|
|
|
loading from bytecode, the optional methods specified directly by this ABC
|
|
|
|
is required.
|
|
|
|
|
|
|
|
Inherited abstractmethods not implemented in this ABC:
|
|
|
|
|
|
|
|
* ResourceLoader.get_data
|
|
|
|
* ExecutionLoader.get_filename
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2011-01-12 22:31:25 -04:00
|
|
|
def path_mtime(self, path):
|
2011-01-13 15:08:04 -04:00
|
|
|
"""Return the (int) modification time for the path (str)."""
|
2012-01-13 13:52:16 -04:00
|
|
|
if self.path_stats.__func__ is SourceLoader.path_stats:
|
2017-04-16 04:46:38 -03:00
|
|
|
raise OSError
|
2012-01-13 13:52:16 -04:00
|
|
|
return int(self.path_stats(path)['mtime'])
|
|
|
|
|
|
|
|
def path_stats(self, path):
|
|
|
|
"""Return a metadata dict for the source pointed to by the path (str).
|
|
|
|
Possible keys:
|
|
|
|
- 'mtime' (mandatory) is the numeric timestamp of last source
|
|
|
|
code modification;
|
|
|
|
- 'size' (optional) is the size in bytes of the source code.
|
|
|
|
"""
|
|
|
|
if self.path_mtime.__func__ is SourceLoader.path_mtime:
|
2017-04-16 04:46:38 -03:00
|
|
|
raise OSError
|
2012-01-13 13:52:16 -04:00
|
|
|
return {'mtime': self.path_mtime(path)}
|
2010-08-22 17:38:47 -03:00
|
|
|
|
2011-01-12 22:31:25 -04:00
|
|
|
def set_data(self, path, data):
|
2010-08-22 17:38:47 -03:00
|
|
|
"""Write the bytes to the path (if possible).
|
|
|
|
|
2011-01-13 15:08:04 -04:00
|
|
|
Accepts a str path and data as bytes.
|
|
|
|
|
2010-08-22 17:38:47 -03:00
|
|
|
Any needed intermediary directories are to be created. If for some
|
|
|
|
reason the file cannot be written because of permissions, fail
|
|
|
|
silently.
|
|
|
|
"""
|
|
|
|
|
2012-04-22 20:58:33 -03:00
|
|
|
_register(SourceLoader, machinery.SourceFileLoader)
|
2017-12-15 20:29:35 -04:00
|
|
|
|
|
|
|
|
2018-01-15 19:07:11 -04:00
|
|
|
class ResourceReader(metaclass=abc.ABCMeta):
|
2017-12-15 20:29:35 -04:00
|
|
|
|
2018-01-12 19:08:59 -04:00
|
|
|
"""Abstract base class to provide resource-reading support.
|
|
|
|
|
|
|
|
Loaders that support resource reading are expected to implement
|
|
|
|
the ``get_resource_reader(fullname)`` method and have it either return None
|
|
|
|
or an object compatible with this ABC.
|
|
|
|
"""
|
2017-12-15 20:29:35 -04:00
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def open_resource(self, resource):
|
|
|
|
"""Return an opened, file-like object for binary reading.
|
|
|
|
|
|
|
|
The 'resource' argument is expected to represent only a file name
|
|
|
|
and thus not contain any subdirectory components.
|
|
|
|
|
|
|
|
If the resource cannot be found, FileNotFoundError is raised.
|
|
|
|
"""
|
|
|
|
raise FileNotFoundError
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def resource_path(self, resource):
|
|
|
|
"""Return the file system path to the specified resource.
|
|
|
|
|
|
|
|
The 'resource' argument is expected to represent only a file name
|
|
|
|
and thus not contain any subdirectory components.
|
|
|
|
|
|
|
|
If the resource does not exist on the file system, raise
|
|
|
|
FileNotFoundError.
|
|
|
|
"""
|
|
|
|
raise FileNotFoundError
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def is_resource(self, name):
|
|
|
|
"""Return True if the named 'name' is consider a resource."""
|
|
|
|
raise FileNotFoundError
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def contents(self):
|
2018-04-30 15:31:45 -03:00
|
|
|
"""Return an iterable of strings over the contents of the package."""
|
|
|
|
return []
|
2018-01-15 19:07:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
_register(ResourceReader, machinery.SourceFileLoader)
|
2020-05-08 20:20:26 -03:00
|
|
|
|
|
|
|
|
|
|
|
@runtime_checkable
|
|
|
|
class Traversable(Protocol):
|
|
|
|
"""
|
|
|
|
An object with a subset of pathlib.Path methods suitable for
|
|
|
|
traversing directories and opening files.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def iterdir(self):
|
|
|
|
"""
|
|
|
|
Yield Traversable objects in self
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def read_bytes(self):
|
|
|
|
"""
|
|
|
|
Read contents of self as bytes
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def read_text(self, encoding=None):
|
|
|
|
"""
|
|
|
|
Read contents of self as bytes
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def is_dir(self):
|
|
|
|
"""
|
|
|
|
Return True if self is a dir
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def is_file(self):
|
|
|
|
"""
|
|
|
|
Return True if self is a file
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def joinpath(self, child):
|
|
|
|
"""
|
|
|
|
Return Traversable child in self
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def __truediv__(self, child):
|
|
|
|
"""
|
|
|
|
Return Traversable child in self
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def open(self, mode='r', *args, **kwargs):
|
|
|
|
"""
|
|
|
|
mode may be 'r' or 'rb' to open as text or binary. Return a handle
|
|
|
|
suitable for reading (same as pathlib.Path.open).
|
|
|
|
|
|
|
|
When opening as text, accepts encoding parameters such as those
|
|
|
|
accepted by io.TextIOWrapper.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abc.abstractproperty
|
|
|
|
def name(self):
|
|
|
|
# type: () -> str
|
|
|
|
"""
|
|
|
|
The base name of this object without any parent references.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class TraversableResources(ResourceReader):
|
|
|
|
@abc.abstractmethod
|
|
|
|
def files(self):
|
|
|
|
"""Return a Traversable object for the loaded package."""
|
|
|
|
|
|
|
|
def open_resource(self, resource):
|
|
|
|
return self.files().joinpath(resource).open('rb')
|
|
|
|
|
|
|
|
def resource_path(self, resource):
|
|
|
|
raise FileNotFoundError(resource)
|
|
|
|
|
|
|
|
def is_resource(self, path):
|
2020-06-07 22:00:51 -03:00
|
|
|
return self.files().joinpath(path).is_file()
|
2020-05-08 20:20:26 -03:00
|
|
|
|
|
|
|
def contents(self):
|
|
|
|
return (item.name for item in self.files().iterdir())
|