summaryrefslogtreecommitdiff
path: root/Lib/importlib/_bootstrap_external.py
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2018-01-15 15:07:11 -0800
committerGitHub <noreply@github.com>2018-01-15 15:07:11 -0800
commit5ec0feeeecc1617987ec6cdc6d62b916e718a5cf (patch)
tree158c8a278acd9a7d2de71553c22199aa9b5b6ec6 /Lib/importlib/_bootstrap_external.py
parent21102f0dc20cc347677191817c1b66e20ef7bf21 (diff)
downloadcpython-git-5ec0feeeecc1617987ec6cdc6d62b916e718a5cf.tar.gz
Implement the get_resource_reader() API for file system imports (#5168)
Diffstat (limited to 'Lib/importlib/_bootstrap_external.py')
-rw-r--r--Lib/importlib/_bootstrap_external.py38
1 files changed, 32 insertions, 6 deletions
diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py
index e808507d3b..cf75719c86 100644
--- a/Lib/importlib/_bootstrap_external.py
+++ b/Lib/importlib/_bootstrap_external.py
@@ -6,12 +6,11 @@ such it requires the injection of specific modules and attributes in order to
work. One should use importlib as the public-facing version of this module.
"""
-#
-# IMPORTANT: Whenever making changes to this module, be sure to run
-# a top-level make in order to get the frozen version of the module
-# updated. Not doing so will result in the Makefile to fail for
-# all others who don't have a ./python around to freeze the module
-# in the early stages of compilation.
+# IMPORTANT: Whenever making changes to this module, be sure to run a top-level
+# `make regen-importlib` followed by `make` in order to get the frozen version
+# of the module updated. Not doing so will result in the Makefile to fail for
+# all others who don't have a ./python around to freeze the module in the early
+# stages of compilation.
#
# See importlib._setup() for what is injected into the global namespace.
@@ -911,6 +910,33 @@ class FileLoader:
with _io.FileIO(path, 'r') as file:
return file.read()
+ # ResourceReader ABC API.
+
+ @_check_name
+ def get_resource_reader(self, module):
+ if self.is_package(module):
+ return self
+ return None
+
+ def open_resource(self, resource):
+ path = _path_join(_path_split(self.path)[0], resource)
+ return _io.FileIO(path, 'r')
+
+ def resource_path(self, resource):
+ if not self.is_resource(resource):
+ raise FileNotFoundError
+ path = _path_join(_path_split(self.path)[0], resource)
+ return path
+
+ def is_resource(self, name):
+ if path_sep in name:
+ return False
+ path = _path_join(_path_split(self.path)[0], name)
+ return _path_isfile(path)
+
+ def contents(self):
+ return iter(_os.listdir(_path_split(self.path)[0]))
+
class SourceFileLoader(FileLoader, SourceLoader):