Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions biosimdb_interface/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,19 @@ def create_app(test_config=None):
instance_relative_config=True,
) # create flask instance

app.config["APPLICATION_BASE"] = os.getenv("APPLICATION_BASE", "")

app.config["APPLICATION_BASE"] = APPLICATION_BASE
app.config["BIOSIM_SCHEMA_PATH"] = os.getenv("BIOSIM_SCHEMA_PATH", "")
app.config["ENGINE_MAPPING_SCHEMA_PATH"] = os.getenv(
"ENGINE_MAPPING_SCHEMA_PATH", ""
)
# App and Invenio OAuth2 configuration — values loaded from .env
app.config.from_mapping(
SECRET_KEY=os.getenv("SECRET_KEY", "dev"),
WORKFLOW_TTL_SECONDS=int(os.getenv("WORKFLOW_TTL_SECONDS", "14400")),
UPLOAD_FOLDER=os.getenv("UPLOAD_FOLDER", "/tmp"), # App specific
CLIENT_ID=os.getenv("CLIENT_ID", ""),
CLIENT_SECRET=os.getenv("CLIENT_SECRET", ""),
BIOSIMDB_SERVICE_TOKEN=os.getenv("BIOSIMDB_SERVICE_TOKEN", ""),
AUTH_URL=os.getenv("AUTH_URL", ""),
TOKEN_URL=os.getenv("TOKEN_URL", ""),
BASE_URL=os.getenv("BASE_URL", ""),
Expand Down
2 changes: 1 addition & 1 deletion biosimdb_interface/form/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

form_bp = Blueprint("form", __name__)

from . import extract, webform # noqa: E402, F401
from . import errors, extract, submission, webform # noqa: E402, F401
86 changes: 86 additions & 0 deletions biosimdb_interface/form/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/env python

from flask import (
current_app,
flash,
jsonify,
redirect,
request,
url_for,
)
from werkzeug.exceptions import HTTPException

from . import form_bp
from .workflows import WorkflowNotFound


class WebformError(Exception):
status_code = 400

def __init__(self, message, status_code=None, payload=None):
super().__init__()
self.message = message
if status_code is not None:
self.status_code = status_code
self.payload = payload

def to_dict(self):
rv = dict(self.payload or ())
rv["error"] = self.message
return rv


@form_bp.errorhandler(WebformError)
def handle_metadata_extraction_error(exc):
current_app.logger.exception(f"Metadata extraction Error: {exc}")
return jsonify(exc.to_dict()), exc.status_code


class ExternalAPIError(Exception):
"""An external API call failed or returned an unexpected response."""


class FormConversionError(ValueError):
"""Raised when form values can't be parsed; holds every message in ``errors``."""

def __init__(self, errors, heading="Form conversion failed:"):
self.errors = list(errors)
self.heading = heading
super().__init__(heading + "\n" + "\n".join(self.errors))


@form_bp.errorhandler(ExternalAPIError)
def handle_external_api_error(exc):
"""Show a friendly message when an external service fails."""
current_app.logger.error("External API error: %s", exc)
flash(
"A remote service is currently unavailable. Please try again later.", "danger"
)
return redirect(url_for("form.webform"))


@form_bp.errorhandler(Exception)
def handle_unexpected_error(exc):
"""Log an unexpected blueprint error and return a JSON error response."""
if isinstance(exc, HTTPException):
return exc

current_app.logger.exception(f"Unexpected error in form blueprint: {exc}")
message = (
f"{type(exc).__name__}: {exc}"
if current_app.debug
else "An unexpected server error occurred. Please try again."
)

# extract_metadata is called via fetch, which expects JSON rather than a redirect
if request.endpoint == "form.extract_metadata":
try:
current_app.extensions["workflow_store"].delete(
request.form.get("workflow_id")
)
except WorkflowNotFound:
pass
return jsonify({"error": f"Extraction failed: {message}"}), 500

flash(f"{message}", "danger")
return redirect(url_for("form.webform"))
89 changes: 30 additions & 59 deletions biosimdb_interface/form/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from flask import current_app, jsonify, request
from werkzeug.utils import secure_filename

from biosimdb_interface.form.errors import WebformError

from . import form_bp
from .upload import cache_extracted_files
from .workflows import WorkflowNotFound
Expand All @@ -28,25 +30,19 @@ def extract_files_validate(top_file, traj_file):
Returns:
tuple: A tuple containing:
- result (dict): The extracted and populated metadata dictionary.
- validation_errors (list[str]): A list of validation error messages,
empty if validation succeeded.
"""
populator = MetadataPopulator(
schema_path=os.getenv("ENGINE_MAPPING_SCHEMA_PATH", ""),
schema_path=current_app.config["ENGINE_MAPPING_SCHEMA_PATH"],
top_file=top_file,
traj_file=traj_file,
store_file_metadata=False, # not on webform so exclude
)

result = populator.populate()
biosimschema_path = os.getenv("BIOSIM_SCHEMA_PATH", "")
validation_errors = []
try:
populator.validate(result, biosimschema_path, strict=True)
except ValueError as e:
validation_errors = str(e).splitlines()
biosimschema_path = current_app.config["BIOSIM_SCHEMA_PATH"]
populator.validate(result, biosimschema_path, strict=True)

return result, validation_errors
return result


@form_bp.route("/extract_metadata", methods=["POST"])
Expand All @@ -62,68 +58,43 @@ def extract_metadata():
and the result is validated against the schema at ``BIOSIM_SCHEMA_PATH``.

Returns:
JSON response with one of:
- ``{"simulation_metadata": ..., "message": "..."}`` on success.
- ``{"simulation_metadata": ..., "validation_errors": [...]}`` if schema validation fails.
- ``{"error": "..."}`` with status 400 if files are missing, or 500 on unexpected error.
JSON response:
- ``{"simulation_metadata": ..., "message": "..."}``
"""
workflow_id = request.form.get("workflow_id")
topology = request.files.get("topology")
trajectories = request.files.getlist("trajectory[]")

if not topology or not trajectories:
return jsonify({"error": "Simulation files are missing."}), 400
raise WebformError("Simulation files are missing!", status_code=404)

try:
workflow = current_app.extensions["workflow_store"].reset(workflow_id)
except WorkflowNotFound as exc:
return jsonify({"error": str(exc)}), 400
return jsonify({"Workflow error": str(exc)}), 400

tmpdir = workflow.tmpdir

try:
topology = request.files.get("topology")
trajectories = request.files.getlist("trajectory[]")

if not topology or not trajectories:
return jsonify({"error": "Simulation files are missing."}), 400

topo_path = os.path.join(tmpdir, secure_filename(topology.filename))
topology.save(topo_path, buffer_size=16 * 1024 * 1024) # 16 MB chunks

traj_files = []
for traj in trajectories:
traj_path = os.path.join(tmpdir, secure_filename(traj.filename))
traj.save(traj_path, buffer_size=16 * 1024 * 1024)
traj_files.append(traj_path)

saved_files = {"topology": [topo_path], "trajectory": traj_files}
cache_extracted_files(tmpdir, saved_files)

result, validation_errors = extract_files_validate(topo_path, traj_files)

if validation_errors:
return jsonify(
{
"simulation_metadata": result,
"validation_errors": validation_errors,
}
)
else:
return jsonify(
{
"simulation_metadata": result,
"message": "Metadata extracted successfully.",
}
)

except Exception as e:
current_app.extensions["workflow_store"].delete(workflow_id)
print(f"ERROR: {e}")
import traceback

traceback.print_exc()
return jsonify({"error": str(e)}), 500
topo_path = os.path.join(tmpdir, secure_filename(topology.filename))
topology.save(topo_path, buffer_size=16 * 1024 * 1024) # 16 MB chunks

traj_files = []
for traj in trajectories:
traj_path = os.path.join(tmpdir, secure_filename(traj.filename))
traj.save(traj_path, buffer_size=16 * 1024 * 1024)
traj_files.append(traj_path)

saved_files = {"topology": [topo_path], "trajectory": traj_files}
cache_extracted_files(tmpdir, saved_files)

result = extract_files_validate(topo_path, traj_files)

return jsonify(
{
"simulation_metadata": result,
"message": "Metadata extracted successfully.",
}
)


@form_bp.route("/clear_extraction", methods=["POST"])
Expand Down
Loading
Loading