|
6 | 6 | work. One should use importlib as the public-facing version of this module. |
7 | 7 |
|
8 | 8 | """ |
9 | | -# |
10 | | -# IMPORTANT: Whenever making changes to this module, be sure to run |
11 | | -# a top-level make in order to get the frozen version of the module |
12 | | -# updated. Not doing so will result in the Makefile to fail for |
13 | | -# all others who don't have a ./python around to freeze the module |
14 | | -# in the early stages of compilation. |
| 9 | +# IMPORTANT: Whenever making changes to this module, be sure to run a top-level |
| 10 | +# `make regen-importlib` followed by `make` in order to get the frozen version |
| 11 | +# of the module updated. Not doing so will result in the Makefile to fail for |
| 12 | +# all others who don't have a ./python around to freeze the module in the early |
| 13 | +# stages of compilation. |
15 | 14 | # |
16 | 15 |
|
17 | 16 | # See importlib._setup() for what is injected into the global namespace. |
@@ -911,6 +910,33 @@ def get_data(self, path): |
911 | 910 | with _io.FileIO(path, 'r') as file: |
912 | 911 | return file.read() |
913 | 912 |
|
| 913 | + # ResourceReader ABC API. |
| 914 | + |
| 915 | + @_check_name |
| 916 | + def get_resource_reader(self, module): |
| 917 | + if self.is_package(module): |
| 918 | + return self |
| 919 | + return None |
| 920 | + |
| 921 | + def open_resource(self, resource): |
| 922 | + path = _path_join(_path_split(self.path)[0], resource) |
| 923 | + return _io.FileIO(path, 'r') |
| 924 | + |
| 925 | + def resource_path(self, resource): |
| 926 | + if not self.is_resource(resource): |
| 927 | + raise FileNotFoundError |
| 928 | + path = _path_join(_path_split(self.path)[0], resource) |
| 929 | + return path |
| 930 | + |
| 931 | + def is_resource(self, name): |
| 932 | + if path_sep in name: |
| 933 | + return False |
| 934 | + path = _path_join(_path_split(self.path)[0], name) |
| 935 | + return _path_isfile(path) |
| 936 | + |
| 937 | + def contents(self): |
| 938 | + return iter(_os.listdir(_path_split(self.path)[0])) |
| 939 | + |
914 | 940 |
|
915 | 941 | class SourceFileLoader(FileLoader, SourceLoader): |
916 | 942 |
|
|
0 commit comments