-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpo-39791 native hooks for importlib.resources.files #20576
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e42bf42
Provide native .files support on SourceFileLoader.
jaraco f8c96fa
Add native importlib.resources.files() support to zipimporter. Remove…
jaraco 6304bee
make regen-all
jaraco 948779a
📜🤖 Added by blurb_it.
blurb-it[bot] 3b59071
Merge branch 'master' into feature/39791-resources-files
jaraco e4d05ef
Move 'files' into the ResourceReader so it can carry the relevant mod…
jaraco e40ad3f
Create 'importlib.readers' module and add FileReader to it.
jaraco 5ad02bf
Add zip reader and rely on it for a TraversableResources object on zi…
jaraco 3efe34b
Remove TraversableAdapter, no longer needed.
jaraco 10bed14
Update blurb.
jaraco c44661b
Replace backslashes with forward slashes.
jaraco 2c5755a
Incorporate changes from importlib_metadata 2.0, finalizing the inter…
jaraco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import zipfile | ||
import pathlib | ||
from . import abc | ||
|
||
|
||
class FileReader(abc.TraversableResources): | ||
def __init__(self, loader): | ||
self.path = pathlib.Path(loader.path).parent | ||
|
||
def files(self): | ||
return self.path | ||
|
||
|
||
class ZipReader(FileReader): | ||
def __init__(self, loader, module): | ||
_, _, name = module.rpartition('.') | ||
prefix = loader.prefix.replace('\\', '/') + name + '/' | ||
self.path = zipfile.Path(loader.archive, prefix) | ||
|
||
def open_resource(self, resource): | ||
try: | ||
return super().open_resource(resource) | ||
except KeyError as exc: | ||
raise FileNotFoundError(exc.args[0]) | ||
|
||
def is_resource(self, path): | ||
# workaround for `zipfile.Path.is_file` returning true | ||
# for non-existent paths. | ||
target = self.files().joinpath(path) | ||
return target.is_file() and target.exists() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm probably going to decouple FileReader from ZipReader. The only shared functionality is the one-line
files
method.