~ubuntu-branches/ubuntu/karmic/eric/karmic

« back to all changes in this revision

Viewing changes to eric/DebugClients/Python/AsyncFile.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2008-01-28 18:02:25 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080128180225-6nrox6yrworh2c4v
Tags: 4.0.4-1ubuntu1
* Add python-qt3 to build-depends becuase that's where Ubuntu puts 
  pyqtconfig
* Change maintainer to MOTU

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# -*- coding: utf-8 -*-
2
2
 
3
3
# Copyright (c) 2002 - 2007 Detlev Offenbach <detlev@die-offenbachs.de>
4
 
# Copyright (c) 2000 Phil Thompson <phil@river-bank.demon.co.uk>
5
4
#
6
5
 
7
6
"""
30
29
    return pending
31
30
 
32
31
 
33
 
class AsyncFile:
 
32
class AsyncFile(object):
34
33
    """
35
34
    Class wrapping a socket object with a file interface.
36
35
    """
54
53
 
55
54
        self.wpending = u''
56
55
 
57
 
    def checkMode(self,mode):
 
56
    def __checkMode(self,mode):
58
57
        """
59
58
        Private method to check the mode.
60
59
        
66
65
        if mode != self.mode:
67
66
            raise IOError, '[Errno 9] Bad file descriptor'
68
67
 
69
 
    def nWrite(self,n):
 
68
    def __nWrite(self,n):
70
69
        """
71
70
        Private method to write a specific number of pending bytes.
72
71
        
75
74
        if n:
76
75
            try :
77
76
                buf = "%s%s" % (self.wpending[:n], EOT)
 
77
                try:
 
78
                    buf = buf.encode('utf8')
 
79
                except (UnicodeEncodeError, UnicodeDecodeError):
 
80
                    pass
78
81
                sent = self.sock.send(buf)
79
82
                if sent > n:
80
83
                    sent -= len(EOT)
108
111
        """
109
112
        Public method to write all pending bytes.
110
113
        """
111
 
        self.nWrite(len(self.wpending))
 
114
        self.__nWrite(len(self.wpending))
112
115
 
113
116
    def isatty(self):
114
117
        """
133
136
        @param size maximum number of bytes to be read (int)
134
137
        @return the bytes read (any)
135
138
        """
136
 
        self.checkMode('r')
 
139
        self.__checkMode('r')
137
140
 
138
141
        if size < 0:
139
142
            size = 20000
140
143
 
141
 
        return self.sock.recv(size)
 
144
        return self.sock.recv(size).decode('utf8')
142
145
 
143
146
    def read(self,size=-1):
144
147
        """
147
150
        @param size maximum number of bytes to be read (int)
148
151
        @return the bytes read (any)
149
152
        """
150
 
        self.checkMode('r')
 
153
        self.__checkMode('r')
151
154
 
152
155
        buf = raw_input()
153
156
        if size >= 0:
164
167
        @param size maximum number of bytes to be read (int)
165
168
        @return one line of text up to size bytes (string)
166
169
        """
167
 
        self.checkMode('r')
 
170
        self.__checkMode('r')
168
171
 
169
172
        if size < 0:
170
173
            size = 20000
173
176
        # to the debugger relies on the two lines of the debugger command being
174
177
        # delivered as two separate events.  Therefore we make sure we only
175
178
        # read a line at a time.
176
 
        line = self.sock.recv(size,socket.MSG_PEEK)
 
179
        line = self.sock.recv(size, socket.MSG_PEEK)
177
180
 
178
181
        eol = line.find('\n')
179
182
 
183
186
            size = len(line)
184
187
 
185
188
        # Now we know how big the line is, read it for real.
186
 
        return self.sock.recv(size)
187
 
 
188
 
    def readline(self, sizehint=-1):
189
 
        """
190
 
        Public method to read one line from this file.
191
 
        
192
 
        @param sizehint hint of the numbers of bytes to be read (int)
193
 
        @return one line read (string)
194
 
        """
195
 
        self.checkMode('r')
196
 
 
197
 
        line = raw_input() + '\n'
198
 
        if sizehint >= 0:
199
 
            line = line[:sizehint]
200
 
        return line
201
 
        
202
 
    def readlines(self, sizehint=-1):
 
189
        return self.sock.recv(size).decode('utf8')
 
190
 
 
191
    def readlines(self,sizehint=-1):
203
192
        """
204
193
        Public method to read all lines from this file.
205
194
        
206
195
        @param sizehint hint of the numbers of bytes to be read (int)
207
196
        @return list of lines read (list of strings)
208
197
        """
 
198
        self.__checkMode('r')
 
199
 
209
200
        lines = []
210
201
        room = sizehint
211
202
 
226
217
 
227
218
        return lines
228
219
 
 
220
    def readline(self, sizehint=-1):
 
221
        """
 
222
        Public method to read one line from this file.
 
223
        
 
224
        @param sizehint hint of the numbers of bytes to be read (int)
 
225
        @return one line read (string)
 
226
        """
 
227
        self.__checkMode('r')
 
228
 
 
229
        line = raw_input() + '\n'
 
230
        if sizehint >= 0:
 
231
            line = line[:sizehint]
 
232
        return line
 
233
        
229
234
    def seek(self,offset,whence=0):
230
235
        """
231
236
        Public method to move the filepointer.
259
264
        
260
265
        @param s bytes to be written (string)
261
266
        """
262
 
        self.checkMode('w')
263
 
##        if type(s) is not type(u""):
264
 
##            s = unicode(s, sys.getdefaultencoding())
265
 
##        self.wpending = self.wpending + s
 
267
        self.__checkMode('w')
266
268
        tries = 0
267
269
        if not self.wpending:
268
270
            self.wpending = s
271
273
            # flush wpending so that different string types are not concatenated
272
274
            while self.wpending:
273
275
                # if we have a persistent error in sending the data, an exception
274
 
                # will be raised in nWrite
 
276
                # will be raised in __nWrite
275
277
                self.flush()
276
278
                tries += 1
277
279
                if tries > self.maxtries:
279
281
            self.wpending = s
280
282
        else:
281
283
            self.wpending += s
282
 
        self.nWrite(self.pendingWrite())
 
284
        self.__nWrite(self.pendingWrite())
283
285
 
284
286
    def writelines(self,list):
285
287
        """