On Apr 15, 2010, at 7:19 PM, Christian Meisenbichler wrote:
>>            params = dict(urlparse.parse_psl(url.query)
> 
> my urlparse module doesn't have a parse_psl attribute (so it says)

Grr. Next time I'll copy&paste . It's parse_qsl not pql.

("qsl" here is "parse Query String and return a List")



> I tried to understand what  WSGI  may mean here. I didn't hear of this
> before. Does it mean to have a URL like /convert/informat/outformat ?
> 
> I think, I do not see a big advantage for this here. Should I?

WSGI is a relatively standard way to write modules and middleware for Python 
which is supported by many Python web servers. The goal is to improve 
portability, so that code you write for one system doesn't need to be rewritten 
for another. Details about it are in

  http://docs.python.org/library/wsgiref.html
  http://www.wsgi.org/wsgi/

I've written your module to work under WSGI, although I've got a problem with 
my local OB install so I can't fully test it. It's not a direct comparison 
regarding LOC because I simplified your XML generation set a bit, but it's 
enough to show a few things that are easier:

  - I can send the response headers as a list name/value tuples rather than a 
function call for each one,
  - I use 'yield' more like a sort of print statement, rather then 
wfile.write(text)
  - The most important HTTP variables are preextracted (like the query string)

One thing is more complicated - it wants the text which goes along with the 
HTTP error code.

I also didn't port over the exception handling code you wrote.

#Copyright Christian Meisenbichler
# WSGI modifications by Andrew Dalke

from wsgiref.simple_server import make_server
import openbabel
import urlparse
import cgi


def convert_service_app(environ, start_response):
    method = environ["REQUEST_METHOD"]
    if (method == "GET" and
        environ["PATH_INFO"].endswith("/supportedformats")):
        return list_supported_formats(environ, start_response)
    elif method == "POST":
        return convert(environ, start_response)    
    else:
        start_response("404 Not Found", [])
        return []

def _formats_to_xml(formats):
    for format in formats:
        suffix, description = [s.strip() for s in format.split("--", 1)]
        yield '    <format suffix="%s">%s</format>\n' % (
            suffix, cgi.escape(description))

def list_supported_formats(environ, start_response):
    start_response("200 Ok", [
        ("Content-Type", "text/xml")])
    obConversion = openbabel.OBConversion()
    informs = obConversion.GetSupportedInputFormat()
    outforms = obConversion.GetSupportedOutputFormat()
    yield "<formats>\n"
    yield "  <in>\n"
    for xml_line in _formats_to_xml(informs):
        yield xml_line
    yield "  </in>\n"
    yield "  <out>\n"
    for xml_line in _formats_to_xml(outforms):
        yield xml_line
    yield "  </out>\n"
    yield "</formats>\n"


def convert(environ, start_response):
    #content_type = environ.get("CONTENT_TYPE", None)
    #print content_type
    content_length = int(environ["CONTENT_LENGTH"])
    params = dict(urlparse.parse_qsl(environ.get("QUERY_STRING", "")))
    obConversion = openbabel.OBConversion()
    obConversion.SetInAndOutFormats(params['in'], params['out'])
    mol = openbabel.OBMol()
    obConversion.ReadString(mol, environ["wsgi.input"].read(content_length))

    start_response("303 See Other", [
        ("Content-Type", "application/text")])
    return obConversion.WriteString(mol)


def main():
    httpd = make_server("", 8071, convert_service_app)
    print 'started httpserver...'
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        print '^C received, shutting down server'

if __name__ == '__main__':
    main()



                                Andrew
                                da...@dalkescientific.com


------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
OpenBabel-discuss mailing list
OpenBabel-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbabel-discuss

Reply via email to