Mercurial > hg > simpypi
annotate simpypi/factory.py @ 17:77357c5c33c2
commit to a namespace
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 28 Feb 2012 15:38:46 -0800 |
parents | d3efc504c0b1 |
children | bf70fc5a115f |
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 | |
16
d3efc504c0b1
more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
11 def __init__(self, app, directory): |
0 | 12 self.app = app |
16
d3efc504c0b1
more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
13 self.directory = directory |
d3efc504c0b1
more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
14 self.fileserver = StaticURLParser(directory) |
0 | 15 |
16 def __call__(self, environ, start_response): | |
16
d3efc504c0b1
more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
17 path = self.path(environ['PATH_INFO'].strip('/')) |
d3efc504c0b1
more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
18 if path and os.path.exists(os.path.join(self.directory, path)): |
d3efc504c0b1
more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
19 return self.fileserver(environ, start_response) |
d3efc504c0b1
more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
20 return self.app(environ, start_response) |
d3efc504c0b1
more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
21 |
d3efc504c0b1
more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
22 class NamespacedFileserver(PassthroughFileserver): |
d3efc504c0b1
more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
23 |
d3efc504c0b1
more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
24 def __init__(self, app, directory, namespace): |
d3efc504c0b1
more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
25 PassthroughFileserver.__init__(self, app, directory) |
d3efc504c0b1
more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
26 self.namespace = namespace |
d3efc504c0b1
more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
27 |
d3efc504c0b1
more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
28 def __call__(self, environ, start_response): |
d3efc504c0b1
more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
29 path = environ['PATH_INFO'] |
d3efc504c0b1
more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
30 if path == self.namespace: |
d3efc504c0b1
more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
31 environ['PATH_INFO'] = '/' |
d3efc504c0b1
more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
32 return self.fileserver(environ, start_response) |
d3efc504c0b1
more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
33 elif path.startswith(self.namespace + '/'): |
d3efc504c0b1
more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
34 environ['PATH_INFO'] = path[len(self.namespace):] |
d3efc504c0b1
more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
35 return self.fileserver(environ, start_response) |
0 | 36 return self.app(environ, start_response) |
37 | |
1 | 38 def factory(**app_conf): |
0 | 39 """create a webob view and wrap it in middleware""" |
4 | 40 directory = app_conf['directory'] |
1 | 41 app = Dispatcher(**app_conf) |
17 | 42 return PassthroughFileserver(app, directory, '/index') |
0 | 43 |
1 | 44 if __name__ == '__main__': |
2 | 45 import shutil |
1 | 46 import tempfile |
47 from wsgiref import simple_server | |
12
1cdb25cef7ee
print helpful message when running test app
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
48 port = 8080 |
1cdb25cef7ee
print helpful message when running test app
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
49 print "http://localhost:%d/" % port |
2 | 50 tempdir = tempfile.mkdtemp() |
51 try: | |
3 | 52 app = factory(directory=tempdir) |
12
1cdb25cef7ee
print helpful message when running test app
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
53 server = simple_server.make_server(host='0.0.0.0', port=port, app=app) |
3 | 54 server.serve_forever() |
2 | 55 finally: |
56 shutil.rmtree(tempdir) |