Skip to content

Commit

Permalink
replace usage of deprecate pkg_resources
Browse files Browse the repository at this point in the history
  • Loading branch information
tdesveaux committed Oct 17, 2024
1 parent 9121604 commit 7d0abfd
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
9 changes: 4 additions & 5 deletions nimp/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@
'''Abstract class for commands'''

import abc
import argparse
import logging
import os
import pkg_resources
import re
import sys
import argparse

import nimp.base_commands
import nimp.command

from nimp.utils.python import get_class_instances
from nimp.utils.python import iter_plugins_entry_points


class Command(metaclass=abc.ABCMeta):
Expand Down Expand Up @@ -142,12 +141,12 @@ def discover(env):
pass

# Import commands from plugins
for entry_point in pkg_resources.iter_entry_points('nimp.plugins'):
for entry_point in iter_plugins_entry_points():
try:
module = entry_point.load()
get_class_instances(module, nimp.command.Command, all_commands)
except Exception as exception:
logging.debug("Failed to get platforms from plugin %s", entry_point.module_name, exc_info=exception)
logging.debug("Failed to get platforms from plugin %s", entry_point.module, exc_info=exception)

env.command_list = sorted(
[it for it in all_commands.values() if not it.__class__.__name__.startswith('_')],
Expand Down
11 changes: 5 additions & 6 deletions nimp/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,20 @@
values and command line parameters set for this nimp execution'''

import argparse
import re
import logging
import os
import pkg_resources
import re
import sys
import time

import nimp.command
from nimp.exceptions import NimpCommandFailed
import nimp.summary
import nimp.sys.platform
import nimp.system
import nimp.unreal
import nimp.utils.profiling

from nimp.exceptions import NimpCommandFailed
from nimp.utils.python import iter_plugins_entry_points

_LOG_FORMATS = { # pylint: disable = invalid-name
'standard': '%(asctime)s [%(levelname)s] %(message)s'
Expand Down Expand Up @@ -394,8 +393,8 @@ def execute_hook(hook_name, *args):
# Always look for project level hook first
hook_module = nimp.system.try_import('hooks.' + hook_name)
if hook_module is None: # If none found, try plugins level
for entry in pkg_resources.iter_entry_points('nimp.plugins'):
hook_module = nimp.system.try_import(entry.module_name + '.hooks.' + hook_name)
for entry in iter_plugins_entry_points():
hook_module = nimp.system.try_import(entry.module + '.hooks.' + hook_name)
if hook_module:
break
if hook_module is None:
Expand Down
7 changes: 3 additions & 4 deletions nimp/sys/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import abc
import logging
import platform
import pkg_resources

import nimp.base_platforms

from nimp.utils.python import get_class_instances
from nimp.utils.python import iter_plugins_entry_points

_all_platforms = {}
_all_aliases = {}
Expand Down Expand Up @@ -74,12 +73,12 @@ def discover(env):
discovered_platforms = {}
get_class_instances(nimp.base_platforms, Platform, discovered_platforms, instance_args=[env])

for module_entrypoint in pkg_resources.iter_entry_points('nimp.plugins'):
for module_entrypoint in iter_plugins_entry_points():
try:
module = module_entrypoint.load()
get_class_instances(module, Platform, discovered_platforms, instance_args=[env])
except Exception as exception:
logging.debug("Failed to get platforms from plugin %s", module_entrypoint.module_name, exc_info=exception)
logging.debug("Failed to get platforms from plugin %s", module_entrypoint.module, exc_info=exception)

for platform_instance in discovered_platforms.values():
# Set env.is_win32, env.is_linux, etc. to False by default
Expand Down
8 changes: 4 additions & 4 deletions nimp/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@
'''System utilities (paths, processes)'''

import fnmatch
import importlib
import json
import logging
import os
import re
import shutil
import stat
import time
import importlib
import pkg_resources

import glob2

import nimp.environment
import nimp.sys.platform
import nimp.sys.process
from nimp.utils.python import iter_plugins_entry_points


def try_import(module_name):
Expand Down Expand Up @@ -329,9 +329,9 @@ def load_set(self, set_name):
try:
set_module = importlib.import_module('filesets.' + set_module_name)
except ModuleNotFoundError:
for entry in pkg_resources.iter_entry_points('nimp.plugins'):
for entry in iter_plugins_entry_points():
try:
set_module = importlib.import_module(entry.module_name + '.filesets.' + set_module_name)
set_module = importlib.import_module(entry.module + '.filesets.' + set_module_name)
break
except ModuleNotFoundError:
pass
Expand Down
12 changes: 12 additions & 0 deletions nimp/utils/python.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
'''Helper functions for module handling'''

from __future__ import annotations

import inspect
import logging
from importlib.metadata import entry_points
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from importlib.metadata import EntryPoint
from typing import Generator


def get_class_instances(module, instance_type, result, instance_args=[], instance_kwargs={}):
Expand Down Expand Up @@ -32,3 +40,7 @@ def get_class_instances(module, instance_type, result, instance_args=[], instanc
result[attribute_value.__name__] = attribute_value(*instance_args, **instance_kwargs)
except Exception as ex:
logging.warning('Error creating %s %s: %s', instance_type.__name__, attribute_value.__name__, ex)


def iter_plugins_entry_points() -> Generator[EntryPoint, None, None]:
yield from entry_points().select(group="nimp.plugins")

0 comments on commit 7d0abfd

Please sign in to comment.