~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/request.py

  • Committer: Vincent Ladeuil
  • Date: 2007-04-26 09:01:46 UTC
  • mfrom: (2420.2.2 bzr.http.auth)
  • mto: This revision was merged to the branch mainline in revision 2463.
  • Revision ID: v.ladeuil+lp@free.fr-20070426090146-jbwl0muvk76wlw6c
Merge spiv modifications

Show diffs side-by-side

added added

removed removed

Lines of Context:
77
77
 
78
78
 
79
79
class SmartServerResponse(object):
80
 
    """Response generated by SmartServerRequestHandler."""
 
80
    """A response to a client request.
 
81
    
 
82
    This base class should not be used. Instead use
 
83
    SuccessfulSmartServerResponse and FailedSmartServerResponse as appropriate.
 
84
    """
81
85
 
82
86
    def __init__(self, args, body=None):
83
87
        self.args = args
89
93
        return other.args == self.args and other.body == self.body
90
94
 
91
95
    def __repr__(self):
92
 
        return "<SmartServerResponse args=%r body=%r>" % (self.args, self.body)
 
96
        return "<SmartServerResponse args=%r body=%r>" % (self.is_successful(), 
 
97
            self.args, self.body)
 
98
 
 
99
 
 
100
class FailedSmartServerResponse(SmartServerResponse):
 
101
    """A SmartServerResponse for a request which failed."""
 
102
 
 
103
    def is_successful(self):
 
104
        """FailedSmartServerResponse are not successful."""
 
105
        return False
 
106
 
 
107
 
 
108
class SuccessfulSmartServerResponse(SmartServerResponse):
 
109
    """A SmartServerResponse for a successfully completed request."""
 
110
 
 
111
    def is_successful(self):
 
112
        """SuccessfulSmartServerResponse are successful."""
 
113
        return True
93
114
 
94
115
 
95
116
class SmartServerRequestHandler(object):
171
192
        try:
172
193
            return callable(*args, **kwargs)
173
194
        except errors.NoSuchFile, e:
174
 
            return SmartServerResponse(('NoSuchFile', e.path))
 
195
            return FailedSmartServerResponse(('NoSuchFile', e.path))
175
196
        except errors.FileExists, e:
176
 
            return SmartServerResponse(('FileExists', e.path))
 
197
            return FailedSmartServerResponse(('FileExists', e.path))
177
198
        except errors.DirectoryNotEmpty, e:
178
 
            return SmartServerResponse(('DirectoryNotEmpty', e.path))
 
199
            return FailedSmartServerResponse(('DirectoryNotEmpty', e.path))
179
200
        except errors.ShortReadvError, e:
180
 
            return SmartServerResponse(('ShortReadvError',
 
201
            return FailedSmartServerResponse(('ShortReadvError',
181
202
                e.path, str(e.offset), str(e.length), str(e.actual)))
182
203
        except UnicodeError, e:
183
204
            # If it is a DecodeError, than most likely we are starting
190
211
            else:
191
212
                val = 's:' + str_or_unicode.encode('base64')
192
213
            # This handles UnicodeEncodeError or UnicodeDecodeError
193
 
            return SmartServerResponse((e.__class__.__name__,
 
214
            return FailedSmartServerResponse((e.__class__.__name__,
194
215
                    e.encoding, val, str(e.start), str(e.end), e.reason))
195
216
        except errors.TransportNotPossible, e:
196
217
            if e.msg == "readonly transport":
197
 
                return SmartServerResponse(('ReadOnlyError', ))
 
218
                return FailedSmartServerResponse(('ReadOnlyError', ))
198
219
            else:
199
220
                raise
200
221
 
201
222
 
202
223
class HelloRequest(SmartServerRequest):
203
 
    """Answer a version request with my version."""
 
224
    """Answer a version request with the highest protocol version this server
 
225
    supports.
 
226
    """
204
227
 
205
228
    def do(self):
206
 
        return SmartServerResponse(('ok', '1'))
 
229
        return SuccessfulSmartServerResponse(('ok', '2'))
207
230
 
208
231
 
209
232
class GetBundleRequest(SmartServerRequest):
218
241
        base_revision = revision.NULL_REVISION
219
242
        write_bundle(repo, revision_id, base_revision, tmpf)
220
243
        tmpf.seek(0)
221
 
        return SmartServerResponse((), tmpf.read())
 
244
        return SuccessfulSmartServerResponse((), tmpf.read())
222
245
 
223
246
 
224
247
class SmartServerIsReadonly(SmartServerRequest):
229
252
            answer = 'yes'
230
253
        else:
231
254
            answer = 'no'
232
 
        return SmartServerResponse((answer,))
 
255
        return SuccessfulSmartServerResponse((answer,))
233
256
 
234
257
 
235
258
request_handlers = registry.Registry()
293
316
request_handlers.register_lazy(
294
317
    'Repository.unlock', 'bzrlib.smart.repository', 'SmartServerRepositoryUnlock')
295
318
request_handlers.register_lazy(
 
319
    'Repository.tarball', 'bzrlib.smart.repository',
 
320
    'SmartServerRepositoryTarball')
 
321
request_handlers.register_lazy(
296
322
    'rmdir', 'bzrlib.smart.vfs', 'RmdirRequest')
297
323
request_handlers.register_lazy(
298
324
    'stat', 'bzrlib.smart.vfs', 'StatRequest')