Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to reduce cost of module imports, by only importing files tha… #21

Merged
merged 5 commits into from
Jul 7, 2022
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
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ jobs:
- name: Build documentation
run: |
cd docs
pip install -r requirements.txt
make html
4 changes: 4 additions & 0 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ def test_registered_plugins():
plugins = Plugins()
found_shell = False
found_rsync = False
found_globus = False
for plugin in plugins.registered:
if plugin == "shell":
found_shell = True
elif plugin == "rsync":
found_rsync = True
elif plugin == "globus":
found_globus = True

assert found_shell
assert found_rsync
assert found_globus


@pytest.mark.unit
Expand Down
47 changes: 23 additions & 24 deletions zambeze/orchestration/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,17 @@ def __init__(self, logger: Optional[logging.Logger] = None) -> None:
self.__logger: logging.Logger = (
logging.getLogger(__name__) if logger is None else logger
)
self.__module_names = []
self._plugins = {}
self.__registerPlugins()

def __registerPlugins(self) -> None:
"""Will register all the plugins provided in the plugin_modules folder"""
self._plugins = {}
module_names = []

plugin_path = [str(Path(__file__).resolve().parent) + "/plugin_modules"]
for importer, module_name, ispkg in pkgutil.walk_packages(path=plugin_path):
if module_name == "abstract_plugin":
continue

module = import_module(
f"zambeze.orchestration.plugin_modules.{module_name}"
)
module_names.append(module_name)
for attribute_name in dir(module):
potential_plugin = getattr(module, attribute_name)
if isclass(potential_plugin):
if (
issubclass(potential_plugin, Plugin)
and attribute_name != "Plugin"
):
self._plugins[attribute_name.lower()] = potential_plugin(
logger=self.__logger
)
self.__logger.debug(f"Registered Plugins: {', '.join(module_names)}")
if module_name != "abstract_plugin" and module_name != "__init__":
self.__module_names.append(module_name)
self.__logger.debug(f"Registered Plugins: {', '.join(self.__module_names)}")

@property
def registered(self) -> list[Plugin]:
Expand All @@ -90,9 +74,9 @@ def registered(self) -> list[Plugin]:
>>> shell
>>> rsync
"""
plugins: list[Plugin] = []
for key in self._plugins:
plugins.append(deepcopy(key))
plugins: list[str] = []
for module_name in self.__module_names:
plugins.append(deepcopy(module_name))
return plugins

def configure(self, config: dict):
Expand Down Expand Up @@ -129,6 +113,21 @@ def configure(self, config: dict):

This will just configure the "shell" plugin
"""
for module_name in self.__module_names:
module = import_module(
f"zambeze.orchestration.plugin_modules.{module_name}"
)
for attribute_name in dir(module):
potential_plugin = getattr(module, attribute_name)
if isclass(potential_plugin):
if (
issubclass(potential_plugin, Plugin)
and attribute_name != "Plugin"
):
self._plugins[attribute_name.lower()] = potential_plugin(
logger=self.__logger
)

for key in self._plugins:
if key in config.keys():
obj = self._plugins.get(key)
Expand Down