views:

16

answers:

1

It's a bit ugly that many lines of code fall into "__main__". Can someone give me a tip of how to move SessionMiddleware into handle method? I should notice that I use session in CoreXmlParser. Thanks in advance !

def handle(environ, start_response):
        req = webob.Request(environ)
        c = CoreXmlParser(req)
        resp = webob.Response(body=c(), charset = 'utf-8', status='200 OK', \
        request=req, content_type='text/xml')
        resp(environ, start_response)
        return resp.app_iter

    if __name__ == '__main__':
        #parse config file for session options
        app = SessionMiddleware(handle, some_session_opts_here)
        from flup.server.fcgi import WSGIServer
        WSGIServer(app).run()
A: 

I'm not sure I understand why you're trying to move just one line. If you want to reduce the amount of stuff in "__main__", why not just move all that "#parse config file" stuff into a separate function?

def handle(environ, start_response):
    # same as before

def create_app(config_file):
    #parse config file for session options
    return SessionMiddleWare(handle, some_session_opts_here)

if __name__ == '__main__':
    app = create_app(config_file)
    from flup.server.fcgi import WSGIServer
    WSGIServer(app).run()
Peter Milley