ó b@RTc@s«dZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddl m Z ddlmZddlmZmZmZmZmZmZmZmZmZmZmZddlmZmZdd lm Z dd l!m"Z"efZ#d4Z$e j%ƒZ&d „Z'd„Z(d„Z)d„Z*d„Z+d„Z,d„Z-d„Z.e/d„Z0d„Z1d„Z2d„Z3d„Z4d„Z5d„Z6d„Z7e8ddƒZ9d„Z:d „Z;d!„Z<d"„Z=d#„Z>d$„Z?d%„Z@d&„ZAd'd(„ZBd)„ZCd*„ZDd+jEd,ƒZFeFd-ZGeFd.ZHd/„ZId0„ZJd1„ZKd,d2„ZLd3„ZMdS(5s• requests.utils ~~~~~~~~~~~~~~ This module provides utility functions that are used within Requests that are also useful for external consumption. iÿÿÿÿNi(t __version__(tcerts(tparse_http_list( tquoteturlparsetbyteststrt OrderedDicttunquotetis_py2t builtin_strt getproxiest proxy_bypasst urlunparse(tRequestsCookieJartcookiejar_from_dict(tCaseInsensitiveDict(t InvalidURLs.netrct_netrccCs"t|dƒr|jƒ}n|S(s/Returns an internal sequence dictionary update.titems(thasattrR(td((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pytdict_to_sequence'scCs›t|dƒrt|ƒSt|dƒr/|jSt|dƒrxy|jƒ}Wntjk rdqxXtj|ƒjSnt|dƒr—t|jƒƒSdS(Nt__len__tlentfilenotgetvalue( RRRtiotUnsupportedOperationtostfstattst_sizeR(toR((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pyt super_len0s c Cs2yddlm}m}d}x^tD]V}ytjjdj|ƒƒ}Wntk r_dSXtjj |ƒr&|}Pq&q&W|dkr�dSt |ƒ}|j j dƒd}yG||ƒj |ƒ}|rø|dràdnd} || |dfSWn|tfk rnXWnttfk r-nXdS( s;Returns the Requests tuple auth for a given url from netrc.iÿÿÿÿ(tnetrctNetrcParseErrors~/{0}Nt:iii(R"R#tNonet NETRC_FILESRtpatht expandusertformattKeyErrortexistsRtnetloctsplittauthenticatorstIOErrort ImportErrortAttributeError( turlR"R#t netrc_pathtftloctrithostRtlogin_i((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pytget_netrc_authDs0    cCsLt|ddƒ}|rH|ddkrH|ddkrHtjj|ƒSdS(s0Tries to guess the filename of the given object.tnameitN(tgetattrR%RR'tbasename(tobjR:((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pytguess_filenamers&cCsD|dkrdSt|ttttfƒr:tdƒ‚nt|ƒS(s¬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')]) s+cannot encode objects that are not 2-tuplesN(R%t isinstanceRRtbooltintt ValueErrorR(tvalue((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pytfrom_key_val_listys  cCse|dkrdSt|ttttfƒr:tdƒ‚nt|tjƒr[|j ƒ}nt |ƒS(szTake 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. s+cannot encode objects that are not 2-tuplesN( R%RARRRBRCRDt collectionstMappingRtlist(RE((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pytto_key_val_list�s cCshg}x[t|ƒD]M}|d |dko8dknrSt|dd!ƒ}n|j|ƒqW|S(sñ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` iiÿÿÿÿt"(t_parse_list_headertunquote_header_valuetappend(REtresulttitem((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pytparse_list_headerªs $cCs™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` t=iiÿÿÿÿRKN(RLR%R-RM(RERORPR:((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pytparse_dict_headerÉs  $cCsq|rm|d|dko%dknrm|dd!}| sN|d dkrm|jddƒjddƒSn|S( s×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. iiÿÿÿÿRKiis\\s\s\"(treplace(REt is_filename((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pyRMës * cCs+i}x|D]}|j||j]tflagss+]s$^<\?xml.*?encoding=["\']*(.+?)["\'>](tretcompiletItfindall(tcontentt charset_ret pragma_retxml_re((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pytget_encodings_from_contents cCs_|jdƒ}|sdStj|ƒ\}}d|krK|djdƒSd|kr[dSdS(smReturns encodings from given HTTP Header Dict. :param headers: dictionary to extract encoding from. s content-typetcharsets'"ttexts ISO-8859-1N(tgetR%tcgit parse_headertstrip(theaderst content_typetparams((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pytget_encoding_from_headers,s  ccs™|jdkr)x|D] }|VqWdStj|jƒddƒ}x+|D]#}|j|ƒ}|rK|VqKqKW|jddtƒ}|r•|VndS(sStream decodes a iterator.NterrorsRTttfinal(tencodingR%tcodecstgetincrementaldecodertdecodetTrue(titeratortrRPtdecodertchunktrv((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pytstream_decode_response_unicode@s    ccs=d}x0|t|ƒkr8||||!V||7}q WdS(s Iterate over slices of a string.iN(R(tstringt slice_lengthtpos((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pyt iter_slicesRscCs‰g}t|jƒ}|rSyt|j|ƒSWqStk rO|j|ƒqSXnyt|j|ddƒSWntk r„|jSXdS(sÒReturns 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 RqRTN(RpRmRRbt UnicodeErrorRNt TypeError(Rzttried_encodingsRt((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pytget_unicode_from_responseZs   t4ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzs0123456789-._~cCsô|jdƒ}xÕtdt|ƒƒD]¾}||dd!}t|ƒdkrÑ|jƒrÑytt|dƒƒ}Wn!tk r–td|ƒ‚nX|tkr¼|||d||dA}tjtjd|ƒƒS(st Converts mask from /xx format to xxx.xxx.xxx.xxx Example: if mask is 24 function returns 255.255.255.0 Iÿÿÿÿii s>I(R™t inet_ntoaR—tpack(tmaskR ((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pyR›­scCs-ytj|ƒWntjk r(tSXtS(N(R™RšterrortFalseRx(t string_ip((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pytis_ipv4_address¶s cCs¤|jdƒdkrœyt|jdƒdƒ}Wntk rFtSX|dks_|dkrctSytj|jdƒdƒWq tjk r˜tSXntStS(s9Very simple check of the cidr format in no_proxy variableR–ii i( tcountRCR-RDR¨R™RšR§Rx(tstring_networkR¦((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pyt is_valid_cidr¾s cCsd„}|dƒ}t|ƒj}|rÝ|jddƒjdƒ}|jdƒd}t|ƒršxs|D](}t|ƒrkt||ƒr“tSqkqkWqÝx@|D]5}|j|ƒsÒ|jdƒdj|ƒr¡tSq¡Wnyt |ƒ}Wn t t j fk rt }nX|rtSt S(s: Returns whether we should bypass proxies or not. cSs(tjj|ƒp'tjj|jƒƒS(N(RtenvironRitupper(tk((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pytÖstno_proxyt Rrt,R$i(RR,RTR-RªR­R£RxtendswithR R„R™tgaierrorR¨(R2t get_proxyR²R,Rœtproxy_ipR7tbypass((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pytshould_bypass_proxiesÒs*      +  cCst|ƒriStƒSdS(s%Return a dict of environment proxies.N(RºR (R2((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pytget_environ_proxiesÿs spython-requestscCs7tjƒ}|dkr'tjƒ}n |dkr‹dtjjtjjtjjf}tjjdkrÇdj |tjjgƒ}qÇn<|dkr¦tjƒ}n!|dkrÁtjƒ}nd}ytj ƒ}tj ƒ}Wnt k rd}d}nXd j d |t fd ||fd ||fgƒS( s4Return a string representing the default user agent.tCPythontPyPys%s.%s.%sRsRrtJythont IronPythontUnknownR³s%s/%s(tplatformtpython_implementationtpython_versiontsystpypy_version_infotmajortminortmicrot releaselevelR�tsystemtreleaseR/R(R:t_implementationt_implementation_versiontp_systemt p_release((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pytdefault_user_agents.     !      cCs2titƒd6djd ƒd6dd6dd 6ƒS( Ns User-Agents, tgziptdeflatesAccept-Encodings*/*tAccepts keep-alivet Connection(sgzipsdeflate(RRÐR�(((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pytdefault_headers&s  c Csñg}d}xÞ|jdƒD]Í}y|jddƒ\}}Wntk r^|d}}nXi}|jdƒ|d; rel=front; type="image/jpeg",; rel=back;type="image/jpeg" s '"R´t;iRrs<> '"R2RR(R-RDRlRN( REtlinkst replace_charstvalR2Rotlinktparamtkey((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pytparse_header_links/s"   ttasciiiicCs|d }|tjtjfkr&dS|d tjkr=dS|d tjtjfkr]dS|jtƒ}|dkr|dS|dkrÅ|ddd…tkr¥d S|d dd…tkrÅd Sn|dkrü|d t kråd S|d t krüd SndS(Nisutf-32is utf-8-sigisutf-16isutf-8s utf-16-beis utf-16-les utf-32-bes utf-32-le( Rut BOM_UTF32_LEtBOM32_BEtBOM_UTF8t BOM_UTF16_LEt BOM_UTF16_BER«t_nullt_null2t_null3R%(tdatatsamplet nullcount((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pytguess_json_utfWs*    cCsSt||ƒ\}}}}}}|s7||}}nt||||||fƒS(s”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.(RR (R2t new_schemetschemeR,R'Rotquerytfragment((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pytprepend_scheme_if_neededts!cCsRt|ƒ}y"t|jƒt|jƒf}Wnttfk rMd}nX|S(s_Given a url with authentication components, extract them into a tuple of username,password.Rr(RrRr(RRtusernametpasswordR1R„(R2tparsedtauth((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pytget_auth_from_url‚s  " cCsId}t|tƒr|}n'tr6|j|ƒ}n|j|ƒ}|S(sÐ 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(R%RAR R tencodeRw(RRttout((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pytto_native_string�s cCsft|ƒ\}}}}}}|s4||}}n|jddƒd}t|||||dfƒS(sE Given a url remove the fragment and the authentication part t@iiÿÿÿÿRr(RtrsplitR (R2RíR,R'RoRîRï((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pyt urldefragauth¢s (s.netrcs_netrc(Nt__doc__RjRuRGRRRÁR^RÄR™R—RrRRtcompatRRLRRRRRRR R R R R tcookiesRRt structuresRt exceptionsRt_hush_pyflakesR&twheretDEFAULT_CA_BUNDLE_PATHRR!R9R@RFRJRQRSR¨RMRYR\RfRpR~R‚R†t frozensetRŒR“R•R£R›RªR­RºR»RÐRÕRÝRöRåRæRçRëRðRõRøRû(((s;/opt/freeware/lib/python2.7/site-packages/requests/utils.pyt st          L    .     "            -   #