Skip to content

Commit

Permalink
replace pkg_rsources with importlib.metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-monch committed Oct 9, 2023
1 parent 7fd109b commit 5351793
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
34 changes: 19 additions & 15 deletions datalad_metalad/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"""
import json
import logging
import sys
import time
from os import curdir
from pathlib import (
Expand Down Expand Up @@ -489,42 +490,45 @@ def get_extractor_class(extractor_name: str) -> Union[
Type[FileMetadataExtractor]]:

""" Get an extractor from its name """
from pkg_resources import iter_entry_points
if sys.version_info < (3, 10):
from importlib_metadata import entry_points
else:
from importlib.metadata import entry_points

# The extractor class names of the old datalad-contained extractors have
# been changed, when the extractors were moved to datalad_metalad.
# Therefore, we have to use to extractors in
# `datalad_metalad.extractors.legacy` instead of any old extractor code
# from datalad core.
entry_points = [
entry_point_list = [
entry_point
for entry_point in iter_entry_points(
"datalad.metadata.extractors",
extractor_name
for entry_point in entry_points(
group="datalad.metadata.extractors",
name=extractor_name,
)
if entry_point.dist.project_name != "datalad"
if entry_point.dist.name != "datalad"
]

if not entry_points:
entry_points = [
if not entry_point_list:
entry_point_list = [
entry_point
for entry_point in iter_entry_points(
"datalad.metadata.extractors",
extractor_name
for entry_point in entry_points(
group="datalad.metadata.extractors",
name=extractor_name,
)
if entry_point.dist.project_name == "datalad"
if entry_point.dist.name == "datalad"
]

if not entry_points:
if not entry_point_list:
raise ExtractorNotFoundError(
"Requested metadata extractor '{}' not available".format(
extractor_name))

entry_point, ignored_entry_points = entry_points[-1], entry_points[:-1]
entry_point, ignored_entry_points = entry_point_list[-1], entry_point_list[:-1]
lgr.debug(
"Using metadata extractor %s from distribution %s",
extractor_name,
entry_point.dist.project_name)
entry_point.dist.name)

# Inform about overridden entry points
for ignored_entry_point in ignored_entry_points:
Expand Down
8 changes: 6 additions & 2 deletions datalad_metalad/extractors/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
"""Test all extractors at a basic level"""

from pkg_resources import iter_entry_points
import sys

import pytest
if sys.version_info < (3, 10):
from importlib_metadata import entry_points
else:
from importlib.metadata import entry_points

from datalad.api import (
Dataset,
Expand Down Expand Up @@ -47,7 +51,7 @@ def test_api(path=None, *, annex):
assert_repo_status(ds.path)

processed_extractors, skipped_extractors = [], []
for extractor_ep in iter_entry_points("datalad.metadata.extractors"):
for extractor_ep in entry_points(group="datalad.metadata.extractors"):

# There are a number of extractors that do not
# work on empty datasets, or datasets, or without
Expand Down
8 changes: 6 additions & 2 deletions datalad_metalad/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import json
import logging
from pathlib import Path
import sys
from typing import (
Dict,
Iterable,
Expand Down Expand Up @@ -285,10 +286,13 @@ def run_filter(filter_name: str,

def get_filter_class(filter_name: str) -> Type[MetadataFilterBase]:
""" Get a filter class from its name"""
from pkg_resources import iter_entry_points
if sys.version_info < (3, 10):
from importlib_metadata import entry_points
else:
from importlib.metadata import entry_points

entry_points = list(
iter_entry_points("datalad.metadata.filters", filter_name))
entry_points(group="datalad.metadata.filters", name=filter_name))

if not entry_points:
raise ValueError(
Expand Down
9 changes: 6 additions & 3 deletions datalad_metalad/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import json
import pkg_resources
import sys
from itertools import islice
from pathlib import Path
from typing import Dict, List, Union

if sys.version_info < (3, 10):
from importlib_resources import files
else:
from importlib.resources import files

from datalad.distribution.dataset import (
Dataset,
require_dataset,
Expand Down Expand Up @@ -82,8 +86,7 @@ def read_json_object(path_or_object: Union[str, JSONType]) -> JSONType:
else:
try:
json_object = json.loads(
pkg_resources.resource_string(
"datalad_metalad.pipeline",
files("datalad_metalad.pipeline").joinpath(
f"pipelines/{path_or_object}_pipeline.json"))
return json_object
except FileNotFoundError:
Expand Down

0 comments on commit 5351793

Please sign in to comment.