DebugClients/Python/AsyncFile.py

branch
jsonrpc
changeset 5132
a094eee9f862
parent 4631
5c1a96925da4
--- a/DebugClients/Python/AsyncFile.py	Sat Sep 03 18:01:19 2016 +0200
+++ b/DebugClients/Python/AsyncFile.py	Sat Sep 03 18:02:37 2016 +0200
@@ -10,7 +10,7 @@
 
 import socket
 
-from DebugProtocol import EOT
+from DebugUtilities import prepareJsonCommand
 
 
 def AsyncPendingWrite(file):
@@ -44,7 +44,7 @@
         @param name name of this file (string)
         """
         # Initialise the attributes.
-        self.closed = 0
+        self.closed = False
         self.sock = sock
         self.mode = mode
         self.name = name
@@ -74,9 +74,9 @@
         """
         if n:
             try:
-                buf = "%s%s" % (self.wpending[:n], EOT)
+                buf = self.wpending[:n]
                 try:
-                    buf = buf.encode('utf-8')
+                    buf = buf.encode('utf-8', 'backslashreplace')
                 except (UnicodeEncodeError, UnicodeDecodeError):
                     pass
                 self.sock.sendall(buf)
@@ -95,7 +95,7 @@
         """
         return self.wpending.rfind('\n') + 1
 
-    def close(self, closeit=0):
+    def close(self, closeit=False):
         """
         Public method to close the file.
         
@@ -105,7 +105,7 @@
         if closeit and not self.closed:
             self.flush()
             self.sock.close()
-            self.closed = 1
+            self.closed = True
 
     def flush(self):
         """
@@ -119,7 +119,7 @@
         
         @return always false
         """
-        return 0
+        return False
 
     def fileno(self):
         """
@@ -132,6 +132,14 @@
         except socket.error:
             return -1
 
+    def readable(self):
+        """
+        Public method to check, if the stream is readable.
+        
+        @return flag indicating a readable stream (boolean)
+        """
+        return self.mode == "r"
+    
     def read_p(self, size=-1):
         """
         Public method to read bytes from this file.
@@ -144,7 +152,7 @@
         if size < 0:
             size = 20000
 
-        return self.sock.recv(size).decode('utf8')
+        return self.sock.recv(size).decode('utf8', 'backslashreplace')
 
     def read(self, size=-1):
         """
@@ -181,7 +189,7 @@
         # read a line at a time.
         line = self.sock.recv(size, socket.MSG_PEEK)
 
-        eol = line.find('\n')
+        eol = line.find(b'\n')
 
         if eol >= 0:
             size = eol + 1
@@ -189,7 +197,7 @@
             size = len(line)
 
         # Now we know how big the line is, read it for real.
-        return self.sock.recv(size).decode('utf8')
+        return self.sock.recv(size).decode('utf8', 'backslashreplace')
 
     def readlines(self, sizehint=-1):
         """
@@ -234,6 +242,14 @@
             line = line[:sizehint]
         return line
         
+    def seekable(self):
+        """
+        Public method to check, if the stream is seekable.
+        
+        @return flag indicating a seekable stream (boolean)
+        """
+        return False
+    
     def seek(self, offset, whence=0):
         """
         Public method to move the filepointer.
@@ -264,21 +280,40 @@
         """
         raise IOError('[Errno 29] Illegal seek')
 
+    def writable(self):
+        """
+        Public method to check, if a stream is writable.
+        
+        @return flag indicating a writable stream (boolean)
+        """
+        return self.mode == "w"
+    
     def write(self, s):
         """
         Public method to write a string to the file.
         
         @param s bytes to be written (string)
+        """
+        self.__checkMode('w')
+        
+        cmd = prepareJsonCommand("ClientOutput", {
+            "text": s,
+        })
+        self.write_p(cmd)
+    
+    def write_p(self, s):
+        """
+        Public method to write a string to the file.
+        
+        @param s text to be written (string)
         @exception socket.error raised to indicate too many send attempts
         """
         self.__checkMode('w')
         tries = 0
         if not self.wpending:
             self.wpending = s
-        elif type(self.wpending) != type(s) or \
-                len(self.wpending) + len(s) > self.maxbuffersize:
-            # flush wpending so that different string types are not
-            # concatenated
+        elif len(self.wpending) + len(s) > self.maxbuffersize:
+            # flush wpending if it is too big
             while self.wpending:
                 # if we have a persistent error in sending the data, an
                 # exception will be raised in __nWrite
@@ -291,13 +326,13 @@
             self.wpending += s
         self.__nWrite(self.pendingWrite())
 
-    def writelines(self, list):
+    def writelines(self, lines):
         """
         Public method to write a list of strings to the file.
         
-        @param list the list to be written (list of string)
+        @param lines list of texts to be written (list of string)
         """
-        map(self.write, list)
+        self.write("".join(lines))
 
 #
 # eflag: FileType = Python2

eric ide

mercurial