Package muntjac :: Module main
[hide private]
[frames] | no frames]

Source Code for Module muntjac.main

  1  # Copyright (C) 2012 Vaadin Ltd.  
  2  # Copyright (C) 2012 Richard Lincoln 
  3  #  
  4  # Licensed under the Apache License, Version 2.0 (the "License");  
  5  # you may not use this file except in compliance with the License.  
  6  # You may obtain a copy of the License at  
  7  #  
  8  #     http://www.apache.org/licenses/LICENSE-2.0  
  9  #  
 10  # Unless required by applicable law or agreed to in writing, software  
 11  # distributed under the License is distributed on an "AS IS" BASIS,  
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 13  # See the License for the specific language governing permissions and  
 14  # limitations under the License. 
 15   
 16  import sys 
 17  import logging 
 18  import webbrowser 
 19   
 20  from os.path import join, dirname 
 21   
 22  from optparse import OptionParser 
 23   
 24  from wsgiref.simple_server import make_server 
 25   
 26  from paste.session import SessionMiddleware 
 27  from paste.fileapp import DirectoryApp, FileApp 
 28   
 29  from muntjac.demo.util import InMemorySession 
 30   
 31  from muntjac.terminal.gwt.server.application_servlet import ApplicationServlet 
 32  from muntjac.demo.main import urlmap 
 33  from muntjac.test.suite import main as test 
 34   
 35   
36 -def muntjac(application, host='localhost', port=8880, nogui=False, 37 debug=False, serve=True, forever=True, servletClass=None, 38 *args, **kw_args):
39 40 if servletClass is None: 41 servletClass = ApplicationServlet 42 43 level = logging.DEBUG if debug else logging.INFO 44 45 logging.basicConfig(stream=sys.stdout, level=level, 46 format='%(levelname)s: %(message)s') 47 48 wsgi_app = servletClass(application, debug=debug, *args, **kw_args) 49 50 wsgi_app = SessionMiddleware(wsgi_app, session_class=InMemorySession) 51 52 url = 'http://%s:%d/' % (host, port) 53 54 if nogui == False: 55 webbrowser.open(url, new=0) 56 57 httpd = make_server(host, port, wsgi_app) 58 59 if serve: 60 print 'Serving at: %s' % url 61 if forever: 62 # Respond to requests until process is killed 63 httpd.serve_forever() 64 else: 65 # Serve one request, then exit 66 httpd.handle_request()
67 68
69 -def main(args=sys.argv[1:]):
70 71 parser = OptionParser( 72 usage='usage: muntjac [options]', 73 version='Muntjac Version %s' % '1.1.2') 74 75 parser.add_option('-t', '--test', action='store_true', 76 help='run tests and exit') 77 78 parser.add_option('--host', default='localhost', type='string', 79 help='WSGI server hostname') 80 81 parser.add_option('--port', default=8080, type='int', 82 help='WSGI server port number') 83 84 parser.add_option('--nogui', action='store_true', default=False, 85 help='do not open browser window') 86 87 parser.add_option('--debug', action='store_true', 88 help='run in debug mode') 89 90 parser.add_option('--contextRoot', default='', type='string', 91 help='path to VAADIN directory') 92 93 94 opts, args = parser.parse_args(args) 95 96 level = logging.DEBUG if opts.debug else logging.INFO 97 98 logging.basicConfig(stream=sys.stdout, level=level, 99 format='%(levelname)s: %(message)s') 100 101 if opts.test: 102 test() 103 else: 104 nargs = len(args) 105 if nargs > 0: 106 sys.stderr.write('Too many arguments') 107 parser.print_help() 108 sys.exit(2) 109 110 rootapp = FileApp(join(dirname(__file__), 'public', 'index.html')) 111 cssapp = DirectoryApp(join(dirname(__file__), 'public', 'css')) 112 imgapp = DirectoryApp(join(dirname(__file__), 'public', 'img')) 113 114 urlmap['/'] = rootapp 115 urlmap['/css'] = cssapp 116 urlmap['/img'] = imgapp 117 118 if opts.contextRoot: 119 ctxapp = DirectoryApp(join(opts.contextRoot, 'VAADIN')) 120 urlmap['/VAADIN'] = ctxapp 121 122 app = SessionMiddleware(urlmap, session_class=InMemorySession) 123 124 url = 'http://%s:%d/' % (opts.host, opts.port) 125 126 if not opts.nogui: 127 webbrowser.open(url, new=0) 128 129 print 'Serving at: %s' % url 130 131 httpd = make_server(opts.host, opts.port, app) 132 133 try: 134 httpd.serve_forever() 135 except KeyboardInterrupt: 136 print "\nExiting" 137 138 sys.exit(0)
139 140 141 if __name__ == '__main__': 142 main() 143