a little wsgi framework

timmyt timmyt at smarttypes.org
Wed Jul 1 13:58:33 EDT 2009


i'm interested in getting opinions on a small wsgi framework i
assembled from webob, sqlalchemy, genshi, and various code fragments i
found on the inter-tubes

here is the interesting glue - any comments / suggestions would be
much appreciated

------------------
the wsgi app
------------------
def application(environ, start_response):
    path = environ.get('PATH_INFO', '').lstrip('/')

    for regex, callback in urls:
        match = re.search(regex, path)

        if match:
            environ['myapp.url_args'] = match.groups()
            request = webob.Request(environ)

            try:
                return callback(request, start_response)
            except Exception, ex:
                start_response('500 Internal Server Error', [('Content-
Type', 'text/plain')])
                return [traceback.format_exc()]

    start_response('404 Not Found', [('Content-Type', 'text/plain')])
    return ["Couldn't find the URL specified."]


----------------------------------
the controller decorator
----------------------------------
def web_decorator(filename, method='html'):

    def decorator(target):

        def wrapper(request, start_response):

            #genshi TemplateLoader
            template = loader.load(filename)

            global config

            try:
                return_dict = target(request, start_response)
                return_string = template.generate(**return_dict).render
(method)
                config['database.Session'].commit()
            except:
                config['database.Session'].rollback()
                raise
            finally:
                config['database.Session'].remove()

            #TODO: alter 'Content-Type' per method being passed
            start_response('200 OK', [('Content-Type', 'text/html')])
            return [return_string]

        return wrapper

    return decorator



More information about the Python-list mailing list