/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 breezy/smart/message.py

  • Committer: Jelmer Vernooij
  • Date: 2017-06-08 23:30:31 UTC
  • mto: This revision was merged to the branch mainline in revision 6690.
  • Revision ID: jelmer@jelmer.uk-20170608233031-3qavls2o7a1pqllj
Update imports.

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
 
try:
18
 
    from collections.abc import deque
19
 
except ImportError:  # python < 3.7
20
 
    from collections import deque
21
 
 
22
 
from io import (
23
 
    BytesIO,
24
 
    )
25
 
 
26
 
from ... import (
 
17
from __future__ import absolute_import
 
18
 
 
19
import collections
 
20
 
 
21
from .. import (
27
22
    debug,
28
23
    errors,
29
24
    )
30
 
from ...trace import mutter
 
25
from ..sixish import (
 
26
    BytesIO,
 
27
    )
 
28
from ..trace import mutter
31
29
 
32
30
 
33
31
class MessageHandler(object):
115
113
        self.responder.send_error(exception)
116
114
 
117
115
    def byte_part_received(self, byte):
118
 
        if not isinstance(byte, bytes):
119
 
            raise TypeError(byte)
120
116
        if self.expecting == 'body':
121
 
            if byte == b'S':
 
117
            if byte == 'S':
122
118
                # Success.  Nothing more to come except the end of message.
123
119
                self.expecting = 'end'
124
 
            elif byte == b'E':
 
120
            elif byte == 'E':
125
121
                # Error.  Expect an error structure.
126
122
                self.expecting = 'error'
127
123
            else:
220
216
        MessageHandler.__init__(self)
221
217
        self.status = None
222
218
        self.args = None
223
 
        self._bytes_parts = deque()
 
219
        self._bytes_parts = collections.deque()
224
220
        self._body_started = False
225
221
        self._body_stream_status = None
226
222
        self._body = None
232
228
        self._medium_request = medium_request
233
229
 
234
230
    def byte_part_received(self, byte):
235
 
        if not isinstance(byte, bytes):
236
 
            raise TypeError(byte)
237
 
        if byte not in [b'E', b'S']:
 
231
        if byte not in ['E', 'S']:
238
232
            raise errors.SmartProtocolError(
239
233
                'Unknown response status: %r' % (byte,))
240
234
        if self._body_started:
263
257
                    % (structure, self.args))
264
258
            self.args = structure
265
259
        else:
266
 
            if self._body_stream_status != b'E':
 
260
            if self._body_stream_status != 'E':
267
261
                raise errors.SmartProtocolError(
268
262
                    'Unexpected structure received after body: %r'
269
263
                    % (structure,))
284
278
            self.finished_reading = True
285
279
            self._medium_request.finished_reading()
286
280
            return
287
 
        data = self._medium_request.read_bytes(next_read_size)
288
 
        if data == b'':
 
281
        bytes = self._medium_request.read_bytes(next_read_size)
 
282
        if bytes == '':
289
283
            # end of file encountered reading from server
290
284
            if 'hpss' in debug.debug_flags:
291
285
                mutter(
296
290
                "Unexpected end of message. "
297
291
                "Please check connectivity and permissions, and report a bug "
298
292
                "if problems persist.")
299
 
        self._protocol_decoder.accept_bytes(data)
 
293
        self._protocol_decoder.accept_bytes(bytes)
300
294
 
301
295
    def protocol_error(self, exception):
302
296
        # Whatever the error is, we're done with this request.
311
305
            self._wait_for_response_end()
312
306
        if 'hpss' in debug.debug_flags:
313
307
            mutter('   result:   %r', self.args)
314
 
        if self.status == b'E':
 
308
        if self.status == 'E':
315
309
            self._wait_for_response_end()
316
310
            _raise_smart_server_error(self.args)
317
311
        return tuple(self.args)
329
323
        # != -1.  (2008/04/30, Andrew Bennetts)
330
324
        if self._body is None:
331
325
            self._wait_for_response_end()
332
 
            body_bytes = b''.join(self._bytes_parts)
 
326
            body_bytes = ''.join(self._bytes_parts)
333
327
            if 'hpss' in debug.debug_flags:
334
328
                mutter('              %d body bytes read', len(body_bytes))
335
329
            self._body = BytesIO(body_bytes)
344
338
                    mutter('              %d byte part read', len(bytes_part))
345
339
                yield bytes_part
346
340
            self._read_more()
347
 
        if self._body_stream_status == b'E':
 
341
        if self._body_stream_status == 'E':
348
342
            _raise_smart_server_error(self._body_error_args)
349
343
 
350
344
    def cancel_read_body(self):
354
348
def _raise_smart_server_error(error_tuple):
355
349
    """Raise exception based on tuple received from smart server
356
350
 
357
 
    Specific error translation is handled by breezy.bzr.remote._translate_error
 
351
    Specific error translation is handled by breezy.remote._translate_error
358
352
    """
359
 
    if error_tuple[0] == b'UnknownMethod':
 
353
    if error_tuple[0] == 'UnknownMethod':
360
354
        raise errors.UnknownSmartMethod(error_tuple[1])
361
355
    raise errors.ErrorFromSmartServer(error_tuple)