Skip to content
Snippets Groups Projects
__init__.py 8.11 KiB
Newer Older
#----------#
#-envelope-#
#----------#
Carla Barquest's avatar
Carla Barquest committed
from flask import Blueprint, request, render_template, \
                  redirect, url_for, send_from_directory, make_response, session
from collections import OrderedDict
Carla Barquest's avatar
Carla Barquest committed

# Get info from flask app
from beam.home.lib import bash_command, get_config_value, request_key_val, set_session, set_session_keyvalue, accDir
envelopeDict = get_config_value('envelopeDict')
perlDir = envelopeDict['perlDir']
optrExampleDir = envelopeDict['optrExampleDir']
optrDir = envelopeDict['envelopeOptrDir']
defaultXMLDir = envelopeDict['defaultXMLDir']
Carla Barquest's avatar
Carla Barquest committed

app_name = 'envelope'
mod_be = Blueprint(app_name,__name__,url_prefix='/'+app_name,template_folder='templates',static_folder='static',static_url_path='/static/'+app_name)
Carla Barquest's avatar
Carla Barquest committed

# Put extra "beamline" = 'optrex' and "beampaths" inside envelope session datadata
# only allows two levels of nested folders--could use recursion for more if needed 
beamlines = OrderedDict()
bl = OrderedDict()
optrbl = 'optrex'
bl[optrbl] = OrderedDict()
bp = OrderedDict()
for bpName in os.listdir(optrExampleDir):
    test_path = os.path.join(optrExampleDir,bpName)
    if os.path.isdir(test_path): 
        syf = os.path.join(test_path,'sy.f')
        datadat = os.path.join(test_path,'data.dat')
        optr = os.path.join(test_path,'optr')
        if os.path.isfile(optr) and os.path.isfile(syf) and os.path.isfile(datadat):
            #we got ourselves a transoptr example directory...
            bp[bpName]=test_path
            bl[optrbl].update(bp)
        #test if there are subdirectories...
        for bpName2 in os.listdir(test_path):
            test_path2 = os.path.join(test_path,bpName2)
            if os.path.isdir(test_path2):
                if os.path.isdir(test_path2):
                    syf = os.path.join(test_path2,'sy.f')
                    datadat = os.path.join(test_path2,'data.dat')
                    optr = os.path.join(test_path2,'optr')
                    if os.path.isfile(optr) and os.path.isfile(syf) and os.path.isfile(datadat):
                        #we got ourselves a transoptr example directory...
                        bp[bpName+':'+bpName2]=test_path2
                        bl[optrbl].update(bp)      
beamlines.update(bl)

def initialize_envelope_session():
    session['envelope'] = {}
    session['envelope']['blbp'] = beamlines
    return   
Carla Barquest's avatar
Carla Barquest committed

@mod_be.route("/")
def landing():
    #check session status, initialize if necessary
    initialize_envelope_session()
    page_title = 'Beam Envelope: Select a Beamline and Beampath'
    current = {'app': app_name, 'title': page_title, 'bl': 'Beamline', 'bp': 'Beampath'}
    set_session_keyvalue('beam', 'current', current)
    return render_template('landing_select_bl_envelope.html')
Carla Barquest's avatar
Carla Barquest committed
@mod_be.route("/home/")
def home():
    initialize_envelope_session()
    page_title = 'Beam Envelope Home'
    current = {'app': app_name, 'title': page_title, 'bl': 'Beamline', 'bp': 'Beampath'}
    set_session_keyvalue('beam', 'current', current)
    return render_template('home_envelope.html')
Carla Barquest's avatar
Carla Barquest committed

@mod_be.route("/<bl_name>/")
def bltemplate(bl_name):
    initialize_envelope_session()
    page_title = 'Beam Envelope: Select a Beampath'
    current = {'app': app_name, 'title': page_title, 'bl': bl_name, 'bp': 'Beampath'}
    set_session_keyvalue('beam', 'current', current)
    return render_template('landing_select_bp_envelope.html')
Carla Barquest's avatar
Carla Barquest committed

@mod_be.route("/<bl_name>/<bp_name>/")
def blbptemplate(bl_name,bp_name):
    initialize_envelope_session()
    page_title = 'Beam Envelope ['+bl_name+': '+bp_name+']'
    current = {'app': app_name, 'title': page_title, 'bl': bl_name, 'bp': bp_name}
    set_session_keyvalue('beam', 'current', current)
    blbp1 = session['beam']['blbp']
    blbp2 = session['envelope']['blbp']
    blbp = blbp1.copy()
    blbp.update(blbp2)

    if bl_name in blbp:
        blbpxml = session['beam']['current']['blbpxml']
        bpdict = blbp[bl_name]
        if bp_name in bpdict:
Carla Barquest's avatar
Carla Barquest committed
            template_name = "beamenvelope.html"
        else:
            template_name = 'home_envelope.html'
    else:
        template_name = 'home_envelope.html'
