~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/request.py

  • Committer: Robert Collins
  • Date: 2007-04-24 12:20:09 UTC
  • mto: (2432.3.5 hpss-vfs-fallback)
  • mto: This revision was merged to the branch mainline in revision 2463.
  • Revision ID: robertc@lifelesswks.robertcollins.net-20070424122009-8bb4dede6a298d93
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.

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)
93
98
 
94
99
 
95
100
class FailedSmartServerResponse(SmartServerResponse):
187
192
        try:
188
193
            return callable(*args, **kwargs)
189
194
        except errors.NoSuchFile, e:
190
 
            return SmartServerResponse(('NoSuchFile', e.path))
 
195
            return FailedSmartServerResponse(('NoSuchFile', e.path))
191
196
        except errors.FileExists, e:
192
 
            return SmartServerResponse(('FileExists', e.path))
 
197
            return FailedSmartServerResponse(('FileExists', e.path))
193
198
        except errors.DirectoryNotEmpty, e:
194
 
            return SmartServerResponse(('DirectoryNotEmpty', e.path))
 
199
            return FailedSmartServerResponse(('DirectoryNotEmpty', e.path))
195
200
        except errors.ShortReadvError, e:
196
 
            return SmartServerResponse(('ShortReadvError',
 
201
            return FailedSmartServerResponse(('ShortReadvError',
197
202
                e.path, str(e.offset), str(e.length), str(e.actual)))
198
203
        except UnicodeError, e:
199
204
            # If it is a DecodeError, than most likely we are starting
206
211
            else:
207
212
                val = 's:' + str_or_unicode.encode('base64')
208
213
            # This handles UnicodeEncodeError or UnicodeDecodeError
209
 
            return SmartServerResponse((e.__class__.__name__,
 
214
            return FailedSmartServerResponse((e.__class__.__name__,
210
215
                    e.encoding, val, str(e.start), str(e.end), e.reason))
211
216
        except errors.TransportNotPossible, e:
212
217
            if e.msg == "readonly transport":
213
 
                return SmartServerResponse(('ReadOnlyError', ))
 
218
                return FailedSmartServerResponse(('ReadOnlyError', ))
214
219
            else:
215
220
                raise
216
221
 
221
226
    """
222
227
 
223
228
    def do(self):
224
 
        return SmartServerResponse(('ok', '2'))
 
229
        return SuccessfulSmartServerResponse(('ok', '2'))
225
230
 
226
231
 
227
232
class GetBundleRequest(SmartServerRequest):
236
241
        base_revision = revision.NULL_REVISION
237
242
        write_bundle(repo, revision_id, base_revision, tmpf)
238
243
        tmpf.seek(0)
239
 
        return SmartServerResponse((), tmpf.read())
 
244
        return SuccessfulSmartServerResponse((), tmpf.read())
240
245
 
241
246
 
242
247
class SmartServerIsReadonly(SmartServerRequest):
247
252
            answer = 'yes'
248
253
        else:
249
254
            answer = 'no'
250
 
        return SmartServerResponse((answer,))
 
255
        return SuccessfulSmartServerResponse((answer,))
251
256
 
252
257
 
253
258
request_handlers = registry.Registry()