Added support for multiple module dirs.

This commit is contained in:
kalzu rekku 2024-08-06 21:33:04 +03:00
parent 2a64d79c51
commit 3b919958af

View File

@ -10,23 +10,39 @@ modules_path = os.path.join(os.path.dirname(__file__), 'modules')
sys.path.append(modules_path) sys.path.append(modules_path)
class ModuleManager: class ModuleManager:
def __init__(self): def __init__(self, module_dirs):
self.module_dirs = module_dirs
self.loaded_modules = {} self.loaded_modules = {}
self.extra_commands = {} 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): def load_module(self, module_name):
for dir in self.module_dirs:
try: try:
# Try to import from the modules folder module = importlib.import_module(f'{dir}.{module_name}')
module = importlib.import_module(f'modules.{module_name}')
self.loaded_modules[module_name] = module self.loaded_modules[module_name] = module
if hasattr(module, 'initialize'): if hasattr(module, 'initialize'):
module.initialize() module.initialize()
if hasattr(module, 'get_commands'): if hasattr(module, 'get_commands'):
new_commands = module.get_commands() new_commands = module.get_commands()
self.extra_commands.update(new_commands) self.extra_commands.update(new_commands)
return True, f"Module '{module_name}' loaded and initialized successfully." return True, f"Module '{module_name}' loaded and initialized successfully from {dir}."
except ImportError as e: except ImportError:
return False, f"Error: Unable to load module '{module_name}'. {str(e)}" continue
return False, f"Error: Unable to load module '{module_name}' from any of the module directories."
def unload_module(self, module_name): def unload_module(self, module_name):
if module_name in self.loaded_modules: if module_name in self.loaded_modules:
@ -53,10 +69,11 @@ class ModuleManager:
return False, "Command not found" return False, "Command not found"
class CoreDaemon: class CoreDaemon:
def __init__(self, host='localhost', port=9999): def __init__(self, host='localhost', port=9999, module_dirs=None):
self.host = host self.host = host
self.port = port 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): def start(self):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
@ -100,10 +117,21 @@ class CoreDaemon:
elif action == 'list_commands': elif action == 'list_commands':
commands = self.module_manager.list_commands() commands = self.module_manager.list_commands()
success, message = True, commands success, message = True, commands
elif action == 'add_module_dir':
success, message = self.module_manager.add_module_dir(command.get('dir'))
else: else:
success, message = False, "Unknown command" success, message = False, "Unknown command"
return {'success': success, 'message': message} return {'success': success, 'message': message}
if __name__ == "__main__": 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() daemon.start()