Mercurial > hg > wsgintegrate
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 |
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 | 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 | 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 | 19 # parse command line options |
20 usage = '%prog [options] config-file' | |
21 parser = OptionParser(usage=usage, description=__doc__) | |
22 parser.add_option('-a', '--app', dest='app', default='', | |
23 help='which app to serve from the configuration') | |
24 parser.add_option('-p', '--port', dest='port', | |
25 type='int', default=80, | |
26 help='which port to serve on, if server not specified in configuration') | |
27 parser.add_option('--list-apps', dest='list_apps', | |
28 action='store_true', default=False, | |
29 help='list the WSGI apps available in the configuration') | |
30 parser.add_option('--print-json', dest='print_json', | |
31 action='store_true', default=False, | |
32 help='print JSON format of the configuration') | |
33 parser.add_option('--print-ini', dest='print_ini', | |
34 action='store_true', default=False, | |
35 help='print .ini format of the configuration') | |
36 options, args = parser.parse_args(args) | |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
37 |
3 | 38 # check for single configuration file |
39 if len(args) != 1: # TODO: could munge multiple configs | |
40 parser.print_usage() | |
41 parser.exit() | |
42 config = args[0] | |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
43 |
3 | 44 # create a factory from configuration |
45 # TODO: interpret if the configuration is .ini, JSON, etc | |
6 | 46 factory = WSGIfactory(config, main=options.app) |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
47 |
3 | 48 # print configuration, if specified |
49 if options.list_apps: | |
50 for app in sorted(factory.config.keys()): | |
51 print app | |
52 return | |
53 if options.print_json: | |
54 print factory.json_config() | |
55 return | |
56 if options.print_ini: | |
57 print factory.ini_config() | |
58 return | |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
59 |
3 | 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 | 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() |