Skip to content

Commit

Permalink
Merge pull request #92 from fernandodpr/main
Browse files Browse the repository at this point in the history
Fix plugin loader to correctly activate plugins
  • Loading branch information
jeremiah-k authored Nov 7, 2024
2 parents 3751c4d + 13dcdf2 commit e8b65a6
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions plugin_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def load_plugins_from_directory(directory, recursive=False):
logger.warning(f"Directory {directory} does not exist.")
return plugins


def load_plugins():
global sorted_active_plugins
if sorted_active_plugins:
Expand All @@ -100,7 +101,7 @@ def load_plugins():
from plugins.debug_plugin import Plugin as DebugPlugin

# Initial list of core plugins
plugins = [
core_plugins = [
HealthPlugin(),
MapPlugin(),
MeshRelayPlugin(),
Expand All @@ -113,6 +114,8 @@ def load_plugins():
DebugPlugin(),
]

plugins = core_plugins.copy()

# Load custom plugins (non-recursive)
custom_plugins_dir = os.path.join(os.path.dirname(__file__), 'plugins', 'custom')
plugins.extend(load_plugins_from_directory(custom_plugins_dir, recursive=False))
Expand All @@ -128,7 +131,7 @@ def load_plugins():
if repo_url:
clone_or_update_repo(repo_url, tag, community_plugins_dir)
else:
logger.error(f"Repository URL not specified for a community plugin")
logger.error("Repository URL not specified for a community plugin")
logger.error("Please specify the repository URL in config.yaml")
sys.exit(1)

Expand All @@ -138,13 +141,33 @@ def load_plugins():
# Filter and sort active plugins by priority
active_plugins = []
for plugin in plugins:
if plugin.config.get("active", False):
plugin.priority = plugin.config.get("priority", plugin.priority)
plugin_name = getattr(plugin, 'plugin_name', plugin.__class__.__name__)

# Determine if the plugin is active based on the configuration
if plugin in core_plugins:
# Core plugins: default to active unless specified otherwise
plugin_config = config.get('plugins', {}).get(plugin_name, {})
is_active = plugin_config.get("active", True)
else:
# Custom and community plugins: default to inactive unless specified
if plugin_name in config.get('custom-plugins', {}):
plugin_config = config.get('custom-plugins', {}).get(plugin_name, {})
elif plugin_name in community_plugins_config:
plugin_config = community_plugins_config.get(plugin_name, {})
else:
plugin_config = {}

is_active = plugin_config.get("active", False)

if is_active:
plugin.priority = plugin_config.get("priority", getattr(plugin, 'priority', 100))
active_plugins.append(plugin)
try:
plugin.start()
except Exception as e:
logger.error(f"Error starting plugin {plugin}: {e}")
logger.error(f"Error starting plugin {plugin_name}: {e}")
else:
logger.info(f"Plugin '{plugin_name}' is inactive or not configured, skipping")

sorted_active_plugins = sorted(active_plugins, key=lambda plugin: plugin.priority)
return sorted_active_plugins
return sorted_active_plugins

0 comments on commit e8b65a6

Please sign in to comment.