~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/__init__.py

  • Committer: Martin Albisetti
  • Date: 2009-06-02 13:46:32 UTC
  • mto: This revision was merged to the branch mainline in revision 364.
  • Revision ID: martin.albisetti@canonical.com-20090602134632-7wcqj1o3k8hu2pet
Add deprecation warning to start-loggerhead

Show diffs side-by-side

added added

removed removed

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