Skip to content

Commit

Permalink
refactor: use importlib instead of pkg_resources (#738)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsstevenson authored May 24, 2024
1 parent f62865c commit 0b128ba
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies=[
"biocommons.seqrepo >= 0.6.5",
"bioutils >= 0.4.0,<1.0",
"configparser >= 3.3.0",
"importlib_resources",
"ipython",
"parsley",
"psycopg2",
Expand Down
7 changes: 3 additions & 4 deletions src/hgvs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@
import re
import sys
import warnings

import pkg_resources
from importlib import metadata

from .config import global_config # noqa (importing symbol)

Expand All @@ -71,11 +70,11 @@

_is_released_version = False
try:
__version__ = pkg_resources.get_distribution("hgvs").version
__version__ = metadata.version("hgvs")
# TODO: match other valid release tags, like .post\d+
if re.match(r"^\d+\.\d+\.\d+$", __version__) is not None:
_is_released_version = True
except pkg_resources.DistributionNotFound:
except metadata.PackageNotFoundError:
warnings.warn("can't get __version__ because %s package isn't installed" % __package__, Warning)
__version__ = None

Expand Down
10 changes: 6 additions & 4 deletions src/hgvs/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
import re
from configparser import ConfigParser, ExtendedInterpolation
from copy import copy

from pkg_resources import resource_stream
try:
from importlib.resources import files as resources_files
except ImportError:
from importlib_resources import files as resources_files

logger = logging.getLogger(__name__)

Expand All @@ -53,7 +55,7 @@ def __init__(self, extended_interpolation=True):

def read_stream(self, flo):
"""read configuration from ini-formatted file-like object"""
self._cp.read_string(flo.read().decode("ascii"))
self._cp.read_string(flo.open("rb").read().decode("ascii"))

def __copy__(self):
new_config = Config.__new__(Config)
Expand Down Expand Up @@ -116,6 +118,6 @@ def _val_xform(v):


_default_config = Config()
_default_config.read_stream(resource_stream(__name__, "_data/defaults.ini"))
_default_config.read_stream(resources_files(__package__) / "_data" / "defaults.ini")

global_config = copy(_default_config)
8 changes: 5 additions & 3 deletions tests/test_hgvs_grammar_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import re
import unittest
from sys import version_info

import pkg_resources
try:
from importlib.resources import files as resources_files
except ImportError:
from importlib_resources import files as resources_files

#
# Tests of the grammar
Expand Down Expand Up @@ -52,7 +54,7 @@ def test_parser_test_completeness(self):
"""ensure that all rules in grammar have tests"""

grammar_rule_re = re.compile(r"^(\w+)")
grammar_fn = pkg_resources.resource_filename("hgvs", "_data/hgvs.pymeta")
grammar_fn = resources_files("hgvs") / "_data" / "hgvs.pymeta"
with open(grammar_fn, "r") as f:
grammar_rules = set(r.group(1) for r in filter(None, map(grammar_rule_re.match, f)))

Expand Down

0 comments on commit 0b128ba

Please sign in to comment.