Reworking the way all() works in plugin_loader

* Don't throw away the full path of the module code being loaded,
  as this can cause conflicts when files of the same name are being
  instantiated
* Generalize the module loading code

Fixes #12738
This commit is contained in:
James Cammarata 2015-10-14 17:52:19 -04:00
parent 45b803efb4
commit d923d05a33

View file

@ -302,6 +302,11 @@ class PluginLoader:
__contains__ = has_plugin __contains__ = has_plugin
def _load_module_source(self, name, path):
with open(path, 'r') as module_file:
module = imp.load_source(name, path, module_file)
return module
def get(self, name, *args, **kwargs): def get(self, name, *args, **kwargs):
''' instantiates a plugin of the given name using arguments ''' ''' instantiates a plugin of the given name using arguments '''
@ -312,7 +317,7 @@ class PluginLoader:
return None return None
if path not in self._module_cache: if path not in self._module_cache:
self._module_cache[path] = imp.load_source('.'.join([self.package, name]), path) self._module_cache[path] = self._load_module_source('.'.join([self.package, name]), path)
if kwargs.get('class_only', False): if kwargs.get('class_only', False):
obj = getattr(self._module_cache[path], self.class_name) obj = getattr(self._module_cache[path], self.class_name)
@ -330,12 +335,12 @@ class PluginLoader:
matches = glob.glob(os.path.join(i, "*.py")) matches = glob.glob(os.path.join(i, "*.py"))
matches.sort() matches.sort()
for path in matches: for path in matches:
name, ext = os.path.splitext(os.path.basename(path)) name, _ = os.path.splitext(path)
if name.startswith("_"): if '__init__' in name:
continue continue
if path not in self._module_cache: if path not in self._module_cache:
self._module_cache[path] = imp.load_source('.'.join([self.package, name]), path) self._module_cache[path] = self._load_module_source(name, path)
if kwargs.get('class_only', False): if kwargs.get('class_only', False):
obj = getattr(self._module_cache[path], self.class_name) obj = getattr(self._module_cache[path], self.class_name)