Skip to content

Commit

Permalink
Merge pull request #103 from jeremiah-k/plugin-loader-fixes2
Browse files Browse the repository at this point in the history
Ironing out the last of the plugin loading issues (hopefully)
  • Loading branch information
jeremiah-k authored Nov 26, 2024
2 parents 559c50e + 51c36cc commit 0d034fe
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from matrix_utils import on_room_message
from meshtastic_utils import connect_meshtastic
from meshtastic_utils import logger as meshtastic_logger
from plugin_loader import load_plugins
import plugin_loader # Changed from 'from plugin_loader import load_plugins'

# Initialize logger
logger = get_logger(name="M<>M Relay")
Expand All @@ -44,7 +44,7 @@ async def main():
initialize_database()

# Load plugins early
load_plugins()
plugin_loader.load_plugins() # Updated function call

# Connect to Meshtastic
meshtastic_utils.meshtastic_client = connect_meshtastic()
Expand Down
8 changes: 4 additions & 4 deletions matrix_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
from config import relay_config
from log_utils import get_logger

# Do not import plugin_loader here to avoid circular imports
from meshtastic_utils import connect_meshtastic

# Import plugin_loader module to access loaded plugins
import plugin_loader

# Extract Matrix configuration
matrix_homeserver = relay_config["matrix"]["homeserver"]
matrix_rooms: List[dict] = relay_config["matrix_rooms"]
Expand Down Expand Up @@ -228,9 +230,7 @@ async def on_room_message(
text = truncate_message(text)

# Plugin functionality
import plugin_loader # Import here to avoid circular imports

plugins = plugin_loader.load_plugins() # Load plugins within the function
plugins = plugin_loader.sorted_active_plugins # Use loaded plugins

found_matching_plugin = False
for plugin in plugins:
Expand Down
10 changes: 4 additions & 6 deletions meshtastic_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
from db_utils import get_longname, get_shortname, save_longname, save_shortname
from log_utils import get_logger

# Do not import plugin_loader here to avoid circular imports
# Import plugin_loader module to access loaded plugins
import plugin_loader

# Extract matrix rooms configuration
matrix_rooms: List[dict] = relay_config["matrix_rooms"]
Expand Down Expand Up @@ -233,9 +234,6 @@ def on_meshtastic_message(packet, interface):
"""
Handle incoming Meshtastic messages and relay them to Matrix.
"""
# Import plugin_loader module to avoid circular imports
import plugin_loader # Import here to avoid circular imports

from matrix_utils import matrix_relay

global event_loop
Expand Down Expand Up @@ -331,7 +329,7 @@ def on_meshtastic_message(packet, interface):
formatted_message = f"[{longname}/{meshnet_name}]: {text}"

# Plugin functionality
plugins = plugin_loader.load_plugins() # Load plugins within the function
plugins = plugin_loader.sorted_active_plugins # Use loaded plugins

found_matching_plugin = False
for plugin in plugins:
Expand Down Expand Up @@ -369,7 +367,7 @@ def on_meshtastic_message(packet, interface):
# Handle non-text messages via plugins
portnum = decoded.get("portnum")

plugins = plugin_loader.load_plugins()
plugins = plugin_loader.sorted_active_plugins # Use loaded plugins
found_matching_plugin = False
for plugin in plugins:
if not found_matching_plugin:
Expand Down
31 changes: 22 additions & 9 deletions plugin_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
from log_utils import get_logger

logger = get_logger(name="Plugins")
sorted_active_plugins = []
plugins_loaded = False

sorted_active_plugins = [] # Store the loaded plugins here

def clone_or_update_repo(repo_url, tag, plugins_dir):
# Extract the repository name from the URL
Expand Down Expand Up @@ -58,13 +57,17 @@ def clone_or_update_repo(repo_url, tag, plugins_dir):
sys.exit(1)


def load_plugins_from_directory(directory, recursive=False):
def load_plugins_from_directory(directory, recursive=False, plugin_names=None):
plugins = []
if os.path.isdir(directory):
for root, _dirs, files in os.walk(directory):
for filename in files:
if filename.endswith(".py"):
plugin_path = os.path.join(root, filename)
# Extract plugin name from file
plugin_file_name = os.path.splitext(filename)[0]
if plugin_names and plugin_file_name not in plugin_names:
continue # Skip loading this plugin
module_name = (
"plugin_"
+ hashlib.sha256(plugin_path.encode("utf-8")).hexdigest()
Expand Down Expand Up @@ -149,19 +152,26 @@ def load_plugins():
):
os.makedirs(community_plugins_dir, exist_ok=True)

for plugin_info in community_plugins_config.values():
active_community_plugins = []
for plugin_name, plugin_info in community_plugins_config.items():
if plugin_info.get("active", False):
repo_url = plugin_info.get("repository")
tag = plugin_info.get("tag", "master")
if repo_url:
clone_or_update_repo(repo_url, tag, community_plugins_dir)
active_community_plugins.append(plugin_name)
else:
logger.error("Repository URL not specified for a community plugin")
logger.error(f"Repository URL not specified for plugin {plugin_name}")
logger.error("Please specify the repository URL in config.yaml")
sys.exit(1)

# Load community plugins (recursive)
plugins.extend(load_plugins_from_directory(community_plugins_dir, recursive=True))
# Only load active community plugins
plugins.extend(
load_plugins_from_directory(
community_plugins_dir, recursive=True, plugin_names=active_community_plugins
)
)

# Filter and sort active plugins by priority
active_plugins = []
Expand All @@ -177,12 +187,13 @@ def load_plugins():
# 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, {})
is_active = plugin_config.get("active", False)
elif plugin_name in community_plugins_config:
plugin_config = community_plugins_config.get(plugin_name, {})
is_active = plugin_config.get("active", False)
else:
plugin_config = {}

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

if is_active:
plugin.priority = plugin_config.get(
Expand All @@ -195,4 +206,6 @@ def load_plugins():
logger.error(f"Error starting plugin {plugin_name}: {e}")

sorted_active_plugins = sorted(active_plugins, key=lambda plugin: plugin.priority)
plugins_loaded = True # Set the flag to indicate that plugins have been load
plugins_loaded = True # Set the flag to indicate that plugins have been loaded

return sorted_active_plugins

0 comments on commit 0d034fe

Please sign in to comment.