eggs/djangorecipe-0.20-py2.6.egg/djangorecipe/wsgi.py
changeset 307 c6bca38c1cbf
equal deleted inserted replaced
306:5ff1fc726848 307:c6bca38c1cbf
       
     1 import sys
       
     2 
       
     3 from django.core import management
       
     4 
       
     5 def main(settings_file, logfile=None):
       
     6     try:
       
     7         mod = __import__(settings_file)
       
     8         components = settings_file.split('.')
       
     9         for comp in components[1:]:
       
    10             mod = getattr(mod, comp)
       
    11 
       
    12     except ImportError, e:
       
    13         sys.stderr.write("Error loading the settings module '%s': %s"
       
    14                             % (settings_file, e))
       
    15         sys.exit(1)
       
    16 
       
    17     # Setup settings
       
    18     management.setup_environ(mod)
       
    19 
       
    20     if logfile:
       
    21         import datetime
       
    22         class logger(object):
       
    23             def __init__(self, logfile):
       
    24                 self.logfile = logfile
       
    25 
       
    26             def write(self, data):
       
    27                 self.log(data)
       
    28 
       
    29             def writeline(self, data):
       
    30                 self.log(data)
       
    31 
       
    32             def log(self, msg):
       
    33                 line = '%s - %s\n' % (
       
    34                     datetime.datetime.now().strftime('%Y%m%d %H:%M:%S'), msg)
       
    35                 fp = open(self.logfile, 'a')
       
    36                 try:
       
    37                     fp.write(line)
       
    38                 finally:
       
    39                     fp.close()
       
    40         sys.stdout = sys.stderr = logger(logfile)
       
    41 
       
    42     from django.core.handlers.wsgi import WSGIHandler
       
    43 
       
    44     # Run WSGI handler for the application
       
    45     return WSGIHandler()