Package restkit :: Package contrib :: Module webob_helper
[hide private]
[frames] | no frames]

Source Code for Module restkit.contrib.webob_helper

 1  # -*- coding: utf-8 - 
 2  # 
 3  # This file is part of restkit released under the MIT license.  
 4  # See the NOTICE for more information. 
 5   
 6   
 7  import restkit.errors 
 8  import webob.exc 
 9   
10 -class WebobResourceError(webob.exc.WSGIHTTPException):
11 """ 12 Wrapper to return webob exceptions instead of restkit errors. Usefull 13 for those who want to build `WSGI <http://wsgi.org/wsgi/>`_ applications 14 speaking directly to others via HTTP. 15 16 To do it place somewhere in your application the function 17 `wrap_exceptions`:: 18 19 wrap_exceptions() 20 21 It will automatically replace restkit errors by webob exceptions. 22 """ 23
24 - def __init__(self, msg=None, http_code=None, response=None):
25 webob.exc.WSGIHTTPException.__init__(self) 26 27 http_code = http_code or 500 28 klass = webob.exc.status_map[http_code] 29 self.code = http_code 30 self.title = klass.title 31 self.status = '%s %s' % (self.code, self.title) 32 self.explanation = msg 33 self.response = response 34 # default params 35 self.msg = msg
36
37 - def _status_int__get(self):
38 """ 39 The status as an integer 40 """ 41 return int(self.status.split()[0])
42 - def _status_int__set(self, value):
43 self.status = value
44 status_int = property(_status_int__get, _status_int__set, 45 doc=_status_int__get.__doc__) 46
47 - def _get_message(self):
48 return self.explanation
49 - def _set_message(self, msg):
50 self.explanation = msg or ''
51 message = property(_get_message, _set_message)
52 53 webob_exceptions = False
54 -def wrap_exceptions():
55 """ wrap restkit exception to return WebBob exceptions""" 56 global webob_exceptions 57 if webob_exceptions: return 58 restkit.errors.ResourceError = WebobResourceError 59 webob_exceptions = True
60