Mercurial > hg > simpypi
annotate simpypi/factory.py @ 12:1cdb25cef7ee
print helpful message when running test app
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 28 Feb 2012 14:37:21 -0800 |
parents | 39080c0f1b08 |
children | d3efc504c0b1 |
rev | line source |
---|---|
0 | 1 import os |
2 | |
3 from dispatcher import Dispatcher | |
4 from paste.httpexceptions import HTTPExceptionHandler | |
5 from paste.urlparser import StaticURLParser | |
6 from pkg_resources import resource_filename | |
7 | |
8 class PassthroughFileserver(object): | |
9 """serve files if they exist""" | |
10 | |
4 | 11 def __init__(self, app, *directories): |
0 | 12 self.app = app |
4 | 13 missing = [i for i in directories |
14 if not os.path.exists(i) or not os.path.isdir(i)] | |
15 assert not missing, "Missing directories: %s" % missing | |
16 self.directories = [(directory, StaticURLParser(directory)) | |
17 for directory in directories] | |
0 | 18 |
19 def __call__(self, environ, start_response): | |
20 path = environ['PATH_INFO'].strip('/') | |
4 | 21 for directory, fileserver in self.directories: |
22 if path and os.path.exists(os.path.join(directory, path)): | |
23 return fileserver(environ, start_response) | |
0 | 24 return self.app(environ, start_response) |
25 | |
1 | 26 def factory(**app_conf): |
0 | 27 """create a webob view and wrap it in middleware""" |
4 | 28 directory = app_conf['directory'] |
1 | 29 app = Dispatcher(**app_conf) |
5 | 30 # TODO: something about serving simple_index, etc |
3 | 31 return PassthroughFileserver(app, resource_filename(__name__, 'static')) |
0 | 32 |
1 | 33 if __name__ == '__main__': |
2 | 34 import shutil |
1 | 35 import tempfile |
36 from wsgiref import simple_server | |
12
1cdb25cef7ee
print helpful message when running test app
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
37 port = 8080 |
1cdb25cef7ee
print helpful message when running test app
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
38 print "http://localhost:%d/" % port |
2 | 39 tempdir = tempfile.mkdtemp() |
40 try: | |
3 | 41 app = factory(directory=tempdir) |
12
1cdb25cef7ee
print helpful message when running test app
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
42 server = simple_server.make_server(host='0.0.0.0', port=port, app=app) |
3 | 43 server.serve_forever() |
2 | 44 finally: |
45 shutil.rmtree(tempdir) |