~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to wsgitest.py

  • Committer: Michael Hudson
  • Date: 2008-06-17 00:01:47 UTC
  • mto: This revision was merged to the branch mainline in revision 164.
  • Revision ID: michael.hudson@canonical.com-20080617000147-d3wf2fdf6arrmfo2
extract some methods

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
        self.folder = folder
19
19
        self.root = root
20
20
 
 
21
    def directory_listing(self, path, environ, start_response):
 
22
        request = WSGIRequest(environ)
 
23
        response = WSGIResponse()
 
24
        listing = [d for d in os.listdir(path) if not d.startswith('.')]
 
25
        response.headers['Content-Type'] = 'text/html'
 
26
        print >> response, '<html><body>'
 
27
        for d in sorted(listing):
 
28
            if os.path.isdir(os.path.join(path, d)):
 
29
                d = cgi.escape(d)
 
30
                print >> response, '<li><a href="%s/">%s</a></li>' % (d, d)
 
31
        print >> response, '</body></html>'
 
32
        return response(environ, start_response)
 
33
 
 
34
    def app_for_branch(self, b, path):
 
35
        b.lock_read()
 
36
        try:
 
37
            _history = History.from_branch(b)
 
38
            _history.use_file_cache(FileChangeCache(_history, sql_dir))
 
39
            if not self.folder:
 
40
                name = os.path.basename(os.path.abspath(path))
 
41
            else:
 
42
                name = self.folder
 
43
            h = BranchWSGIApp(_history, name).app
 
44
            self.root.cache[path] = h
 
45
            return h
 
46
        finally:
 
47
            b.unlock()
 
48
 
21
49
    def __call__(self, environ, start_response):
22
50
        path = os.path.join(self.root.folder, self.folder)
23
51
        if not os.path.isdir(path):
31
59
            if segment is None:
32
60
                raise httpexceptions.HTTPMovedPermanently(environ['SCRIPT_NAME'] + '/')
33
61
            elif segment == '':
34
 
                request = WSGIRequest(environ)
35
 
                response = WSGIResponse()
36
 
                listing = [d for d in os.listdir(path) if not d.startswith('.')]
37
 
                response.headers['Content-Type'] = 'text/html'
38
 
                print >> response, '<html><body>'
39
 
                for d in sorted(listing):
40
 
                    if os.path.isdir(os.path.join(path, d)):
41
 
                        d = cgi.escape(d)
42
 
                        print >> response, '<li><a href="%s/">%s</a></li>' % (d, d)
43
 
                print >> response, '</body></html>'
44
 
                return response(environ, start_response)
 
62
                return self.directory_listing(path, environ, start_response)
45
63
            else:
46
64
                relpath = os.path.join(self.folder, segment)
47
65
                return BranchesFromFileSystemServer(relpath, self.root)(
48
66
                    environ, start_response)
49
67
        else:
50
 
            b.lock_read()
51
 
            try:
52
 
                _history = History.from_branch(b)
53
 
                _history.use_file_cache(FileChangeCache(_history, sql_dir))
54
 
                if not self.folder:
55
 
                    name = os.path.basename(os.path.abspath(path))
56
 
                else:
57
 
                    name = self.folder
58
 
                h = BranchWSGIApp(_history, name).app
59
 
                self.root.cache[path] = h
60
 
                return h(environ, start_response)
61
 
            finally:
62
 
                b.unlock()
 
68
            return self.app_for_branch(b, path)(environ, start_response)
 
69
 
63
70
 
64
71
class BranchesFromFileSystemRoot(object):
65
72
    def __init__(self, folder):