Mercurial > python-hglib
comparison hglib/client.py @ 180:ff6efc1ab9e4
protocol: 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.
author | Barry A. Scott <barry@barrys-emacs.org> |
---|---|
date | Fri, 28 Oct 2016 11:33:20 +0100 |
parents | c4c0efb37187 |
children | cdb5a320d2bf |
comparison
equal
deleted
inserted
replaced
179:c4c0efb37187 | 180:ff6efc1ab9e4 |
---|---|
57 self.server = None | 57 self.server = None |
58 self._version = None | 58 self._version = None |
59 # include the hidden changesets if True | 59 # include the hidden changesets if True |
60 self.hidden = None | 60 self.hidden = None |
61 | 61 |
62 self._cbout = None | |
63 self._cberr = None | |
64 self._cbprompt = None | |
65 | |
62 if connect: | 66 if connect: |
63 self.open() | 67 self.open() |
64 | 68 |
65 self._protocoltracefn = None | 69 self._protocoltracefn = None |
70 | |
71 def setcbout(self, cbout): | |
72 """ | |
73 cbout is a function that will be called with the stdout data of | |
74 the command as it runs. Call with None to stop getting call backs. | |
75 """ | |
76 self._cbout = cbout | |
77 | |
78 def setcberr(self, cberr): | |
79 """ | |
80 cberr is a function that will be called with the stderr data of | |
81 the command as it runs.Call with None to stop getting call backs. | |
82 """ | |
83 self._cberr = cberr | |
84 | |
85 def setcbprompt(self, cbprompt): | |
86 """ | |
87 cbprompt is used to reply to prompts by the server | |
88 It receives the max number of bytes to return and the | |
89 contents of stdout received so far. | |
90 | |
91 Call with None to stop getting call backs. | |
92 | |
93 cbprompt is never called from merge() or import_() | |
94 which already handle the prompt. | |
95 """ | |
96 self._cbprompt = cbprompt | |
66 | 97 |
67 def setprotocoltrace(self, tracefn=None): | 98 def setprotocoltrace(self, tracefn=None): |
68 """ | 99 """ |
69 if tracefn is None no trace calls will be made. | 100 if tracefn is None no trace calls will be made. |
70 Otherwise tracefn is call as tracefn( direction, channel, data ) | 101 Otherwise tracefn is call as tracefn( direction, channel, data ) |
178 | 209 |
179 input is used to reply to bulk data requests by the server | 210 input is used to reply to bulk data requests by the server |
180 It receives the max number of bytes to return | 211 It receives the max number of bytes to return |
181 """ | 212 """ |
182 out, err = BytesIO(), BytesIO() | 213 out, err = BytesIO(), BytesIO() |
183 outchannels = {b('o') : out.write, b('e') : err.write} | 214 outchannels = {} |
215 if self._cbout is None: | |
216 outchannels[b('o')] = out.write | |
217 else: | |
218 def out_handler(data): | |
219 out.write(data) | |
220 self._cbout(data) | |
221 outchannels[b('o')] = out_handler | |
222 if self._cberr is None: | |
223 outchannels[b('e')] = err.write | |
224 else: | |
225 def err_handler(data): | |
226 err.write(data) | |
227 self._cberr(data) | |
228 outchannels[b('e')] = err_handler | |
184 | 229 |
185 inchannels = {} | 230 inchannels = {} |
231 if prompt is None: | |
232 prompt = self._cbprompt | |
186 if prompt is not None: | 233 if prompt is not None: |
187 def func(size): | 234 def func(size): |
188 reply = prompt(size, out.getvalue()) | 235 reply = prompt(size, out.getvalue()) |
189 return reply | 236 return reply |
190 inchannels[b('L')] = func | 237 inchannels[b('L')] = func |