Mercurial > hg > wsgintegrate
comparison wsgintegrate/main.py @ 3:05683af3240c
fix up main.py
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 13 Jun 2011 17:55:34 -0700 |
parents | ec815b7cb142 |
children | 30e69abaadbb |
comparison
equal
deleted
inserted
replaced
2:70fa0bda7726 | 3:05683af3240c |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 """ | 3 """ |
4 command line entry point for wsgiblob | 4 command line entry point for wsgintegrate |
5 serves an application from a .ini file using the [server] | 5 serves an application from a .ini file |
6 section specified or wsgiref otherwise | |
7 """ | 6 """ |
8 | 7 |
9 import sys | 8 import sys |
10 from pyloader.factory import IniFactory | 9 from factory import WSGIfactory |
11 from optparse import OptionParser | 10 from optparse import OptionParser |
12 from server import wsgiref | 11 from server import wsgiref |
13 | 12 |
14 def main(args=sys.argv[1:]): | 13 def main(args=sys.argv[1:]): |
15 | 14 |
16 # parse command line options | 15 # parse command line options |
17 usage = '%prog [options] config-file' | 16 usage = '%prog [options] config-file' |
18 parser = OptionParser(usage=usage, description=__doc__) | 17 parser = OptionParser(usage=usage, description=__doc__) |
19 parser.add_option('-a', '--app', dest='app', default='', | 18 parser.add_option('-a', '--app', dest='app', default='', |
20 help='which app to serve from the configuration') | 19 help='which app to serve from the configuration') |
21 parser.add_option('-p', '--port', dest='port', | 20 parser.add_option('-p', '--port', dest='port', |
22 type='int', default=80, | 21 type='int', default=80, |
23 help='which port to serve on, if server not specified in configuration') | 22 help='which port to serve on, if server not specified in configuration') |
24 parser.add_option('--list-apps', dest='list_apps', | 23 parser.add_option('--list-apps', dest='list_apps', |
25 action='store_true', default=False, | 24 action='store_true', default=False, |
26 help='list the WSGI apps available in the configuration') | 25 help='list the WSGI apps available in the configuration') |
27 parser.add_option('--print-json', dest='print_json', | 26 parser.add_option('--print-json', dest='print_json', |
28 action='store_true', default=False, | 27 action='store_true', default=False, |
29 help='print JSON format of the configuration') | 28 help='print JSON format of the configuration') |
30 parser.add_option('--print-ini', dest='print_ini', | 29 parser.add_option('--print-ini', dest='print_ini', |
31 action='store_true', default=False, | 30 action='store_true', default=False, |
32 help='print .ini format of the configuration') | 31 help='print .ini format of the configuration') |
33 options, args = parser.parse_args(args) | 32 options, args = parser.parse_args(args) |
34 | 33 |
35 # check for single configuration file | 34 # check for single configuration file |
36 if len(args) != 1: # TODO: could munge multiple configs | 35 if len(args) != 1: # TODO: could munge multiple configs |
37 parser.print_usage() | 36 parser.print_usage() |
38 parser.exit() | 37 parser.exit() |
39 config = args[0] | 38 config = args[0] |
40 | 39 |
41 # create a factory from configuration | 40 # create a factory from configuration |
42 # TODO: interpret if the configuration is .ini, JSON, etc | 41 # TODO: interpret if the configuration is .ini, JSON, etc |
43 factory = IniFactory(config, main=options.app) | 42 factory = IniFactory(config, main=options.app) |
44 | 43 |
45 # print configuration, if specified | 44 # print configuration, if specified |
46 if options.list_apps: | 45 if options.list_apps: |
47 for app in sorted(factory.config.keys()): | 46 for app in sorted(factory.config.keys()): |
48 print app | 47 print app |
49 return | 48 return |
50 if options.print_json: | 49 if options.print_json: |
51 print factory.json_config() | 50 print factory.json_config() |
52 return | 51 return |
53 if options.print_ini: | 52 if options.print_ini: |
54 print factory.ini_config() | 53 print factory.ini_config() |
55 return | 54 return |
56 | 55 |
57 wsgiref(app=factory.load(), port=options.port) | 56 # serve it |
57 wsgiref(app=factory.load(), port=options.port) | |
58 | 58 |
59 if __name__ == '__main__': | 59 if __name__ == '__main__': |
60 main() | 60 main() |