##// END OF EJS Templates
templater: return data in increasing chunk sizes...
Brendan Cully -
r7396:526c40a7 default
parent child Browse files
Show More
@@ -182,20 +182,20 b' class hgweb(object):'
182 182 content = getattr(webcommands, cmd)(self, req, tmpl)
183 183 req.respond(HTTP_OK, ctype)
184 184
185 return ''.join(content),
185 return content
186 186
187 187 except revlog.LookupError, err:
188 188 req.respond(HTTP_NOT_FOUND, ctype)
189 189 msg = str(err)
190 190 if 'manifest' not in msg:
191 191 msg = 'revision not found: %s' % err.name
192 return ''.join(tmpl('error', error=msg)),
192 return tmpl('error', error=msg)
193 193 except (RepoError, revlog.RevlogError), inst:
194 194 req.respond(HTTP_SERVER_ERROR, ctype)
195 return ''.join(tmpl('error', error=str(inst))),
195 return tmpl('error', error=str(inst))
196 196 except ErrorResponse, inst:
197 197 req.respond(inst.code, ctype)
198 return ''.join(tmpl('error', error=inst.message)),
198 return tmpl('error', error=inst.message)
199 199
200 200 def templater(self, req):
201 201
@@ -116,7 +116,7 b' class hgwebdir(object):'
116 116 # top-level index
117 117 elif not virtual:
118 118 req.respond(HTTP_OK, ctype)
119 return ''.join(self.makeindex(req, tmpl)),
119 return self.makeindex(req, tmpl)
120 120
121 121 # nested indexes and hgwebs
122 122
@@ -138,7 +138,7 b' class hgwebdir(object):'
138 138 subdir = virtual + '/'
139 139 if [r for r in repos if r.startswith(subdir)]:
140 140 req.respond(HTTP_OK, ctype)
141 return ''.join(self.makeindex(req, tmpl, subdir)),
141 return self.makeindex(req, tmpl, subdir)
142 142
143 143 up = virtual.rfind('/')
144 144 if up < 0:
@@ -147,11 +147,11 b' class hgwebdir(object):'
147 147
148 148 # prefixes not found
149 149 req.respond(HTTP_NOT_FOUND, ctype)
150 return ''.join(tmpl("notfound", repo=virtual)),
150 return tmpl("notfound", repo=virtual)
151 151
152 152 except ErrorResponse, err:
153 153 req.respond(err.code, ctype)
154 return ''.join(tmpl('error', error=err.message or '')),
154 return tmpl('error', error=err.message or '')
155 155 finally:
156 156 tmpl = None
157 157
@@ -44,7 +44,8 b' class templater(object):'
44 44 template_re = re.compile(r"(?:(?:#(?=[\w\|%]+#))|(?:{(?=[\w\|%]+})))"
45 45 r"(\w+)(?:(?:%(\w+))|((?:\|\w+)*))[#}]")
46 46
47 def __init__(self, mapfile, filters={}, defaults={}, cache={}):
47 def __init__(self, mapfile, filters={}, defaults={}, cache={},
48 minchunk=1024, maxchunk=65536):
48 49 '''set up template engine.
49 50 mapfile is name of file to read map definitions from.
50 51 filters is dict of functions. each transforms a value into another.
@@ -55,6 +56,7 b' class templater(object):'
55 56 self.base = (mapfile and os.path.dirname(mapfile)) or ''
56 57 self.filters = filters
57 58 self.defaults = defaults
59 self.minchunk, self.maxchunk = minchunk, maxchunk
58 60
59 61 if not mapfile:
60 62 return
@@ -130,6 +132,13 b' class templater(object):'
130 132 yield v
131 133
132 134 def __call__(self, t, **map):
135 stream = self.expand(t, **map)
136 if self.minchunk:
137 stream = util.increasingchunks(stream, min=self.minchunk,
138 max=self.maxchunk)
139 return stream
140
141 def expand(self, t, **map):
133 142 '''Perform expansion. t is name of map element to expand. map contains
134 143 added elements for use during expansion. Is a generator.'''
135 144 tmpl = self._template(t)
@@ -290,6 +290,37 b' def sort(l):'
290 290 l.sort()
291 291 return l
292 292
293 def increasingchunks(source, min=1024, max=65536):
294 '''return no less than min bytes per chunk while data remains,
295 doubling min after each chunk until it reaches max'''
296 def log2(x):
297 if not x:
298 return 0
299 i = 0
300 while x:
301 x >>= 1
302 i += 1
303 return i - 1
304
305 buf = []
306 blen = 0
307 for chunk in source:
308 buf.append(chunk)
309 blen += len(chunk)
310 if blen >= min:
311 if min < max:
312 min = min << 1
313 nmin = 1 << log2(blen)
314 if nmin > min:
315 min = nmin
316 if min > max:
317 min = max
318 yield ''.join(buf)
319 blen = 0
320 buf = []
321 if buf:
322 yield ''.join(buf)
323
293 324 class Abort(Exception):
294 325 """Raised if a command needs to print an error and exit."""
295 326
General Comments 0
You need to be logged in to leave comments. Login now