compat.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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.request import urlopen
  51. if six.PY3:
  52. # StandardError was removed, so use the base exception type instead
  53. StandardError = Exception
  54. long_type = int
  55. from configparser import ConfigParser
  56. else:
  57. StandardError = StandardError
  58. long_type = long
  59. from ConfigParser import SafeConfigParser as ConfigParser