spacepaste

  1.  
  2. # -*- coding: utf-8 -*-
  3. class LookupDict(dict):
  4. """Dictionary lookup object."""
  5. def __init__(self, name=None):
  6. self.name = name
  7. super(LookupDict, self).__init__()
  8. def __repr__(self):
  9. return '<lookup \'%s\'>' % (self.name)
  10. def __getitem__(self, key):
  11. # We allow fall-through here, so values default to None
  12. return self.__dict__.get(key, None)
  13. def get(self, key, default=None):
  14. return self.__dict__.get(key, default)
  15. _codes = {
  16. # Informational.
  17. 100: ('continue',),
  18. 101: ('switching_protocols',),
  19. 102: ('processing',),
  20. 103: ('checkpoint',),
  21. 122: ('uri_too_long', 'request_uri_too_long'),
  22. 200: ('ok', 'okay', 'all_ok', 'all_okay', 'all_good', '\\o/', '✓'),
  23. 201: ('created',),
  24. 202: ('accepted',),
  25. 203: ('non_authoritative_info', 'non_authoritative_information'),
  26. 204: ('no_content',),
  27. 205: ('reset_content', 'reset'),
  28. 206: ('partial_content', 'partial'),
  29. 207: ('multi_status', 'multiple_status', 'multi_stati', 'multiple_stati'),
  30. 208: ('already_reported',),
  31. 226: ('im_used',),
  32. # Redirection.
  33. 300: ('multiple_choices',),
  34. 301: ('moved_permanently', 'moved', '\\o-'),
  35. 302: ('found',),
  36. 303: ('see_other', 'other'),
  37. 304: ('not_modified',),
  38. 305: ('use_proxy',),
  39. 306: ('switch_proxy',),
  40. 307: ('temporary_redirect', 'temporary_moved', 'temporary'),
  41. 308: ('permanent_redirect',
  42. 'resume_incomplete', 'resume',), # These 2 to be removed in 3.0
  43. # Client Error.
  44. 400: ('bad_request', 'bad'),
  45. 401: ('unauthorized',),
  46. 402: ('payment_required', 'payment'),
  47. 403: ('forbidden',),
  48. 404: ('not_found', '-o-'),
  49. 405: ('method_not_allowed', 'not_allowed'),
  50. 406: ('not_acceptable',),
  51. 407: ('proxy_authentication_required', 'proxy_auth', 'proxy_authentication'),
  52. 408: ('request_timeout', 'timeout'),
  53. 409: ('conflict',),
  54. 410: ('gone',),
  55. 411: ('length_required',),
  56. 412: ('precondition_failed', 'precondition'),
  57. 413: ('request_entity_too_large',),
  58. 414: ('request_uri_too_large',),
  59. 415: ('unsupported_media_type', 'unsupported_media', 'media_type'),
  60. 416: ('requested_range_not_satisfiable', 'requested_range', 'range_not_satisfiable'),
  61. 417: ('expectation_failed',),
  62. 418: ('im_a_teapot', 'teapot', 'i_am_a_teapot'),
  63. 421: ('misdirected_request',),
  64. 422: ('unprocessable_entity', 'unprocessable'),
  65. 423: ('locked',),
  66. 424: ('failed_dependency', 'dependency'),
  67. 425: ('unordered_collection', 'unordered'),
  68. 426: ('upgrade_required', 'upgrade'),
  69. 428: ('precondition_required', 'precondition'),
  70. 429: ('too_many_requests', 'too_many'),
  71. 431: ('header_fields_too_large', 'fields_too_large'),
  72. 444: ('no_response', 'none'),
  73. 449: ('retry_with', 'retry'),
  74. 450: ('blocked_by_windows_parental_controls', 'parental_controls'),
  75. 451: ('unavailable_for_legal_reasons', 'legal_reasons'),
  76. 499: ('client_closed_request',),
  77. # Server Error.
  78. 500: ('internal_server_error', 'server_error', '/o\\', '✗'),
  79. 501: ('not_implemented',),
  80. 502: ('bad_gateway',),
  81. 503: ('service_unavailable', 'unavailable'),
  82. 504: ('gateway_timeout',),
  83. 505: ('http_version_not_supported', 'http_version'),
  84. 506: ('variant_also_negotiates',),
  85. 507: ('insufficient_storage',),
  86. 509: ('bandwidth_limit_exceeded', 'bandwidth'),
  87. 510: ('not_extended',),
  88. 511: ('network_authentication_required', 'network_auth', 'network_authentication'),
  89. }
  90. codes = LookupDict(name='status_codes')
  91. for code, titles in _codes.items():
  92. for title in titles:
  93. setattr(codes, title, code)
  94. if not title.startswith('\\'):
  95. setattr(codes, title.upper(), code)
  96. print('codes.moved', codes.moved)
  97.