From 3b919958af62c11240251cabe71831d511461412 Mon Sep 17 00:00:00 2001 From: kalzu rekku Date: Tue, 6 Aug 2024 21:33:04 +0300 Subject: [PATCH] Added support for multiple module dirs. --- core_daemon.py | 60 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/core_daemon.py b/core_daemon.py index b84b5e7..f18846c 100644 --- a/core_daemon.py +++ b/core_daemon.py @@ -10,23 +10,39 @@ modules_path = os.path.join(os.path.dirname(__file__), 'modules') sys.path.append(modules_path) class ModuleManager: - def __init__(self): + def __init__(self, module_dirs): + self.module_dirs = module_dirs self.loaded_modules = {} self.extra_commands = {} + self._update_sys_path() + + def _update_sys_path(self): + for dir in self.module_dirs: + full_path = os.path.abspath(dir) + if full_path not in sys.path: + sys.path.append(full_path) + + def add_module_dir(self, new_dir): + if new_dir not in self.module_dirs: + self.module_dirs.append(new_dir) + self._update_sys_path() + return True, f"Added module directory: {new_dir}" + return False, f"Module directory already exists: {new_dir}" def load_module(self, module_name): - try: - # Try to import from the modules folder - module = importlib.import_module(f'modules.{module_name}') - self.loaded_modules[module_name] = module - if hasattr(module, 'initialize'): - module.initialize() - if hasattr(module, 'get_commands'): - new_commands = module.get_commands() - self.extra_commands.update(new_commands) - return True, f"Module '{module_name}' loaded and initialized successfully." - except ImportError as e: - return False, f"Error: Unable to load module '{module_name}'. {str(e)}" + for dir in self.module_dirs: + try: + module = importlib.import_module(f'{dir}.{module_name}') + self.loaded_modules[module_name] = module + if hasattr(module, 'initialize'): + module.initialize() + if hasattr(module, 'get_commands'): + new_commands = module.get_commands() + self.extra_commands.update(new_commands) + return True, f"Module '{module_name}' loaded and initialized successfully from {dir}." + except ImportError: + continue + return False, f"Error: Unable to load module '{module_name}' from any of the module directories." def unload_module(self, module_name): if module_name in self.loaded_modules: @@ -53,10 +69,11 @@ class ModuleManager: return False, "Command not found" class CoreDaemon: - def __init__(self, host='localhost', port=9999): + def __init__(self, host='localhost', port=9999, module_dirs=None): self.host = host self.port = port - self.module_manager = ModuleManager() + self.module_dirs = module_dirs or ['modules'] # Default to 'modules' if not specified + self.module_manager = ModuleManager(self.module_dirs) def start(self): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: @@ -100,10 +117,21 @@ class CoreDaemon: elif action == 'list_commands': commands = self.module_manager.list_commands() success, message = True, commands + elif action == 'add_module_dir': + success, message = self.module_manager.add_module_dir(command.get('dir')) else: success, message = False, "Unknown command" return {'success': success, 'message': message} if __name__ == "__main__": - daemon = CoreDaemon() + import argparse + + parser = argparse.ArgumentParser(description="Core Daemon with dynamic module loading") + parser.add_argument('--host', default='localhost', help='Host to bind the daemon to') + parser.add_argument('--port', type=int, default=9999, help='Port to bind the daemon to') + parser.add_argument('--module-dirs', nargs='*', default=['modules'], help='Directories to load modules from') + + args = parser.parse_args() + + daemon = CoreDaemon(host=args.host, port=args.port, module_dirs=args.module_dirs) daemon.start()