comparison dogdish/dispatcher.py @ 11:5babc2ae6c27

more stubbing
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 17 Oct 2012 10:18:28 -0700
parents f517d80bafe0
children 234c2427e52b
comparison
equal deleted inserted replaced
10:235738f81a3d 11:5babc2ae6c27
8 import os 8 import os
9 import sys 9 import sys
10 from urlparse import urlparse 10 from urlparse import urlparse
11 from webob import Request 11 from webob import Request
12 from webob import Response, exc 12 from webob import Response, exc
13 from ConfigParser import RawConfigParser as ConfigParser
13 14
14 here = os.path.dirname(os.path.abspath(__file__)) 15 here = os.path.dirname(os.path.abspath(__file__))
16
17
18 class Application(object):
19 """class for storing application.ini data"""
20
21 def __init__(self, filename):
22 """
23 - filename : path to an application.ini file
24 """
25
26 ### request handlers
15 27
16 class Handler(object): 28 class Handler(object):
17 """abstract handler object for a request""" 29 """abstract handler object for a request"""
18 30
19 def __init__(self, request): 31 def __init__(self, request):
73 85
74 ### class level variables 86 ### class level variables
75 defaults = {'directory': here} 87 defaults = {'directory': here}
76 88
77 def __init__(self, **kw): 89 def __init__(self, **kw):
90
91 # set defaults
78 for key in self.defaults: 92 for key in self.defaults:
79 setattr(self, key, kw.get(key, self.defaults[key])) 93 setattr(self, key, kw.get(key, self.defaults[key]))
80 self.handlers = [ Get ] 94 self.handlers = [ Get ]
95 self.updates = {}
96 self.current_update = None
81 97
82 ### methods dealing with HTTP 98 # scan directory
99 self.scan()
100
83 def __call__(self, environ, start_response): 101 def __call__(self, environ, start_response):
102 """WSGI application"""
103
84 request = Request(environ) 104 request = Request(environ)
85 for h in self.handlers: 105 for h in self.handlers:
86 if h.match(request): 106 if h.match(request):
87 handler = h(request) 107 handler = h(request)
88 break 108 break
89 else: 109 else:
90 handler = exc.HTTPNotFound 110 handler = exc.HTTPNotFound
91 res = handler() 111 res = handler()
92 return res(environ, start_response) 112 return res(environ, start_response)
93 113
114 def scan(self):
115 """scan the directory for updates"""
116 contents = os.listdir(self.directory)
117
118
94 def main(args=sys.argv[1:]): 119 def main(args=sys.argv[1:]):
95 """CLI entry point""" 120 """CLI entry point"""
96 121
122 # imports for CLI
97 import optparse 123 import optparse
98 from wsgiref import simple_server 124 from wsgiref import simple_server
99 125
126 # parse CLI options
100 parser = optparse.OptionParser() 127 parser = optparse.OptionParser()
101
102 parser.add_option('-p', '--port', dest='port', 128 parser.add_option('-p', '--port', dest='port',
103 default=8080, type='int', 129 default=8080, type='int',
104 help="port to serve on") 130 help="port to serve on")
131 parser.add_option('-d', '--directory', dest='directory',
132 default=os.getcwd(),
133 help="directory of update files")
105 options, args = parser.parse_args() 134 options, args = parser.parse_args()
106 135
107 app = Dispatcher() 136 # create the app
137 app = Dispatcher(directory=options.directory)
108 138
139 # serve the app
109 print "http://localhost:%s/" % options.port 140 print "http://localhost:%s/" % options.port
110 server = simple_server.make_server(host='0.0.0.0', port=options.port, app=app) 141 server = simple_server.make_server(host='0.0.0.0', port=options.port, app=app)
111 server.serve_forever() 142 server.serve_forever()
112 143
113 if __name__ == '__main__': 144 if __name__ == '__main__':