From a16dbed4035c31e09c9471dd67df612a95c3644e Mon Sep 17 00:00:00 2001 From: pradal Date: Tue, 16 Sep 2025 10:21:59 +0200 Subject: [PATCH 1/2] Remove pkg_resource dependency --- src/openalea/core/pkgmanager.py | 29 +++++++++++---------- src/openalea/core/plugin/formatting/text.py | 18 ++++++------- src/openalea/core/plugin/manager.py | 10 ++++--- src/openalea/core/plugin/plugin.py | 19 +++++++++++--- 4 files changed, 45 insertions(+), 31 deletions(-) diff --git a/src/openalea/core/pkgmanager.py b/src/openalea/core/pkgmanager.py index 53f94489..f021861b 100644 --- a/src/openalea/core/pkgmanager.py +++ b/src/openalea/core/pkgmanager.py @@ -46,7 +46,10 @@ import six.moves.urllib.parse from openalea.core.path import path from fnmatch import fnmatch -from pkg_resources import iter_entry_points + +# Remove old dependency to openalea +#from pkg_resources import iter_entry_points +from importlib.metadata import entry_points from openalea.core.singleton import Singleton from openalea.core.observer import Observed @@ -245,12 +248,12 @@ def set_sys_wralea_path(self): if DEBUG: res = {} - # Use setuptools entry_point - for epoint in iter_entry_points("wralea"): - # Get Deprecated packages + # Deprecated : Use setuptools entry_point + # Replace by importlib metadat + for epoint in entry_points(group="wralea"): if self.verbose: - pmanLogger.debug(epoint.name + " " + epoint.module_name) - if(epoint.module_name == "deprecated"): + pmanLogger.debug(epoint.name + " " + epoint.module) + if(epoint.module == "deprecated"): self.deprecated_pkg.add(epoint.name.lower()) continue @@ -261,28 +264,26 @@ def set_sys_wralea_path(self): # Be careful, this lines will import __init__.py and all its predecessor # to find the path. if DEBUG: - print(epoint.module_name) + print(epoint.module) t1 = clock() try: - m = importlib.import_module(epoint.module_name) + m = importlib.import_module(epoint.module) #m = __import__(epoint.module_name, fromlist=epoint.module_name) except ImportError as e: - logger.error("Cannot load %s : %s" % (epoint.module_name, e)) - # self.log.add("Cannot load %s : %s"%(epoint.module_name, e)) + logger.error("Cannot load %s : %s" % (epoint.module, e)) continue if DEBUG: - print((epoint.module_name)) + print((epoint.module)) tn = clock() - t1 - res[tn] = epoint.module_name + res[tn] = epoint.module l = list(m.__path__) for p in l: p = os.path.abspath(p) - logger.info("Wralea entry point: %s (%s) " % (epoint.module_name, p)) - # self.log.add("Wralea entry point: %s (%s) "%(epoint.module_name, p)) + logger.info("Wralea entry point: %s (%s) " % (epoint.module, p)) self.add_wralea_path(p, self.sys_wralea_path) # Search the path based on the old method (by hand). diff --git a/src/openalea/core/plugin/formatting/text.py b/src/openalea/core/plugin/formatting/text.py index 2769ad72..fb12a5ad 100644 --- a/src/openalea/core/plugin/formatting/text.py +++ b/src/openalea/core/plugin/formatting/text.py @@ -22,7 +22,9 @@ unicode = str from openalea.core.plugin.plugin import plugin_name - +from openalea.core.plugin.manager import PluginManager +from openalea.core.plugin import iter_groups +from importlib.metadata import entry_points def format_author(author, key=None, **kwds): @@ -108,10 +110,8 @@ def format_criterion(criterion, value, indent=0): def list_plugins(lst, verbose=False): - from openalea.core.plugin.manager import PluginManager - pm = PluginManager() - import pkg_resources - from openalea.core.plugin import iter_groups + + pm = PluginManager() if lst in ['summary', 'all']: prefixes = ['openalea', 'oalab', 'vpltk'] @@ -124,13 +124,13 @@ def list_plugins(lst, verbose=False): match = True break if match: - eps = [ep for ep in pkg_resources.iter_entry_points(group)] + eps = [ep for ep in entry_points(group=group)] if lst == 'summary': print('\n\033[91m%s\033[0m (%d plugins)' % (group, len(eps))) for ep in eps: - parts = [str(s) for s in (ep.module_name, ep.name)] + parts = [str(s) for s in (ep.module, ep.name)] identifier = ':'.join(parts) - print(' - %s \033[90m%s (%s)\033[0m' % (ep.name, identifier, ep.dist.egg_name())) + print(' - %s \033[90m%s (%s)\033[0m' % (ep.name, identifier, ep.dist.name)) else: print('\033[44m%s\033[0m' % group) UNDEF = 'Not defined' @@ -155,7 +155,7 @@ def list_plugins(lst, verbose=False): if verbose: print(' tags: %s' % ', '.join(plugin.tags)) print(' plugin: %s, egg: %s\n path: %s' % ( - ep.name, ep.dist.egg_name(), ep.dist.location)) + ep.name, ep.dist.name, ep.dist.locate_file(''))) print(' criteria:') for crit_name in sorted(dir(plugin)): if crit_name in ('implementation', '__call__'): diff --git a/src/openalea/core/plugin/manager.py b/src/openalea/core/plugin/manager.py index b1778fb4..3f82ec55 100644 --- a/src/openalea/core/plugin/manager.py +++ b/src/openalea/core/plugin/manager.py @@ -25,7 +25,9 @@ import inspect from warnings import warn import importlib -from pkg_resources import iter_entry_points +# Deprecated : replace by importlib metadata +#from pkg_resources import iter_entry_points +from importlib.metadata import entry_points from openalea.core import logger from openalea.core.manager import GenericManager @@ -104,7 +106,7 @@ def generate_item_id(self, plugin): def discover(self, group=None, item_proxy=None): if "entry_points" in self._autoload: - for ep in iter_entry_points(group): + for ep in entry_points(group=group): self._load_entry_point_plugin(group, ep, item_proxy=item_proxy) def instantiate(self, item): @@ -127,7 +129,7 @@ def patch_item(self, item): def patch_ep_plugin(self, plugin, ep): plugin.plugin_ep = ep.name - plugin.plugin_modulename = ep.module_name + plugin.plugin_modulename = ep.module plugin.plugin_dist = ep.dist def _is_plugin_class(self, obj): @@ -154,7 +156,7 @@ def _add_plugin_from_ep(self, group, ep, plugin_class, plugin_proxy=None): for plugin_class in plugin_classes: name = plugin_class.name if hasattr(plugin_class, 'name') else plugin_class.__name__ - parts = [str(s) for s in (ep.dist.egg_name(), group, ep.module_name, ep.name, name)] + parts = [str(s) for s in (ep.dist.name, group, ep.module, ep.name, name)] identifier = ':'.join(parts) item = self.add(plugin_class, group, item_proxy=plugin_proxy, identifier=identifier) self.patch_ep_plugin(item, ep) diff --git a/src/openalea/core/plugin/plugin.py b/src/openalea/core/plugin/plugin.py index 5057509e..650cd4f2 100644 --- a/src/openalea/core/plugin/plugin.py +++ b/src/openalea/core/plugin/plugin.py @@ -29,13 +29,24 @@ """ -import pkg_resources +#import pkg_resources +from importlib.metadata import entry_points, distribution, PackageNotFoundError + + import site import sys from openalea.core.factory import AbstractFactory import six + +def find_distribution(name): + try: + return distribution(name) + except PackageNotFoundError: + return None + + def plugin_name(plugin): return plugin.name if hasattr(plugin, 'name') else plugin.__name__ @@ -53,7 +64,7 @@ def discover(group, name=None): :todo: check that the same name is not used by several plugins """ - plugin_map = {ep.name:ep for ep in pkg_resources.iter_entry_points(group, name)} + plugin_map = {ep.name:ep for ep in entry_points(group=group, name=name)} return plugin_map def iter_groups(): @@ -67,7 +78,7 @@ def iter_groups(): paths += sys.path # scan all entry_point and list different groups for path in set(paths): - distribs = pkg_resources.find_distributions(path) + distribs = find_distribution(path) for distrib in distribs : for group in distrib.get_entry_map(): groups.add(group) @@ -76,7 +87,7 @@ def iter_groups(): def iter_plugins(group, name=None, debug=False): - for ep in pkg_resources.iter_entry_points(group, name): + for ep in entry_points(group, name): if debug is True or debug == 'all' or debug == group: ep = ep.load() if isinstance(ep, (list, tuple)): From 3f821db2667362062cf230249efb8219a388f738 Mon Sep 17 00:00:00 2001 From: pradal Date: Tue, 16 Sep 2025 10:45:56 +0200 Subject: [PATCH 2/2] fix warning for regexp --- src/openalea/core/model_inout.py | 20 +++++++++++--------- src/openalea/core/signature.py | 13 ++----------- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/openalea/core/model_inout.py b/src/openalea/core/model_inout.py index 61000912..cfc21e96 100644 --- a/src/openalea/core/model_inout.py +++ b/src/openalea/core/model_inout.py @@ -295,11 +295,11 @@ def parse_input_and_output(docstring): docsplit = docstring.splitlines() for line in docsplit: line = line.strip() - if re.search('^#*\s*input\s*=', line): + if re.search(r'^#*\s*input\s*=', line): line = "input".join(line.split('input')[1:]) line = "=".join(line.split('=', 1)[1:]).strip() inputs = _safe_split(line) - if re.search('^#*\s*output\s*=', line): + if re.search(r'^#*\s*output\s*=', line): line = "output".join(line.split('output')[1:]) line = line.split('=', 1)[1].strip() outputs = _safe_split(line) @@ -339,15 +339,15 @@ def extract_functions(codestring, filename='tmp'): ################################################# def _replace_regex(line, regex, replaced="_"): - """ + ''' Search *regex* inside *line* and replace it by *replaced* :return: line replaced :use: >>> line = "a=(1,2,3), b=[1,2], c=4, d=([1,2,3],4)" - >>> _replace_regex(line, "\([A-Za-z0-9_,()]*\)", "_") + >>> _replace_bracket(line) >>> "a=_______, b=[1,2], c=4, d=___________" - """ + ''' line2 = line search = re.search(regex, line2) while search is not None: @@ -366,7 +366,7 @@ def _replace_bracket(line): # - one opening bracket ( # - what you want # - one closing bracket ) - return _replace_regex(line, "\([A-Za-z0-9_,()]*\)") + return _replace_regex(line, r"\([A-Za-z0-9_,()]*\)") def _replace_square_bracket(line): @@ -374,7 +374,7 @@ def _replace_square_bracket(line): # - one opening square bracket [ # - what you want # - one closing square bracket ] - return _replace_regex(line, "\[[A-Za-z0-9_,()]*\]") + return _replace_regex(line, r"\[[A-Za-z0-9_,()]*\]") def _replace_quoted(line): @@ -382,7 +382,8 @@ def _replace_quoted(line): # - one opening quote ' # - what you want # - one closing quote ' - return _replace_regex(line, "\'\s*([^\"]*?)\s*\'") + pattern = r"'\s*([^\"]*?)\s*'" + return _replace_regex(line, pattern) def _replace_double_quoted(line): @@ -390,7 +391,8 @@ def _replace_double_quoted(line): # - one opening quote " # - what you want # - one closing quote " - return _replace_regex(line, "\"\s*([^\"]*?)\s*\"") + pattern = r'"\s*([^"]*?)\s*"' + return _replace_regex(line, pattern) def _safe_split(line): diff --git a/src/openalea/core/signature.py b/src/openalea/core/signature.py index aeb4fd29..988f5218 100644 --- a/src/openalea/core/signature.py +++ b/src/openalea/core/signature.py @@ -29,12 +29,6 @@ import six from six.moves import zip -# Fix Error in 3.11 -INSPECT_FULL=False -if not hasattr(inspect, 'getargspec'): - inspect.getargspec = inspect.getfullargspec - INSPECT_FULL=True - class Signature: """Object to represent the signature of a function/method. @@ -166,7 +160,7 @@ def get_callable_arguments(function): # different behavior in Python 3 isMethod = inspect.ismethod(function) if isMethod or inspect.isfunction(function): - argspec = inspect.getargspec(function) + argspec = inspect.getfullargspec(function) if argspec.defaults: ndefs = len(argspec.defaults) args = argspec.args[:-ndefs] @@ -175,10 +169,7 @@ def get_callable_arguments(function): args = argspec.args defaults = [] - if not INSPECT_FULL: - keywords = argspec.keywords - else: - keywords = argspec.varkw + keywords = argspec.varkw return args, defaults, argspec.varargs, keywords, isMethod