Skip to content

Commit

Permalink
Merge pull request #95 from jeremiah-k/plugins-path-fixes
Browse files Browse the repository at this point in the history
Add get_app_path() function
  • Loading branch information
jeremiah-k authored Nov 8, 2024
2 parents d72d9c4 + b3b8f3d commit 8f7937c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
22 changes: 20 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import yaml
from yaml.loader import SafeLoader
import sys
import os

def get_app_path():
"""
Returns the base directory of the application, whether running from source or as an executable.
"""
if getattr(sys, 'frozen', False):
# Running in a bundle (PyInstaller)
return os.path.dirname(sys.executable)
else:
# Running in a normal Python environment
return os.path.dirname(os.path.abspath(__file__))

relay_config = {}
with open("config.yaml", "r") as f:
relay_config = yaml.load(f, Loader=SafeLoader)
config_path = os.path.join(get_app_path(), "config.yaml")

if not os.path.isfile(config_path):
print(f"Configuration file not found: {config_path}")
else:
with open(config_path, "r") as f:
relay_config = yaml.load(f, Loader=SafeLoader)
25 changes: 6 additions & 19 deletions plugin_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,12 @@
import subprocess
import yaml
from log_utils import get_logger
from config import relay_config, get_app_path # Import get_app_path from config.py

logger = get_logger(name="Plugins")
sorted_active_plugins = []
plugins_loaded = False # Add this flag to track if plugins have been loaded

def load_config():
config_path = os.path.join(os.path.dirname(__file__), 'config.yaml')
if not os.path.isfile(config_path):
logger.error(f"Configuration file not found: {config_path}")
return {}
with open(config_path, 'r') as f:
try:
config = yaml.safe_load(f)
return config
except yaml.YAMLError as e:
logger.error(f"Error parsing configuration file: {e}")
return {}

def clone_or_update_repo(repo_url, tag, plugins_dir):
# Extract the repository name from the URL
repo_name = os.path.splitext(os.path.basename(repo_url.rstrip('/')))[0]
Expand Down Expand Up @@ -92,7 +80,7 @@ def load_plugins():

logger.debug("Loading plugins...") # Optional: Log when plugins are being loaded

config = load_config()
config = relay_config # Use relay_config loaded in config.py

# Import core plugins
from plugins.health_plugin import Plugin as HealthPlugin
Expand Down Expand Up @@ -123,18 +111,18 @@ def load_plugins():
plugins = core_plugins.copy()

# Load custom plugins (non-recursive)
custom_plugins_dir = os.path.join(os.path.dirname(__file__), 'plugins', 'custom')
custom_plugins_dir = os.path.join(get_app_path(), 'plugins', 'custom') # Use get_app_path()
plugins.extend(load_plugins_from_directory(custom_plugins_dir, recursive=False))

# Process and download community plugins
community_plugins_config = config.get('community-plugins', {})
community_plugins_dir = os.path.join(os.path.dirname(__file__), 'plugins', 'community')
community_plugins_dir = os.path.join(get_app_path(), 'plugins', 'community') # Use get_app_path()

# Create community plugins directory if needed
if any(plugin_info.get('active', False) for plugin_info in community_plugins_config.values()):
os.makedirs(community_plugins_dir, exist_ok=True)

for plugin_info in community_plugins_config.values():
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')
Expand Down Expand Up @@ -181,5 +169,4 @@ def load_plugins():
logger.debug(f"Plugin '{plugin_name}' is inactive or not configured, skipping") # Changed to DEBUG level

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

0 comments on commit 8f7937c

Please sign in to comment.