~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/__init__.py

Added navigating to the parent branch in directory listing by Michael, and a few other UI tweaks by, well, me!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#
2
 
# Copyright (C) 2008  Canonical Ltd.
3
2
# Copyright (C) 2006  Robey Pointer <robey@lag.net>
4
3
# Copyright (C) 2006  Goffredo Baroncelli <kreijack@inwind.it>
5
4
#
17
16
# along with this program; if not, write to the Free Software
18
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
18
 
 
19
import re
20
20
import time
21
21
 
22
22
from paste.request import path_info_pop, parse_querystring
25
25
from loggerhead.templatefunctions import templatefunctions
26
26
from loggerhead.zptsupport import load_template
27
27
 
28
 
 
29
28
class BufferingWriter(object):
30
29
 
31
30
    def __init__(self, writefunc, buf_limit):
33
32
        self.buf = []
34
33
        self.buflen = 0
35
34
        self.writefunc = writefunc
 
35
        self.bytes_saved = 0
36
36
        self.buf_limit = buf_limit
37
37
 
38
38
    def flush(self):
39
 
        self.writefunc(''.join(self.buf))
 
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)
40
44
        self.buf = []
41
45
        self.buflen = 0
42
46
 
47
51
        if self.buflen > self.buf_limit:
48
52
            self.flush()
49
53
 
50
 
 
51
54
class TemplatedBranchView(object):
52
55
 
53
56
    template_path = None
54
57
 
55
 
    def __init__(self, branch, history_callable):
 
58
    def __init__(self, branch, history):
56
59
        self._branch = branch
57
 
        self._history_callable = history_callable
58
 
        self.__history = None
 
60
        self._history = history
59
61
        self.log = branch.log
60
62
 
61
 
    @property
62
 
    def _history(self):
63
 
        if self.__history is not None:
64
 
            return self.__history
65
 
        self.__history = self._history_callable()
66
 
        return self.__history
67
 
 
68
63
    def __call__(self, environ, start_response):
69
64
        z = time.time()
70
 
        kwargs = dict(parse_querystring(environ))
71
 
        util.set_context(kwargs)
 
65
        h = self._history
 
66
        kw = dict(parse_querystring(environ))
 
67
        util.set_context(kw)
72
68
        args = []
73
69
        while 1:
74
70
            arg = path_info_pop(environ)
76
72
                break
77
73
            args.append(arg)
78
74
 
79
 
        path = None
80
 
        if len(args) > 1:
81
 
            path = unicode('/'.join(args[1:]), 'utf-8')
82
 
        self.args = args
83
 
 
84
75
        vals = {
85
 
            'static_url': self._branch.static_url,
86
76
            'branch': self._branch,
87
77
            'util': util,
 
78
            'history': h,
88
79
            'url': self._branch.context_url,
89
80
        }
90
81
        vals.update(templatefunctions)
91
82
        headers = {}
92
 
 
93
 
        vals.update(self.get_values(path, kwargs, headers))
 
83
        vals.update(self.get_values(h, args, kw, headers))
94
84
 
95
85
        self.log.info('Getting information for %s: %r secs' % (
96
 
            self.__class__.__name__, time.time() - z))
 
86
            self.__class__.__name__, time.time() - z,))
97
87
        if 'Content-Type' not in headers:
98
88
            headers['Content-Type'] = 'text/html'
99
89
        writer = start_response("200 OK", headers.items())
102
92
        w = BufferingWriter(writer, 8192)
103
93
        template.expand_into(w, **vals)
104
94
        w.flush()
105
 
        self.log.info(
106
 
            'Rendering %s: %r secs, %s bytes' % (
107
 
                self.__class__.__name__, time.time() - z, w.bytes))
 
95
        self.log.info('Rendering %s: %r secs, %s bytes, %s (%2.1f%%) bytes saved' % (
 
96
            self.__class__.__name__, time.time() - z, w.bytes, w.bytes_saved, 100.0*w.bytes_saved/w.bytes))
108
97
        return []
109
 
 
110
 
    def get_revid(self):
111
 
        h = self._history
112
 
        if h is None:
113
 
            return None
114
 
        if len(self.args) > 0 and self.args != ['']:
115
 
            return h.fix_revid(self.args[0])
116
 
        else:
117
 
            return h.last_revid