/loggerhead/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerhead/trunk
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
1
import os
2
from bzrlib import branch, errors
159.2.1 by Michael Hudson
things start to work a little already!
3
from loggerhead.history import History
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
4
from loggerhead.wsgiapp import BranchWSGIApp, static_app
5
from paste.request import path_info_pop
6
from paste import httpexceptions
159.2.1 by Michael Hudson
things start to work a little already!
7
from paste import httpserver
159.2.2 by Michael Hudson
more workingness
8
from paste.httpexceptions import make_middleware
159.2.4 by Michael Hudson
more progress
9
from paste.translogger import make_filter
10
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
11
12
13
class BranchesFromFileSystemServer(object):
14
    def __init__(self, folder, root):
15
        self.folder = folder
16
        self.root = root
17
18
    def __call__(self, environ, start_response):
19
        segment = path_info_pop(environ)
20
        if segment is None:
21
            raise httpexceptions.HTTPNotFound()
22
        f = os.path.join(self.folder, segment)
23
        print f
24
        if not os.path.isdir(f):
25
            raise httpexceptions.HTTPNotFound()
26
        if f in self.root.cache:
27
            return self.root.cache[f](environ, start_response)
28
        try:
29
            b = branch.Branch.open(f)
30
        except errors.NotBranchError:
31
            return BranchesFromFileSystemServer(f, self.root)(environ, start_response)
32
        else:
33
            b.lock_read()
34
            try:
35
                h = BranchWSGIApp(History.from_branch(b), 'hello').app
36
                self.root.cache[f] = h
37
                return h(environ, start_response)
38
            finally:
39
                b.unlock()
40
41
class BranchesFromFileSystemRoot(object):
42
    def __init__(self, folder):
43
        self.cache = {}
44
        self.folder = folder
45
    def __call__(self, environ, start_response):
46
        environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
47
        segment = path_info_pop(environ)
48
        if segment == 'static':
49
            return static_app(environ, start_response)
50
        else:
51
            return BranchesFromFileSystemServer(
52
                os.path.join(self.folder, segment), self)(environ, start_response)
53
54
app = BranchesFromFileSystemRoot('../..')
55
56
app = app
57
app = make_middleware(app)
159.2.4 by Michael Hudson
more progress
58
app = make_filter(app, None)
59
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
60
from paste.evalexception.middleware import EvalException
61
62
httpserver.serve(EvalException(app), host='127.0.0.1', port='9876')
159.2.1 by Michael Hudson
things start to work a little already!
63