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# 22import os 23 24# This allows boto modules to say "from boto.compat import json". This is 25# preferred so that all modules don't have to repeat this idiom. 26try: 27 import simplejson as json 28except ImportError: 29 import json 30 31 32# Switch to use encodebytes, which deprecates encodestring in Python 3 33try: 34 from base64 import encodebytes 35except ImportError: 36 from base64 import encodestring as encodebytes 37 38 39# If running in Google App Engine there is no "user" and 40# os.path.expanduser() will fail. Attempt to detect this case and use a 41# no-op expanduser function in this case. 42try: 43 os.path.expanduser('~') 44 expanduser = os.path.expanduser 45except (AttributeError, ImportError): 46 # This is probably running on App Engine. 47 expanduser = (lambda x: x) 48 49from boto.vendored import six 50 51from boto.vendored.six import BytesIO, StringIO 52from boto.vendored.six.moves import filter, http_client, map, _thread, \ 53 urllib, zip 54from boto.vendored.six.moves.queue import Queue 55from boto.vendored.six.moves.urllib.parse import parse_qs, quote, unquote, \ 56 urlparse, urlsplit 57from boto.vendored.six.moves.urllib.parse import unquote_plus 58from boto.vendored.six.moves.urllib.request import urlopen 59 60if six.PY3: 61 # StandardError was removed, so use the base exception type instead 62 StandardError = Exception 63 long_type = int 64 from configparser import ConfigParser, NoOptionError, NoSectionError 65 unquote_str = unquote_plus 66 parse_qs_safe = parse_qs 67else: 68 StandardError = StandardError 69 long_type = long 70 from ConfigParser import SafeConfigParser as ConfigParser 71 from ConfigParser import NoOptionError, NoSectionError 72 73 def unquote_str(value, encoding='utf-8'): 74 # In python2, unquote() gives us a string back that has the urldecoded 75 # bits, but not the unicode parts. We need to decode this manually. 76 # unquote has special logic in which if it receives a unicode object it 77 # will decode it to latin1. This is hard coded. To avoid this, we'll 78 # encode the string with the passed in encoding before trying to 79 # unquote it. 80 byte_string = value.encode(encoding) 81 return unquote_plus(byte_string).decode(encoding) 82 83 # These are the same default arguments for python3's 84 # urllib.parse.parse_qs. 85 def parse_qs_safe(qs, keep_blank_values=False, strict_parsing=False, 86 encoding='utf-8', errors='replace'): 87 """Parse a query handling unicode arguments properly in Python 2.""" 88 is_text_type = isinstance(qs, six.text_type) 89 if is_text_type: 90 # URL encoding uses ASCII code points only. 91 qs = qs.encode('ascii') 92 qs_dict = parse_qs(qs, keep_blank_values, strict_parsing) 93 if is_text_type: 94 # Decode the parsed dictionary back to unicode. 95 result = {} 96 for (name, value) in qs_dict.items(): 97 decoded_name = name.decode(encoding, errors) 98 decoded_value = [item.decode(encoding, errors) 99 for item in value] 100 result[decoded_name] = decoded_value 101 return result 102 return qs_dict 103