From fd2e782b5b627035bf380bb9b26e91bb668738b4 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Thu, 3 Apr 2025 21:03:54 -0400 Subject: [PATCH] Fixed some more issues with plugins. Fixed bugs in self_voice --- src/cthulhu/plugin_system_manager.py | 32 +++++++++++++++ src/cthulhu/plugins/hello_world/plugin.py | 48 ++++++++++++++++++----- src/cthulhu/plugins/self_voice/plugin.py | 24 +++++++++--- src/cthulhu/settings.py | 2 +- 4 files changed, 89 insertions(+), 17 deletions(-) diff --git a/src/cthulhu/plugin_system_manager.py b/src/cthulhu/plugin_system_manager.py index a02c06b..960c9b8 100644 --- a/src/cthulhu/plugin_system_manager.py +++ b/src/cthulhu/plugin_system_manager.py @@ -182,6 +182,17 @@ class PluginSystemManager: logger.info(f"Found plugin: {module_name} in {plugin_dir}") metadata = self._load_plugin_metadata(metadata_file) + # Try to get the actual plugin class name from the plugin file + class_name = self._get_plugin_class_name(plugin_file) + if class_name: + logger.info(f"Found plugin class: {class_name} in {plugin_file}") + # Use the class name instead of module name for more accurate registration + if class_name != module_name: + logger.info(f"Using class name {class_name} instead of module name {module_name}") + # Store both to allow flexible access + metadata['module_name'] = module_name + module_name = class_name + plugin_info = PluginInfo( metadata.get('name', module_name), module_name, @@ -195,8 +206,29 @@ class PluginSystemManager: logger.info(f"Adding plugin to registry: {module_name}") self._plugins[module_name] = plugin_info + + # Also register with lowercase name for case-insensitive access + if module_name.lower() != module_name: + logger.info(f"Also registering lowercase alias: {module_name.lower()}") + self._plugins[module_name.lower()] = plugin_info else: logger.warning(f"No plugin file found in directory: {plugin_dir}") + + def _get_plugin_class_name(self, plugin_file): + """Extract the plugin class name from a plugin file.""" + try: + with open(plugin_file, 'r') as f: + content = f.read() + + # Simple regex-like search for class definition that inherits from Plugin + import re + matches = re.findall(r'class\s+(\w+)\s*\([^)]*Plugin[^)]*\)', content) + if matches: + return matches[0] + except Exception as e: + logger.error(f"Error extracting plugin class name from {plugin_file}: {e}") + + return None def _load_plugin_metadata(self, metadata_file): """Load plugin metadata from a file.""" diff --git a/src/cthulhu/plugins/hello_world/plugin.py b/src/cthulhu/plugins/hello_world/plugin.py index 874861c..e372c60 100644 --- a/src/cthulhu/plugins/hello_world/plugin.py +++ b/src/cthulhu/plugins/hello_world/plugin.py @@ -33,12 +33,26 @@ class HelloWorld(Plugin): logger.info("Activating Hello World plugin") # Register our keyboard shortcut - self.registerGestureByString( - self.speakTest, - "hello world", - "kb:cthulhu+z", - learnModeEnabled=True - ) + if hasattr(self, 'registerGestureByString'): + logger.info("Registering keyboard shortcut kb:cthulhu+z") + self.registerGestureByString( + self.speakTest, + "hello world", + "kb:cthulhu+z", + learnModeEnabled=True + ) + elif self.app and hasattr(self.app, 'getAPIHelper'): + helper = self.app.getAPIHelper() + if helper: + logger.info("Registering keyboard shortcut kb:cthulhu+z via API helper") + helper.registerGestureByString( + self.speakTest, + "hello world", + "kb:cthulhu+z", + learnModeEnabled=True + ) + else: + logger.error("Cannot register keyboard shortcut - no API helper available") except Exception as e: logger.error(f"Error activating Hello World plugin: {e}") @@ -56,14 +70,28 @@ class HelloWorld(Plugin): def speakTest(self, script=None, inputEvent=None): """Speak a test message.""" + logger.info("speakTest called from hello_world plugin") try: if self.app: - self.app.getDynamicApiManager().getAPI('CthulhuState').activeScript.presentMessage( - 'hello world', - resetStyles=False - ) + dynamicApi = self.app.getDynamicApiManager() + if dynamicApi: + state = dynamicApi.getAPI('CthulhuState') + if state and state.activeScript: + logger.info("Presenting message: 'hello world'") + state.activeScript.presentMessage( + 'hello world', + resetStyles=False + ) + else: + logger.error("No active script available") + else: + logger.error("No dynamic API manager available") + else: + logger.error("No app reference available") return True except Exception as e: logger.error(f"Error in speakTest: {e}") + import traceback + logger.error(traceback.format_exc()) return False diff --git a/src/cthulhu/plugins/self_voice/plugin.py b/src/cthulhu/plugins/self_voice/plugin.py index 324f15b..7ef6a71 100644 --- a/src/cthulhu/plugins/self_voice/plugin.py +++ b/src/cthulhu/plugins/self_voice/plugin.py @@ -51,18 +51,30 @@ class SelfVoice(Plugin): self.voiceThread.daemon = True # Make thread exit when main thread exits @cthulhu_hookimpl - def activate(self): + def activate(self, plugin=None): """Activate the self-voice plugin.""" - super().activate() + # Skip if this activation call isn't for us + if plugin is not None and plugin is not self: + return + logger.info("Activating Self Voice Plugin") - self.activateWorker() + try: + self.activateWorker() + except Exception as e: + logger.error(f"Error activating Self Voice plugin: {e}") @cthulhu_hookimpl - def deactivate(self): + def deactivate(self, plugin=None): """Deactivate the self-voice plugin.""" + # Skip if this deactivation call isn't for us + if plugin is not None and plugin is not self: + return + logger.info("Deactivating Self Voice Plugin") - self.deactivateWorker() - super().deactivate() + try: + self.deactivateWorker() + except Exception as e: + logger.error(f"Error deactivating Self Voice plugin: {e}") def activateWorker(self): """Start the voice worker thread.""" diff --git a/src/cthulhu/settings.py b/src/cthulhu/settings.py index 1c05ff9..be1759d 100644 --- a/src/cthulhu/settings.py +++ b/src/cthulhu/settings.py @@ -413,4 +413,4 @@ presentChatRoomLast = False presentLiveRegionFromInactiveTab = False # Plugins -activePlugins = ['Clipboard', 'DisplayVersion', 'MouseReview', 'Date', 'ByeCthulhu', 'Time', 'HelloCthulhu', 'hello_world', 'self_voice', 'PluginManager', 'SimplePluginSystem'] +activePlugins = ['Clipboard', 'DisplayVersion', 'MouseReview', 'Date', 'ByeCthulhu', 'Time', 'HelloCthulhu', 'hello_world', 'SelfVoice', 'PluginManager', 'SimplePluginSystem']