1. Massimo Di Pierro
  2. web2py

Source

web2py / gaehandler.py

Massimo Di Pierr… 71384d6 



































































#!/usr/bin/env python
# -*- coding: utf-8 -*-

import time
import os
import sys
import logging
import cPickle
import pickle
import wsgiref.handlers
import google

sys.path.append(os.path.dirname(__file__))
sys.modules['cPickle'] = sys.modules['pickle']

from gluon.settings import settings


if os.environ.get('SERVER_SOFTWARE', '').startswith('Devel'):
    (settings.web2py_runtime, settings.web2py_runtime_gae, debug) = \
        ('gae:development', True, True)
else:
    (settings.web2py_runtime, settings.web2py_runtime_gae, debug) = \
        ('gae:production', True, False)

import gluon.main


def log_stats(fun):
    """Function that will act as a decorator to make logging"""

    if debug:

        def newfun(env, res):
            """Log the execution time of the passed function"""

            timer = lambda t: (t.time(), t.clock())
            (t0, c0) = timer(time)
            executed_function = fun(env, res)
            (t1, c1) = timer(time)
            log_info = """**** Request: %.2fms/%.2fms (real time/cpu time)"""
            log_info = log_info % ((t1 - t0) * 1000, (c1 - c0) * 1000)
            logging.info(log_info)

            return executed_function

        return newfun
    else:
        return fun

# comment the line below and uncomment the decorator @log_stats to enable
# logging of stats on GAE
logging.basicConfig(level=35)

# @log_stats
def wsgiapp(env, res):
    """Return the wsgiapp"""

    return gluon.main.wsgibase(env, res)


def main():
    """Run the wsgi app"""

    wsgiref.handlers.CGIHandler().run(wsgiapp)


if __name__ == '__main__':
    main()