Patchwork [python-hglib,v3] Allow hglib user to get call backs for prompts, output and errors

login
register
mail settings
Submitter Barry A. Scott
Date Oct. 28, 2016, 10:35 a.m.
Message ID <dd7ce6a2c32d179eb288.1477650927@varric.chelsea.private>
Download mbox | patch
Permalink /patch/17209/
State Accepted
Headers show

Comments

Barry A. Scott - Oct. 28, 2016, 10:35 a.m.
# HG changeset patch
# User Barry A. Scott <barry@barrys-emacs.org>
# Date 1477650800 -3600
#      Fri Oct 28 11:33:20 2016 +0100
# Node ID dd7ce6a2c32d179eb2881d08e13d11631bf341fa
# Parent  6f15cb7cc9cb4427f35c60080f85dbf4ca5abd10
Allow hglib user to get call backs for prompts, output and errors.

setcbout(cbout), setcberr(cberr) and setcbprompt(cbprompt) are used to
set the call back function used by the hgclient class. cb stands for
call back.

cbout is a function that will be called with the stdout data of the
command as it runs. cbout is called with output as it is made available,
which can be as partial lines or multiple lines.

cberr is a function that will be called with the stderr data of the
command as it runs. cberr is called with output as it is made available,
which can be as partial lines or multiple lines.

Command that make remote connects can prompt for username and password
for HTTP/HTTPS connections.

cbprompt is called when hgclient need a response to a prompt from the
server. It receives the max number of bytes to return and the contents
of stdout received so far. The last text sent to either cbout or cberr
will contain the prompt text itself.
Yuya Nishihara - Oct. 28, 2016, 1:24 p.m.
On Fri, 28 Oct 2016 11:35:27 +0100, Barry A. Scott wrote:
> # HG changeset patch
> # User Barry A. Scott <barry@barrys-emacs.org>
> # Date 1477650800 -3600
> #      Fri Oct 28 11:33:20 2016 +0100
> # Node ID dd7ce6a2c32d179eb2881d08e13d11631bf341fa
> # Parent  6f15cb7cc9cb4427f35c60080f85dbf4ca5abd10
> Allow hglib user to get call backs for prompts, output and errors.
> 
> setcbout(cbout), setcberr(cberr) and setcbprompt(cbprompt) are used to
> set the call back function used by the hgclient class. cb stands for
> call back.
> 
> cbout is a function that will be called with the stdout data of the
> command as it runs. cbout is called with output as it is made available,
> which can be as partial lines or multiple lines.
> 
> cberr is a function that will be called with the stderr data of the
> command as it runs. cberr is called with output as it is made available,
> which can be as partial lines or multiple lines.
> 
> Command that make remote connects can prompt for username and password
> for HTTP/HTTPS connections.
> 
> cbprompt is called when hgclient need a response to a prompt from the
> server. It receives the max number of bytes to return and the contents
> of stdout received so far. The last text sent to either cbout or cberr
> will contain the prompt text itself.

I'm not a big fan of these APIs, but they look okay and solve the real
problem, so seem good. Marked as pre-reviewed, thanks.

Can you add some tests as a follow-up?
Matt Mackall - Nov. 4, 2016, 6:04 p.m.
On Fri, 2016-10-28 at 22:24 +0900, Yuya Nishihara wrote:
> On Fri, 28 Oct 2016 11:35:27 +0100, Barry A. Scott wrote:
> > 
> > # HG changeset patch
> > # User Barry A. Scott <barry@barrys-emacs.org>
> > # Date 1477650800 -3600
> > #      Fri Oct 28 11:33:20 2016 +0100
> > # Node ID dd7ce6a2c32d179eb2881d08e13d11631bf341fa
> > # Parent  6f15cb7cc9cb4427f35c60080f85dbf4ca5abd10
> > Allow hglib user to get call backs for prompts, output and errors.
> > 
> > setcbout(cbout), setcberr(cberr) and setcbprompt(cbprompt) are used to
> > set the call back function used by the hgclient class. cb stands for
> > call back.
> > 
> > cbout is a function that will be called with the stdout data of the
> > command as it runs. cbout is called with output as it is made available,
> > which can be as partial lines or multiple lines.
> > 
> > cberr is a function that will be called with the stderr data of the
> > command as it runs. cberr is called with output as it is made available,
> > which can be as partial lines or multiple lines.
> > 
> > Command that make remote connects can prompt for username and password
> > for HTTP/HTTPS connections.
> > 
> > cbprompt is called when hgclient need a response to a prompt from the
> > server. It receives the max number of bytes to return and the contents
> > of stdout received so far. The last text sent to either cbout or cberr
> > will contain the prompt text itself.
> I'm not a big fan of these APIs, but they look okay and solve the real
> problem, so seem good. Marked as pre-reviewed, thanks.

These and two other patches are pushed to hglib, thanks.

-- 
Mathematics is the supreme nostalgia of our time.

Patch

diff -r 6f15cb7cc9cb -r dd7ce6a2c32d hglib/client.py
--- a/hglib/client.py	Mon Jul 18 23:40:45 2016 -0500
+++ b/hglib/client.py	Fri Oct 28 11:33:20 2016 +0100
@@ -59,9 +59,40 @@ 
         # include the hidden changesets if True
         self.hidden = None
 
+        self._cbout = None
+        self._cberr = None
+        self._cbprompt = None
+
         if connect:
             self.open()
 
+    def setcbout(self, cbout):
+        """
+        cbout is a function that will be called with the stdout data of
+         the command as it runs. Call with None to stop getting call backs.
+        """
+        self._cbout = cbout
+
+    def setcberr(self, cberr):
+        """
+        cberr is a function that will be called with the stderr data of
+         the command as it runs.Call with None to stop getting call backs.
+        """
+        self._cberr = cberr
+
+    def setcbprompt(self, cbprompt):
+        """
+        cbprompt is used to reply to prompts by the server
+         It receives the max number of bytes to return and the
+         contents of stdout received so far.
+
+        Call with None to stop getting call backs.
+
+        cbprompt is never called from merge() or import_()
+        which already handle the prompt.
+        """
+        self._cbprompt = cbprompt
+
     def __enter__(self):
         if self.server is None:
             self.open()
@@ -164,9 +195,25 @@ 
         It receives the max number of bytes to return
         """
         out, err = BytesIO(), BytesIO()
-        outchannels = {b('o') : out.write, b('e') : err.write}
+        outchannels = {}
+        if self._cbout is None:
+            outchannels[b('o')] = out.write
+        else:
+            def out_handler(data):
+                out.write(data)
+                self._cbout(data)
+            outchannels[b('o')] = out_handler
+        if self._cberr is None:
+            outchannels[b('e')] = err.write
+        else:
+            def err_handler(data):
+                err.write(data)
+                self._cberr(data)
+            outchannels[b('e')] = err_handler
 
         inchannels = {}
+        if prompt is None:
+            prompt = self._cbprompt
         if prompt is not None:
             def func(size):
                 reply = prompt(size, out.getvalue())