1# -*- coding: iso-8859-1 -*-
2"""
3    MoinMoin - Common code for frontends (CGI/FCGI/SCGI)
4
5    @copyright: 2008 MoinMoin:FlorianKrupicka
6    @license: GNU GPL, see COPYING for details.
7"""
8import optparse
9
10from MoinMoin.web.serving import make_application
11
12from MoinMoin import log
13logging = log.getLogger(__name__)
14
15class FrontEnd(object):
16    def __init__(self):
17        self.parser = optparse.OptionParser()
18        self.add_options()
19
20    def add_options(self):
21        parser = self.parser
22        parser.add_option("-d", "--debug", dest="debug",
23                          help="Debug mode of server (off/web/external, default is to use MOIN_DEBUGGER env var)")
24        parser.add_option("-c", "--config-dir", dest="config_dir", metavar="DIR",
25                          help=("Path to the directory containing the wiki "
26                                "configuration files. Default: current directory"))
27        parser.add_option("--htdocs", dest="htdocs",
28                          help=("Path to the directory containing Moin's "
29                                "static files. Default: use builtin MoinMoin/web/static/htdocs"))
30        parser.set_default('htdocs', True)
31
32    def run(self, args=None):
33        options, args = self.parser.parse_args(args)
34        logging.debug('Options: %r', options)
35
36        application = make_application(shared=options.htdocs)
37
38        try:
39            self.run_server(application, options)
40        except SystemExit, err:
41            # the flup CGIRequest uses sys.exit(0) to terminate
42            if err.code: # log a non-zero exit status (0 means no error)
43                logging.exception('A SystemExit(%d) exception occurred.' % err.code)
44            raise # exit now with this exit status
45        except:
46            logging.exception('An exception occurred while running %s' % self.__class__.__name__)
47
48class ServerFrontEnd(FrontEnd):
49    def add_options(self):
50        super(ServerFrontEnd, self).add_options()
51        parser = self.parser
52        parser.add_option("-p", "--port", dest="port", type="int",
53                          help="Set the port to listen on.")
54        parser.add_option("-i", "--interface", dest="interface",
55                          help=("Set the interface/socket to listen on. If starts "
56                                "with '/' or './' it is interpreted as a path "
57                                "to a unix socket."))
58        # Note: interface default MUST be None, do not set it to something else!
59        # Otherwise CGI (and also when the FCGI process is spawned by the web server) won't work.
60
61class FrontEndNotAvailable(Exception):
62    """ Raised if a frontend is not available for one reason or another. """
63    pass
64