| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,12 +6,11 @@ | |||
| 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 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -342,7 +342,7 @@ def set_data(self, path, data): | |||
| 342 | 342 | _register(SourceLoader, machinery.SourceFileLoader) | |
| 343 | 343 | ||
| 344 | 344 | ||
| 345 | - class ResourceReader: | ||
| 345 | + class ResourceReader(metaclass=abc.ABCMeta): | ||
| 346 | 346 | ||
| 347 | 347 | """Abstract base class to provide resource-reading support. | |
| 348 | 348 | ||
@@ -383,3 +383,6 @@ def is_resource(self, name): | |||
| 383 | 383 | def contents(self): | |
| 384 | 384 | """Return an iterator of strings over the contents of the package.""" | |
| 385 | 385 | return iter([]) | |
| 386 | + | ||
| 387 | + | ||
| 388 | + _register(ResourceReader, machinery.SourceFileLoader) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -59,8 +59,10 @@ def _get_resource_reader( | |||
| 59 | 59 | # hook wants to create a weak reference to the object, but | |
| 60 | 60 | # zipimport.zipimporter does not support weak references, resulting in a | |
| 61 | 61 | # TypeError. That seems terrible. | |
| 62 | - if hasattr(package.__spec__.loader, 'open_resource'): | ||
| 63 | - return cast(resources_abc.ResourceReader, package.__spec__.loader) | ||
| 62 | + spec = package.__spec__ | ||
| 63 | + if hasattr(spec.loader, 'get_resource_reader'): | ||
| 64 | + return cast(resources_abc.ResourceReader, | ||
| 65 | + spec.loader.get_resource_reader(spec.name)) | ||
| 64 | 66 | return None | |
| 65 | 67 | ||
| 66 | 68 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,10 +1,10 @@ | |||
| 1 | 1 | import sys | |
| 2 | 2 | import unittest | |
| 3 | 3 | ||
| 4 | - from importlib import resources | ||
| 5 | 4 | from . import data01 | |
| 6 | 5 | from . import zipdata02 | |
| 7 | 6 | from . import util | |
| 7 | + from importlib import resources | ||
| 8 | 8 | ||
| 9 | 9 | ||
| 10 | 10 | class ResourceTests: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -397,6 +397,9 @@ def caseok_env_changed(self, *, should_exist): | |||
| 397 | 397 | ||
| 398 | 398 | def create_package(file, path, is_package=True, contents=()): | |
| 399 | 399 | class Reader(ResourceReader): | |
| 400 | + def get_resource_reader(self, package): | ||
| 401 | + return self | ||
| 402 | + | ||
| 400 | 403 | def open_resource(self, path): | |
| 401 | 404 | self._path = path | |
| 402 | 405 | if isinstance(file, Exception): | |
| Back | FazBrowse Home | New Git URL |
0 commit comments