ž 7’ReNc@sÿdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddl m Z ddlmZddlmZmZmZmZmZmZmZmZmZmZmZddlmZmZdd lm Z dd l!m"Z"efZ#dRZ$e j%ƒZ&d d„Z'dd„Z(dd„Z)dd„Z*dd„Z+dd„Z,dd„Z-dd„Z.dSdd„Z0dd „Z1d!d"„Z2d#d$„Z3d%d&„Z4d'd(„Z5d)d*„Z6d+d,„Z7e8d-d.ƒZ9d/d0„Z:d1d2„Z;d3d4„Z<d5d6„Z=d7d8„Z>d9d:„Z?d;d<„Z@d=d>„ZAd?d@dA„ZBdBdC„ZCdDdE„ZDdFjEdGƒZFeFdHZGeFdIZHdJdK„ZIdLdM„ZJdNdO„ZKdGdPdQ„ZLdS(Tu• requests.utils ~~~~~~~~~~~~~~ This module provides utility functions that are used within Requests that are also useful for external consumption. iNi(u __version__(ucerts(uparse_http_list( uquoteuurlparseubytesustru OrderedDictuunquoteuis_py2u builtin_stru getproxiesu proxy_bypassu urlunparse(uRequestsCookieJarucookiejar_from_dict(uCaseInsensitiveDict(u InvalidURLu.netrcu_netrccCs"t|dƒr|jƒ}n|S(u/Returns an internal sequence dictionary update.uitems(uhasattruitems(ud((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyudict_to_sequence'sudict_to_sequencec Csœt|dƒrt|ƒSt|dƒr/|jSt|dƒryy|jƒ}Wntjk reYqyXtj|ƒjSnt|dƒr˜t|jƒƒSdS(Nu__len__ulenufilenougetvalue( uhasattrulenufilenouiouUnsupportedOperationuosufstatust_sizeugetvalue(uoufileno((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyu super_len0s u super_lenc Cs8yddlm}m}d}xbtD]Z}ytjjdj|ƒƒ}Wntk rcdSYnXtjj |ƒr&|}Pq&q&W|dkr”dSt |ƒ}|j j dƒd}yG||ƒj |ƒ}|rü|drädnd} || |dfSWn|tfk rYnXWnttfk r3YnXdS(u;Returns the Requests tuple auth for a given url from netrc.i(unetrcuNetrcParseErroru~/{0}Nu:ii(unetrcuNetrcParseErroruNoneu NETRC_FILESuosupathu expanduseruformatuKeyErroruexistsuurlparseunetlocusplituauthenticatorsuIOErroru ImportErroruAttributeError( uurlunetrcuNetrcParseErroru netrc_pathufulocuriuhostu_netrculogin_i((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyuget_netrc_authDs0      uget_netrc_authcCsLt|ddƒ}|rH|ddkrH|ddkrHtjj|ƒSdS(u0Tries to guess the filename of the given object.unameiuNiÿÿÿÿ(ugetattruNoneuosupathubasename(uobjuname((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyuguess_filenamers&uguess_filenamecCsD|dkrdSt|ttttfƒr:tdƒ‚nt|ƒS(u¬Take an object and test to see if it can be represented as a dictionary. Unless it can not be represented as such, return an OrderedDict, e.g., :: >>> from_key_val_list([('key', 'val')]) OrderedDict([('key', 'val')]) >>> from_key_val_list('string') ValueError: need more than 1 value to unpack >>> from_key_val_list({'key': 'val'}) OrderedDict([('key', 'val')]) u+cannot encode objects that are not 2-tuplesN(uNoneu isinstanceustrubytesubooluintu ValueErroru OrderedDict(uvalue((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyufrom_key_val_listys  ufrom_key_val_listcCse|dkrdSt|ttttfƒr:tdƒ‚nt|tjƒr[|j ƒ}nt |ƒS(uzTake an object and test to see if it can be represented as a dictionary. If it can be, return a list of tuples, e.g., :: >>> to_key_val_list([('key', 'val')]) [('key', 'val')] >>> to_key_val_list({'key': 'val'}) [('key', 'val')] >>> to_key_val_list('string') ValueError: cannot encode objects that are not 2-tuples. u+cannot encode objects that are not 2-tuplesN( uNoneu isinstanceustrubytesubooluintu ValueErroru collectionsuMappinguitemsulist(uvalue((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyuto_key_val_lists uto_key_val_listcCswg}xjt|ƒD]\}|dd…|dd…koDdknrbt|dd…ƒ}n|j|ƒqW|S(uñParse lists as described by RFC 2068 Section 2. In particular, parse comma-separated lists where the elements of the list may include quoted-strings. A quoted-string could contain a comma. A non-quoted string could have quotes in the middle. Quotes are removed automatically after parsing. It basically works like :func:`parse_set_header` just that items may appear multiple times and case sensitivity is preserved. The return value is a standard :class:`list`: >>> parse_list_header('token, "quoted value"') ['token', 'quoted value'] To create a header from the :class:`list` again, use the :func:`dump_header` function. :param value: a string with a list header. :return: :class:`list` Niu"iÿÿÿÿiÿÿÿÿ(u_parse_list_headeruunquote_header_valueuappend(uvalueuresultuitem((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyuparse_list_headerªs 0uparse_list_headercCs¨i}x›t|ƒD]}d|kr5d||>> d = parse_dict_header('foo="is a fish", bar="as well"') >>> type(d) is dict True >>> sorted(d.items()) [('bar', 'as well'), ('foo', 'is a fish')] If there is no value for a key it will be `None`: >>> parse_dict_header('key_without_value') {'key_without_value': None} To create a header from the :class:`dict` again, use the :func:`dump_header` function. :param value: a string with a dict header. :return: :class:`dict` u=iNu"iÿÿÿÿiÿÿÿÿ(u_parse_list_headeruNoneusplituunquote_header_value(uvalueuresultuitemuname((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyuparse_dict_headerÉs  0uparse_dict_headercCsz|rv|d|d ko%dknrv|dd …}| sW|dd…dkrv|jddƒjddƒSn|S( u×Unquotes a header value. (Reversal of :func:`quote_header_value`). This does not use the real unquoting but what browsers are actually using for quoting. :param value: the header value to unquote. iiu"Niu\\u\u\"iÿÿÿÿiÿÿÿÿ(ureplace(uvalueu is_filename((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyuunquote_header_valueës *uunquote_header_valuecCs+i}x|D]}|j||j]uflagsu+]u$^<\?xml.*?encoding=["\']*(.+?)["\'>](ureucompileuIufindall(ucontentu charset_reu pragma_reuxml_re((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyuget_encodings_from_contents uget_encodings_from_contentcCs_|jdƒ}|sdStj|ƒ\}}d|krK|djdƒSd|kr[dSdS(umReturns encodings from given HTTP Header Dict. :param headers: dictionary to extract encoding from. u content-typeucharsetu'"utextu ISO-8859-1N(ugetuNoneucgiu parse_headerustrip(uheadersu content_typeuparams((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyuget_encoding_from_headers,s  uget_encoding_from_headersccs™|jdkr)x|D] }|VqWdStj|jƒddƒ}x+|D]#}|j|ƒ}|rK|VqKqKW|jdddƒ}|r•|VndS(uStream decodes a iterator.NuerrorsureplacesufinalT(uencodinguNoneucodecsugetincrementaldecoderudecodeuTrue(uiteratoruruitemudecoderuchunkurv((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyustream_decode_response_unicode@s    ustream_decode_response_unicodeccs@d}x3|t|ƒkr;||||…V||7}q WdS(u Iterate over slices of a string.iN(ulen(ustringu slice_lengthupos((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyu iter_slicesRsu iter_slicescCsŽg}t|jƒ}|rTyt|j|ƒSWqTtk rP|j|ƒYqTXnyt|j|ddƒSWntk r‰|jSYnXdS(u Returns the requested content back in unicode. :param r: Response object to get unicode content from. Tried: 1. charset from content-type 2. every encodings from ```` 3. fall back and replace all unicode characters uerrorsureplaceN(uget_encoding_from_headersuheadersustrucontentu UnicodeErroruappendu TypeError(urutried_encodingsuencoding((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyuget_unicode_from_responseZs  uget_unicode_from_responseu4ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzu0123456789-._~cCsþ|jdƒ}xßtdt|ƒƒD]È}||dd…}t|ƒdkrÛ|jƒrÛytt|dƒƒ}Wn"tk rštd|ƒ‚YnX|tkrÆ|||dd…||dA}tjtjd|ƒƒS(ut Converts mask from /xx format to xxx.xxx.xxx.xxx Example: if mask is 24 function returns 255.255.255.0 lÿÿii u>I(usocketu inet_ntoaustructupack(umaskubits((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyudotted_netmask°sudotted_netmaskc Cs1ytj|ƒWntjk r,dSYnXdS(NFT(usocketu inet_atonuerroruFalseuTrue(u string_ip((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyuis_ipv4_address¹s  uis_ipv4_addresscCs¬|jdƒdkr¤yt|jdƒdƒ}Wntk rJdSYnX|dksc|dkrgdSytj|jdƒdƒWq¨tjk r dSYq¨XndSdS(u9Very simple check of the cidr format in no_proxy variableu/ii iFT( ucountuintusplitu ValueErroruFalseusocketu inet_atonuerroruTrue(ustring_networkumask((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyu is_valid_cidrÁs   u is_valid_cidrc Cs"dd„}|dƒ}t|ƒj}|rà|jddƒjdƒ}|jdƒd}t|ƒrxs|D](}t|ƒrnt||ƒr–d SqnqnWqàx@|D]5}|j|ƒsÕ|jdƒdj|ƒr¤d Sq¤Wnyt |ƒ}Wn!t t j fk rd }YnX|rd Sd S( u: Returns whether we should bypass proxies or not. cSs(tjj|ƒp'tjj|jƒƒS(N(uosuenvironugetuupper(uk((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyuÙsu'should_bypass_proxies..uno_proxyu uu,u:iTF(uurlparseunetlocureplaceusplituis_ipv4_addressu is_valid_cidruaddress_in_networkuTrueuendswithu proxy_bypassu TypeErrorusocketugaierroruFalse(uurlu get_proxyuno_proxyunetlocuipuproxy_ipuhostubypass((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyushould_bypass_proxiesÕs*      +  ushould_bypass_proxiescCst|ƒriStƒSdS(u%Return a dict of environment proxies.N(ushould_bypass_proxiesu getproxies(uurl((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyuget_environ_proxiess uget_environ_proxiesupython-requestscCs8tjƒ}|dkr'tjƒ}n |dkr‹dtjjtjjtjjf}tjjdkrÇdj |tjjgƒ}qÇn<|dkr¦tjƒ}n!|dkrÁtjƒ}nd}ytj ƒ}tj ƒ}Wnt k rd}d}YnXd j d |t fd ||fd ||fgƒS( u4Return a string representing the default user agent.uCPythonuPyPyu%s.%s.%sufinaluuJythonu IronPythonuUnknownu u%s/%s(uplatformupython_implementationupython_versionusysupypy_version_infoumajoruminorumicrou releaselevelujoinusystemureleaseuIOErroru __version__(unameu_implementationu_implementation_versionup_systemu p_release((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyudefault_user_agent s.     !      udefault_user_agentcCs+titƒd6djdƒd6dd6ƒS( Nu User-Agentu, ugzipudeflateuAccept-Encodingu*/*uAccept(ugzipudeflate(uCaseInsensitiveDictudefault_user_agentujoin(((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyudefault_headers)s udefault_headersc Csóg}d}xà|jdƒD]Ï}y|jddƒ\}}Wntk r_|d}}YnXi}|jdƒ|d; rel=front; type="image/jpeg",; rel=back;type="image/jpeg" u '"u,u;iuu<> '"uurlu=(usplitu ValueErrorustripuappend( uvalueulinksu replace_charsuvaluurluparamsulinkuparamukey((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyuparse_header_links1s"   uparse_header_linksuuasciiiicCs|dd…}|tjtjfkr,dS|dd…tjkrIdS|dd…tjtjfkrodS|jtƒ}|dkrŽdS|dkr×|ddd…tkr·d S|d dd…tkr×d Sn|dkr|dd…t krýd S|d d…t krd SndS(Niuutf-32iu utf-8-sigiuutf-16iuutf-8u utf-16-beiu utf-16-leu utf-32-beu utf-32-le( ucodecsu BOM_UTF32_LEuBOM32_BEuBOM_UTF8u BOM_UTF16_LEu BOM_UTF16_BEucountu_nullu_null2u_null3uNone(udatausampleu nullcount((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyuguess_json_utfYs*"   uguess_json_utfcCsSt||ƒ\}}}}}}|s7||}}nt||||||fƒS(u”Given a URL that may or may not have a scheme, prepend the given scheme. Does not replace a present scheme with the one provided as an argument.(uurlparseu urlunparse(uurlu new_schemeuschemeunetlocupathuparamsuqueryufragment((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyuprepend_scheme_if_neededvs!uprepend_scheme_if_neededc CsSt|ƒ}y"t|jƒt|jƒf}Wnttfk rNd}YnX|S(u_Given a url with authentication components, extract them into a tuple of username,password.u(uu(uurlparseuunquoteuusernameupassworduAttributeErroru TypeError(uurluparseduauth((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyuget_auth_from_url„s  " uget_auth_from_urlcCsId}t|tƒr|}n'tr6|j|ƒ}n|j|ƒ}|S(uÐ Given a string object, regardless of type, returns a representation of that string in the native string type, encoding and decoding where necessary. This assumes ASCII unless told otherwise. N(uNoneu isinstanceu builtin_struis_py2uencodeudecode(ustringuencodinguout((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyuto_native_string‘s uto_native_string(u.netrcu_netrcF(Mu__doc__ucgiucodecsu collectionsuiouosuplatformureusysusocketustructuu __version__ucertsucompatuparse_http_listu_parse_list_headeruquoteuurlparseubytesustru OrderedDictuunquoteuis_py2u builtin_stru getproxiesu proxy_bypassu urlunparseucookiesuRequestsCookieJarucookiejar_from_dictu structuresuCaseInsensitiveDictu exceptionsu InvalidURLu_hush_pyflakesu NETRC_FILESuwhereuDEFAULT_CA_BUNDLE_PATHudict_to_sequenceu super_lenuget_netrc_authuguess_filenameufrom_key_val_listuto_key_val_listuparse_list_headeruparse_dict_headeruFalseuunquote_header_valueudict_from_cookiejaruadd_dict_to_cookiejaruget_encodings_from_contentuget_encoding_from_headersustream_decode_response_unicodeu iter_slicesuget_unicode_from_responseu frozensetuUNRESERVED_SETuunquote_unreservedu requote_uriuaddress_in_networkudotted_netmaskuis_ipv4_addressu is_valid_cidrushould_bypass_proxiesuget_environ_proxiesudefault_user_agentudefault_headersuparse_header_linksuencodeu_nullu_null2u_null3uguess_json_utfuprepend_scheme_if_neededuget_auth_from_urluto_native_string(((u3/tmp/pip-zej_zi-build/pip/_vendor/requests/utils.pyu sr          L    .     "      "     -   #