Back to index

moin  1.9.0~rc2
frontend.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - Common code for frontends (CGI/FCGI/SCGI)
00004 
00005     @copyright: 2008 MoinMoin:FlorianKrupicka
00006     @license: GNU GPL, see COPYING for details.
00007 """
00008 import optparse
00009 
00010 from MoinMoin.web.serving import make_application
00011 
00012 from MoinMoin import log
00013 logging = log.getLogger(__name__)
00014 
00015 class FrontEnd(object):
00016     def __init__(self):
00017         self.parser = optparse.OptionParser()
00018         self.add_options()
00019 
00020     def add_options(self):
00021         parser = self.parser
00022         parser.add_option("-d", "--debug", dest="debug",
00023                           help="Debug mode of server (off/web/external, default is to use MOIN_DEBUGGER env var)")
00024         parser.add_option("-c", "--config-dir", dest="config_dir", metavar="DIR",
00025                           help=("Path to the directory containing the wiki "
00026                                 "configuration files. Default: current directory"))
00027         parser.add_option("--htdocs", dest="htdocs",
00028                           help=("Path to the directory containing Moin's "
00029                                 "static files. Default: use builtin MoinMoin/web/static/htdocs"))
00030         parser.set_default('htdocs', True)
00031 
00032     def run(self, args=None):
00033         options, args = self.parser.parse_args(args)
00034         logging.debug('Options: %r', options)
00035 
00036         application = make_application(shared=options.htdocs)
00037 
00038         try:
00039             self.run_server(application, options)
00040         except SystemExit, err:
00041             # the flup CGIRequest uses sys.exit(0) to terminate
00042             if err.code: # log a non-zero exit status (0 means no error)
00043                 logging.exception('A SystemExit(%d) exception occurred.' % err.code)
00044             raise # exit now with this exit status
00045         except:
00046             logging.exception('An exception occurred while running %s' % self.__class__.__name__)
00047 
00048 class ServerFrontEnd(FrontEnd):
00049     def add_options(self):
00050         super(ServerFrontEnd, self).add_options()
00051         parser = self.parser
00052         parser.add_option("-p", "--port", dest="port", type="int",
00053                           help="Set the port to listen on. If not given, act as CGI/FCGI script.")
00054         parser.add_option("-i", "--interface", dest="interface",
00055                           help=("Set the interface/socket to listen on. If starts "
00056                                 "with '/' or './' it is interpreted as a path "
00057                                 "to a unix socket. If not given, act as CGI/FCGI script."))
00058         # Note: interface default MUST be None, do not set it to something else!
00059         # Otherwise CGI (and also when the FCGI process is spawned by the web server) won't work.
00060 
00061 class FrontEndNotAvailable(Exception):
00062     """ Raised if a frontend is not available for one reason or another. """
00063     pass