Plugins/VcsPlugins/vcsMercurial/HgClient.py

changeset 2962
d6c9d1ca2da4
parent 2816
05aab5164d64
child 3008
7848489bcb92
equal deleted inserted replaced
2961:e4e2efb4846a 2962:d6c9d1ca2da4
8 """ 8 """
9 9
10 import struct 10 import struct
11 import io 11 import io
12 12
13 from PyQt4.QtCore import QProcess, QObject, QByteArray, QCoreApplication, QThread 13 from PyQt4.QtCore import QProcess, QObject, QByteArray, QCoreApplication, \
14 QThread
14 from PyQt4.QtGui import QDialog 15 from PyQt4.QtGui import QDialog
15 16
16 from .HgUtilities import prepareProcess 17 from .HgUtilities import prepareProcess
17 18
18 import Preferences 19 import Preferences
132 return False, self.trUtf8("Received data on unexpected channel.") 133 return False, self.trUtf8("Received data on unexpected channel.")
133 134
134 msg = msg.split("\n") 135 msg = msg.split("\n")
135 136
136 if not msg[0].startswith("capabilities: "): 137 if not msg[0].startswith("capabilities: "):
137 return False, self.trUtf8("Bad 'hello' message, expected 'capabilities: '" 138 return False, self.trUtf8(
138 " but got '{0}'.").format(msg[0]) 139 "Bad 'hello' message, expected 'capabilities: '"
140 " but got '{0}'.").format(msg[0])
139 self.__capabilities = msg[0][len('capabilities: '):] 141 self.__capabilities = msg[0][len('capabilities: '):]
140 if not self.__capabilities: 142 if not self.__capabilities:
141 return False, self.trUtf8("'capabilities' message did not contain" 143 return False, self.trUtf8("'capabilities' message did not contain"
142 " any capability.") 144 " any capability.")
143 145
144 self.__capabilities = set(self.__capabilities.split()) 146 self.__capabilities = set(self.__capabilities.split())
145 if "runcommand" not in self.__capabilities: 147 if "runcommand" not in self.__capabilities:
146 return False, "'capabilities' did not contain 'runcommand'." 148 return False, "'capabilities' did not contain 'runcommand'."
147 149
148 if not msg[1].startswith("encoding: "): 150 if not msg[1].startswith("encoding: "):
149 return False, self.trUtf8("Bad 'hello' message, expected 'encoding: '" 151 return False, self.trUtf8(
150 " but got '{0}'.").format(msg[1]) 152 "Bad 'hello' message, expected 'encoding: '"
153 " but got '{0}'.").format(msg[1])
151 encoding = msg[1][len('encoding: '):] 154 encoding = msg[1][len('encoding: '):]
152 if not encoding: 155 if not encoding:
153 return False, self.trUtf8("'encoding' message did not contain" 156 return False, self.trUtf8("'encoding' message did not contain"
154 " any encoding.") 157 " any encoding.")
155 self.__encoding = encoding 158 self.__encoding = encoding
204 207
205 @param data data to be sent (string) 208 @param data data to be sent (string)
206 """ 209 """
207 if not isinstance(data, bytes): 210 if not isinstance(data, bytes):
208 data = data.encode(self.__encoding) 211 data = data.encode(self.__encoding)
209 self.__server.write(QByteArray(struct.pack(HgClient.InputFormat, len(data)))) 212 self.__server.write(
213 QByteArray(struct.pack(HgClient.InputFormat, len(data))))
210 self.__server.write(QByteArray(data)) 214 self.__server.write(QByteArray(data))
211 self.__server.waitForBytesWritten() 215 self.__server.waitForBytesWritten()
212 216
213 def __runcommand(self, args, inputChannels, outputChannels): 217 def __runcommand(self, args, inputChannels, outputChannels):
214 """ 218 """
215 Private method to run a command in the server (low level). 219 Private method to run a command in the server (low level).
216 220
217 @param args list of arguments for the command (list of string) 221 @param args list of arguments for the command (list of string)
218 @param inputChannels dictionary of input channels. The dictionary must 222 @param inputChannels dictionary of input channels. The dictionary must
219 have the keys 'I' and 'L' and each entry must be a function receiving 223 have the keys 'I' and 'L' and each entry must be a function
220 the number of bytes to write. 224 receiving the number of bytes to write.
221 @param outputChannels dictionary of output channels. The dictionary must 225 @param outputChannels dictionary of output channels. The dictionary
222 have the keys 'o' and 'e' and each entry must be a function receiving 226 must have the keys 'o' and 'e' and each entry must be a function
223 the data. 227 receiving the data.
224 @return result code of the command, -1 if the command server wasn't started or 228 @return result code of the command, -1 if the command server wasn't
225 -10, if the command was canceled (integer) 229 started or -10, if the command was canceled (integer)
230 @exception RuntimeError raised to indicate an unexpected command
231 channel
226 """ 232 """
227 if not self.__started: 233 if not self.__started:
228 return -1 234 return -1
229 235
230 self.__server.write(QByteArray(b'runcommand\n')) 236 self.__server.write(QByteArray(b'runcommand\n'))
273 """ 279 """
274 Private method to prompt the user for some input. 280 Private method to prompt the user for some input.
275 281
276 @param size maximum length of the requested input (integer) 282 @param size maximum length of the requested input (integer)
277 @param message message sent by the server (string) 283 @param message message sent by the server (string)
284 @return data entered by the user (string)
278 """ 285 """
279 from .HgClientPromptDialog import HgClientPromptDialog 286 from .HgClientPromptDialog import HgClientPromptDialog
280 input = "" 287 input = ""
281 dlg = HgClientPromptDialog(size, message) 288 dlg = HgClientPromptDialog(size, message)
282 if dlg.exec_() == QDialog.Accepted: 289 if dlg.exec_() == QDialog.Accepted:
283 input = dlg.getInput() + '\n' 290 input = dlg.getInput() + '\n'
284 return input 291 return input
285 292
286 def runcommand(self, args, prompt=None, input=None, output=None, error=None): 293 def runcommand(self, args, prompt=None, input=None, output=None,
294 error=None):
287 """ 295 """
288 Public method to execute a command via the command server. 296 Public method to execute a command via the command server.
289 297
290 @param args list of arguments for the command (list of string) 298 @param args list of arguments for the command (list of string)
291 @keyparam prompt function to reply to prompts by the server. It 299 @keyparam prompt function to reply to prompts by the server. It
293 of the output channel received so far. 301 of the output channel received so far.
294 @keyparam input function to reply to bulk data requests by the server. 302 @keyparam input function to reply to bulk data requests by the server.
295 It receives the max number of bytes to return. 303 It receives the max number of bytes to return.
296 @keyparam output function receiving the data from the server (string). 304 @keyparam output function receiving the data from the server (string).
297 If a prompt function is given, this parameter will be ignored. 305 If a prompt function is given, this parameter will be ignored.
298 @keyparam error function receiving error messages from the server (string) 306 @keyparam error function receiving error messages from the server
299 @return output and errors of the command server (string). In case output 307 (string)
300 and/or error functions were given, the respective return value will 308 @return output and errors of the command server (string). In case
301 be an empty string. 309 output and/or error functions were given, the respective return
310 value will be an empty string.
302 """ 311 """
303 self.__commandRunning = True 312 self.__commandRunning = True
304 outputChannels = {} 313 outputChannels = {}
305 outputBuffer = None 314 outputBuffer = None
306 errorBuffer = None 315 errorBuffer = None
357 self.restartServer() 366 self.restartServer()
358 367
359 def wasCanceled(self): 368 def wasCanceled(self):
360 """ 369 """
361 Public method to check, if the last command was canceled. 370 Public method to check, if the last command was canceled.
371
372 @return flag indicating the cancel state (boolean)
362 """ 373 """
363 return self.__cancel 374 return self.__cancel
364 375
365 def isExecuting(self): 376 def isExecuting(self):
366 """ 377 """
367 Public method to check, if the server is executing a command. 378 Public method to check, if the server is executing a command.
368 379
369 @return flag indicating the execution of a command (boolean) 380 @return flag indicating the execution of a command (boolean)
370 """ 381 """
382 return self.__commandRunning

eric ide

mercurial