Skip to content

Commit

Permalink
fix(core): use caller's package name in plugin loading
Browse files Browse the repository at this point in the history
In the `core.py` file, the plugin loading mechanism has been refactored to extract the package name of the caller rather than using a hardcoded package name. The necessary utility functions to get the package name and to check if a plugin is importable have been imported from the `packages` utility file. The logger has also been used to log information or debug messages about which plugins are being loaded and whether they are importable.
  • Loading branch information
entelecheia committed Aug 18, 2023
1 parent a638659 commit 04156be
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/hyfi/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pydantic import BaseModel

from hyfi.utils.logging import LOGGING
from hyfi.utils.packages import PKGs

logger = LOGGING.getLogger(__name__)

Expand Down Expand Up @@ -152,30 +153,37 @@ def secrets_dir(self) -> str:
@property
def plugins(self) -> Optional[List[str]]:
"""Returns the list of plugins to load."""
if self.__plugins__ and self.__package_name__ in self.__plugins__:
return self.__plugins__[self.__package_name__]
caller_pkg_name = PKGs.get_next_level_caller_package_name()
if self.__plugins__ and caller_pkg_name in self.__plugins__:
logger.info(
"Loading plugins for %s: %s",
caller_pkg_name,
self.__plugins__[caller_pkg_name],
)
return self.__plugins__[caller_pkg_name]
return None

def init_plugins(self, plugins: List[str]) -> Dict[str, List[str]]:
"""Returns the list of plugins to load.
A plugin is a python module which contains a configuration module.
Be careful!
It does not check if the plugin is importable.
When several plugins are specified and those plugins also use plugins,
the order of the plugins is important.
To handle this, need to store the plugins in the dictionary. (TODO)
ex) plugins = {'__package_name__': ['plugin1.conf', 'plugin2.conf']}
Args:
plugins: List[str]: A list of plugins to load.
Returns:
Dict[str, List[str]]: A dictionary of plugins to load. ex) plugins = {'__package_name__': ['plugin1.conf', 'plugin2.conf']}
"""
caller_pkg_name = PKGs.get_next_level_caller_package_name()
_plugins = []
for plugin in plugins:
plugin = plugin.split(".")[0]
config_module = f"{plugin}.{self.__config_dirname__}"
if PKGs.is_importable(plugin):
logger.debug("Plugin %s is importable.", plugin)
config_module = f"{plugin}.{self.__config_dirname__}"
else:
logger.debug("Plugin %s is not importable.", plugin)
_plugins.append(config_module)
return {self.__package_name__: _plugins}
return {caller_pkg_name: _plugins}

@property
def package_name(self) -> str:
Expand Down

0 comments on commit 04156be

Please sign in to comment.