Advertisement
Guest User

webapi.py diff

a guest
Mar 9th, 2010
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 3.70 KB | None | 0 0
  1. diff --git a/web/webapi.py b/web/webapi.py
  2. index 0981181..1ee1e9d 100644
  3. --- a/web/webapi.py
  4. +++ b/web/webapi.py
  5.  -46,7 +46,20 @@ class HTTPError(Exception):
  6.              header(k, v)
  7.          self.data = data
  8.          Exception.__init__(self, status)
  9. -        
  10. +
  11. +def HTTPErrorSwitch(dft_class):
  12. +    """Returns a function that returns an app-specific handler for the given
  13. +    exception, or an instance of `dft_class` by default
  14. +    """
  15. +    handler_name = dft_class.__name__.lower()
  16. +    def switch(*args, **kwargs):
  17. +        for app in ctx.get('app_stack', [])[::-1]:
  18. +            app_handler = app.get(handler_name)
  19. +            if callable(app_handler):
  20. +                return app_handler(*args, **kwargs)
  21. +        return dft_class()
  22. +    return switch
  23. +
  24.  def _status_code(status, data=None, classname=None, docstring=None):
  25.      if data is None:
  26.          data = status.split(" ", 1)[1]
  27.  -119,41 +132,27 @@ class TempRedirect(Redirect):
  28.  
  29.  tempredirect = TempRedirect
  30.  
  31. -class BadRequest(HTTPError):
  32. -    """`400 Bad Request` error."""
  33. -    message = "bad request"
  34. -    def __init__(self):
  35. -        status = "400 Bad Request"
  36. -        headers = {'Content-Type': 'text/html'}
  37. -        HTTPError.__init__(self, status, headers, self.message)
  38. +BadRequest = _status_code("400 Bad Request")
  39. +badrequest = HTTPErrorSwitch(BadRequest)
  40.  
  41. -badrequest = BadRequest
  42. +NotFound = _status_code("404 Not Found")
  43. +notfound = HTTPErrorSwitch(NotFound)
  44.  
  45. -class _NotFound(HTTPError):
  46. -    """`404 Not Found` error."""
  47. -    message = "not found"
  48. -    def __init__(self, message=None):
  49. -        status = '404 Not Found'
  50. -        headers = {'Content-Type': 'text/html'}
  51. -        HTTPError.__init__(self, status, headers, message or self.message)
  52. +Unauthorized = _status_code("401 Unauthorized")
  53. +unauthorized = HTTPErrorSwitch(Unauthorized)
  54.  
  55. -def NotFound(message=None):
  56. -    """Returns HTTPError with '404 Not Found' error from the active application.
  57. -    """
  58. -    if message:
  59. -        return _NotFound(message)
  60. -    elif ctx.get('app_stack'):
  61. -        return ctx.app_stack[-1].notfound()
  62. -    else:
  63. -        return _NotFound()
  64. +Forbidden = _status_code("403 Forbidden")
  65. +forbidden = HTTPErrorSwitch(Forbidden)
  66. +
  67. +NotAcceptable = _status_code("406 Not Acceptable")
  68. +notacceptable = HTTPErrorSwitch(NotAcceptable)
  69. +
  70. +Conflict = _status_code("409 Conflict")
  71. +conflict = HTTPErrorSwitch(Conflict)
  72.  
  73. -notfound = NotFound
  74. +PreconditionFailed = _status_code("412 Precondition Failed")
  75. +preconditionfailed = HTTPErrorSwitch(PreconditionFailed)
  76.  
  77. -unauthorized = Unauthorized = _status_code("401 Unauthorized")
  78. -forbidden = Forbidden = _status_code("403 Forbidden")
  79. -notacceptable = NotAcceptable = _status_code("406 Not Acceptable")
  80. -conflict = Conflict = _status_code("409 Conflict")
  81. -preconditionfailed = PreconditionFailed = _status_code("412 Precondition Failed")
  82.  
  83.  class NoMethod(HTTPError):
  84.      """A `405 Method Not Allowed` error."""
  85.  -170,7 +169,7 @@ class NoMethod(HTTPError):
  86.          data = None
  87.          HTTPError.__init__(self, status, headers, data)
  88.          
  89. -nomethod = NoMethod
  90. +nomethod = HTTPErrorSwitch(NoMethod)
  91.  
  92.  class Gone(HTTPError):
  93.      """`410 Gone` error."""
  94.  -180,7 +179,7 @@ class Gone(HTTPError):
  95.          headers = {'Content-Type': 'text/html'}
  96.          HTTPError.__init__(self, status, headers, self.message)
  97.  
  98. -gone = Gone
  99. +gone = HTTPErrorSwitch(Gone)
  100.  
  101.  class _InternalError(HTTPError):
  102.      """500 Internal Server Error`."""
  103.  -201,7 +200,7 @@ def InternalError(message=None):
  104.      else:
  105.          return _InternalError()
  106.  
  107. -internalerror = InternalError
  108. +internalerror = HTTPErrorSwitch(InternalError)
  109.  
  110.  def header(hdr, value, unique=False):
  111.      """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement