DebugClients/Python3/AsyncFile.py

branch
jsonrpc
changeset 5128
b6cbdba69967
parent 4631
5c1a96925da4
child 5129
e4ab234cf071
equal deleted inserted replaced
5125:eb1b3e0577e4 5128:b6cbdba69967
7 Module implementing an asynchronous file like socket interface for the 7 Module implementing an asynchronous file like socket interface for the
8 debugger. 8 debugger.
9 """ 9 """
10 10
11 import socket 11 import socket
12
13 from DebugProtocol import EOT
14 12
15 13
16 def AsyncPendingWrite(file): 14 def AsyncPendingWrite(file):
17 """ 15 """
18 Module function to check for data to be written. 16 Module function to check for data to be written.
75 73
76 @param n the number of bytes to be written (int) 74 @param n the number of bytes to be written (int)
77 """ 75 """
78 if n: 76 if n:
79 try: 77 try:
80 buf = "{0!s}{1!s}".format(self.wpending[:n], EOT) 78 buf = self.wpending[:n]
81 try: 79 try:
82 buf = buf.encode('utf-8', 'backslashreplace') 80 buf = buf.encode('utf-8', 'backslashreplace')
83 except (UnicodeEncodeError, UnicodeDecodeError): 81 except (UnicodeEncodeError, UnicodeDecodeError):
84 pass 82 pass
85 self.sock.sendall(buf) 83 self.sock.sendall(buf)
293 291
294 def write(self, s): 292 def write(self, s):
295 """ 293 """
296 Public method to write a string to the file. 294 Public method to write a string to the file.
297 295
298 @param s bytes to be written (string) 296 @param s text to be written (string)
297 """
298 self.__checkMode('w')
299
300 import json
301 commandDict = {
302 "jsonrpc": "2.0",
303 "method": "ClientOutput",
304 "params": {
305 "text": s,
306 }
307 }
308 cmd = json.dumps(commandDict) + '\n'
309
310 self.write_p(cmd)
311
312 def write_p(self, s):
313 """
314 Public method to write a string to the file.
315
316 @param s text to be written (string)
299 @exception socket.error raised to indicate too many send attempts 317 @exception socket.error raised to indicate too many send attempts
300 """ 318 """
301 self.__checkMode('w') 319 self.__checkMode('w')
302 tries = 0 320 tries = 0
303 if not self.wpending: 321 if not self.wpending:
315 self.wpending = s 333 self.wpending = s
316 else: 334 else:
317 self.wpending += s 335 self.wpending += s
318 self.__nWrite(self.pendingWrite()) 336 self.__nWrite(self.pendingWrite())
319 337
320 def writelines(self, list): 338 def writelines(self, lines):
321 """ 339 """
322 Public method to write a list of strings to the file. 340 Public method to write a list of strings to the file.
323 341
324 @param list the list to be written (list of string) 342 @param lines list of texts to be written (list of string)
325 """ 343 """
326 for l in list: 344 self.write("".join(lines))
327 self.write(l)
328 345
329 # 346 #
330 # eflag: noqa = M702 347 # eflag: noqa = M702

eric ide

mercurial