Skip to content

Commit

Permalink
Replace pkg_resources.resource_stream
Browse files Browse the repository at this point in the history
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
victorlin committed Sep 20, 2023
1 parent 9e8c9e0 commit cb9cf73
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
27 changes: 27 additions & 0 deletions LICENSE.nextstrain-cli
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.
32 changes: 32 additions & 0 deletions augur/data/__init__.py
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)
7 changes: 3 additions & 4 deletions augur/util_support/color_parser.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from collections import defaultdict
from io import TextIOWrapper
import functools
from pkg_resources import resource_stream

from augur.data import as_file
from augur.util_support.color_parser_line import ColorParserLine


Expand All @@ -17,8 +16,8 @@ def mapping(self):
colors = {}

if self.use_defaults:
with resource_stream("augur", "data/colors.tsv") as stream:
with TextIOWrapper(stream, "utf-8") as defaults:
with as_file("colors.tsv") as file:
with open(file, encoding="utf-8") as defaults:
colors = {**colors, **self.parse_file(defaults)}

if self.mapping_filename:
Expand Down
7 changes: 3 additions & 4 deletions augur/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
import shlex
from contextlib import contextmanager
from collections import defaultdict, OrderedDict
from pkg_resources import resource_stream
from io import TextIOWrapper
from .__version__ import __version__

from augur.data import as_file
from augur.io.file import open_file

from augur.types import ValidationMode
Expand Down Expand Up @@ -247,8 +246,8 @@ def add_line_to_coordinates(line):
else:
print("WARNING: geo-coordinate file contains invalid line. Please make sure not to mix tabs and spaces as delimiters (use only tabs):",line)
if use_defaults:
with resource_stream(__package__, "data/lat_longs.tsv") as stream:
with TextIOWrapper(stream, "utf-8") as defaults:
with as_file("lat_longs.tsv") as file:
with open(file, encoding="utf-8") as defaults:
for line in defaults:
add_line_to_coordinates(line)
if overrides:
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"bcbio-gff >=0.7.0, ==0.7.*",
"biopython >=1.67, !=1.77, !=1.78",
"cvxopt >=1.1.9, ==1.*",
"importlib_resources >=5.3.0; python_version < '3.11'",
"isodate ==0.6.*",
"jsonschema >=3.0.0, ==3.*",
"networkx >= 2.5, ==2.*",
Expand Down

0 comments on commit cb9cf73

Please sign in to comment.