Carla Barquest's avatar
Carla Barquest committed

    ##run javascript automatically (for running envelope plot from nav-bar beampath selection)
    #post_beam_data(bl_name,bp_name)
    #post_beam_envelope(bl_name,bp_name)
    return render_template(template_name)       
Carla Barquest's avatar
Carla Barquest committed

@mod_be.route("/beam_data/<bl_name>/<bp_name>",methods = ["POST"])
def post_beam_data(bl_name,bp_name):
    initialize_envelope_session()
Carla Barquest's avatar
Carla Barquest committed
    #Call adapted Paul Jung's BeamData.pl in static/perl/BeamData.pl (now called optics.pl)
    dir_perl = os.path.abspath(os.path.join(os.path.dirname( __file__ ),'static','envelope','perl'))
Carla Barquest's avatar
Carla Barquest committed
    dlist = dir_perl.split(',')
    perl_dir_name = os.path.join(*dlist)
    os.chdir(perl_dir_name)
    script_root = request.script_root

    bl_name = "--building \"" + bl_name + "\""
    bp_name = "--bppath \"" + bp_name + "\""
    default_tune = "--default_tune \"Default\"" 
    acc_dir = "--acc_dir \"" +  accDir + "\""
    default_xml_path = "--default_xml_path \"" + defaultXMLDir + "\"" 
    beamdata_cmd = os.path.join(perlDir,"optics.pl") + " " + bl_name + " " + bp_name + " " + default_tune + \
                   " " + acc_dir + " " + default_xml_path
Carla Barquest's avatar
Carla Barquest committed
    perlxmlstr = bash_command(beamdata_cmd) 
    response = make_response(perlxmlstr)   
Carla Barquest's avatar
Carla Barquest committed
    response.headers['Content-Type'] = 'text/xml; charset=utf-8' 
    return response
def get_commandline_string(varnamestr, request):
    varstring = request_key_val(request,varnamestr) 
    if varstring is None:
        varstring = 'None'
    commandline_string = '--' + varnamestr + ' \"' + varstring + '\"'
    return commandline_string

 
Carla Barquest's avatar
Carla Barquest committed
@mod_be.route("/beam_envelope/<bl_name>/<bp_name>",methods = ["POST"])
def post_beam_envelope(bl_name,bp_name):
    initialize_envelope_session()
Carla Barquest's avatar
Carla Barquest committed
    if request.method == "POST":
        post_data = dict(request.form)
        #print(post_data)
        bl_name = "--building \"" + bl_name + "\""
        bp_name = "--bppath \"" + bp_name + "\""            
        mode = get_commandline_string('mode', request)
        mass = get_commandline_string('mass', request)
        energy = get_commandline_string('energy', request)
        current = get_commandline_string('current', request)
        pastMass = get_commandline_string('pastMass', request)
        pastEnergy = get_commandline_string('pastEnergy', request)
        values = get_commandline_string('values', request)
Carla Barquest's avatar
Carla Barquest committed
        #print(values)
        datadat = get_commandline_string('datadat', request)
        tunename = get_commandline_string('tunename', request)
        optimize = get_commandline_string('optimize', request)
        syf = get_commandline_string('syf', request)
        data_path = "--data_path \"" + accDir + "\""
        optr_example_path = "--optr_example_path \"" + optrExampleDir + "\""
        optr_path = "--optr_path \"" + optrDir + "\"" 
Carla Barquest's avatar
Carla Barquest committed
    #Call adapted Paul Jung's BeamEnvelope.pl in static/perl/BeamEnvelope.pl (now called envelope.pl)
    dir_perl = os.path.abspath(os.path.join(os.path.dirname( __file__ ),'static','envelope','perl'))
Carla Barquest's avatar
Carla Barquest committed
    dlist = dir_perl.split(',')
    perl_dir_name = os.path.join(*dlist)
    os.chdir(perl_dir_name)
    #print(mode)
    #if mode == '--mode \"loadtune\"':
    #    mode = '--mode \"graph\"'
    #    print(mode)
    perlxmlstr = bash_command(os.path.join(perlDir,"envelope.pl") + \
Carla Barquest's avatar
Carla Barquest committed
      " " + bl_name + " " + bp_name + " " + mode + " " + mass + " " + energy + \
      " " + current + " " + pastMass + " " + pastEnergy + " " + values + " " + datadat + \
      " " + tunename + " " + optimize + " " + syf + \
      " " + data_path + " " + optr_example_path + " " + optr_path) #need default_tune variable(?)
Carla Barquest's avatar
Carla Barquest committed
    perlxmlbyt = perlxmlstr.decode('utf8') #required for python3 as perxmlstr is a string (decode so it is a bytes)
    perlxmlbyt = '<'+perlxmlbyt.split('<', 1)[-1]
    response = make_response(perlxmlbyt)                                           
    response.headers['Content-Type'] = 'text/xml; charset=utf-8' 
Carla Barquest's avatar
Carla Barquest committed
    return response