Files
PowerToys/PythonHome/Lib/site-packages/requests/utils.pyc

189 lines
20 KiB
Plaintext
Raw Normal View History

2014-07-09 18:15:23 +08:00
<03>
2014-07-10 23:57:08 +08:00
l<EFBFBD><EFBFBD>Sc@s<>dZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl
2014-07-09 18:15:23 +08:00
Z
ddl m Z ddl m Z ddlmZddlmZmZmZmZmZmZmZmZmZmZmZddlmZmZdd lm Z dd
l!m"Z"efZ#d3Z$e j%<00>Z&d <00>Z'd<00>Z(d<00>Z)d<00>Z*d<00>Z+d<00>Z,d<00>Z-d<00>Z.e/d<00>Z0d<00>Z1d<00>Z2d<00>Z3d<00>Z4d<00>Z5d<00>Z6d<00>Z7e8dd<17>Z9d<00>Z:d <00>Z;d!<00>Z<d"<00>Z=d#<00>Z>d$<00>Z?d%<00>Z@d&<00>ZAd'd(<00>ZBd)<00>ZCd*<00>ZDd+jEd,<00>ZFeFd-ZGeFd.ZHd/<00>ZId0<00>ZJd1<00>ZKd,d2<00>ZLdS(4s<>
requests.utils
~~~~~~~~~~~~~~
This module provides utility functions that are used within Requests
that are also useful for external consumption.
i<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ni(t __version__(tcerts(tparse_http_list( tquoteturlparsetbyteststrt OrderedDicttunquotetis_py2t builtin_strt
getproxiest proxy_bypasst
urlunparse(tRequestsCookieJartcookiejar_from_dict(tCaseInsensitiveDict(t
2014-07-10 23:57:08 +08:00
InvalidURLs.netrct_netrccCs"t|d<00>r|j<00>}n|S(s/Returns an internal sequence dictionary update.titems(thasattrR(td((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytdict_to_sequence'scCs<>t|d<00>rt|<00>St|d<00>r/|jSt|d<00>rxy|j<00>}Wntjk
rdqxXtj|<00>jSnt|d<00>r<>t|j<00><00>SdS(Nt__len__tlentfilenotgetvalue( RRRtiotUnsupportedOperationtostfstattst_sizeR(toR((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pyt super_len0s
2014-07-09 18:15:23 +08:00
c
Cs2yddlm}m}d}x^tD]V}ytjjdj|<00><00>}Wntk
r_dSXtjj |<00>r&|}Pq&q&W|dkr<>dSt
|<00>}|j j d<00>d}yG||<00>j |<00>}|r<>|dr<>dnd} || |dfSWn|tfk
rnXWnttfk
r-nXdS( s;Returns the Requests tuple auth for a given url from netrc.i<><69><EFBFBD><EFBFBD>(tnetrctNetrcParseErrors~/{0}Nt:iii(R"R#tNonet NETRC_FILESRtpatht
expandusertformattKeyErrortexistsRtnetloctsplittauthenticatorstIOErrort ImportErrortAttributeError(
turlR"R#t
2014-07-10 23:57:08 +08:00
netrc_pathtftloctrithostRtlogin_i((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytget_netrc_authDs0    cCsLt|dd<00>}|rH|ddkrH|ddkrHtjj|<00>SdS(s0Tries to guess the filename of the given object.tnameit<i<><69><EFBFBD><EFBFBD>t>N(tgetattrR%RR'tbasename(tobjR:((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytguess_filenamers&cCsD|dkrdSt|ttttf<00>r:td<00><00>nt|<00>S(s<>Take an object and test to see if it can be represented as a
2014-07-09 18:15:23 +08:00
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
2014-07-10 23:57:08 +08:00
ValueErrorR(tvalue((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytfrom_key_val_listys
2014-07-09 18:15:23 +08:00
 cCse|dkrdSt|ttttf<00>r:td<00><00>nt|tj<00>r[|j <00>}nt
|<00>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.
2014-07-10 23:57:08 +08:00
s+cannot encode objects that are not 2-tuplesN( R%RARRRBRCRDt collectionstMappingRtlist(RE((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytto_key_val_list<73>s cCshg}x[t|<00>D]M}|d |dko8dknrSt|dd!<21>}n|j|<00>qW|S(s<>Parse lists as described by RFC 2068 Section 2.
2014-07-09 18:15:23 +08:00
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`
2014-07-10 23:57:08 +08:00
ii<><69><EFBFBD><EFBFBD>t"(t_parse_list_headertunquote_header_valuetappend(REtresulttitem((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytparse_list_header<65>s $cCs<>i}x<>t|<00>D]~}d|kr5d||<qn|jdd<00>\}}|d |dkoldknr<>t|dd!<21>}n|||<qW|S(sMParse lists of key, value pairs as described by RFC 2068 Section 2 and
2014-07-09 18:15:23 +08:00
convert them into a python dict:
>>> 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`
2014-07-10 23:57:08 +08:00
t=ii<><69><EFBFBD><EFBFBD>RKN(RLR%R-RM(RERORPR:((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytparse_dict_header<65>s 
$cCsq|rm|d|dko%dknrm|dd!}| sN|d dkrm|jdd<00>jdd<00>Sn|S( s<>Unquotes a header value. (Reversal of :func:`quote_header_value`).
2014-07-09 18:15:23 +08:00
This does not use the real unquoting but what browsers are actually
using for quoting.
:param value: the header value to unquote.
2014-07-10 23:57:08 +08:00
ii<><69><EFBFBD><EFBFBD>RKiis\\s\s\"(treplace(REt is_filename((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pyRM<00>s
2014-07-09 18:15:23 +08:00
* cCs+i}x|D]}|j||j<q W|S(soReturns a key/value dictionary from a CookieJar.
:param cj: CookieJar object to extract cookies from.
2014-07-10 23:57:08 +08:00
(RER:(tcjt cookie_dicttcookie((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytdict_from_cookiejars cCst|<00>}|j|<00>|S(s<>Returns a CookieJar from a key/value dictionary.
2014-07-09 18:15:23 +08:00
:param cj: CookieJar to insert cookies into.
:param cookie_dict: Dict of key/values to insert into CookieJar.
2014-07-10 23:57:08 +08:00
(Rtupdate(RVRWtcj2((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytadd_dict_to_cookiejars  cCsftjddtj<00>}tjddtj<00>}tjd<00>}|j|<00>|j|<00>|j|<00>S(slReturns encodings from given content string.
2014-07-09 18:15:23 +08:00
:param content: bytestring to extract encodings from.
s!<meta.*?charset=["\']*(.+?)["\'>]tflagss+<meta.*?content=["\']*;?charset=(.+?)["\'>]s$^<\?xml.*?encoding=["\']*(.+?)["\'>](tretcompiletItfindall(tcontentt
2014-07-10 23:57:08 +08:00
charset_ret pragma_retxml_re((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytget_encodings_from_contents
2014-07-09 18:15:23 +08:00
cCs_|jd<00>}|sdStj|<00>\}}d|krK|djd<00>Sd|kr[dSdS(smReturns encodings from given HTTP Header Dict.
:param headers: dictionary to extract encoding from.
s content-typetcharsets'"ttexts
2014-07-10 23:57:08 +08:00
ISO-8859-1N(tgetR%tcgit parse_headertstrip(theaderst content_typetparams((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytget_encoding_from_headers,s  ccs<>|jdkr)x|D] }|VqWdStj|j<00>dd<00>}x+|D]#}|j|<00>}|rK|VqKqKW|jddt<00>}|r<>|VndS(sStream decodes a iterator.NterrorsRTttfinal(tencodingR%tcodecstgetincrementaldecodertdecodetTrue(titeratortrRPtdecodertchunktrv((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytstream_decode_response_unicode@s    ccs=d}x0|t|<00>kr8||||!V||7}q WdS(s Iterate over slices of a string.iN(R(tstringt slice_lengthtpos((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pyt iter_slicesRscCs<>g}t|j<00>}|rSyt|j|<00>SWqStk
2014-07-09 18:15:23 +08:00
rO|j|<00>qSXnyt|j|dd<00>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. every encodings from ``<meta ... charset=XXX>``
3. fall back and replace all unicode characters
2014-07-10 23:57:08 +08:00
RqRTN(RpRmRRbt UnicodeErrorRNt TypeError(Rzttried_encodingsRt((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytget_unicode_from_responseZs  t4ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzs0123456789-._~cCs<>|jd<00>}x<>tdt|<00><00>D]<5D>}||dd!}t|<00>dkr<>|j<00>r<>ytt|d<00><00>}Wn!tk
2014-07-09 18:15:23 +08:00
r<>td|<16><00>nX|tkr<>|||d||<q<>d||||<q%d||||<q%Wdj |<00>S(s<>Un-escape any percent-escape sequences in a URI that are unreserved
characters. This leaves all reserved, illegal and non-ASCII bytes encoded.
t%iiiis%Invalid percent-escape sequence: '%s'Rr(
2014-07-10 23:57:08 +08:00
R-trangeRtisalnumtchrRCRDRtUNRESERVED_SETtjoin(turitpartstithtc((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytunquote_unreserved<65>s  cCstt|<00>dd<00>S(s<>Re-quote the given URI.
2014-07-09 18:15:23 +08:00
This function passes the given URI through an unquote/quote cycle to
ensure that it is fully and consistently quoted.
2014-07-10 23:57:08 +08:00
tsafes!#$%&'()*+,/:;=?@[]~(RR<>(R<>((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pyt requote_uri<72>s cCs<>tjdtj|<00><00>d}|jd<00>\}}tjdtjtt|<00><00><00><00>d}tjdtj|<00><00>d|@}||@||@kS(s<>
2014-07-09 18:15:23 +08:00
This function allows you to check if on IP belongs to a network subnet
Example: returns True if ip = 192.168.1.1 and net = 192.168.1.0/24
returns False if ip = 192.168.1.1 and net = 192.168.100.0/24
2014-07-10 23:57:08 +08:00
s=Lit/(tstructtunpacktsockett inet_atonR-tdotted_netmaskRC(tiptnettipaddrtnetaddrtbitstnetmasktnetwork((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytaddress_in_network<72>s
2014-07-09 18:15:23 +08:00
+#cCs/ddd|>dA}tjtjd|<00><00>S(st
Converts mask from /xx format to xxx.xxx.xxx.xxx
Example: if mask is 24 function returns 255.255.255.0
2014-07-10 23:57:08 +08:00
l<00><EFBFBD>ii s>I(R<>t inet_ntoaR<61>tpack(tmaskR<6B>((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pyR<79><00>scCs-ytj|<00>Wntjk
r(tSXtS(N(R<>R<>terrortFalseRx(t string_ip((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytis_ipv4_address<73>s
cCs<>|jd<00>dkr<>yt|jd<00>d<19>}Wntk
2014-07-09 18:15:23 +08:00
rFtSX|dks_|dkrctSytj|jd<00>d<19>Wq<57>tjk
2014-07-10 23:57:08 +08:00
r<>tSXntStS(s9Very simple check of the cidr format in no_proxy variableR<65>ii i( tcountRCR-RDR<>R<>R<>R<>Rx(tstring_networkR<6B>((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pyt is_valid_cidr<64>s cCsd<00>}|d<00>}t|<00>j}|r<>|jdd<00>jd<00>}|jd<00>d}t|<00>r<>xs|D](}t|<00>rkt||<00>r<>tSqkqkWq<57>x@|D]5}|j|<00>s<>|jd<00>dj|<00>r<>tSq<53>Wnyt |<00>}Wn t
2014-07-09 18:15:23 +08:00
t j fk
rt }nX|rtSt S(s:
Returns whether we should bypass proxies or not.
2014-07-10 23:57:08 +08:00
cSs(tjj|<00>p'tjj|j<00><00>S(N(RtenvironRitupper(tk((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pyt<lambda><3E>stno_proxyt Rrt,R$i(RR,RTR-R<>R<>R<>RxtendswithR R<>R<>tgaierrorR<72>(R2t get_proxyR<79>R,R<>tproxy_ipR7tbypass((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytshould_bypass_proxies<65>s*      + 
cCst|<00>riSt<00>SdS(s%Return a dict of environment proxies.N(R<>R (R2((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytget_environ_proxiess spython-requestscCs7tj<00>}|dkr'tj<00>}n<>|dkr<>dtjjtjjtjjf}tjjdkr<>dj |tjjg<00>}q<>n<|dkr<>tj<00>}n!|dkr<>tj<00>}nd}ytj
2014-07-09 18:15:23 +08:00
<00>}tj <00>}Wnt k
rd}d}nXd j d
|t fd
||fd
||fg<00>S( s4Return a string representing the default user agent.tCPythontPyPys%s.%s.%sRsRrtJythont
2014-07-10 23:57:08 +08:00
IronPythontUnknownR<6E>s%s/%s(tplatformtpython_implementationtpython_versiontsystpypy_version_infotmajortminortmicrot releaselevelR<6C>tsystemtreleaseR/R(R:t_implementationt_implementation_versiontp_systemt p_release((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytdefault_user_agent
2014-07-09 18:15:23 +08:00
s.     !    
 cCs+tit<00>d6djd<00>d6dd6<>S( Ns
2014-07-10 23:57:08 +08:00
User-Agents, tgziptdeflatesAccept-Encodings*/*tAccept(sgzipsdeflate(RR<>R<>(((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytdefault_headers)s
2014-07-09 18:15:23 +08:00
c Cs<>g}d}x<>|jd<00>D]<5D>}y|jdd<00>\}}Wntk
r^|d}}nXi}|jd<00>|d<xa|jd<00>D]P}y|jd<00>\}}Wntk
r<>PnX|j|<00>||j|<00><q<>W|j|<00>qW|S( s<>Return a dict of parsed link headers proxies.
i.e. Link: <http:/.../front.jpeg>; rel=front; type="image/jpeg",<http://.../back.jpeg>; rel=back;type="image/jpeg"
2014-07-10 23:57:08 +08:00
s '"R<>t;iRrs<> '"R2RR(R-RDRlRN( REtlinkst replace_charstvalR2Rotlinktparamtkey((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytparse_header_links1s"   ttasciiiicCs|d }|tjtjfkr&dS|d tjkr=dS|d tjtjfkr]dS|jt<00>}|dkr|dS|dkr<>|ddd<00>tkr<>d S|d
2014-07-09 18:15:23 +08:00
dd<00>tkr<>d Sn|dkr<>|d t kr<>d S|d
2014-07-10 23:57:08 +08:00
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<45>t_nullt_null2t_null3R%(tdatatsamplet nullcount((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytguess_json_utfYs*
2014-07-09 18:15:23 +08:00
   cCsSt||<00>\}}}}}}|s7||}}nt||||||f<00>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
2014-07-10 23:57:08 +08:00
new_schemetschemeR,R'Rotquerytfragment((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytprepend_scheme_if_neededvs!cCsRt|<00>}y"t|j<00>t|j<00>f}Wnttfk
2014-07-09 18:15:23 +08:00
rMd}nX|S(s_Given a url with authentication components, extract them into a tuple of
2014-07-10 23:57:08 +08:00
username,password.Rr(RrRr(RRtusernametpasswordR1R<>(R2tparsedtauth((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytget_auth_from_url<72>s  "
2014-07-09 18:15:23 +08:00
cCsId}t|t<00>r|}n'tr6|j|<00>}n|j|<00>}|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
2014-07-10 23:57:08 +08:00
R tencodeRw(RRttout((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pytto_native_string<6E>s (s.netrcs_netrc(Mt__doc__RjRuRGRRR<>R^R<>R<>R<>RrRRtcompatRRLRRRRRRR R
2014-07-09 18:15:23 +08:00
R R R tcookiesRRt
structuresRt
2014-07-10 23:57:08 +08:00
exceptionsRt_hush_pyflakesR&twheretDEFAULT_CA_BUNDLE_PATHRR!R9R@RFRJRQRSR<>RMRYR\RfRpR~R<>R<>t frozensetR<74>R<>R<>R<>R<>R<>R<>R<>R<>R<>R<>R<>R<>R<>R<>R<>R<>R<>R<>R<>(((sQe:\github\Wox.JSONRPC\Output\Debug\PythonHome\lib\site-packages\requests\utils.pyt<module>
2014-07-09 18:15:23 +08:00
sr          L    .     "       "
    -    #