/lib/webob-1.1.1/webob/util.py

https://github.com/theosp/google_appengine · Python · 110 lines · 86 code · 8 blank · 16 comment · 10 complexity · b3b1763198f061736e48f51164cab229 MD5 · raw file

  1. import cgi, warnings
  2. from webob.headers import _trans_key
  3. def html_escape(s):
  4. """HTML-escape a string or object
  5. This converts any non-string objects passed into it to strings
  6. (actually, using ``unicode()``). All values returned are
  7. non-unicode strings (using ``&#num;`` entities for all non-ASCII
  8. characters).
  9. None is treated specially, and returns the empty string.
  10. """
  11. if s is None:
  12. return ''
  13. if hasattr(s, '__html__'):
  14. return s.__html__()
  15. if not isinstance(s, basestring):
  16. if hasattr(s, '__unicode__'):
  17. s = unicode(s)
  18. else:
  19. s = str(s)
  20. s = cgi.escape(s, True)
  21. if isinstance(s, unicode):
  22. s = s.encode('ascii', 'xmlcharrefreplace')
  23. return s
  24. def header_docstring(header, rfc_section):
  25. if header.isupper():
  26. header = _trans_key(header)
  27. major_section = rfc_section.split('.')[0]
  28. link = 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec%s.html#sec%s' % (major_section, rfc_section)
  29. return "Gets and sets the ``%s`` header (`HTTP spec section %s <%s>`_)." \
  30. % (header, rfc_section, link)
  31. def warn_deprecation(text, version, stacklevel):
  32. # version specifies when to start raising exceptions instead of warnings
  33. if version == '1.2':
  34. cls = DeprecationWarning
  35. elif version == '1.3':
  36. cls = PendingDeprecationWarning
  37. else:
  38. cls = DeprecationWarning
  39. warnings.warn("Unknown warn_deprecation version arg: %r" % version,
  40. RuntimeWarning,
  41. stacklevel=1
  42. )
  43. warnings.warn(text, cls, stacklevel=stacklevel+1)
  44. status_reasons = {
  45. # Status Codes
  46. # Informational
  47. 100: 'Continue',
  48. 101: 'Switching Protocols',
  49. 102: 'Processing',
  50. # Successful
  51. 200: 'OK',
  52. 201: 'Created',
  53. 202: 'Accepted',
  54. 203: 'Non-Authoritative Information',
  55. 204: 'No Content',
  56. 205: 'Reset Content',
  57. 206: 'Partial Content',
  58. 207: 'Multi Status',
  59. 226: 'IM Used',
  60. # Redirection
  61. 300: 'Multiple Choices',
  62. 301: 'Moved Permanently',
  63. 302: 'Found',
  64. 303: 'See Other',
  65. 304: 'Not Modified',
  66. 305: 'Use Proxy',
  67. 307: 'Temporary Redirect',
  68. # Client Error
  69. 400: 'Bad Request',
  70. 401: 'Unauthorized',
  71. 402: 'Payment Required',
  72. 403: 'Forbidden',
  73. 404: 'Not Found',
  74. 405: 'Method Not Allowed',
  75. 406: 'Not Acceptable',
  76. 407: 'Proxy Authentication Required',
  77. 408: 'Request Timeout',
  78. 409: 'Conflict',
  79. 410: 'Gone',
  80. 411: 'Length Required',
  81. 412: 'Precondition Failed',
  82. 413: 'Request Entity Too Large',
  83. 414: 'Request URI Too Long',
  84. 415: 'Unsupported Media Type',
  85. 416: 'Requested Range Not Satisfiable',
  86. 417: 'Expectation Failed',
  87. 422: 'Unprocessable Entity',
  88. 423: 'Locked',
  89. 424: 'Failed Dependency',
  90. 426: 'Upgrade Required',
  91. # Server Error
  92. 500: 'Internal Server Error',
  93. 501: 'Not Implemented',
  94. 502: 'Bad Gateway',
  95. 503: 'Service Unavailable',
  96. 504: 'Gateway Timeout',
  97. 505: 'HTTP Version Not Supported',
  98. 507: 'Insufficient Storage',
  99. 510: 'Not Extended',
  100. }