-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace pkg_resources.resource_stream
There are several alternatives¹. Only one (importlib.resources.as_file + open) avoids reading the entire file into memory by using a temporary file on disk. ¹ https://stackoverflow.com/a/58941536
- Loading branch information
Showing
5 changed files
with
66 additions
and
8 deletions.
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
This license applies to the original copy of resource functions from the | ||
Nextstrain CLI project into this project, incorporated in | ||
"augur/data/__init__.py". Any subsequent modifications to this project's copy of | ||
those functions are licensed under the license of this project, not of | ||
Nextstrain CLI. | ||
|
||
MIT License | ||
|
||
Copyright (c) 2018–2021 Trevor Bedford and Richard Neher | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,32 @@ | ||
""" | ||
Resource files. | ||
""" | ||
# Originally copied from nextstrain/cli/resources/__init__.py in the Nextstrain | ||
# CLI project¹. | ||
# | ||
# ¹ <https://github.com/nextstrain/cli/blob/a5dda9c0579ece7acbd8e2c32a4bbe95df7c0bce/nextstrain/cli/resources/__init__.py> | ||
|
||
# We gate usage of the stdlib implementation on 3.11 because that's the first | ||
# version with a full adapter for making the new files() / Traversable API | ||
# backwards compatible with importers only providing the original path() / | ||
# ResourceReader API. The PyPI backport, on the other hand, contains the full | ||
# adapter since 5.3.0, which we declare as our minimum version in setup.py, so | ||
# we use that even on 3.9 and 3.10. | ||
# | ||
# We're using the new API at all because the original one is being deprecated | ||
# and we want to avoid warnings both from the stdlib implementation on 3.11 and | ||
# from the PyPI backport implementation on older Python versions. | ||
# -trs, 13 Sept 2022 | ||
import sys | ||
|
||
if sys.version_info >= (3, 11): | ||
from importlib.resources import files as _files, as_file as _as_file | ||
else: | ||
from importlib_resources import files as _files, as_file as _as_file | ||
|
||
from pathlib import Path | ||
from typing import ContextManager | ||
|
||
|
||
def as_file(path: str) -> ContextManager[Path]: | ||
return _as_file(_files(__name__) / 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
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