Do not change sys.path during pluggables discovery
@@ -20,7 +20,6 @@ Methods for pluggable objects discovery, registration, information
"""
import logging
-import sys
import os
import pkgutil
import importlib
@@ -165,10 +164,18 @@ def _discover_entry_point_pluggables():
logger.debug("Failed to load entry point '{}' error {}".format(entry_point, str(e)))
-def _discover_local_pluggables_in_dirs(directory,
- parentname,
+def _discover_local_pluggables(directory=os.path.dirname(__file__),
+ parentname=os.path.splitext(__name__)[0],
names_to_exclude=_NAMES_TO_EXCLUDE,
folders_to_exclude=_FOLDERS_TO_EXCLUDE):
+ """
+ Discovers the pluggable modules on the directory and subdirectories of the current module
+ and attempts to register them. Pluggable modules should subclass Pluggable Base classes.
+ Args:
+ directory (str, optional): Directory to search for pluggable. Defaults
+ to the directory of this module.
+ parentname (str, optional): Module parent name. Defaults to current directory name
+ """
for _, name, ispackage in pkgutil.iter_modules([directory]):
if ispackage:
continue
@@ -202,38 +209,7 @@ def _discover_local_pluggables_in_dirs(directory,
for item in sorted(os.listdir(directory)):
fullpath = os.path.join(directory, item)
if item not in folders_to_exclude and not item.endswith('dSYM') and os.path.isdir(fullpath):
- _discover_local_pluggables_in_dirs(
- fullpath, parentname + '.' + item, names_to_exclude, folders_to_exclude)
-
-
-def _discover_local_pluggables(directory=os.path.dirname(__file__),
- parentname=os.path.splitext(__name__)[0],
- names_to_exclude=_NAMES_TO_EXCLUDE,
- folders_to_exclude=_FOLDERS_TO_EXCLUDE):
- """
- Discovers the pluggable modules on the directory and subdirectories of the current module
- and attempts to register them. Pluggable modules should subclass Pluggable Base classes.
- Args:
- directory (str, optional): Directory to search for pluggable. Defaults
- to the directory of this module.
- parentname (str, optional): Module parent name. Defaults to current directory name
- """
-
- def _get_sys_path(directory):
- syspath = [os.path.abspath(directory)]
- for item in os.listdir(directory):
- fullpath = os.path.join(directory, item)
- if item != '__pycache__' and not item.endswith('dSYM') and os.path.isdir(fullpath):
- syspath += _get_sys_path(fullpath)
-
- return syspath
-
- syspath_save = sys.path
- sys.path = sys.path + _get_sys_path(directory)
- try:
- _discover_local_pluggables_in_dirs(directory, parentname)
- finally:
- sys.path = syspath_save
+ _discover_local_pluggables(fullpath, parentname + '.' + item, names_to_exclude, folders_to_exclude)
def register_pluggable(cls):
