# backend/app.py import os import sys from pathlib import Path # Add the parent directory to Python path backend_dir = Path(__file__).parent.parent # Sube un nivel más para incluir la carpeta raíz if str(backend_dir) not in sys.path: sys.path.append(str(backend_dir)) from flask import Flask, render_template, jsonify, request, send_from_directory from core.directory_handler import select_directory from core.script_manager import ScriptManager from core.profile_manager import ProfileManager app = Flask(__name__, template_folder='../frontend/templates', static_folder='../frontend/static') # Initialize managers data_dir = Path(__file__).parent.parent / 'data' script_groups_dir = Path(__file__).parent / 'script_groups' profile_manager = ProfileManager(data_dir) script_manager = ScriptManager(script_groups_dir) @app.route('/') def index(): """Render main page""" return render_template('index.html') # Profile endpoints @app.route('/api/profiles', methods=['GET']) def get_profiles(): """Get all profiles""" return jsonify(profile_manager.get_all_profiles()) @app.route('/api/profiles/', methods=['GET']) def get_profile(profile_id): """Get specific profile""" profile = profile_manager.get_profile(profile_id) if profile: return jsonify(profile) return jsonify({"error": "Profile not found"}), 404 @app.route('/api/profiles', methods=['POST']) def create_profile(): """Create new profile""" profile_data = request.json try: profile = profile_manager.create_profile(profile_data) return jsonify(profile) except Exception as e: return jsonify({"error": str(e)}), 400 @app.route('/api/profiles/', methods=['PUT']) def update_profile(profile_id): """Update existing profile""" try: profile_data = request.json print(f"Received update request for profile {profile_id}: {profile_data}") # Debug profile = profile_manager.update_profile(profile_id, profile_data) print(f"Profile updated: {profile}") # Debug return jsonify(profile) except Exception as e: print(f"Error updating profile: {e}") # Debug return jsonify({"error": str(e)}), 400 @app.route('/api/profiles/', methods=['DELETE']) def delete_profile(profile_id): """Delete profile""" try: profile_manager.delete_profile(profile_id) return jsonify({"status": "success"}) except Exception as e: return jsonify({"error": str(e)}), 400 @app.route('/api/script-groups', methods=['GET']) def get_script_groups(): """Get all available script groups""" try: groups = script_manager.get_available_groups() return jsonify(groups) except Exception as e: return jsonify({"error": str(e)}), 500 # Directory handling endpoints @app.route('/api/select-directory', methods=['GET']) def handle_select_directory(): """Handle directory selection""" print("Handling directory selection request") # Debug result = select_directory() print(f"Directory selection result: {result}") # Debug if "error" in result: return jsonify(result), 400 return jsonify(result) # Script management endpoints @app.route('/api/scripts', methods=['GET']) def get_scripts(): """Get all available script groups""" try: groups = script_manager.discover_groups() return jsonify(groups) except Exception as e: return jsonify({"error": str(e)}), 500 @app.route('/api/scripts///run', methods=['POST']) def run_script(group_id, script_id): """Execute a specific script""" data = request.json work_dir = data.get('work_dir') profile = data.get('profile') if not work_dir: return jsonify({"error": "Work directory not specified"}), 400 if not profile: return jsonify({"error": "Profile not specified"}), 400 try: result = script_manager.execute_script(group_id, script_id, work_dir, profile) return jsonify(result) except Exception as e: return jsonify({"error": str(e)}), 500 # Work directory configuration endpoints @app.route('/api/workdir-config/', methods=['GET']) def get_workdir_config(work_dir): """Get work directory configuration""" from core.workdir_config import WorkDirConfigManager config_manager = WorkDirConfigManager(work_dir) return jsonify(config_manager.get_config()) @app.route('/api/workdir-config//group/', methods=['GET']) def get_group_config(work_dir, group_id): """Get group configuration from work directory""" from core.workdir_config import WorkDirConfigManager config_manager = WorkDirConfigManager(work_dir) return jsonify(config_manager.get_group_config(group_id)) @app.route('/api/workdir-config//group/', methods=['PUT']) def update_group_config(work_dir, group_id): """Update group configuration in work directory""" from core.workdir_config import WorkDirConfigManager config_manager = WorkDirConfigManager(work_dir) try: settings = request.json config_manager.update_group_config(group_id, settings) return jsonify({"status": "success"}) except Exception as e: return jsonify({"error": str(e)}), 400 @app.route('/api/script-groups//config-schema', methods=['PUT']) def update_group_config_schema(group_id): """Update configuration schema for a script group""" try: schema = request.json config_file = Path(script_manager.script_groups_dir) / group_id / "config.json" with open(config_file, 'w', encoding='utf-8') as f: json.dump(schema, f, indent=4) return jsonify({"status": "success"}) except Exception as e: return jsonify({"error": str(e)}), 500 @app.route('/api/script-groups//scripts', methods=['GET']) def get_group_scripts(group_id): """Get scripts for a specific group""" try: print(f"Loading scripts for group: {group_id}") # Debug scripts = script_manager.get_group_scripts(group_id) print(f"Scripts found: {scripts}") # Debug return jsonify(scripts) except Exception as e: print(f"Error loading scripts: {str(e)}") # Debug return jsonify({"error": str(e)}), 500 @app.route('/api/script-groups//config-schema', methods=['GET']) def get_group_config_schema(group_id): """Get configuration schema for a script group""" try: print(f"Loading config schema for group: {group_id}") # Debug schema = script_manager.get_group_config_schema(group_id) print(f"Schema loaded: {schema}") # Debug return jsonify(schema) except Exception as e: print(f"Error loading schema: {str(e)}") # Debug return jsonify({"error": str(e)}), 500 if __name__ == '__main__': app.run(debug=True, port=5000)