Mercurial > hg > wsgintegrate
annotate wsgintegrate/main.py @ 22:37b47aedcc3d
STUB: wsgintegrate/main.py
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Thu, 06 Mar 2014 23:14:37 -0800 |
parents | 3621afe99a72 |
children | d742aff49859 |
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 """ |
20 | 4 command line interface 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) |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 """ |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 import sys |
3 | 10 from factory import WSGIfactory |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 from optparse import OptionParser |
20 | 12 from server import servers |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 def main(args=sys.argv[1:]): |
20 | 15 """CLI""" |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 |
3 | 17 # parse command line options |
18 usage = '%prog [options] config-file' | |
19 parser = OptionParser(usage=usage, description=__doc__) | |
20 parser.add_option('-a', '--app', dest='app', default='', | |
21 help='which app to serve from the configuration') | |
22 parser.add_option('-p', '--port', dest='port', | |
23 type='int', default=80, | |
24 help='which port to serve on, if server not specified in configuration') | |
25 parser.add_option('--list-apps', dest='list_apps', | |
26 action='store_true', default=False, | |
27 help='list the WSGI apps available in the configuration') | |
28 parser.add_option('--print-json', dest='print_json', | |
29 action='store_true', default=False, | |
30 help='print JSON format of the configuration') | |
31 parser.add_option('--print-ini', dest='print_ini', | |
32 action='store_true', default=False, | |
33 help='print .ini format of the configuration') | |
20 | 34 if len(servers) > 1: |
35 parser.add_option('-s', '--server', dest='server', | |
36 choices=servers.keys(), default='wsgiref', | |
21 | 37 help="which WSGI server to use [Choices: %s]" % ', '.join(servers.keys())) |
3 | 38 options, args = parser.parse_args(args) |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
39 |
3 | 40 # check for single configuration file |
41 if len(args) != 1: # TODO: could munge multiple configs | |
42 parser.print_usage() | |
43 parser.exit() | |
44 config = args[0] | |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
45 |
3 | 46 # create a factory from configuration |
47 # TODO: interpret if the configuration is .ini, JSON, etc | |
6 | 48 factory = WSGIfactory(config, main=options.app) |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
49 |
3 | 50 # print configuration, if specified |
51 if options.list_apps: | |
52 for app in sorted(factory.config.keys()): | |
53 print app | |
54 return | |
55 if options.print_json: | |
56 print factory.json_config() | |
57 return | |
58 if options.print_ini: | |
59 print factory.ini_config() | |
60 return | |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
61 |
3 | 62 # serve it |
20 | 63 server_name = getattr(options, 'server', 'wsgiref') |
22 | 64 print ("Serving with %s" % (server_name)) |
20 | 65 server = servers[server_name] |
66 print ('http://localhost:%d/' % options.port) | |
67 server(app=factory.load(), port=options.port) | |
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
68 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
69 if __name__ == '__main__': |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
70 main() |