Пример #1
0
def serve_demo():
  filename = "demo/demo-chemokine.web"
  assert os.path.exists(filename)

  spydertype = Spyder.AttractEasyModel    
  formlib = form_standard
  cgiscript = "attractserver-easy.py"
  model = spydertype.fromfile(filename)
  
  import random
  from spyder.formtools import embed
  embed(model)     
  filename2 = os.path.split(filename)[0] + "-" + str(random.randint(1,1000000))  
  os.chdir("/tmp")
  model.tofile(filename2)
  
  f = spydertype._form()
  f = formlib.webform_easy(f, model)
  header = formlib.header.replace("<head>", "<head>\n        <base href=\"%s\" target=\"_blank\">" % webdir, 1)
  html = attracthtmlform.htmlform(
    obj=model, 
    form=f, 
    cgi=cgidir+cgiscript, 
    header=header, 
    footer=formlib.footer, 
    header_indentation = 12,
    resourcefilename=filename2,
  )
  return html
def serve_upload():
    webform = cgi.FieldStorage()
    if "protocolfile" not in webform:
        raise Exception  ### TODO, nice error message
    data = webform["protocolfile"].value
    interface = webform["interface"].value
    typ, content = spyder.core.parse(data)

    conversion = False
    if interface == "auto":
        interface = typ
    else:
        conversion = True
    if interface == "AttractModel":
        spydertype = Spyder.AttractModel
        formlib = form_model
        cgiscript = "attractserver.py"
    elif interface == "AttractEasyModel":
        spydertype = Spyder.AttractEasyModel
        formlib = form_standard
        cgiscript = "attractserver-easy.py"
    elif interface == "AttractPeptideModel":
        spydertype = Spyder.AttractPeptideModel
        formlib = form_peptide
        cgiscript = "attractserver-peptide.py"
    else:
        raise ValueError(interface)  ### TODO, nice error message

    if conversion:
        spydertype2 = getattr(Spyder, typ)
        model = spydertype2.fromdict(content)
        model = model.convert(spydertype)
    else:
        model = spydertype.fromdict(content)
    import random
    from spyder.formtools import embed
    embed(model)
    mydir = "attract-upload" + str(random.randint(1, 1000000))
    fname = "attract.web"
    os.chdir("/tmp")
    os.mkdir(mydir)
    os.chdir(mydir)
    model.tofile(fname)

    f = spydertype._form()
    f = formlib.webform(f, model)
    header = formlib.header.replace(
        "<head>",
        "<head>\n        <base href=\"%s\" target=\"_blank\">" % webdir, 1)
    html = attracthtmlform.htmlform(
        obj=model,
        form=f,
        cgi=cgidir + cgiscript,
        header=header,
        footer=formlib.footer,
        header_indentation=12,
        resourcefilename=mydir + "/" + fname,
    )
    return html
Пример #3
0
def html(form, cgi, spyderobj, newtab=False, header=header0, footer=footer0):
    import random
    import attracthtmlform
    args = dict(
        obj=spyderobj,
        form=form,
        cgi=cgi,
        header=header,
        footer=footer,
        header_indentation=12,
        newtab=newtab,
    )
    if spyderobj is not None:
        from spyder.formtools import embed
        embed(spyderobj)
        mydir = "attractrun" + str(random.randint(1, 1000000))
        fname = "attract.web"
        os.chdir("/tmp/")
        os.mkdir(mydir)
        os.chdir(mydir)
        spyderobj.tofile(fname)
        args["resourcefilename"] = mydir + "/" + fname
    html = attracthtmlform.htmlform(**args)
    return html
#!/usr/bin/python
"""
Command line tool to embed a Spyder model
"""

import sys, os
currdir = os.path.abspath(os.path.split(__file__)[0])
guidir = currdir + "/../gui"
sys.path.insert(0, guidir)
import spyder, Spyder
import attractmodel
from spyder.formtools import embed

