/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/smart/message.py

  • Committer: Marius Kruger
  • Date: 2010-07-10 21:28:56 UTC
  • mto: (5384.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5385.
  • Revision ID: marius.kruger@enerweb.co.za-20100710212856-uq4ji3go0u5se7hx
* Update documentation
* add NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
from __future__ import absolute_import
18
 
 
19
 
try:
20
 
    from collections.abc import deque
21
 
except ImportError:  # python < 3.7
22
 
    from collections import deque
23
 
 
24
 
from ... import (
 
17
import collections
 
18
from cStringIO import StringIO
 
19
 
 
20
from bzrlib import (
25
21
    debug,
26
22
    errors,
27
23
    )
28
 
from ...sixish import (
29
 
    BytesIO,
30
 
    )
31
 
from ...trace import mutter
 
24
from bzrlib.trace import mutter
32
25
 
33
26
 
34
27
class MessageHandler(object):
116
109
        self.responder.send_error(exception)
117
110
 
118
111
    def byte_part_received(self, byte):
119
 
        if not isinstance(byte, bytes):
120
 
            raise TypeError(byte)
121
112
        if self.expecting == 'body':
122
 
            if byte == b'S':
 
113
            if byte == 'S':
123
114
                # Success.  Nothing more to come except the end of message.
124
115
                self.expecting = 'end'
125
 
            elif byte == b'E':
 
116
            elif byte == 'E':
126
117
                # Error.  Expect an error structure.
127
118
                self.expecting = 'error'
128
119
            else:
221
212
        MessageHandler.__init__(self)
222
213
        self.status = None
223
214
        self.args = None
224
 
        self._bytes_parts = deque()
 
215
        self._bytes_parts = collections.deque()
225
216
        self._body_started = False
226
217
        self._body_stream_status = None
227
218
        self._body = None
233
224
        self._medium_request = medium_request
234
225
 
235
226
    def byte_part_received(self, byte):
236
 
        if not isinstance(byte, bytes):
237
 
            raise TypeError(byte)
238
 
        if byte not in [b'E', b'S']:
 
227
        if byte not in ['E', 'S']:
239
228
            raise errors.SmartProtocolError(
240
229
                'Unknown response status: %r' % (byte,))
241
230
        if self._body_started:
254
243
        self._bytes_parts.append(bytes)
255
244
 
256
245
    def structure_part_received(self, structure):
257
 
        if not isinstance(structure, tuple):
 
246
        if type(structure) is not tuple:
258
247
            raise errors.SmartProtocolError(
259
248
                'Args structure is not a sequence: %r' % (structure,))
260
249
        if not self._body_started:
264
253
                    % (structure, self.args))
265
254
            self.args = structure
266
255
        else:
267
 
            if self._body_stream_status != b'E':
 
256
            if self._body_stream_status != 'E':
268
257
                raise errors.SmartProtocolError(
269
258
                    'Unexpected structure received after body: %r'
270
259
                    % (structure,))
285
274
            self.finished_reading = True
286
275
            self._medium_request.finished_reading()
287
276
            return
288
 
        data = self._medium_request.read_bytes(next_read_size)
289
 
        if data == b'':
 
277
        bytes = self._medium_request.read_bytes(next_read_size)
 
278
        if bytes == '':
290
279
            # end of file encountered reading from server
291
280
            if 'hpss' in debug.debug_flags:
292
281
                mutter(
297
286
                "Unexpected end of message. "
298
287
                "Please check connectivity and permissions, and report a bug "
299
288
                "if problems persist.")
300
 
        self._protocol_decoder.accept_bytes(data)
 
289
        self._protocol_decoder.accept_bytes(bytes)
301
290
 
302
291
    def protocol_error(self, exception):
303
292
        # Whatever the error is, we're done with this request.
312
301
            self._wait_for_response_end()
313
302
        if 'hpss' in debug.debug_flags:
314
303
            mutter('   result:   %r', self.args)
315
 
        if self.status == b'E':
 
304
        if self.status == 'E':
316
305
            self._wait_for_response_end()
317
 
            _raise_smart_server_error(self.args)
 
306
            _translate_error(self.args)
318
307
        return tuple(self.args)
319
308
 
320
309
    def read_body_bytes(self, count=-1):
330
319
        # != -1.  (2008/04/30, Andrew Bennetts)
331
320
        if self._body is None:
332
321
            self._wait_for_response_end()
333
 
            body_bytes = b''.join(self._bytes_parts)
 
322
            body_bytes = ''.join(self._bytes_parts)
334
323
            if 'hpss' in debug.debug_flags:
335
324
                mutter('              %d body bytes read', len(body_bytes))
336
 
            self._body = BytesIO(body_bytes)
 
325
            self._body = StringIO(body_bytes)
337
326
            self._bytes_parts = None
338
327
        return self._body.read(count)
339
328
 
345
334
                    mutter('              %d byte part read', len(bytes_part))
346
335
                yield bytes_part
347
336
            self._read_more()
348
 
        if self._body_stream_status == b'E':
349
 
            _raise_smart_server_error(self._body_error_args)
 
337
        if self._body_stream_status == 'E':
 
338
            _translate_error(self._body_error_args)
350
339
 
351
340
    def cancel_read_body(self):
352
341
        self._wait_for_response_end()
353
342
 
354
343
 
355
 
def _raise_smart_server_error(error_tuple):
356
 
    """Raise exception based on tuple received from smart server
357
 
 
358
 
    Specific error translation is handled by breezy.bzr.remote._translate_error
359
 
    """
360
 
    if error_tuple[0] == b'UnknownMethod':
361
 
        raise errors.UnknownSmartMethod(error_tuple[1])
362
 
    raise errors.ErrorFromSmartServer(error_tuple)
 
344
def _translate_error(error_tuple):
 
345
    # Many exceptions need some state from the requestor to be properly
 
346
    # translated (e.g. they need a branch object).  So this only translates a
 
347
    # few errors, and the rest are turned into a generic ErrorFromSmartServer.
 
348
    error_name = error_tuple[0]
 
349
    error_args = error_tuple[1:]
 
350
    if error_name == 'UnknownMethod':
 
351
        raise errors.UnknownSmartMethod(error_args[0])
 
352
    if error_name == 'LockContention':
 
353
        raise errors.LockContention('(remote lock)')
 
354
    elif error_name == 'LockFailed':
 
355
        raise errors.LockFailed(*error_args[:2])
 
356
    elif error_name == 'FileExists':
 
357
        raise errors.FileExists(error_args[0])
 
358
    elif error_name == 'NoSuchFile':
 
359
        raise errors.NoSuchFile(error_args[0])
 
360
    else:
 
361
        raise errors.ErrorFromSmartServer(error_tuple)