1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

teach plugin loader to find modules in subdirectories

This commit is contained in:
Michael DeHaan 2013-04-28 15:22:46 -04:00
parent 391fb98ee2
commit 68f711d5ae

View file

@ -71,16 +71,20 @@ class PluginLoader(object):
ret.append(i) ret.append(i)
return os.pathsep.join(ret) return os.pathsep.join(ret)
def _get_package_path(self): def _get_package_paths(self):
''' Gets the path of a Python package ''' ''' Gets the path of a Python package '''
paths = []
if not self.package: if not self.package:
return [] return []
if not hasattr(self, 'package_path'): if not hasattr(self, 'package_path'):
m = __import__(self.package) m = __import__(self.package)
parts = self.package.split('.')[1:] parts = self.package.split('.')[1:]
self.package_path = os.path.join(os.path.dirname(m.__file__), *parts) self.package_path = os.path.join(os.path.dirname(m.__file__), *parts)
return [self.package_path] paths.append(self.package_path)
return paths
else:
return [ self.package_path ]
def _get_paths(self): def _get_paths(self):
''' Return a list of paths to search for plugins in ''' ''' Return a list of paths to search for plugins in '''
@ -95,14 +99,23 @@ class PluginLoader(object):
if os.path.isdir(fullpath): if os.path.isdir(fullpath):
files = glob.glob("%s/*" % fullpath) files = glob.glob("%s/*" % fullpath)
for file in files: for file in files:
if os.path.isdir(file): if os.path.isdir(file) and file not in ret:
ret.append(file) ret.append(file)
else: else:
if fullpath not in ret: if fullpath not in ret:
ret.append(fullpath) ret.append(fullpath)
ret += self.config.split(os.pathsep) # look in any configured plugin paths, allow one level deep for subcategories
ret += self._get_package_path() configured_paths = self.config.split(os.pathsep)
for path in configured_paths:
ret.append(path)
contents = glob.glob("%s/*" % path)
for c in contents:
if os.path.isdir(c):
ret.append(c)
# look for any plugins installed in the package subtree
ret.extend(self._get_package_paths())
self._paths = ret self._paths = ret