-
-
Notifications
You must be signed in to change notification settings - Fork 48
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
Support non-package modules. #258
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
94d59b6
Add test capturing current behavior and missed expectation. Ref #203.
jaraco 5b1281d
Remove restriction that a 'package' cannot be a module. Allows resolu…
jaraco a60496b
Update changelog. Ref #203.
jaraco 8c3edeb
Update docs to prefer the name 'anchor' to 'package', as the anchor m…
jaraco 091f229
Extend type spec for clarity.
jaraco 4c672fb
Correct the changelog to mention the correct function affected.
jaraco eb7f123
Rename the 'package' parameter to 'anchor'.
jaraco 4e63613
Add doctests to flesh out coverage.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ omit = | |
*/_itertools.py | ||
*/_legacy.py | ||
*/simple.py | ||
*/_path.py | ||
|
||
[report] | ||
show_missing = True |
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
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,50 @@ | ||
import pathlib | ||
import functools | ||
|
||
|
||
#### | ||
# from jaraco.path 3.4 | ||
|
||
|
||
def build(spec, prefix=pathlib.Path()): | ||
""" | ||
Build a set of files/directories, as described by the spec. | ||
|
||
Each key represents a pathname, and the value represents | ||
the content. Content may be a nested directory. | ||
|
||
>>> spec = { | ||
... 'README.txt': "A README file", | ||
... "foo": { | ||
... "__init__.py": "", | ||
... "bar": { | ||
... "__init__.py": "", | ||
... }, | ||
... "baz.py": "# Some code", | ||
... } | ||
... } | ||
>>> tmpdir = getfixture('tmpdir') | ||
>>> build(spec, tmpdir) | ||
""" | ||
for name, contents in spec.items(): | ||
create(contents, pathlib.Path(prefix) / name) | ||
|
||
|
||
@functools.singledispatch | ||
def create(content, path): | ||
path.mkdir(exist_ok=True) | ||
build(content, prefix=path) # type: ignore | ||
|
||
|
||
@create.register | ||
def _(content: bytes, path): | ||
path.write_bytes(content) | ||
|
||
|
||
@create.register | ||
def _(content: str, path): | ||
path.write_text(content) | ||
|
||
|
||
# end from jaraco.path | ||
#### |
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
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.
This is surprising to me. I would expect, a
pathlib.Path('...', 'mod.py')
to be returned, requiringresources.files(mod).parent.joinpath('res.txt')
.res.txt
is not a resource ofmod
, but rather of the parent module, which might not even exist ifmod
is top level.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.
Thanks for sharing your surprise. Surely this would need some improved documentation to illustrate how
files()
now accepts anAnchor
(instead ofPackage
), where the anchor indicates:This is the behavior indicated by pkg_resources:
However, I've also observed that the pkg_resources implementation also allows for loading resources against unpackaged modules. And while that behavior may have been unintentional, it works because it's a natural consequence of relying on the importer/loader to resolve the resources from the same location as the indicated package/module. That is, because the importlib mechanism supports loading modules from outside packages, so too should resources be loadable from that location.
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.
A parent module may not exist, but a loader for that module does exist and can load resources adjacent to that module:
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.
In 1f3c226, I've updated the documentation to honor an
Anchor
type which need not be a package.