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
22
from paste.request import path_info_pop, parse_querystring
21
from paste.request import path_info_pop
24
23
from loggerhead import util
25
24
from loggerhead.templatefunctions import templatefunctions
26
25
from loggerhead.zptsupport import load_template
28
class BufferingWriter(object):
30
def __init__(self, writefunc, buf_limit):
34
self.writefunc = writefunc
36
self.buf_limit = buf_limit
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)
47
def write(self, data):
49
self.buflen += len(data)
50
self.bytes += len(data)
51
if self.buflen > self.buf_limit:
54
27
class TemplatedBranchView(object):
56
29
template_path = None
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
63
def __call__(self, environ, start_response):
35
def default(self, request, response):
66
kw = dict(parse_querystring(environ))
37
h = self._branch.history
67
39
util.set_context(kw)
71
arg = path_info_pop(environ)
77
'branch': self._branch,
80
'url': self._branch.context_url,
82
vals.update(templatefunctions)
84
vals.update(self.get_values(h, args, kw, headers))
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)
93
w = BufferingWriter(writer, 8192)
94
template.expand_into(w, **vals)
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))
45
arg = path_info_pop(request.environ)
51
'branch': self._branch,
54
'url': self._branch.context_url,
56
vals.update(templatefunctions)
57
del response.headers['Content-Type']
58
vals.update(self.get_values(h, args, kw, response))
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)