compat.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # <HTTPretty - HTTP client mock for Python>
  5. # Copyright (C) <2011-2018> Gabriel Falcao <gabriel@nacaolivre.org>
  6. #
  7. # Permission is hereby granted, free of charge, to any person
  8. # obtaining a copy of this software and associated documentation
  9. # files (the "Software"), to deal in the Software without
  10. # restriction, including without limitation the rights to use,
  11. # copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. # copies of the Software, and to permit persons to whom the
  13. # Software is furnished to do so, subject to the following
  14. # conditions:
  15. #
  16. # The above copyright notice and this permission notice shall be
  17. # included in all copies or substantial portions of the Software.
  18. #
  19. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. # OTHER DEALINGS IN THE SOFTWARE.
  27. from __future__ import unicode_literals
  28. import io
  29. import types
  30. from six import PY3, text_type, string_types, binary_type
  31. if PY3: # pragma: no cover
  32. StringIO = io.BytesIO
  33. basestring = string_types
  34. else: # pragma: no cover
  35. import StringIO
  36. StringIO = StringIO.StringIO
  37. basestring = string_types
  38. class BaseClass(object):
  39. def __repr__(self):
  40. ret = self.__str__()
  41. if PY3: # pragma: no cover
  42. return ret
  43. else:
  44. return ret.encode('utf-8')
  45. try: # pragma: no cover
  46. from urllib.parse import urlsplit
  47. from urllib.parse import urlunsplit
  48. from urllib.parse import parse_qs
  49. from urllib.parse import quote
  50. from urllib.parse import quote_plus
  51. from urllib.parse import unquote
  52. from urllib.parse import urlencode
  53. unquote_utf8 = unquote
  54. def encode_obj(in_obj):
  55. return in_obj
  56. except ImportError: # pragma: no cover
  57. from urlparse import urlsplit, urlunsplit, parse_qs, unquote
  58. from urllib import quote, quote_plus, urlencode
  59. def unquote_utf8(qs):
  60. if isinstance(qs, text_type):
  61. qs = qs.encode('utf-8')
  62. s = unquote(qs)
  63. if isinstance(s, binary_type):
  64. return s.decode('utf-8', errors='ignore')
  65. else:
  66. return s
  67. def encode_obj(in_obj):
  68. def encode_list(in_list):
  69. out_list = []
  70. for el in in_list:
  71. out_list.append(encode_obj(el))
  72. return out_list
  73. def encode_dict(in_dict):
  74. out_dict = {}
  75. for k, v in in_dict.iteritems():
  76. out_dict[k] = encode_obj(v)
  77. return out_dict
  78. if isinstance(in_obj, unicode):
  79. return in_obj.encode('utf-8')
  80. elif isinstance(in_obj, list):
  81. return encode_list(in_obj)
  82. elif isinstance(in_obj, tuple):
  83. return tuple(encode_list(in_obj))
  84. elif isinstance(in_obj, dict):
  85. return encode_dict(in_obj)
  86. return in_obj
  87. try: # pragma: no cover
  88. from http.server import BaseHTTPRequestHandler
  89. except ImportError: # pragma: no cover
  90. from BaseHTTPServer import BaseHTTPRequestHandler
  91. ClassTypes = (type,)
  92. if not PY3: # pragma: no cover
  93. ClassTypes = (type, types.ClassType)
  94. __all__ = [
  95. 'PY3',
  96. 'StringIO',
  97. 'text_type',
  98. 'binary_type',
  99. 'BaseClass',
  100. 'BaseHTTPRequestHandler',
  101. 'quote',
  102. 'quote_plus',
  103. 'urlencode',
  104. 'urlunsplit',
  105. 'urlsplit',
  106. 'parse_qs',
  107. 'ClassTypes',
  108. ]