You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
4.3 KiB

4 years ago
  1. # Copyright (c) 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a
  4. # copy of this software and associated documentation files (the
  5. # "Software"), to deal in the Software without restriction, including
  6. # without limitation the rights to use, copy, modify, merge, publish, dis-
  7. # tribute, sublicense, and/or sell copies of the Software, and to permit
  8. # persons to whom the Software is furnished to do so, subject to the fol-
  9. # lowing conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included
  12. # in all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
  16. # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  17. # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. # IN THE SOFTWARE.
  21. #
  22. import os
  23. # This allows boto modules to say "from boto.compat import json". This is
  24. # preferred so that all modules don't have to repeat this idiom.
  25. try:
  26. import simplejson as json
  27. except ImportError:
  28. import json
  29. # Switch to use encodebytes, which deprecates encodestring in Python 3
  30. try:
  31. from base64 import encodebytes
  32. except ImportError:
  33. from base64 import encodestring as encodebytes
  34. # If running in Google App Engine there is no "user" and
  35. # os.path.expanduser() will fail. Attempt to detect this case and use a
  36. # no-op expanduser function in this case.
  37. try:
  38. os.path.expanduser('~')
  39. expanduser = os.path.expanduser
  40. except (AttributeError, ImportError):
  41. # This is probably running on App Engine.
  42. expanduser = (lambda x: x)
  43. from boto.vendored import six
  44. from boto.vendored.six import BytesIO, StringIO
  45. from boto.vendored.six.moves import filter, http_client, map, _thread, \
  46. urllib, zip
  47. from boto.vendored.six.moves.queue import Queue
  48. from boto.vendored.six.moves.urllib.parse import parse_qs, quote, unquote, \
  49. urlparse, urlsplit
  50. from boto.vendored.six.moves.urllib.parse import unquote_plus
  51. from boto.vendored.six.moves.urllib.request import urlopen
  52. if six.PY3:
  53. # StandardError was removed, so use the base exception type instead
  54. StandardError = Exception
  55. long_type = int
  56. from configparser import ConfigParser, NoOptionError, NoSectionError
  57. unquote_str = unquote_plus
  58. parse_qs_safe = parse_qs
  59. else:
  60. StandardError = StandardError
  61. long_type = long
  62. from ConfigParser import SafeConfigParser as ConfigParser
  63. from ConfigParser import NoOptionError, NoSectionError
  64. def unquote_str(value, encoding='utf-8'):
  65. # In python2, unquote() gives us a string back that has the urldecoded
  66. # bits, but not the unicode parts. We need to decode this manually.
  67. # unquote has special logic in which if it receives a unicode object it
  68. # will decode it to latin1. This is hard coded. To avoid this, we'll
  69. # encode the string with the passed in encoding before trying to
  70. # unquote it.
  71. byte_string = value.encode(encoding)
  72. return unquote_plus(byte_string).decode(encoding)
  73. # These are the same default arguments for python3's
  74. # urllib.parse.parse_qs.
  75. def parse_qs_safe(qs, keep_blank_values=False, strict_parsing=False,
  76. encoding='utf-8', errors='replace'):
  77. """Parse a query handling unicode arguments properly in Python 2."""
  78. is_text_type = isinstance(qs, six.text_type)
  79. if is_text_type:
  80. # URL encoding uses ASCII code points only.
  81. qs = qs.encode('ascii')
  82. qs_dict = parse_qs(qs, keep_blank_values, strict_parsing)
  83. if is_text_type:
  84. # Decode the parsed dictionary back to unicode.
  85. result = {}
  86. for (name, value) in qs_dict.items():
  87. decoded_name = name.decode(encoding, errors)
  88. decoded_value = [item.decode(encoding, errors)
  89. for item in value]
  90. result[decoded_name] = decoded_value
  91. return result
  92. return qs_dict