Docker-in-Docker (DinD) capabilities of public runners deactivated. More info

pspa.py 3.41 KB
Newer Older
TOUZE Francois's avatar
TOUZE Francois committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
#!/bin/env python3

from flask import Flask, render_template, jsonify
from flask import request
import os
import json
import urllib.parse

from modules import pspacode
from modules import pspaplot

from flask_cors import CORS


app = Flask(__name__, template_folder='.')
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})

curdir = os.getcwd()
print( "current directory => ", curdir)
pkgRoot = curdir.split('pspa-web')
print( "root directory => ", pkgRoot[0])

@app.route('/api/importe', methods=['GET', 'POST'])
def importe():
    
    up_bytes= str.encode( pkgRoot[0] )
    ptr= pspacode.Pypspa( up_bytes )

    x= request.json
    print( "/api/importe => ", x['fileName'] )
    x_bytes= str.encode( x['fileName'] )
    s= ptr.read( x_bytes ).decode('utf-8')
    
    response = {
        'import': s
    }
    return jsonify(response)
    
@app.route('/api/simulate', methods=['GET', 'POST'])
def simulate():
    
    x= request.json
    #print( "x => ", x["machine"])
    #print( "y => ", x["simulations"])
    #print( "z => ", x["projectName"])

    up_bytes= str.encode( pkgRoot[0] )
    ptr= pspacode.Pypspa( up_bytes )
    
    data= json.dumps(x, ensure_ascii=False)
    x_bytes= str.encode( data )
    status= ptr.execute( x_bytes )
    #print( "/api/simulate:: status => ", status )
    
    s= "failure"
    if (status): s= "success"
    
    response = {
        'status': s
    }
    return jsonify(response)

@app.route('/api/console', methods=['GET', 'POST'])
def console():

    #print( "api/console => ", request.full_path)
    
    """ x is the list after splitting by the separator 'ids='
    """
    x = request.full_path.split("ids=")
     
    """ y is the list after splitting the 2nd item by the separator ','
    """
    y = x[1].split(",")
    #print( "api/pourVoir y => ", type(y), len(y))
    #print( "api/pourVoir y => ", y)

    z= []
    if len(y[0]) > 0:
        path= os.path.join(pkgRoot[0],'workZone/')
        for item in y:
            d={"id":urllib.parse.unquote( item )}
            ids= urllib.parse.unquote( item ).split() 
            out= ids[0]+"-output.txt"
            fname= os.path.join(path,out)
            if os.path.exists( fname ):
                fp= open(fname).read()
                d["output"]= fp
                d["status"]= "loaded"
            else:
                d["output"]= "unknown result"
                d["status"]= "not loaded"
            z.append(d)

    response = {
        'results': z
    }
    return jsonify(response)
    
@app.route('/api/plots', methods=['GET', 'POST'])
def plots():

    x= request.json

    """
       x1= x['plotSimulation']
       print( "/api/plots x1=> ", x1, type(x1), len(x1) )
       x2= x['plotItem']
       print( "/api/plots x2=> ", x2, type(x2) )
    """
    up_bytes= str.encode( pkgRoot[0] )
    ptr= pspaplot.Pypspaplot( up_bytes )
    
    data= json.dumps(x, ensure_ascii=False)
    x_bytes= str.encode( data )
    ptr.settings( x_bytes )
    
    
    data= os.path.join(pkgRoot[0],'workZone/simulator-plot.txt')
    
    """
    data= os.path.join(pkgRoot[0],"workZone/raf.txt")
    """

    with open(data) as f:
         ofi= json.load(f)
         
    response = {
        'chartConfig': ofi
    }
    return jsonify(response)
    
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
    """Our entry point"""
    return render_template("index.html")

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