~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:
17
17
# along with this program; if not, write to the Free Software
18
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
19
 
20
 
import bzrlib.errors
 
20
import re
21
21
import time
22
22
 
23
 
from paste.httpexceptions import HTTPNotFound
24
23
from paste.request import path_info_pop, parse_querystring
25
24
 
26
25
from loggerhead import util
35
34
        self.buf = []
36
35
        self.buflen = 0
37
36
        self.writefunc = writefunc
 
37
        self.bytes_saved = 0
38
38
        self.buf_limit = buf_limit
39
39
 
40
40
    def flush(self):
41
 
        self.writefunc(''.join(self.buf))
 
41
        chunk = ''.join(self.buf)
 
42
        chunk = re.sub(r'\s*\n\s*', '\n', chunk)
 
43
        chunk = re.sub(r'[ \t]+', ' ', chunk)
 
44
        self.bytes_saved += self.buflen - len(chunk)
 
45
        self.writefunc(chunk)
42
46
        self.buf = []
43
47
        self.buflen = 0
44
48
 
72
76
        kwargs = dict(parse_querystring(environ))
73
77
        util.set_context(kwargs)
74
78
        args = []
75
 
        while True:
 
79
        while 1:
76
80
            arg = path_info_pop(environ)
77
81
            if arg is None:
78
82
                break
80
84
 
81
85
        path = None
82
86
        if len(args) > 1:
83
 
            path = unicode('/'.join(args[1:]), 'utf-8')
 
87
            path = '/'.join(args[1:])
84
88
        self.args = args
85
89
 
86
90
        vals = {
94
98
 
95
99
        vals.update(self.get_values(path, kwargs, headers))
96
100
 
97
 
        self.log.info('Getting information for %s: %.3f secs' % (
 
101
        self.log.info('Getting information for %s: %r secs' % (
98
102
            self.__class__.__name__, time.time() - z))
99
103
        if 'Content-Type' not in headers:
100
104
            headers['Content-Type'] = 'text/html'
101
105
        writer = start_response("200 OK", headers.items())
102
 
        if environ.get('REQUEST_METHOD') == 'HEAD':
103
 
            # No content for a HEAD request
104
 
            return []
105
106
        template = load_template(self.template_path)
106
107
        z = time.time()
107
108
        w = BufferingWriter(writer, 8192)
108
109
        template.expand_into(w, **vals)
109
110
        w.flush()
110
111
        self.log.info(
111
 
            'Rendering %s: %.3f secs, %s bytes' % (
112
 
                self.__class__.__name__, time.time() - z, w.bytes))
 
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))
113
118
        return []
114
119
 
115
120
    def get_revid(self):
116
121
        h = self._history
117
122
        if h is None:
118
123
            return None
119
 
        if len(self.args) > 0 and self.args != ['']:
120
 
            try:
121
 
                revid = h.fix_revid(self.args[0])
122
 
            except bzrlib.errors.NoSuchRevision:
123
 
                raise HTTPNotFound;
 
124
        if len(self.args) > 0:
 
125
            return h.fix_revid(self.args[0])
124
126
        else:
125
 
            revid = h.last_revid
126
 
        return revid
 
127
            return h.last_revid