Skip to content

Commit

Permalink
Use importlib instead of pkg_resources
Browse files Browse the repository at this point in the history
pkg_resources is now deprecated, as importlib has covered
most use-cases since python 3.8.
  • Loading branch information
ndevenish committed Jun 29, 2023
1 parent 6c6d665 commit 4bebe48
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 17 deletions.
11 changes: 3 additions & 8 deletions libtbx_refresh.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import contextlib
import importlib
import inspect
import io
import os
Expand All @@ -17,11 +18,6 @@
except ModuleNotFoundError:
pass

try:
import pkg_resources
except ModuleNotFoundError:
pkg_resources = None


def _install_setup_readonly_fallback(package_name: str):
"""
Expand Down Expand Up @@ -67,9 +63,8 @@ def _install_setup_readonly_fallback(package_name: str):
if import_path not in sys.path:
sys.path.insert(0, import_path)

# ...and add to the existing pkg_resources working_set
if pkg_resources:
pkg_resources.working_set.add_entry(import_path)
# ...and make sure it is picked up by the import system
importlib.invalidate_caches()

# This is already generated by this point, but will get picked up
# on the second libtbx.refresh.
Expand Down
1 change: 1 addition & 0 deletions newsfragments/XXX.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Move from deprecated pkg_resources to importlib.
5 changes: 2 additions & 3 deletions src/dxtbx/format/Registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
"""
from __future__ import annotations

import importlib.metadata
import os
import typing
from typing import Callable

import pkg_resources

from dxtbx.util import get_url_scheme

if typing.TYPE_CHECKING:
Expand All @@ -35,7 +34,7 @@ def get_format_class_index() -> dict[str, tuple[Callable[[], type[Format]], list
"""
if not hasattr(get_format_class_index, "cache"):
class_index = {}
for e in pkg_resources.iter_entry_points("dxtbx.format"):
for e in importlib.metadata.entry_points(group="dxtbx.format"):
if ":" in e.name:
format_name, base_classes_str = e.name.split(":", 1)
base_classes = tuple(base_classes_str.split(","))
Expand Down
7 changes: 4 additions & 3 deletions src/dxtbx/model/experiment_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import collections
import copy
import errno
import importlib.metadata
import itertools
import json
import logging
Expand All @@ -11,8 +12,6 @@
import pickle
from typing import Any, Callable, Generator, Iterable

import pkg_resources

import dxtbx.datablock
from dxtbx.datablock import (
BeamComparison,
Expand Down Expand Up @@ -467,7 +466,9 @@ def _lookup_model(self, name, experiment_dict):
@staticmethod
def _scaling_model_from_dict(obj):
"""Get the scaling model from a dictionary."""
for entry_point in pkg_resources.iter_entry_points("dxtbx.scaling_model_ext"):
for entry_point in importlib.metadata.entry_points(
group="dxtbx.scaling_model_ext"
):
if entry_point.name == obj["__id__"]:
return entry_point.load().from_dict(obj)

Expand Down
5 changes: 2 additions & 3 deletions src/dxtbx/model/profile.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from __future__ import annotations

import importlib.metadata
import logging

import pkg_resources


class ProfileModelFactory:
"""
Expand All @@ -17,7 +16,7 @@ def from_dict(obj):
"""
if obj is None:
return None
for entry_point in pkg_resources.iter_entry_points("dxtbx.profile_model"):
for entry_point in importlib.metadata.entry_points(group="dxtbx.profile_model"):
if entry_point.name == obj["__id__"]:
return entry_point.load().from_dict(obj)
logging.getLogger("dxtbx.model.profile").warn(
Expand Down

0 comments on commit 4bebe48

Please sign in to comment.