Skip to content

Commit cc2fd5c

Browse files
author
Thomas Desveaux
committed
replace usage of deprecate pkg_resources
1 parent e5fceed commit cc2fd5c

File tree

5 files changed

+34
-19
lines changed

5 files changed

+34
-19
lines changed

nimp/command.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,16 @@
2323
'''Abstract class for commands'''
2424

2525
import abc
26+
import argparse
2627
import logging
2728
import os
28-
import pkg_resources
2929
import re
3030
import sys
31-
import argparse
3231

3332
import nimp.base_commands
3433
import nimp.command
35-
3634
from nimp.utils.python import get_class_instances
35+
from nimp.utils.python import iter_plugins_entry_points
3736

3837

3938
class Command(metaclass=abc.ABCMeta):
@@ -142,12 +141,12 @@ def discover(env):
142141
pass
143142

144143
# Import commands from plugins
145-
for entry_point in pkg_resources.iter_entry_points('nimp.plugins'):
144+
for entry_point in iter_plugins_entry_points():
146145
try:
147146
module = entry_point.load()
148147
get_class_instances(module, nimp.command.Command, all_commands)
149148
except Exception as exception:
150-
logging.debug("Failed to get platforms from plugin %s", entry_point.module_name, exc_info=exception)
149+
logging.debug("Failed to get platforms from plugin %s", entry_point.module, exc_info=exception)
151150

152151
env.command_list = sorted(
153152
[it for it in all_commands.values() if not it.__class__.__name__.startswith('_')],

nimp/environment.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,20 @@
2424
values and command line parameters set for this nimp execution'''
2525

2626
import argparse
27-
import re
2827
import logging
2928
import os
30-
import pkg_resources
29+
import re
3130
import sys
3231
import time
3332

3433
import nimp.command
35-
from nimp.exceptions import NimpCommandFailed
3634
import nimp.summary
3735
import nimp.sys.platform
3836
import nimp.system
3937
import nimp.unreal
4038
import nimp.utils.profiling
41-
39+
from nimp.exceptions import NimpCommandFailed
40+
from nimp.utils.python import iter_plugins_entry_points
4241

4342
_LOG_FORMATS = { # pylint: disable = invalid-name
4443
'standard': '%(asctime)s [%(levelname)s] %(message)s'
@@ -394,8 +393,8 @@ def execute_hook(hook_name, *args):
394393
# Always look for project level hook first
395394
hook_module = nimp.system.try_import('hooks.' + hook_name)
396395
if hook_module is None: # If none found, try plugins level
397-
for entry in pkg_resources.iter_entry_points('nimp.plugins'):
398-
hook_module = nimp.system.try_import(entry.module_name + '.hooks.' + hook_name)
396+
for entry in iter_plugins_entry_points():
397+
hook_module = nimp.system.try_import(entry.module + '.hooks.' + hook_name)
399398
if hook_module:
400399
break
401400
if hook_module is None:

nimp/sys/platform.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
import abc
44
import logging
55
import platform
6-
import pkg_resources
76

87
import nimp.base_platforms
9-
108
from nimp.utils.python import get_class_instances
9+
from nimp.utils.python import iter_plugins_entry_points
1110

1211
_all_platforms = {}
1312
_all_aliases = {}
@@ -74,12 +73,12 @@ def discover(env):
7473
discovered_platforms = {}
7574
get_class_instances(nimp.base_platforms, Platform, discovered_platforms, instance_args=[env])
7675

77-
for module_entrypoint in pkg_resources.iter_entry_points('nimp.plugins'):
76+
for module_entrypoint in iter_plugins_entry_points():
7877
try:
7978
module = module_entrypoint.load()
8079
get_class_instances(module, Platform, discovered_platforms, instance_args=[env])
8180
except Exception as exception:
82-
logging.debug("Failed to get platforms from plugin %s", module_entrypoint.module_name, exc_info=exception)
81+
logging.debug("Failed to get platforms from plugin %s", module_entrypoint.module, exc_info=exception)
8382

8483
for platform_instance in discovered_platforms.values():
8584
# Set env.is_win32, env.is_linux, etc. to False by default

nimp/system.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@
2323
'''System utilities (paths, processes)'''
2424

2525
import fnmatch
26+
import importlib
2627
import json
2728
import logging
2829
import os
2930
import re
3031
import shutil
3132
import stat
3233
import time
33-
import importlib
34-
import pkg_resources
3534

3635
import glob2
3736

3837
import nimp.environment
3938
import nimp.sys.platform
4039
import nimp.sys.process
40+
from nimp.utils.python import iter_plugins_entry_points
4141

4242

4343
def try_import(module_name):
@@ -329,9 +329,9 @@ def load_set(self, set_name):
329329
try:
330330
set_module = importlib.import_module('filesets.' + set_module_name)
331331
except ModuleNotFoundError:
332-
for entry in pkg_resources.iter_entry_points('nimp.plugins'):
332+
for entry in iter_plugins_entry_points():
333333
try:
334-
set_module = importlib.import_module(entry.module_name + '.filesets.' + set_module_name)
334+
set_module = importlib.import_module(entry.module + '.filesets.' + set_module_name)
335335
break
336336
except ModuleNotFoundError:
337337
pass

nimp/utils/python.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
'''Helper functions for module handling'''
22

3+
from __future__ import annotations
4+
35
import inspect
46
import logging
7+
from importlib.metadata import entry_points
8+
from typing import TYPE_CHECKING
9+
10+
if TYPE_CHECKING:
11+
from importlib.metadata import EntryPoint
12+
from typing import Generator
513

614

715
def get_class_instances(module, instance_type, result, instance_args=[], instance_kwargs={}):
@@ -32,3 +40,13 @@ def get_class_instances(module, instance_type, result, instance_args=[], instanc
3240
result[attribute_value.__name__] = attribute_value(*instance_args, **instance_kwargs)
3341
except Exception as ex:
3442
logging.warning('Error creating %s %s: %s', instance_type.__name__, attribute_value.__name__, ex)
43+
44+
45+
def iter_plugins_entry_points() -> Generator[EntryPoint, None, None]:
46+
eps = entry_points()
47+
if hasattr(eps, "select"):
48+
yield from eps.select(group="nimp.plugins")
49+
elif hasattr(eps, "get"):
50+
yield from eps.get("nimp.plugins", [])
51+
else:
52+
raise NotImplementedError(f"unexpected value for importlib.metadata.entry_points ({type(eps)})")

0 commit comments

Comments
 (0)