diff wsgintegrate/main.py @ 0:ec815b7cb142

initial commit of wsgintegrate
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 07 Jun 2011 08:03:09 -0700
parents
children 05683af3240c
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wsgintegrate/main.py	Tue Jun 07 08:03:09 2011 -0700
@@ -0,0 +1,60 @@
+#!/usr/bin/env python
+
+"""
+command line entry point for wsgiblob
+serves an application from a .ini file using the [server]
+section specified or wsgiref otherwise
+"""
+
+import sys
+from pyloader.factory import IniFactory
+from optparse import OptionParser
+from server import wsgiref
+
+def main(args=sys.argv[1:]):
+
+  # parse command line options
+  usage = '%prog [options] config-file'
+  parser = OptionParser(usage=usage, description=__doc__)
+  parser.add_option('-a', '--app', dest='app', default='',
+                    help='which app to serve from the configuration')
+  parser.add_option('-p', '--port', dest='port',
+                    type='int', default=80,
+                    help='which port to serve on, if server not specified in configuration')
+  parser.add_option('--list-apps', dest='list_apps',
+                    action='store_true', default=False,
+                    help='list the WSGI apps available in the configuration')
+  parser.add_option('--print-json', dest='print_json',
+                    action='store_true', default=False,
+                    help='print JSON format of the configuration')
+  parser.add_option('--print-ini', dest='print_ini',
+                    action='store_true', default=False,
+                    help='print .ini format of the configuration')
+  options, args = parser.parse_args(args)
+
+  # check for single configuration file
+  if len(args) != 1: # TODO: could munge multiple configs
+    parser.print_usage()
+    parser.exit()
+  config = args[0]
+
+  # create a factory from configuration
+  # TODO: interpret if the configuration is .ini, JSON, etc
+  factory = IniFactory(config, main=options.app)
+
+  # print configuration, if specified
+  if options.list_apps:
+    for app in sorted(factory.config.keys()):
+      print app
+    return
+  if options.print_json:
+    print factory.json_config()
+    return
+  if options.print_ini:
+    print factory.ini_config()
+    return
+
+  wsgiref(app=factory.load(), port=options.port)
+
+if __name__ == '__main__':
+  main()