Skip to content

Commit 0fe7fc2

Browse files
author
Thomas Desveaux
committed
replace usage of deprecate pkg_resources
1 parent 7f48d95 commit 0fe7fc2

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

nimp/command.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
import abc
2626
import logging
2727
import os
28-
import pkg_resources
2928
import re
3029
import sys
3130
import argparse
3231

3332
import nimp.base_commands
3433
import nimp.command
3534

35+
from nimp.system import iter_plugins_entry_points
3636
from nimp.utils.python import get_class_instances
3737

3838

@@ -142,12 +142,12 @@ def discover(env):
142142
pass
143143

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

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

nimp/environment.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import re
2828
import logging
2929
import os
30-
import pkg_resources
3130
import sys
3231
import time
3332

@@ -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 nimp.system.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,10 +3,9 @@
33
import abc
44
import logging
55
import platform
6-
import pkg_resources
76

87
import nimp.base_platforms
9-
8+
from nimp.system import iter_plugins_entry_points
109
from nimp.utils.python import get_class_instances
1110

1211
_all_platforms = {}
@@ -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: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,30 @@
2222

2323
'''System utilities (paths, processes)'''
2424

25+
from __future__ import annotations
26+
2527
import fnmatch
28+
import importlib
2629
import json
2730
import logging
2831
import os
2932
import re
3033
import shutil
3134
import stat
3235
import time
33-
import importlib
34-
import pkg_resources
36+
from importlib.metadata import entry_points
37+
from typing import TYPE_CHECKING
3538

3639
import glob2
3740

3841
import nimp.environment
3942
import nimp.sys.platform
4043
import nimp.sys.process
4144

45+
if TYPE_CHECKING:
46+
from importlib.metadata import EntryPoint
47+
from typing import Generator
48+
4249

4350
def try_import(module_name):
4451
'''Tries to import a module, return none if unavailable'''
@@ -54,6 +61,12 @@ def try_import(module_name):
5461
return None
5562

5663

64+
def iter_plugins_entry_points() -> Generator[EntryPoint, None, None]:
65+
plugins_ep = entry_points().get('nimp.plugins')
66+
if plugins_ep is not None:
67+
yield from plugins_ep
68+
69+
5770
def try_execute(action, exception_types, attempt_maximum=5, retry_delay=10):
5871
'''Attempts to execute an action, and retries when catching one of the specified exceptions'''
5972
attempt = 1
@@ -329,9 +342,9 @@ def load_set(self, set_name):
329342
try:
330343
set_module = importlib.import_module('filesets.' + set_module_name)
331344
except ModuleNotFoundError:
332-
for entry in pkg_resources.iter_entry_points('nimp.plugins'):
345+
for entry in iter_plugins_entry_points():
333346
try:
334-
set_module = importlib.import_module(entry.module_name + '.filesets.' + set_module_name)
347+
set_module = importlib.import_module(entry.module + '.filesets.' + set_module_name)
335348
break
336349
except ModuleNotFoundError:
337350
pass

0 commit comments

Comments
 (0)