mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Merge pull request #15095 from jjshoe/sort_plugins
Sort plugins by basename to support ordering callbacks
This commit is contained in:
commit
2f118e546f
1 changed files with 27 additions and 25 deletions
|
@ -348,36 +348,38 @@ class PluginLoader:
|
||||||
''' instantiates all plugins with the same arguments '''
|
''' instantiates all plugins with the same arguments '''
|
||||||
|
|
||||||
class_only = kwargs.pop('class_only', False)
|
class_only = kwargs.pop('class_only', False)
|
||||||
|
all_matches = []
|
||||||
|
|
||||||
for i in self._get_paths():
|
for i in self._get_paths():
|
||||||
matches = glob.glob(os.path.join(i, "*.py"))
|
all_matches.extend(glob.glob(os.path.join(i, "*.py")))
|
||||||
matches.sort()
|
|
||||||
for path in matches:
|
|
||||||
name, _ = os.path.splitext(path)
|
|
||||||
if '__init__' in name:
|
|
||||||
continue
|
|
||||||
|
|
||||||
if path not in self._module_cache:
|
for path in sorted(all_matches, key=lambda match: os.path.basename(match)):
|
||||||
self._module_cache[path] = self._load_module_source(name, path)
|
name, _ = os.path.splitext(path)
|
||||||
|
if '__init__' in name:
|
||||||
|
continue
|
||||||
|
|
||||||
obj = getattr(self._module_cache[path], self.class_name)
|
if path not in self._module_cache:
|
||||||
if self.base_class:
|
self._module_cache[path] = self._load_module_source(name, path)
|
||||||
# The import path is hardcoded and should be the right place,
|
|
||||||
# so we are not expecting an ImportError.
|
|
||||||
module = __import__(self.package, fromlist=[self.base_class])
|
|
||||||
# Check whether this obj has the required base class.
|
|
||||||
try:
|
|
||||||
plugin_class = getattr(module, self.base_class)
|
|
||||||
except AttributeError:
|
|
||||||
continue
|
|
||||||
if not issubclass(obj, plugin_class):
|
|
||||||
continue
|
|
||||||
|
|
||||||
if not class_only:
|
obj = getattr(self._module_cache[path], self.class_name)
|
||||||
obj = obj(*args, **kwargs)
|
if self.base_class:
|
||||||
|
# The import path is hardcoded and should be the right place,
|
||||||
|
# so we are not expecting an ImportError.
|
||||||
|
module = __import__(self.package, fromlist=[self.base_class])
|
||||||
|
# Check whether this obj has the required base class.
|
||||||
|
try:
|
||||||
|
plugin_class = getattr(module, self.base_class)
|
||||||
|
except AttributeError:
|
||||||
|
continue
|
||||||
|
if not issubclass(obj, plugin_class):
|
||||||
|
continue
|
||||||
|
|
||||||
# set extra info on the module, in case we want it later
|
if not class_only:
|
||||||
setattr(obj, '_original_path', path)
|
obj = obj(*args, **kwargs)
|
||||||
yield obj
|
|
||||||
|
# set extra info on the module, in case we want it later
|
||||||
|
setattr(obj, '_original_path', path)
|
||||||
|
yield obj
|
||||||
|
|
||||||
action_loader = PluginLoader(
|
action_loader = PluginLoader(
|
||||||
'ActionModule',
|
'ActionModule',
|
||||||
|
|
Loading…
Reference in a new issue