LocalScriptsWeb/backend/app.py

167 lines
5.7 KiB
Python

# 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/<profile_id>', 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/<profile_id>', 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/<profile_id>', 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
@app.route('/api/script-groups/<group_id>/scripts', methods=['GET'])
def get_group_scripts(group_id):
"""Get scripts for a specific group"""
try:
scripts = script_manager.get_group_scripts(group_id)
return jsonify(scripts)
except ValueError as e:
return jsonify({"error": str(e)}), 404
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/<group_id>/<script_id>/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/<path:work_dir>', 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/<path:work_dir>/group/<group_id>', 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/<path:work_dir>/group/<group_id>', 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
if __name__ == '__main__':
app.run(debug=True, port=5000)