[PATCH WIP] formatter: add option to redirect output to file object (WORK IN PROGRESS)
Yuya Nishihara
yuya at tcha.org
Thu May 25 15:45:20 UTC 2017
# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1421571884 -32400
# Sun Jan 18 18:04:44 2015 +0900
# Node ID 9eb6cdad8f41f2e20274a20ee10130706639a47c
# Parent b647b923486f38d83b92089eafa9faafaa79785d
formatter: add option to redirect output to file object (WORK IN PROGRESS)
Maybe this could be used when porting 'export' to formatter. I tried to port
'cat' as an example, but the result was a bit mess because I had to manage
closable file and formatter resources carefully. Maybe we'll need some helper
functions in cmdutil.
diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -237,24 +237,28 @@ class _plainconverter(object):
class plainformatter(baseformatter):
'''the default text output scheme'''
- def __init__(self, ui, topic, opts):
+ def __init__(self, ui, out, topic, opts):
baseformatter.__init__(self, ui, topic, opts, _plainconverter)
if ui.debugflag:
self.hexfunc = hex
else:
self.hexfunc = short
+ if ui is out:
+ self._write = ui.write
+ else:
+ self._write = lambda s, **opts: out.write(s)
def startitem(self):
pass
def data(self, **data):
pass
def write(self, fields, deftext, *fielddata, **opts):
- self._ui.write(deftext % fielddata, **opts)
+ self._write(deftext % fielddata, **opts)
def condwrite(self, cond, fields, deftext, *fielddata, **opts):
'''do conditional write'''
if cond:
- self._ui.write(deftext % fielddata, **opts)
+ self._write(deftext % fielddata, **opts)
def plain(self, text, **opts):
- self._ui.write(text, **opts)
+ self._write(text, **opts)
def isplain(self):
return True
def nested(self, field):
@@ -411,20 +415,20 @@ def maketemplater(ui, topic, tmpl, cache
t.cache[topic] = tmpl
return t
-def formatter(ui, topic, opts):
+def formatter(ui, out, topic, opts):
template = opts.get("template", "")
if template == "json":
- return jsonformatter(ui, ui, topic, opts)
+ return jsonformatter(ui, out, topic, opts)
elif template == "pickle":
- return pickleformatter(ui, ui, topic, opts)
+ return pickleformatter(ui, out, topic, opts)
elif template == "debug":
- return debugformatter(ui, ui, topic, opts)
+ return debugformatter(ui, out, topic, opts)
elif template != "":
- return templateformatter(ui, ui, topic, opts)
+ return templateformatter(ui, out, topic, opts)
# developer config: ui.formatdebug
elif ui.configbool('ui', 'formatdebug'):
- return debugformatter(ui, ui, topic, opts)
+ return debugformatter(ui, out, topic, opts)
# deprecated config: ui.formatjson
elif ui.configbool('ui', 'formatjson'):
- return jsonformatter(ui, ui, topic, opts)
- return plainformatter(ui, topic, opts)
+ return jsonformatter(ui, out, topic, opts)
+ return plainformatter(ui, out, topic, opts)
diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -262,8 +262,10 @@ class ui(object):
self._blockedtimes[key + '_blocked'] += \
(util.timer() - starttime) * 1000
- def formatter(self, topic, opts):
- return formatter.formatter(self, topic, opts)
+ def formatter(self, topic, opts, out=None):
+ if out is None:
+ out = self
+ return formatter.formatter(self, out, topic, opts)
def _trusted(self, fp, f):
st = util.fstat(fp)
More information about the Mercurial-devel
mailing list