Package restkit :: Package contrib :: Module webob_helper
[hide private]

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