annotate wsgintegrate/main.py @ 19:4d9a2a2fb8c0

print out the localhost url and other minor fixes
author Jeff Hammel <jhammel@mozilla.com>
date Sat, 09 Mar 2013 23:03:48 -0800
parents 30e69abaadbb
children caf763fc1c7d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ec815b7cb142 initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
ec815b7cb142 initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
ec815b7cb142 initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
3
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
4 command line entry point for wsgintegrate
19
4d9a2a2fb8c0 print out the localhost url and other minor fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
5 serves an application from a .ini file
4d9a2a2fb8c0 print out the localhost url and other minor fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
6 (really, a DAG)
4d9a2a2fb8c0 print out the localhost url and other minor fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
7
4d9a2a2fb8c0 print out the localhost url and other minor fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
8 uses wsgiref, though could in theory make use of
4d9a2a2fb8c0 print out the localhost url and other minor fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
9 other WSGI servers, if available (TODO)
0
ec815b7cb142 initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 """
ec815b7cb142 initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11
ec815b7cb142 initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 import sys
3
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
13 from factory import WSGIfactory
0
ec815b7cb142 initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 from optparse import OptionParser
ec815b7cb142 initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 from server import wsgiref
ec815b7cb142 initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16
ec815b7cb142 initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 def main(args=sys.argv[1:]):
ec815b7cb142 initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18
3
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
19 # parse command line options
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
20 usage = '%prog [options] config-file'
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
21 parser = OptionParser(usage=usage, description=__doc__)
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
22 parser.add_option('-a', '--app', dest='app', default='',
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
23 help='which app to serve from the configuration')
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
24 parser.add_option('-p', '--port', dest='port',
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
25 type='int', default=80,
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
26 help='which port to serve on, if server not specified in configuration')
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
27 parser.add_option('--list-apps', dest='list_apps',
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
28 action='store_true', default=False,
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
29 help='list the WSGI apps available in the configuration')
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
30 parser.add_option('--print-json', dest='print_json',
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
31 action='store_true', default=False,
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
32 help='print JSON format of the configuration')
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
33 parser.add_option('--print-ini', dest='print_ini',
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
34 action='store_true', default=False,
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
35 help='print .ini format of the configuration')
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
36 options, args = parser.parse_args(args)
0
ec815b7cb142 initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37
3
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
38 # check for single configuration file
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
39 if len(args) != 1: # TODO: could munge multiple configs
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
40 parser.print_usage()
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
41 parser.exit()
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
42 config = args[0]
0
ec815b7cb142 initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43
3
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
44 # create a factory from configuration
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
45 # TODO: interpret if the configuration is .ini, JSON, etc
6
30e69abaadbb use correct factory type
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
46 factory = WSGIfactory(config, main=options.app)
0
ec815b7cb142 initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47
3
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
48 # print configuration, if specified
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
49 if options.list_apps:
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
50 for app in sorted(factory.config.keys()):
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
51 print app
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
52 return
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
53 if options.print_json:
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
54 print factory.json_config()
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
55 return
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
56 if options.print_ini:
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
57 print factory.ini_config()
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
58 return
0
ec815b7cb142 initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59
3
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
60 # serve it
19
4d9a2a2fb8c0 print out the localhost url and other minor fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
61 print 'http://localhost:%d/' % options.port
3
05683af3240c fix up main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
62 wsgiref(app=factory.load(), port=options.port)
0
ec815b7cb142 initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
63
ec815b7cb142 initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
64 if __name__ == '__main__':
ec815b7cb142 initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
65 main()