Spaces:
Sleeping
Sleeping
| import os | |
| import json | |
| import uuid | |
| MODULES_DIR = os.path.join(os.path.dirname(__file__), 'modules') | |
| class ModuleManager: | |
| def __init__(self): | |
| self.modules = {} | |
| self.load_modules() | |
| def load_modules(self): | |
| """Scan the modules directory and load all valid JSON module definitions.""" | |
| self.modules = {} | |
| if not os.path.exists(MODULES_DIR): | |
| os.makedirs(MODULES_DIR) | |
| for filename in os.listdir(MODULES_DIR): | |
| if filename.endswith('.json'): | |
| filepath = os.path.join(MODULES_DIR, filename) | |
| try: | |
| with open(filepath, 'r') as f: | |
| module_data = json.load(f) | |
| # Validate required fields | |
| required = ['id', 'title', 'category', 'description'] | |
| if all(k in module_data for k in required): | |
| self.modules[module_data['id']] = module_data | |
| else: | |
| print(f"Warning: Module {filename} missing required fields.") | |
| except Exception as e: | |
| print(f"Error loading module {filename}: {e}") | |
| def get_all_modules(self): | |
| """Return a list of all loaded module metadata.""" | |
| return list(self.modules.values()) | |
| def get_module(self, module_id): | |
| """Retrieve a specific module by ID.""" | |
| return self.modules.get(module_id) | |
| def create_module_session(self, module_id): | |
| """Initialize a new assessment session for a module.""" | |
| module = self.get_module(module_id) | |
| if not module: | |
| return None | |
| session_id = f"sess_{uuid.uuid4().hex[:8]}" | |
| return { | |
| 'session_id': session_id, | |
| 'module_id': module_id, | |
| 'category': module['category'], | |
| 'title': module['title'], | |
| 'config': module.get('config', {}) | |
| } | |
| # Global instance | |
| module_manager = ModuleManager() | |