##// END OF EJS Templates
formatter: add argument to change output file of non-plain formatter...
Yuya Nishihara -
r31182:5660c45e default
parent child Browse files
Show More
@@ -261,24 +261,26 b' class plainformatter(baseformatter):'
261 261 pass
262 262
263 263 class debugformatter(baseformatter):
264 def __init__(self, ui, topic, opts):
264 def __init__(self, ui, out, topic, opts):
265 265 baseformatter.__init__(self, ui, topic, opts, _nullconverter)
266 self._ui.write("%s = [\n" % self._topic)
266 self._out = out
267 self._out.write("%s = [\n" % self._topic)
267 268 def _showitem(self):
268 self._ui.write(" " + repr(self._item) + ",\n")
269 self._out.write(" " + repr(self._item) + ",\n")
269 270 def end(self):
270 271 baseformatter.end(self)
271 self._ui.write("]\n")
272 self._out.write("]\n")
272 273
273 274 class pickleformatter(baseformatter):
274 def __init__(self, ui, topic, opts):
275 def __init__(self, ui, out, topic, opts):
275 276 baseformatter.__init__(self, ui, topic, opts, _nullconverter)
277 self._out = out
276 278 self._data = []
277 279 def _showitem(self):
278 280 self._data.append(self._item)
279 281 def end(self):
280 282 baseformatter.end(self)
281 self._ui.write(pickle.dumps(self._data))
283 self._out.write(pickle.dumps(self._data))
282 284
283 285 def _jsonifyobj(v):
284 286 if isinstance(v, dict):
@@ -299,28 +301,29 b' def _jsonifyobj(v):'
299 301 return '"%s"' % encoding.jsonescape(v)
300 302
301 303 class jsonformatter(baseformatter):
302 def __init__(self, ui, topic, opts):
304 def __init__(self, ui, out, topic, opts):
303 305 baseformatter.__init__(self, ui, topic, opts, _nullconverter)
304 self._ui.write("[")
306 self._out = out
307 self._out.write("[")
305 308 self._ui._first = True
306 309 def _showitem(self):
307 310 if self._ui._first:
308 311 self._ui._first = False
309 312 else:
310 self._ui.write(",")
313 self._out.write(",")
311 314
312 self._ui.write("\n {\n")
315 self._out.write("\n {\n")
313 316 first = True
314 317 for k, v in sorted(self._item.items()):
315 318 if first:
316 319 first = False
317 320 else:
318 self._ui.write(",\n")
319 self._ui.write(' "%s": %s' % (k, _jsonifyobj(v)))
320 self._ui.write("\n }")
321 self._out.write(",\n")
322 self._out.write(' "%s": %s' % (k, _jsonifyobj(v)))
323 self._out.write("\n }")
321 324 def end(self):
322 325 baseformatter.end(self)
323 self._ui.write("\n]\n")
326 self._out.write("\n]\n")
324 327
325 328 class _templateconverter(object):
326 329 '''convert non-primitive data types to be processed by templater'''
@@ -346,8 +349,9 b' class _templateconverter(object):'
346 349 lambda d: fmt % d[name])
347 350
348 351 class templateformatter(baseformatter):
349 def __init__(self, ui, topic, opts):
352 def __init__(self, ui, out, topic, opts):
350 353 baseformatter.__init__(self, ui, topic, opts, _templateconverter)
354 self._out = out
351 355 self._topic = topic
352 356 self._t = gettemplater(ui, topic, opts.get('template', ''),
353 357 cache=templatekw.defaulttempl)
@@ -371,7 +375,7 b' class templateformatter(baseformatter):'
371 375 else:
372 376 props = self._item
373 377 g = self._t(self._topic, ui=self._ui, cache=self._cache, **props)
374 self._ui.write(templater.stringify(g))
378 self._out.write(templater.stringify(g))
375 379
376 380 def lookuptemplate(ui, topic, tmpl):
377 381 # looks like a literal template?
@@ -423,17 +427,17 b' def maketemplater(ui, topic, tmpl, cache'
423 427 def formatter(ui, topic, opts):
424 428 template = opts.get("template", "")
425 429 if template == "json":
426 return jsonformatter(ui, topic, opts)
430 return jsonformatter(ui, ui, topic, opts)
427 431 elif template == "pickle":
428 return pickleformatter(ui, topic, opts)
432 return pickleformatter(ui, ui, topic, opts)
429 433 elif template == "debug":
430 return debugformatter(ui, topic, opts)
434 return debugformatter(ui, ui, topic, opts)
431 435 elif template != "":
432 return templateformatter(ui, topic, opts)
436 return templateformatter(ui, ui, topic, opts)
433 437 # developer config: ui.formatdebug
434 438 elif ui.configbool('ui', 'formatdebug'):
435 return debugformatter(ui, topic, opts)
439 return debugformatter(ui, ui, topic, opts)
436 440 # deprecated config: ui.formatjson
437 441 elif ui.configbool('ui', 'formatjson'):
438 return jsonformatter(ui, topic, opts)
442 return jsonformatter(ui, ui, topic, opts)
439 443 return plainformatter(ui, topic, opts)
General Comments 0
You need to be logged in to leave comments. Login now