~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/client.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-04-08 06:17:41 UTC
  • mfrom: (4797.33.16 apport)
  • Revision ID: pqm@pqm.ubuntu.com-20100408061741-m7vl6z97vu33riv7
(robertc) Make sure ExecutablePath and InterpreterPath are set in
        Apport. (Martin Pool, James Westby, lp:528114)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2008 Canonical Ltd
 
1
# Copyright (C) 2006-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
import bzrlib
18
18
from bzrlib.smart import message, protocol
19
19
from bzrlib.trace import warning
20
 
from bzrlib import errors
 
20
from bzrlib import (
 
21
    errors,
 
22
    hooks,
 
23
    )
21
24
 
22
25
 
23
26
class _SmartClient(object):
33
36
        else:
34
37
            self._headers = dict(headers)
35
38
 
 
39
    def __repr__(self):
 
40
        return '%s(%r)' % (self.__class__.__name__, self._medium)
 
41
 
36
42
    def _send_request(self, protocol_version, method, args, body=None,
37
 
                      readv_body=None):
 
43
                      readv_body=None, body_stream=None):
38
44
        encoder, response_handler = self._construct_protocol(
39
45
            protocol_version)
40
46
        encoder.set_headers(self._headers)
42
48
            if readv_body is not None:
43
49
                raise AssertionError(
44
50
                    "body and readv_body are mutually exclusive.")
 
51
            if body_stream is not None:
 
52
                raise AssertionError(
 
53
                    "body and body_stream are mutually exclusive.")
45
54
            encoder.call_with_body_bytes((method, ) + args, body)
46
55
        elif readv_body is not None:
47
 
            encoder.call_with_body_readv_array((method, ) + args,
48
 
                    readv_body)
 
56
            if body_stream is not None:
 
57
                raise AssertionError(
 
58
                    "readv_body and body_stream are mutually exclusive.")
 
59
            encoder.call_with_body_readv_array((method, ) + args, readv_body)
 
60
        elif body_stream is not None:
 
61
            encoder.call_with_body_stream((method, ) + args, body_stream)
49
62
        else:
50
63
            encoder.call(method, *args)
51
64
        return response_handler
52
65
 
 
66
    def _run_call_hooks(self, method, args, body, readv_body):
 
67
        if not _SmartClient.hooks['call']:
 
68
            return
 
69
        params = CallHookParams(method, args, body, readv_body, self._medium)
 
70
        for hook in _SmartClient.hooks['call']:
 
71
            hook(params)
 
72
 
53
73
    def _call_and_read_response(self, method, args, body=None, readv_body=None,
54
 
            expect_response_body=True):
 
74
            body_stream=None, expect_response_body=True):
 
75
        self._run_call_hooks(method, args, body, readv_body)
55
76
        if self._medium._protocol_version is not None:
56
77
            response_handler = self._send_request(
57
78
                self._medium._protocol_version, method, args, body=body,
58
 
                readv_body=readv_body)
 
79
                readv_body=readv_body, body_stream=body_stream)
59
80
            return (response_handler.read_response_tuple(
60
81
                        expect_body=expect_response_body),
61
82
                    response_handler)
66
87
                    self._medium._remember_remote_is_before((1, 6))
67
88
                response_handler = self._send_request(
68
89
                    protocol_version, method, args, body=body,
69
 
                    readv_body=readv_body)
 
90
                    readv_body=readv_body, body_stream=body_stream)
70
91
                try:
71
92
                    response_tuple = response_handler.read_response_tuple(
72
93
                        expect_body=expect_response_body)
114
135
 
115
136
    def call_expecting_body(self, method, *args):
116
137
        """Call a method and return the result and the protocol object.
117
 
        
 
138
 
118
139
        The body can be read like so::
119
140
 
120
141
            result, smart_protocol = smart_client.call_expecting_body(...)
154
175
                args[0], args[1:], readv_body=body, expect_response_body=True)
155
176
        return (response, response_handler)
156
177
 
 
178
    def call_with_body_stream(self, args, stream):
 
179
        response, response_handler = self._call_and_read_response(
 
180
                args[0], args[1:], body_stream=stream,
 
181
                expect_response_body=False)
 
182
        return (response, response_handler)
 
183
 
157
184
    def remote_path_from_transport(self, transport):
158
185
        """Convert transport into a path suitable for using in a request.
159
 
        
 
186
 
160
187
        Note that the resulting remote path doesn't encode the host name or
161
188
        anything but path, so it is only safe to use it in requests sent over
162
189
        the medium from the matching transport.
163
190
        """
164
191
        return self._medium.remote_path_from_transport(transport)
165
192
 
 
193
 
 
194
class SmartClientHooks(hooks.Hooks):
 
195
 
 
196
    def __init__(self):
 
197
        hooks.Hooks.__init__(self)
 
198
        self.create_hook(hooks.HookPoint('call',
 
199
            "Called when the smart client is submitting a request to the "
 
200
            "smart server. Called with a bzrlib.smart.client.CallHookParams "
 
201
            "object. Streaming request bodies, and responses, are not "
 
202
            "accessible.", None, None))
 
203
 
 
204
 
 
205
_SmartClient.hooks = SmartClientHooks()
 
206
 
 
207
 
 
208
class CallHookParams(object):
 
209
 
 
210
    def __init__(self, method, args, body, readv_body, medium):
 
211
        self.method = method
 
212
        self.args = args
 
213
        self.body = body
 
214
        self.readv_body = readv_body
 
215
        self.medium = medium
 
216
 
 
217
    def __repr__(self):
 
218
        attrs = dict((k, v) for (k, v) in self.__dict__.iteritems()
 
219
                     if v is not None)
 
220
        return '<%s %r>' % (self.__class__.__name__, attrs)
 
221
 
 
222
    def __eq__(self, other):
 
223
        if type(other) is not type(self):
 
224
            return NotImplemented
 
225
        return self.__dict__ == other.__dict__
 
226
 
 
227
    def __ne__(self, other):
 
228
        return not self == other