`8(Zc@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z ddl mZddl mZmZmZmZmZmZmZmZmZmZmZmZddlmZmZdd lm Z dd l!m"Z"m#Z#m$Z$efZ%d7Z&e j'Z(d Z)dZ*e+dZ,dZ-dZ.dZ/dZ0dZ1e+dZ2dZ3dZ4dZ5dZ6dZ7dZ8dZ9e:ddZ;dZ<d Z=d!Z>d"Z?d#Z@d$ZAd%ZBd&ZCd'ZDd(d)ZEd*ZFd+ZGd,jHd-ZIeId.ZJeId/ZKd0ZLd1ZMd2ZNd-d3ZOejPd4ZQejPd4ZRd5ZSd6ZTdS(8s requests.utils ~~~~~~~~~~~~~~ This module provides utility functions that are used within Requests that are also useful for external consumption. iNi(t __version__(tcerts(tparse_http_list( tquoteturlparsetbyteststrt OrderedDicttunquotetis_py2t builtin_strt getproxiest proxy_bypasst urlunparset basestring(tRequestsCookieJartcookiejar_from_dict(tCaseInsensitiveDict(t InvalidURLt InvalidHeadertFileModeWarnings.netrct_netrccCs"t|dr|j}n|S(s/Returns an internal sequence dictionary update.titems(thasattrR(td((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pytdict_to_sequence&scCs)d}d}t|dr*t|}nt|drE|j}nt|drit|j}nmt|dry|j}Wntjk rqXtj|j}d|j krt j dt qnt|dry|j }Wqttfk r|}qXntd||S( Nit__len__tlentgetvaluetfilenotbs%Requests has determined the content-length for this request using the binary size of the file: however, the file has been opened in text mode (i.e. without the 'b' flag in the mode). This may lead to an incorrect content-length. In Requests 3.0, support will be removed for files in text mode.ttell(RRRRtiotUnsupportedOperationtostfstattst_sizetmodetwarningstwarnRRtOSErrortIOErrortmax(tot total_lengthtcurrent_positionR((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pyt super_len/s0   c CseyGddlm}m}d}x^tD]V}ytjjdj|}Wntk r_dSXtjj |r&|}Pq&q&W|dkrdSt |}d}t |t r|j d}n|jj|d} yG||j| } | r| drdnd} | | | d fSWn#|tfk rE|rFqFnXWnttfk r`nXdS( s;Returns the Requests tuple auth for a given url from netrc.i(tnetrctNetrcParseErrors~/{0}Nt:tasciiiii(R/R0tNonet NETRC_FILESR"tpatht expandusertformattKeyErrortexistsRt isinstanceRtdecodetnetloctsplittauthenticatorsR)t ImportErrortAttributeError( turlt raise_errorsR/R0t netrc_pathtftloctritsplitstrthostRtlogin_i((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pytget_netrc_auth_s8    cCs[t|dd}|rWt|trW|ddkrW|ddkrWtjj|SdS(s0Tries to guess the filename of the given object.tnameitN(tgetattrR3R:RR"R5tbasename(tobjRK((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pytguess_filenames%cCsD|dkrdSt|ttttfr:tdnt|S(sTake 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')]) :rtype: OrderedDict s+cannot encode objects that are not 2-tuplesN(R3R:RRtbooltintt ValueErrorR(tvalue((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pytfrom_key_val_lists  cCse|dkrdSt|ttttfr:tdnt|tjr[|j }nt |S(sTake 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. :rtype: list s+cannot encode objects that are not 2-tuplesN( R3R:RRRRRSRTt collectionstMappingRtlist(RU((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pytto_key_val_lists cCshg}x[t|D]M}|d |dko8dknrSt|dd!}n|j|qW|S(sParse 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` :rtype: list iit"(t_parse_list_headertunquote_header_valuetappend(RUtresulttitem((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pytparse_list_headers $cCsi}xt|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` :rtype: dict t=iiR[N(R\R3R=R](RUR_R`RK((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pytparse_dict_headers  $cCsq|rm|d|dko%dknrm|dd!}| sN|d dkrm|jddjddSn|S( sUnquotes 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. :rtype: str iiR[iis\\s\s\"(treplace(RUt is_filename((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pyR]s * cCs+i}x|D]}|j||j]tflagss+]s$^<\?xml.*?encoding=["\']*(.+?)["\'>](R&R'tDeprecationWarningtretcompiletItfindall(tcontentt charset_ret pragma_retxml_re((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pytget_encodings_from_contentGscCs_|jd}|sdStj|\}}d|krK|djdSd|kr[dSdS(s}Returns encodings from given HTTP Header Dict. :param headers: dictionary to extract encoding from. :rtype: str s content-typetcharsets'"ttexts ISO-8859-1N(tgetR3tcgit parse_headertstrip(theaderst content_typetparams((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pytget_encoding_from_headers[s  ccs|jdkr)x|D] }|VqWdStj|jdd}x+|D]#}|j|}|rK|VqKqKW|jddt}|r|VndS(sStream decodes a iterator.NterrorsRdttfinal(tencodingR3tcodecstgetincrementaldecoderR;tTrue(titeratortrR`tdecodertchunktrv((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pytstream_decode_response_unicodeps    ccsdd}|dks|dkr-t|}nx0|t|kr_||||!V||7}q0WdS(s Iterate over slices of a string.iN(R3R(tstringt slice_lengthtpos((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pyt iter_slicess cCstjdtg}t|j}|rcyt|j|SWqctk r_|j|qcXnyt|j|ddSWnt k r|jSXdS(sReturns the requested content back in unicode. :param r: Response object to get unicode content from. Tried: 1. charset from content-type 2. fall back and replace all unicode characters :rtype: str sIn requests 3.0, get_unicode_from_response will be removed. For more information, please see the discussion on issue #2266. (This warning should only appear once.)RRdN( R&R'RnRR~RRst UnicodeErrorR^t TypeError(Rttried_encodingsR((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pytget_unicode_from_responses   t4ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzs0123456789-._~cCs|jd}xtdt|D]}||dd!}t|dkr|jrytt|d}Wn!tk rtd|nX|tkr|||d||dA}tjtjd|S(sConverts mask from /xx format to xxx.xxx.xxx.xxx Example: if mask is 24 function returns 255.255.255.0 :rtype: str Iii s>I(Rt inet_ntoaRtpack(tmaskR((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pyRscCs-ytj|Wntjk r(tSXtS(s :rtype: bool (RRterrortFalseR(t string_ip((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pytis_ipv4_addresss cCs|jddkryt|jdd}Wntk rFtSX|dks_|dkrctSytj|jddWqtjk rtSXntStS(sV Very simple check of the cidr format in no_proxy variable. :rtype: bool Rii i( tcountRSR=RTRRRRR(tstring_networkR((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pyt is_valid_cidr s cCs8d}|d}t|j}|rd|jddjdD}|jdd}t|rx|D]8}t|rt||rtSqu||krutSquWqx@|D]5}|j|s|jddj|rtSqWnyt |}Wn t t j fk r)t }nX|r4tSt S( sL Returns whether we should bypass proxies or not. :rtype: bool cSs(tjj|p'tjj|jS(N(R"tenvironRztupper(tk((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pyt'stno_proxycss|]}|r|VqdS(N((t.0RH((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pys 2st Rt,R1i(RR<RdR=RRRRtendswithR RRtgaierrorR(RAt get_proxyRR<Rtproxy_ipRHtbypass((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pytshould_bypass_proxies!s.  %      +  cCst|riStSdS(sA Return a dict of environment proxies. :rtype: dict N(RR (RA((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pytget_environ_proxiesWs cCs|p i}t|}|jdkrC|jd|j|jSd|jd|jd|j|jg}d}x(|D] }||krz||}PqzqzW|S(sSelect a proxy for the url, if applicable. :param url: The url being for the request :param proxies: A dictionary of schemes or schemes and hosts to proxy URLs tallsall://s://N(RthostnameR3Rztscheme(RAtproxiesturlpartst proxy_keystproxyt proxy_key((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pyt select_proxycs       spython-requestscCsd|tfS(sO Return a string representing the default user agent. :rtype: str s%s/%s(R(RK((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pytdefault_user_agent}scCs2titd6djd d6dd6dd 6S( s9 :rtype: requests.structures.CaseInsensitiveDict s User-Agents, tgziptdeflatesAccept-Encodings*/*tAccepts keep-alivet Connection(sgzipsdeflate(RRR(((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pytdefault_headerss  c Csg}d}xtjd|D]}y|jdd\}}Wntk ra|d}}nXi|jdd6}xa|jdD]P}y|jd\}}Wntk rPnX|j|||j|; rel=front; type="image/jpeg",; rel=back;type="image/jpeg" :rtype: list s '"s, * '"RARb(RoR=RTR}R^( RUtlinkst replace_charstvalRARtlinktparamtkey((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pytparse_header_linkss    tR2iicCs|d }|tjtjfkr&dS|d tjkr=dS|d tjtjfkr]dS|jt}|dkr|dS|dkr|d d dtkrd S|d d dtkrd Sn|dkr|d t krd S|d t krdSnd S(s :rtype: str isutf-32is utf-8-sigisutf-16isutf-8Ns utf-16-beis utf-16-les utf-32-bes utf-32-le( Rt BOM_UTF32_LEtBOM32_BEtBOM_UTF8t BOM_UTF16_LEt BOM_UTF16_BERt_nullt_null2t_null3R3(tdatatsamplet nullcount((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pytguess_json_utfs*    cCsSt||\}}}}}}|s7||}}nt||||||fS(sGiven 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. :rtype: str (RR (RAt new_schemeRR<R5Rtquerytfragment((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pytprepend_scheme_if_neededs!cCsRt|}y"t|jt|jf}Wnttfk rMd}nX|S(s{Given a url with authentication components, extract them into a tuple of username,password. :rtype: (str,str) R(RR(RRtusernametpasswordR@R(RAtparsedtauth((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pytget_auth_from_urls  " cCsCt|tr|}n'tr0|j|}n|j|}|S(sGiven 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. (R:R R tencodeR;(RRtout((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pytto_native_strings  s^\S[^\r\n]*$|^$cCs|\}}t|tr$t}nt}y&|j|sOtd|nWn-tk rtd|t|fnXdS(sVerifies that header value is a string which doesn't contain leading whitespace or return characters. This prevents unintended header injection. :param header: tuple, in the format (name, value). s7Invalid return character or leading space in header: %ss4Header value %s must be of type str or bytes, not %sN(R:Rt_CLEAN_HEADER_REGEX_BYTEt_CLEAN_HEADER_REGEX_STRtmatchRRttype(theaderRKRUtpat((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pytcheck_header_validitys   cCsft|\}}}}}}|s4||}}n|jddd}t|||||dfS(sW Given a url remove the fragment and the authentication part. :rtype: str t@iiR(RtrsplitR (RARR<R5RRR((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pyt urldefragauth#s (s.netrcs_netrc(Ut__doc__R{RRWR R"RoRRR&RRRtcompatRR\RRRRRRR R R R R RtcookiesRRt structuresRt exceptionsRRRt_hush_pyflakesR4twheretDEFAULT_CA_BUNDLE_PATHRR.RRJRQRVRZRaRcR]RiRlRwRRRRt frozensetRRRRRRRRRRRRRRRRRRRRRRpRRRR(((s7/tmp/pip-build-0nEYGB/pip/pip/_vendor/requests/utils.pyt sz         R   0 3    #      %      6  "