from flask import Flask, request, jsonify
from flask_cors import CORS
from run import run_ansys
from api.validate import spec_present, data_type_validate, spec_keys_validate, ansys_overload_check
ansys_processing_count = 0

# debug
# import ipdb; ipdb.set_trace()

app = Flask(__name__)
CORS(app)  # local development cors

@app.route('/run_simu', methods=["POST"])
def run_simulation():
    global ansys_processing_count
    ansys_processing_count += 1

    ctx = {
        "request": request.get_json(),
        "allow_run": True,
        "process": {
            "limit": 4,
            "count": ansys_processing_count,
        },
        "start_run_response": {"msg": "start run at background"},
        "error": {
            "validate": {"msg": ""}
            }
    }

    if spec_present(ctx) and \
            data_type_validate(ctx) and \
            spec_keys_validate(ctx) and \
            ansys_overload_check(ctx):
        ctx = run_ansys(self.ctx)
    else:
        return jsonify(ctx["error"]["validate"])

    return jsonify(ctx["response"])


if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000, debug=True)

[((10, 6, 10, 21), 'flask.Flask', 'Flask', ({(10, 12, 10, 20): '__name__'}, {}), '(__name__)', False, 'from flask import Flask, request, jsonify\n'), ((11, 0, 11, 9), 'flask_cors.CORS', 'CORS', ({(11, 5, 11, 8): 'app'}, {}), '(app)', False, 'from flask_cors import CORS\n'), ((39, 11, 39, 35), 'flask.jsonify', 'jsonify', ({(39, 19, 39, 34): "ctx['response']"}, {}), "(ctx['response'])", False, 'from flask import Flask, request, jsonify\n'), ((19, 19, 19, 37), 'flask.request.get_json', 'request.get_json', ({}, {}), '()', False, 'from flask import Flask, request, jsonify\n'), ((31, 7, 31, 24), 'api.validate.spec_present', 'spec_present', ({(31, 20, 31, 23): 'ctx'}, {}), '(ctx)', False, 'from api.validate import spec_present, data_type_validate, spec_keys_validate, ansys_overload_check\n'), ((32, 12, 32, 35), 'api.validate.data_type_validate', 'data_type_validate', ({(32, 31, 32, 34): 'ctx'}, {}), '(ctx)', False, 'from api.validate import spec_present, data_type_validate, spec_keys_validate, ansys_overload_check\n'), ((33, 12, 33, 35), 'api.validate.spec_keys_validate', 'spec_keys_validate', ({(33, 31, 33, 34): 'ctx'}, {}), '(ctx)', False, 'from api.validate import spec_present, data_type_validate, spec_keys_validate, ansys_overload_check\n'), ((34, 12, 34, 37), 'api.validate.ansys_overload_check', 'ansys_overload_check', ({(34, 33, 34, 36): 'ctx'}, {}), '(ctx)', False, 'from api.validate import spec_present, data_type_validate, spec_keys_validate, ansys_overload_check\n'), ((35, 14, 35, 33), 'run.run_ansys', 'run_ansys', ({(35, 24, 35, 32): 'self.ctx'}, {}), '(self.ctx)', False, 'from run import run_ansys\n'), ((37, 15, 37, 48), 'flask.jsonify', 'jsonify', ({(37, 23, 37, 47): "ctx['error']['validate']"}, {}), "(ctx['error']['validate'])", False, 'from flask import Flask, request, jsonify\n')]