##// END OF EJS Templates
formatter: add helper to create a formatter optionally backed by file...
Yuya Nishihara -
r32574:35985d40 default
parent child Browse files
Show More
@@ -103,6 +103,7 b' baz: foo, bar'
103
103
104 from __future__ import absolute_import
104 from __future__ import absolute_import
105
105
106 import contextlib
106 import itertools
107 import itertools
107 import os
108 import os
108
109
@@ -432,3 +433,29 b' def formatter(ui, out, topic, opts):'
432 elif ui.configbool('ui', 'formatjson'):
433 elif ui.configbool('ui', 'formatjson'):
433 return jsonformatter(ui, out, topic, opts)
434 return jsonformatter(ui, out, topic, opts)
434 return plainformatter(ui, out, topic, opts)
435 return plainformatter(ui, out, topic, opts)
436
437 @contextlib.contextmanager
438 def openformatter(ui, filename, topic, opts):
439 """Create a formatter that writes outputs to the specified file
440
441 Must be invoked using the 'with' statement.
442 """
443 with util.posixfile(filename, 'wb') as out:
444 with formatter(ui, out, topic, opts) as fm:
445 yield fm
446
447 @contextlib.contextmanager
448 def _neverending(fm):
449 yield fm
450
451 def maybereopen(fm, filename, opts):
452 """Create a formatter backed by file if filename specified, else return
453 the given formatter
454
455 Must be invoked using the 'with' statement. This will never call fm.end()
456 of the given formatter.
457 """
458 if filename:
459 return openformatter(fm._ui, filename, fm._topic, opts)
460 else:
461 return _neverending(fm)
General Comments 0
You need to be logged in to leave comments. Login now