pspa.py 3.51 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
#!/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)
20
pkgRoot = curdir.split('pspa.git')
TOUZE Francois's avatar
TOUZE Francois committed
21
print( "root directory => ", pkgRoot[0])
22 23
upath = os.path.join(pkgRoot[0],'workZone/')
wpath = os.path.join(pkgRoot[0],'pspa.git/softwares/')
TOUZE Francois's avatar
TOUZE Francois committed
24 25 26

@app.route('/api/importe', methods=['GET', 'POST'])
def importe():
27 28 29 30
   
    u_bytes= str.encode( upath )
    w_bytes= str.encode( wpath )
    ptr= pspacode.Pypspa( u_bytes, w_bytes )
TOUZE Francois's avatar
TOUZE Francois committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

    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"])

50 51 52
    u_bytes= str.encode( upath )
    w_bytes= str.encode( wpath )
    ptr= pspacode.Pypspa( u_bytes, w_bytes )
TOUZE Francois's avatar
TOUZE Francois committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    
    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():

70 71
    print( "api/console => ", request.full_path)
     
TOUZE Francois's avatar
TOUZE Francois committed
72 73 74 75 76 77 78 79 80 81 82
    """ 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= []
83
    if len(y[0]) > 0: 
TOUZE Francois's avatar
TOUZE Francois committed
84 85 86 87
        for item in y:
            d={"id":urllib.parse.unquote( item )}
            ids= urllib.parse.unquote( item ).split() 
            out= ids[0]+"-output.txt"
88
            fname= os.path.join(upath,out)
TOUZE Francois's avatar
TOUZE Francois committed
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
            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) )
    """
114 115

    up_bytes= str.encode( upath )
TOUZE Francois's avatar
TOUZE Francois committed
116 117 118 119 120 121 122
    ptr= pspaplot.Pypspaplot( up_bytes )
    
    data= json.dumps(x, ensure_ascii=False)
    x_bytes= str.encode( data )
    ptr.settings( x_bytes )
    
    
123
    data= os.path.join(upath,'simulator-plot.txt')
TOUZE Francois's avatar
TOUZE Francois committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    
    """
    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)