Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/openalea/core/model_inout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -366,31 +366,33 @@ 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):
# Search something with
# - 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):
# Search something with
# - 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):
# Search something with
# - 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):
Expand Down
29 changes: 15 additions & 14 deletions src/openalea/core/pkgmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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).
Expand Down
18 changes: 9 additions & 9 deletions src/openalea/core/plugin/formatting/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down Expand Up @@ -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']
Expand All @@ -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'
Expand All @@ -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__'):
Expand Down
10 changes: 6 additions & 4 deletions src/openalea/core/plugin/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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)
Expand Down
19 changes: 15 additions & 4 deletions src/openalea/core/plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__

Expand All @@ -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():
Expand All @@ -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)
Expand All @@ -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)):
Expand Down
13 changes: 2 additions & 11 deletions src/openalea/core/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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]
Expand All @@ -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

Expand Down
Loading