Skip to content
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

upgrade: replace deprecated pkg_resources with importlib, resolves #966 #977

2 changes: 2 additions & 0 deletions requirements/base.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ mypy
pycryptodome>=3.17.0
pyyaml>=6.0
typing-extensions>=4.4.0
importlib-metadata>=7.0.1
importlib-resources>=6.1.1
Abdul-Muqadim-Arbisoft marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions tutor.spec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- mode: python -*-
import importlib
import os
import pkg_resources
from importlib_metadata import entry_points

block_cipher = None

Expand All @@ -10,7 +10,7 @@ hidden_imports = []

# Auto-discover plugins and include patches & templates folders
for entrypoint_version in ["tutor.plugin.v0", "tutor.plugin.v1"]:
for entrypoint in pkg_resources.iter_entry_points(entrypoint_version):
for entrypoint in entry_points().select(group=entrypoint_version):
plugin_name = entrypoint.name
try:
plugin = entrypoint.load()
Expand Down
6 changes: 4 additions & 2 deletions tutor/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
from copy import deepcopy

import jinja2
import pkg_resources
from importlib_resources import files, as_file
Abdul-Muqadim-Arbisoft marked this conversation as resolved.
Show resolved Hide resolved

from tutor import exceptions, fmt, hooks, plugins, utils
from tutor.__about__ import __app__, __version__
from tutor.types import Config, ConfigValue

TEMPLATES_ROOT = pkg_resources.resource_filename("tutor", "templates")
ref = files("tutor") / "templates"
with as_file(ref) as path:
Abdul-Muqadim-Arbisoft marked this conversation as resolved.
Show resolved Hide resolved
TEMPLATES_ROOT = str(path)
VERSION_FILENAME = "version"
BIN_FILE_EXTENSIONS = [".ico", ".jpg", ".patch", ".png", ".ttf", ".woff", ".woff2"]
JinjaFilter = t.Callable[..., t.Any]
Expand Down
13 changes: 6 additions & 7 deletions tutor/plugins/v0.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from glob import glob

import click
import pkg_resources
from importlib_metadata import EntryPoint, entry_points, Distribution

from tutor import env, exceptions, fmt, hooks, serialize
from tutor.__about__ import __app__
Expand Down Expand Up @@ -246,12 +246,12 @@ class EntrypointPlugin(BasePlugin):

ENTRYPOINT = "tutor.plugin.v0"

def __init__(self, entrypoint: pkg_resources.EntryPoint) -> None:
self.loader: pkg_resources.EntryPoint
def __init__(self, entrypoint: EntryPoint) -> None:
self.loader: EntryPoint = entrypoint
super().__init__(entrypoint.name, entrypoint)

def _load_obj(self) -> None:
self.obj = self.loader.load()
self.obj = self.loader.load() # type: ignore[no-untyped-call]
regisb marked this conversation as resolved.
Show resolved Hide resolved

def _version(self) -> t.Optional[str]:
if not self.loader.dist:
Expand All @@ -260,12 +260,11 @@ def _version(self) -> t.Optional[str]:

@classmethod
def discover_all(cls) -> None:
for entrypoint in pkg_resources.iter_entry_points(cls.ENTRYPOINT):
entrypoints = entry_points().select(group=cls.ENTRYPOINT) # type: ignore[no-untyped-call]
for entrypoint in entrypoints:
try:
error: t.Optional[str] = None
cls(entrypoint)
except pkg_resources.VersionConflict as e:
error = e.report()
except Exception as e: # pylint: disable=broad-except
error = str(e)
if error:
Expand Down
11 changes: 6 additions & 5 deletions tutor/plugins/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
from glob import glob

import pkg_resources
from importlib_metadata import entry_points, EntryPoint

from tutor import hooks

Expand All @@ -26,7 +26,7 @@ def _discover_entrypoint_plugins() -> None:
"""
with hooks.Contexts.PLUGINS.enter():
if "TUTOR_IGNORE_ENTRYPOINT_PLUGINS" not in os.environ:
for entrypoint in pkg_resources.iter_entry_points("tutor.plugin.v1"):
for entrypoint in entry_points().select(group="tutor.plugin.v1"): # type: ignore[no-untyped-call]
discover_package(entrypoint)


Expand Down Expand Up @@ -56,7 +56,7 @@ def load(plugin_name: str) -> None:
spec.loader.exec_module(module)


def discover_package(entrypoint: pkg_resources.EntryPoint) -> None:
def discover_package(entrypoint: EntryPoint) -> None:
"""
Install a plugin from a python package.
"""
Expand All @@ -68,10 +68,11 @@ def discover_package(entrypoint: pkg_resources.EntryPoint) -> None:
# Add plugin information
if entrypoint.dist is None:
raise ValueError(f"Could not read plugin version: {name}")
hooks.Filters.PLUGINS_INFO.add_item((name, entrypoint.dist.version))
dist_version = entrypoint.dist.version if entrypoint.dist else "Unknown"
hooks.Filters.PLUGINS_INFO.add_item((name, dist_version))

# Import module on enable
@hooks.Actions.PLUGIN_LOADED.add()
def load(plugin_name: str) -> None:
if name == plugin_name:
entrypoint.load()
entrypoint.load() # type: ignore[no-untyped-call]
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@
warnings.filterwarnings("ignore", category=DeprecationWarning, module="wiki.plugins.links.wiki_plugin")
warnings.filterwarnings("ignore", category=DeprecationWarning, module="boto.plugin")
warnings.filterwarnings("ignore", category=DeprecationWarning, module="botocore.vendored.requests.packages.urllib3._collections")
warnings.filterwarnings("ignore", category=DeprecationWarning, module="pkg_resources")
Abdul-Muqadim-Arbisoft marked this conversation as resolved.
Show resolved Hide resolved
warnings.filterwarnings("ignore", category=DeprecationWarning, module="fs")
warnings.filterwarnings("ignore", category=DeprecationWarning, module="fs.opener")
SILENCED_SYSTEM_CHECKS = ["2_0.W001", "fields.W903"]
Expand Down