~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/__init__.py

MergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
# along with this program; if not, write to the Free Software
17
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
18
 
19
 
import re
20
19
import time
21
20
 
22
 
from paste.request import path_info_pop, parse_querystring
 
21
from paste.request import path_info_pop
23
22
 
24
23
from loggerhead import util
25
24
from loggerhead.templatefunctions import templatefunctions
26
25
from loggerhead.zptsupport import load_template
27
26
 
28
 
class BufferingWriter(object):
29
 
 
30
 
    def __init__(self, writefunc, buf_limit):
31
 
        self.bytes = 0
32
 
        self.buf = []
33
 
        self.buflen = 0
34
 
        self.writefunc = writefunc
35
 
        self.bytes_saved = 0
36
 
        self.buf_limit = buf_limit
37
 
 
38
 
    def flush(self):
39
 
        chunk = ''.join(self.buf)
40
 
        chunk = re.sub(r'\s*\n\s*', '\n', chunk)
41
 
        chunk = re.sub(r'[ \t]+', ' ', chunk)
42
 
        self.bytes_saved += self.buflen - len(chunk)
43
 
        self.writefunc(chunk)
44
 
        self.buf = []
45
 
        self.buflen = 0
46
 
 
47
 
    def write(self, data):
48
 
        self.buf.append(data)
49
 
        self.buflen += len(data)
50
 
        self.bytes += len(data)
51
 
        if self.buflen > self.buf_limit:
52
 
            self.flush()
53
 
 
54
27
class TemplatedBranchView(object):
55
28
 
56
29
    template_path = None
57
30
 
58
 
    def __init__(self, branch, history):
 
31
    def __init__(self, branch):
59
32
        self._branch = branch
60
 
        self._history = history
61
33
        self.log = branch.log
62
34
 
63
 
    def __call__(self, environ, start_response):
 
35
    def default(self, request, response):
64
36
        z = time.time()
65
 
        h = self._history
66
 
        kw = dict(parse_querystring(environ))
 
37
        h = self._branch.history
 
38
        kw = request.GET
67
39
        util.set_context(kw)
68
40
 
69
 
        args = []
70
 
        while 1:
71
 
            arg = path_info_pop(environ)
72
 
            if arg is None:
73
 
                break
74
 
            args.append(arg)
75
 
 
76
 
        vals = {
77
 
            'branch': self._branch,
78
 
            'util': util,
79
 
            'history': h,
80
 
            'url': self._branch.context_url,
81
 
        }
82
 
        vals.update(templatefunctions)
83
 
        headers = {}
84
 
        vals.update(self.get_values(h, args, kw, headers))
85
 
 
86
 
        self.log.info('Getting information for %s: %r secs' % (
87
 
            self.__class__.__name__, time.time() - z,))
88
 
        if 'Content-Type' not in headers:
89
 
            headers['Content-Type'] = 'text/html'
90
 
        writer = start_response("200 OK", headers.items())
91
 
        template = load_template(self.template_path)
92
 
        z = time.time()
93
 
        w = BufferingWriter(writer, 8192)
94
 
        template.expand_into(w, **vals)
95
 
        w.flush()
96
 
        self.log.info('Rendering %s: %r secs, %s bytes, %s (%2.1f%%) bytes saved' % (
97
 
            self.__class__.__name__, time.time() - z, w.bytes, w.bytes_saved, 100.0*w.bytes_saved/w.bytes))
98
 
        return []
99
 
 
 
41
        h._branch.lock_read()
 
42
        try:
 
43
            args = []
 
44
            while 1:
 
45
                arg = path_info_pop(request.environ)
 
46
                if arg is None:
 
47
                    break
 
48
                args.append(arg)
 
49
 
 
50
            vals = {
 
51
                'branch': self._branch,
 
52
                'util': util,
 
53
                'history': h,
 
54
                'url': self._branch.context_url,
 
55
            }
 
56
            vals.update(templatefunctions)
 
57
            del response.headers['Content-Type']
 
58
            vals.update(self.get_values(h, args, kw, response))
 
59
 
 
60
            self.log.info('/%s: %r secs' % (
 
61
                self.__class__.__name__, time.time() - z,))
 
62
            if 'Content-Type' not in response.headers:
 
63
                response.headers['Content-Type'] = 'text/html'
 
64
            template = load_template(self.template_path)
 
65
            template.expand_into(response, **vals)
 
66
        finally:
 
67
            h._branch.unlock()