comparison wsgraph/web.py @ 12:421d5119e324

reluctantly make this a CLI entry point
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 10 Dec 2012 17:30:44 -0800
parents 9016b126aa87
children 7459702bf574
comparison
equal deleted inserted replaced
11:7b8e40eda563 12:421d5119e324
3 """ 3 """
4 web handler for WSGraph 4 web handler for WSGraph
5 """ 5 """
6 6
7 import json 7 import json
8 import sys
8 from webob import Request, Response, exc 9 from webob import Request, Response, exc
9 10
10 # rank constants 11 # rank constants
11 GRAPH = 0 12 GRAPH = 0
12 NODE = 1 13 NODE = 1
104 105
105 """ 106 """
106 107
107 # get resource requestor 108 # get resource requestor
108 segments = self.path_segments(request.path_info) 109 segments = self.path_segments(request.path_info)
109 if len(segments) not in (0,1,2): 110 rank = len(segments)
111 if rank not in (0,1,2):
110 return exc.HTTPNotFound() 112 return exc.HTTPNotFound()
111 113
112 114
113 # is resource in the graph? [TODO] 115 # is resource in the graph? [TODO]
116 if (rank == EDGE) or (rank == NODE):
117 if tuple(segments) not in self.graph:
118 retun exc.HTTPNotFound()
114 119
115 # formatter 120 # formatter
116 formatter = self.formatters[len(segments)] 121 formatter = self.formatters[len(segments)]
117 return Response(content_type='text/plain', 122 return Response(content_type='text/plain',
118 body="WSGraph") 123 body="WSGraph")
133 raise NotImplementedError 138 raise NotImplementedError
134 139
135 def OPTIONS(self, request): 140 def OPTIONS(self, request):
136 return Response() # TODO: Allow=', '.join(self.methods); Content-Length=0 141 return Response() # TODO: Allow=', '.join(self.methods); Content-Length=0
137 142
138 if __name__ == '__main__':
139 143
140 ### example web server 144 def main(args=sys.argv[1:]):
145 """example web server"""
141 146
142 # imports 147 # imports
143 from wsgiref import simple_server 148 from wsgiref import simple_server
144 from model import MemoryCache 149 from model import MemoryCache
145 from optparse import OptionParser 150 from optparse import OptionParser
157 app = Dispatcher(graph=graph) 162 app = Dispatcher(graph=graph)
158 server = simple_server.make_server(host='0.0.0.0', port=options.port, app=app) 163 server = simple_server.make_server(host='0.0.0.0', port=options.port, app=app)
159 print 'http://localhost:%s/' % options.port 164 print 'http://localhost:%s/' % options.port
160 server.serve_forever() 165 server.serve_forever()
161 166
167 if __name__ == '__main__':
168 main()