Skip to content

refactor: Removed deprecation by switching to importlib.metadata #1254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion datajoint/condition.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" methods for generating SQL WHERE clauses from datajoint restriction conditions """
"""methods for generating SQL WHERE clauses from datajoint restriction conditions"""

import collections
import datetime
Expand Down
36 changes: 25 additions & 11 deletions datajoint/plugin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from importlib.metadata import distribution, entry_points
from pathlib import Path

import pkg_resources
from cryptography.exceptions import InvalidSignature
from otumat import hash_pkg, verify

Expand All @@ -13,12 +13,18 @@
def _update_error_stack(plugin_name):
try:
base_name = "datajoint"
base_meta = pkg_resources.get_distribution(base_name)
plugin_meta = pkg_resources.get_distribution(plugin_name)
base_meta = distribution(base_name)
plugin_meta = distribution(plugin_name)

data = hash_pkg(pkgpath=str(Path(plugin_meta.module_path, plugin_name)))
signature = plugin_meta.get_metadata(f"{plugin_name}.sig")
pubkey_path = str(Path(base_meta.egg_info, f"{base_name}.pub"))
# Get the package location - equivalent to module_path
plugin_location = plugin_meta.locate_file("")
data = hash_pkg(pkgpath=str(Path(plugin_location.parent, plugin_name)))

# Get signature metadata - equivalent to get_metadata()
signature = plugin_meta.read_text(f"{plugin_name}.sig")

# Get public key path - equivalent to egg_info
pubkey_path = str(Path(base_meta.locate_file("").parent, f"{base_name}.pub"))
verify(pubkey_path=pubkey_path, data=data, signature=signature)
logger.info(f"DataJoint verified plugin `{plugin_name}` detected.")
return True
Expand All @@ -28,17 +34,25 @@ def _update_error_stack(plugin_name):


def _import_plugins(category):
# Get entry points for the group - equivalent to iter_entry_points()
group_name = f"datajoint_plugins.{category}"
eps = entry_points()

# Handle both Python 3.9 and 3.10+ entry points API
if hasattr(eps, "select"):
group_eps = eps.select(group=group_name)
else:
group_eps = eps.get(group_name, [])

return {
entry_point.name: dict(
object=entry_point,
verified=_update_error_stack(entry_point.module_name.split(".")[0]),
)
for entry_point in pkg_resources.iter_entry_points(
"datajoint_plugins.{}".format(category)
verified=_update_error_stack(entry_point.module.split(".")[0]),
)
for entry_point in group_eps
if "plugin" not in config
or category not in config["plugin"]
or entry_point.module_name.split(".")[0] in config["plugin"][category]
or entry_point.module.split(".")[0] in config["plugin"][category]
}


Expand Down
2 changes: 1 addition & 1 deletion datajoint/preview.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" methods for generating previews of query expression results in python command line and Jupyter """
"""methods for generating previews of query expression results in python command line and Jupyter"""

from .settings import config

Expand Down
6 changes: 3 additions & 3 deletions docs/src/design/tables/indexes.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ To make searches faster on fields other than the primary key or a foreign key, y
add a secondary index explicitly.

Regular indexes are declared as `index(attr1, ..., attrN)` on a separate line anywhere in
the table declration (below the primary key divide).
the table declaration (below the primary key divide).

Indexes can be declared with unique constraint as `unique index (attr1, ..., attrN)`.

Expand All @@ -62,7 +62,7 @@ Let’s now imagine that rats in a lab are identified by the combination of `lab
@schema
class Rat(dj.Manual):
definition = """
lab_name : char(16)
lab_name : char(16)
rat_id : int unsigned # lab-specific ID
---
date_of_birth = null : date
Expand All @@ -86,7 +86,7 @@ To speed up searches by the `rat_id` and `date_of_birth`, we can explicit indexe
@schema
class Rat2(dj.Manual):
definition = """
lab_name : char(16)
lab_name : char(16)
rat_id : int unsigned # lab-specific ID
---
date_of_birth = null : date
Expand Down
6 changes: 3 additions & 3 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from importlib.metadata import distribution
from os import path

import pkg_resources
import pytest

import datajoint.errors as djerr
Expand All @@ -10,8 +10,8 @@
@pytest.mark.skip(reason="marked for deprecation")
def test_check_pubkey():
base_name = "datajoint"
base_meta = pkg_resources.get_distribution(base_name)
pubkey_meta = base_meta.get_metadata("{}.pub".format(base_name))
base_dist = distribution(base_name)
pubkey_meta = base_dist.read_text("{}.pub".format(base_name))

with open(
path.join(path.abspath(path.dirname(__file__)), "..", "datajoint.pub"), "r"
Expand Down