~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/__init__.py

  • Committer: Martin Albisetti
  • Date: 2008-12-22 19:33:12 UTC
  • Revision ID: argentina@gmail.com-20081222193312-b5ql8ksk75rqgxbb
Updated NEWS

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
#
25
26
from loggerhead.templatefunctions import templatefunctions
26
27
from loggerhead.zptsupport import load_template
27
28
 
 
29
 
28
30
class BufferingWriter(object):
29
31
 
30
32
    def __init__(self, writefunc, buf_limit):
51
53
        if self.buflen > self.buf_limit:
52
54
            self.flush()
53
55
 
 
56
 
54
57
class TemplatedBranchView(object):
55
58
 
56
59
    template_path = None
57
60
 
58
 
    def __init__(self, branch, history):
 
61
    def __init__(self, branch, history_callable):
59
62
        self._branch = branch
60
 
        self._history = history
 
63
        self._history_callable = history_callable
 
64
        self.__history = None
61
65
        self.log = branch.log
62
66
 
 
67
    @property
 
68
    def _history(self):
 
69
        if self.__history is not None:
 
70
            return self.__history
 
71
        self.__history = self._history_callable()
 
72
        return self.__history
 
73
 
63
74
    def __call__(self, environ, start_response):
64
75
        z = time.time()
 
76
        kwargs = dict(parse_querystring(environ))
 
77
        util.set_context(kwargs)
 
78
        args = []
 
79
        while 1:
 
80
            arg = path_info_pop(environ)
 
81
            if arg is None:
 
82
                break
 
83
            args.append(arg)
 
84
 
 
85
        path = None
 
86
        if len(args) > 1:
 
87
            path = '/'.join(args[1:])
 
88
        self.args = args
 
89
 
 
90
        vals = {
 
91
            'static_url': self._branch.static_url,
 
92
            'branch': self._branch,
 
93
            'util': util,
 
94
            'url': self._branch.context_url,
 
95
        }
 
96
        vals.update(templatefunctions)
 
97
        headers = {}
 
98
 
 
99
        vals.update(self.get_values(path, kwargs, headers))
 
100
 
 
101
        self.log.info('Getting information for %s: %r secs' % (
 
102
            self.__class__.__name__, time.time() - z))
 
103
        if 'Content-Type' not in headers:
 
104
            headers['Content-Type'] = 'text/html'
 
105
        writer = start_response("200 OK", headers.items())
 
106
        template = load_template(self.template_path)
 
107
        z = time.time()
 
108
        w = BufferingWriter(writer, 8192)
 
109
        template.expand_into(w, **vals)
 
110
        w.flush()
 
111
        self.log.info(
 
112
            'Rendering %s: %r secs, %s bytes, %s (%2.1f%%) bytes saved' % (
 
113
                self.__class__.__name__,
 
114
                time.time() - z,
 
115
                w.bytes,
 
116
                w.bytes_saved,
 
117
                100.0*w.bytes_saved/w.bytes))
 
118
        return []
 
119
 
 
120
    def get_revid(self):
65
121
        h = self._history
66
 
        kw = dict(parse_querystring(environ))
67
 
        util.set_context(kw)
68
 
 
69
 
        h._branch.lock_read()
70
 
        try:
71
 
            args = []
72
 
            while 1:
73
 
                arg = path_info_pop(environ)
74
 
                if arg is None:
75
 
                    break
76
 
                args.append(arg)
77
 
 
78
 
            vals = {
79
 
                'branch': self._branch,
80
 
                'util': util,
81
 
                'history': h,
82
 
                'url': self._branch.context_url,
83
 
            }
84
 
            vals.update(templatefunctions)
85
 
            headers = {}
86
 
            vals.update(self.get_values(h, args, kw, headers))
87
 
 
88
 
            self.log.info('Getting information for %s: %r secs' % (
89
 
                self.__class__.__name__, time.time() - z,))
90
 
            if 'Content-Type' not in headers:
91
 
                headers['Content-Type'] = 'text/html'
92
 
            writer = start_response("200 OK", headers.items())
93
 
            template = load_template(self.template_path)
94
 
            z = time.time()
95
 
            w = BufferingWriter(writer, 8192)
96
 
            template.expand_into(w, **vals)
97
 
            w.flush()
98
 
            self.log.info('Rendering %s: %r secs, %s bytes, %s (%2.1f%%) bytes saved' % (
99
 
                self.__class__.__name__, time.time() - z, w.bytes, w.bytes_saved, 100.0*w.bytes_saved/w.bytes))
100
 
            return []
101
 
        finally:
102
 
            h._branch.unlock()
103
 
 
 
122
        if h is None:
 
123
            return None
 
124
        if len(self.args) > 0:
 
125
            return h.fix_revid(self.args[0])
 
126
        else:
 
127
            return h.last_revid