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
21
from paste.request import path_info_pop
23
from paste.httpexceptions import HTTPNotFound
24
from paste.request import path_info_pop, parse_querystring
23
26
from loggerhead import util
24
27
from loggerhead.templatefunctions import templatefunctions
25
28
from loggerhead.zptsupport import load_template
31
class BufferingWriter(object):
33
def __init__(self, writefunc, buf_limit):
37
self.writefunc = writefunc
38
self.buf_limit = buf_limit
41
self.writefunc(''.join(self.buf))
45
def write(self, data):
47
self.buflen += len(data)
48
self.bytes += len(data)
49
if self.buflen > self.buf_limit:
27
53
class TemplatedBranchView(object):
29
55
template_path = None
31
def __init__(self, branch):
57
def __init__(self, branch, history_callable):
32
58
self._branch = branch
59
self._history_callable = history_callable
33
61
self.log = branch.log
35
def default(self, request, response):
37
h = self._branch.history
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)
65
if self.__history is not None:
67
self.__history = self._history_callable()
70
def __call__(self, environ, start_response):
72
kwargs = dict(parse_querystring(environ))
73
util.set_context(kwargs)
76
arg = path_info_pop(environ)
83
path = unicode('/'.join(args[1:]), 'utf-8')
87
'static_url': self._branch.static_url,
88
'branch': self._branch,
90
'url': self._branch.context_url,
92
vals.update(templatefunctions)
95
vals.update(self.get_values(path, kwargs, headers))
97
self.log.info('Getting information for %s: %r secs' % (
98
self.__class__.__name__, time.time() - z))
99
if 'Content-Type' not in headers:
100
headers['Content-Type'] = 'text/html'
101
writer = start_response("200 OK", headers.items())
102
if environ.get('REQUEST_METHOD') == 'HEAD':
103
# No content for a HEAD request
105
template = load_template(self.template_path)
107
w = BufferingWriter(writer, 8192)
108
template.expand_into(w, **vals)
111
'Rendering %s: %r secs, %s bytes' % (
112
self.__class__.__name__, time.time() - z, w.bytes))
119
if len(self.args) > 0 and self.args != ['']:
121
revid = h.fix_revid(self.args[0])
122
except bzrlib.errors.NoSuchRevision: