Skip to content

Commit

Permalink
migrate pkg_resources -> importlib.resources (#146)
Browse files Browse the repository at this point in the history
* replace pkg_resources -> importlib.resources

* use importlib_resources for backward compat
  • Loading branch information
anilbey authored Mar 6, 2024
1 parent 2cbe11b commit f12e6fe
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
6 changes: 3 additions & 3 deletions bluecellulab/cell/ballstick/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""A simple ball and stick model."""

import pkg_resources
import importlib_resources as resources
from bluecellulab.cell import Cell


def create_ball_stick() -> Cell:
"""Creates a ball and stick model by using package's hoc and asc."""
hoc = pkg_resources.resource_filename("bluecellulab", "cell/ballstick/emodel.hoc")
morphology = pkg_resources.resource_filename("bluecellulab", "cell/ballstick/morphology.asc")
hoc = resources.files("bluecellulab") / "cell/ballstick/emodel.hoc"
morphology = resources.files("bluecellulab") / "cell/ballstick/morphology.asc"
return Cell(template_path=hoc, morphology_path=morphology)
4 changes: 2 additions & 2 deletions bluecellulab/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
# limitations under the License.
"""Main importer of bluecellulab."""

import importlib_resources as resources
import logging
import os
from types import ModuleType
import pkg_resources

import neuron

Expand Down Expand Up @@ -62,7 +62,7 @@ def import_neurodamus(neuron: ModuleType) -> None:
]

for hoc_file in hoc_files:
hoc_file_path = pkg_resources.resource_filename("bluecellulab", f"hoc/{hoc_file}")
hoc_file_path = str(resources.files("bluecellulab") / f"hoc/{hoc_file}")
neuron.h.load_file(hoc_file_path)


Expand Down
11 changes: 6 additions & 5 deletions tests/test_importer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
from pathlib import Path
import pytest
from types import ModuleType
from unittest.mock import MagicMock, patch
Expand Down Expand Up @@ -51,16 +52,16 @@ def test_import_mod_lib_no_env_with_folder():


@patch.object(
importer.pkg_resources,
"resource_filename",
return_value="/fake/path/to/hoc_file.hoc",
importer.resources,
"files",
return_value=Path("/fake/path/"),
)
def test_import_neurodamus(mocked_pkg_resources):
def test_import_neurodamus(mocked_resources):
mock_neuron = MagicMock()
importer.import_neurodamus(mock_neuron)
assert mock_neuron.h.load_file.called
# Check that it was called with the expected arguments
mock_neuron.h.load_file.assert_any_call("/fake/path/to/hoc_file.hoc")
mock_neuron.h.load_file.assert_any_call("/fake/path/hoc/Cell.hoc")


def test_print_header(caplog):
Expand Down

0 comments on commit f12e6fe

Please sign in to comment.