-
-
Notifications
You must be signed in to change notification settings - Fork 49
Replace legacy API implementation with files() #221
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bd4d6ff
use files() API in contents()
FFY00 4ff68cf
_adapters: replace DegenerateFiles with CompatibilityFiles
FFY00 53fc1c6
use files() api in open_* and read_*
FFY00 30cf1a9
use files() api in is_resource()
FFY00 b1c38ab
compat: fix selecting the correct reader if the spec is None
FFY00 62e905c
use files() api in path()
FFY00 c2d85c9
remove _py3 implementation
FFY00 5e8c1d2
common: ignore PermissionError in _tempfile
FFY00 97083d2
tests: add tests for CompatibiltyFiles
FFY00 ef2f7c2
tests: add test for CompatibilityFiles wrap_spec
FFY00 e016e66
legacy: move legacy API to separate module
FFY00 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,85 @@ | ||
import os | ||
import pathlib | ||
import types | ||
|
||
from typing import Union, Iterable, ContextManager | ||
from typing.io import BinaryIO, TextIO | ||
|
||
from . import _common | ||
|
||
Package = Union[types.ModuleType, str] | ||
Resource = Union[str, os.PathLike] | ||
|
||
|
||
def open_binary(package: Package, resource: Resource) -> BinaryIO: | ||
"""Return a file-like object opened for binary reading of the resource.""" | ||
return (_common.files(package) / _common.normalize_path(resource)).open('rb') | ||
|
||
|
||
def read_binary(package: Package, resource: Resource) -> bytes: | ||
"""Return the binary contents of the resource.""" | ||
return (_common.files(package) / _common.normalize_path(resource)).read_bytes() | ||
|
||
|
||
def open_text( | ||
package: Package, | ||
resource: Resource, | ||
encoding: str = 'utf-8', | ||
errors: str = 'strict', | ||
) -> TextIO: | ||
"""Return a file-like object opened for text reading of the resource.""" | ||
return (_common.files(package) / _common.normalize_path(resource)).open( | ||
'r', encoding=encoding, errors=errors | ||
) | ||
|
||
|
||
def read_text( | ||
package: Package, | ||
resource: Resource, | ||
encoding: str = 'utf-8', | ||
errors: str = 'strict', | ||
) -> str: | ||
"""Return the decoded string of the resource. | ||
|
||
The decoding-related arguments have the same semantics as those of | ||
bytes.decode(). | ||
""" | ||
with open_text(package, resource, encoding, errors) as fp: | ||
return fp.read() | ||
|
||
|
||
def contents(package: Package) -> Iterable[str]: | ||
"""Return an iterable of entries in `package`. | ||
|
||
Note that not all entries are resources. Specifically, directories are | ||
not considered resources. Use `is_resource()` on each entry returned here | ||
to check if it is a resource or not. | ||
""" | ||
return [path.name for path in _common.files(package).iterdir()] | ||
|
||
|
||
def is_resource(package: Package, name: str) -> bool: | ||
"""True if `name` is a resource inside `package`. | ||
|
||
Directories are *not* resources. | ||
""" | ||
resource = _common.normalize_path(name) | ||
return any( | ||
traversable.name == resource and traversable.is_file() | ||
for traversable in _common.files(package).iterdir() | ||
) | ||
|
||
|
||
def path( | ||
package: Package, | ||
resource: Resource, | ||
) -> ContextManager[pathlib.Path]: | ||
"""A context manager providing a file path object to the resource. | ||
|
||
If the resource does not already exist on its own on the file system, | ||
a temporary file will be created. If the file was created, the file | ||
will be deleted upon exiting the context manager (no exception is | ||
raised if the file was deleted prior to the context manager | ||
exiting). | ||
""" | ||
return _common.as_file(_common.files(package) / _common.normalize_path(resource)) |
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.
Uh oh!
There was an error while loading. Please reload this page.