Skip to content

Commit

Permalink
Remove usage of pkg_resources due to deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
ligangty committed Jul 10, 2024
1 parent 4e3e186 commit 49418f6
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 9 deletions.
5 changes: 3 additions & 2 deletions charon/utils/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import jsonschema
import yaml
from pkg_resources import resource_stream
import importlib

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -55,7 +55,8 @@ def load_schema(package, schema):
"""
# Read schema from file
try:
resource = resource_stream(package, schema)
resource = importlib.resources.files(package).joinpath(schema).open("rb")
# resource = resource_stream(package, schema)
schema = codecs.getreader('utf-8')(resource)
except ImportError:
logger.error('Unable to find package %s', package)
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ subresource-integrity>=0.2
jsonschema>=4.19.0
urllib3>=1.26.18
semantic-version>=2.10.0
setuptools>=70.0.0
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
"subresource-integrity>=0.2",
"jsonschema>=4.19.0",
"urllib3>=1.26.18",
"semantic-version>=2.10.0",
"setuptools>=70.0.0",
"semantic-version>=2.10.0"
],
)
8 changes: 4 additions & 4 deletions tests/utils/test_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import os

import jsonschema
import pkg_resources
import pytest
import yaml
from flexmock import flexmock
Expand Down Expand Up @@ -65,16 +64,17 @@ def test_read_yaml_bad_package(caplog):
assert 'Unable to find package bad_package' in caplog.text


@pytest.mark.skip(reason="removed pkg_resources, use importlib instead")
def test_read_yaml_file_bad_extract(tmpdir, caplog):
class FakeProvider(object):
def get_resource_stream(self, pkg, rsc):
raise IOError

# pkg_resources.resource_stream() cannot be mocked directly
# Instead mock the module-level function it calls.
(flexmock(pkg_resources)
.should_receive('get_provider')
.and_return(FakeProvider()))
# (flexmock(pkg_resources)
# .should_receive('get_provider')
# .and_return(FakeProvider()))

config_path = os.path.join(str(tmpdir), 'config.yaml')
with open(config_path, 'w'):
Expand Down

0 comments on commit 49418f6

Please sign in to comment.