inputfile = sys.argv[1]
inputdata = open(inputfile).read()
inputdir = os.path.split(inputfile)[0]
if len(inputdir): os.chdir(inputdir)
spydertypename, spyderdict = spyder.core.parse(inputdata)
spydertype = getattr(Spyder, spydertypename)
model = spydertype.fromdict(spyderdict)
embed(model)
print repr(model)
def serve_attract(spydertype, formlib, deploy, **kwargs):
    # Obtain the webdict (what the user submitted) and f, the spyderform used to generate the HTML
    webdict = spyder.formtools.cgi.dict_from_fieldstorage(cgi.FieldStorage())
    easy = "easy" in kwargs and kwargs["easy"]
    if easy:
        f = formlib.webserverform_easy(webdict, spydertype=spydertype)
    else:
        f = formlib.webserverform(webdict, spydertype=spydertype)

    # Fill in links to embedded resources
    resourceobj = None
    resourcefilevar = getattr(f, "resourcefilevar", None)
    if resourcefilevar is not None and resourcefilevar in webdict:
        tmpf = "/tmp/" + webdict[resourcefilevar]
        resourceobj = spydertype.fromfile(tmpf)
    newmodel, status, delta = spyder.formtools.cgi.cgi(webdict,
                                                       f,
                                                       resourceobj,
                                                       spydertype=spydertype)

    # Detect empty form
    if not len(webdict) or delta is None:
        raise AttractServerError(status="You didn't submit any data",
                                 delta=None)

    #Special case: use_iattract is by default False in the easy web interface, but True in the standard interface
    if easy and "use_iattract" not in delta:
        delta["use_iattract"] = False
    if easy and delta["use_iattract"]:  #bug in Spyder?
        newmodel.use_iattract = True

    # Create a result directory
    cwd = os.getcwd()
    os.chdir(localresultdir)
    mydir = "run" + str(random.randint(1, 1000000))
    os.mkdir(mydir)

    # Detect errors in the form submission
    # We will still give the user the delta file
    deltamessage = format_delta(delta, mydir)
    if status.splitlines()[0].find("OK") == -1:
        raise AttractServerError(status=status, delta=deltamessage)

    # New model was received OK, should be no bugs from here on...
    try:
        # Obtain a runname
        runname = getattr(newmodel, "runname", None)
        if runname is None or runname == "attract":
            runname = "attract-" + datetime.datetime.now().date().isoformat()
        runname = ''.join(
            [str(char) for char in runname if char in string.printable])
        runname = re.sub(r'[^a-zA-Z0-9\.]', '_', runname)

        # Embed the model, and save the delta
        os.chdir(cwd)
        newmodel.runname = runname
        embed(newmodel)
        fname_embedded = "%s-embedded.web" % runname
        fname = "%s.web" % runname
        os.chdir(localresultdir)
        deltamessage = format_delta(delta, mydir, runname)
        os.chdir(mydir)
        newmodel.tofile(fname_embedded)
    except:
        # Processing error 1
        error = traceback.format_exc()
        response = response_processing1_error % error
        response += deltamessage
        return response

    try:
        #Create an archive directory equal to runname
        os.mkdir(runname)
        os.chdir(runname)
        deploy(newmodel, ".")
        newmodel.tofile(fname)
    except:
        # Processing error 2
        error = traceback.format_exc()
        response = response_processing2_error % error
        response += response_embedded % (webresultdir + mydir + "/" +
                                         fname_embedded, fname_embedded)
        response += deltamessage
        return response

    try:
        #Run the generator, write the script and create the archive
        if isinstance(newmodel, Spyder.AttractPeptideModel):
            newmodel = newmodel.convert(Spyder.AttractEasyModel)
            r = newmodel.partners[1].pdbfile
            r.link("peptide.pdb")
            r.save()
        script = newmodel.generate()
        shname = "%s.sh" % runname
        f = open(shname, "w")
        f.write(script)
        f.close()
        os.system("chmod +x %s" % shname)
        os.chdir("..")
        os.system("chmod a+rw %s" % mydir)
        aname = "%s.tgz" % runname
        os.system("tar czf %s %s" % (aname, runname))
        assert os.path.exists(aname)
    except:
        # Generator error
        error = traceback.format_exc()
        response = response_generator_error % error
        response += response_embedded % (webresultdir + mydir + "/" +
                                         fname_embedded, fname_embedded)
        response += deltamessage
        return response

    response = response_success
    response += response_directory % (shname, webresultdir + mydir + "/" +
                                      aname, aname, fname)
    response += response_embedded % (webresultdir + mydir + "/" +
                                     fname_embedded, fname_embedded)
    response += response_rpbs % (rpbs_website, mydir, fname_embedded)
    response += deltamessage
    return response