Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/lazr/restfulclient/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyc


&ˉTc@svdZeZddgZddlZddlZddlZddlZddlZddl	Z	ddl
mZddlm
Z
mZddlZddlmZddlmZdd	lmZdd
lmZddlmZmZddlmZdd
lmZmZmZdZ de
fdYZ!de"fdYZ#de#fdYZ$dddYZ%dS(sBrowser object to make requests of lazr.restful web services.

The `Browser` class does some massage of HTTP requests and responses,
and handles custom caches. It is not part of the public
lazr.restfulclient API. (But maybe it should be?)
tBrowsertRestfulHttpiN(tsleep(tHttpturlnorm(tStringIO(t	urlencode(tApplication(tURI(t	error_fort	HTTPError(tDatetimeJSONEncoder(t_md5t
re_url_schemetre_slashcCs	yUtj|rTt|trB|jd}|jd}qT|jd}nWntk
rhnXt|tr|jd}nt|j	}tj
d|}tj
d|}tj
}|dd}t||kr|| }ndj||fS(sReturn a filename suitable for the cache.

    Strips dangerous and common characters to create a filename we
    can use to store the cache in.
    sutf-8tidnatt,i i(R
tmatcht
isinstancetstrtdecodetencodetUnicodeErrortunicodeRt	hexdigesttsubRRtmaximum_cache_filename_lengthtlentjoin(tfilenametfilemd5tmaximum_filename_lengthtmaximum_length_before_md5_sum((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pytsafename9s$
	
cBs;eZdZdZdddddZdZdZRS(sAn Http subclass with some custom behavior.

    This Http client uses the TE header instead of the Accept-Encoding
    header to ask for compressed representations. It also knows how to
    react when its cache is a MultipleRepresentationCache.
    icCsKtt|j|||||_|jdk	rG|jj|ndS(N(tsuperRt__init__t
authorizertNonetauthorizeSession(tselfR%tcachettimeoutt
proxy_info((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyR$ns	c


Csr|jdr|d=n|jdk	rD|jj||||ntt|j|||||||||		S(s4Use the authorizer to authorize an outgoing request.t
authorizationN(thas_keyR%R&tauthorizeRequestR#Rt_request(
R(tconnthosttabsolute_uritrequest_uritmethodtbodytheaderstredirectionstcachekey((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyR/us
	cCs)t|jtr%|jj||SdS(s+Retrieve a cached value for an HTTP header.N(RR)tMultipleRepresentationCachet_getCachedHeaderR&(R(turitheader((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyR:sN(t__name__t
__module__t__doc__RR&R$R/R:(((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyRds		tAtomicFileCachecBsDeZdZdZedZdZdZdZdZ	RS(sA FileCache that can be shared by multiple processes.

    Based on a patch found at
    <http://code.google.com/p/httplib2/issues/detail?id=125>.
    s.tempcCsdtjj||_||_ytj|jWn+tk
r_}|jtjkr`q`nXdS(sUConstruct an ``AtomicFileCache``.

        :param cache: The directory to use as a cache.
        :param safe: A function that takes a key and returns a name that's
            safe to use as a filename.  The key must never return a string
            that begins with ``TEMPFILE_PREFIX``.  By default uses
            ``safename``.
        N(	tostpathtnormpatht
_cache_dirt_get_safe_nametmakedirstOSErrorterrnotEEXIST(R(R)tsafete((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyR$s		cCsM|j|}|j|jr7td|jntjj|j|S(s0Return the path on disk where ``key`` is stored.s Cache key cannot start with '%s'(REt
startswithtTEMPFILE_PREFIXt
ValueErrorRARBRRD(R(tkeytsafe_key((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyt
_get_key_paths
cCsv|j|}y/t|d}z|jSWd|jXWn1ttfk
rq}|jtjkrrqrnXdS(sGet the value of ``key`` if set.

        This behaves slightly differently to ``FileCache`` in that if
        ``set()`` fails to store a key, this ``get()`` will behave as if that
        key were never set whereas ``FileCache`` returns the empty string.

        :param key: The key to retrieve.  Must be either bytes or unicode
            text.
        :return: The value of ``key`` if set, None otherwise.
        trbN(RQtopentreadtclosetIOErrorRGRHtENOENT(R(ROtcache_full_pathtfRK((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pytgetscCstjd|jd|j\}}tj|d}|j||j|j|}t	j
dkrtjj|rtj
|ntj||dS(sSet ``key`` to ``value``.

        :param key: The key to set.  Must be either bytes or unicode text.
        :param value: The value to set ``key`` to.  Must be bytes.
        tprefixtdirtwbtwin32N(ttempfiletmkstempRMRDRAtfdopentwriteRURQtsystplatformRBtexiststunlinktrename(R(ROtvaluethandlet	path_nameRYRX((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pytsets	

!cCsR|j|}ytj|Wn+tk
rM}|jtjkrNqNnXdS(sDelete ``key`` from the cache.

        If ``key`` has not already been set then has no effect.

        :param key: The key to delete.  Must be either bytes or unicode text.
        N(RQRAtremoveRGRHRW(R(RORXRK((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pytdeletes(
R=R>R?RMR"R$RQRZRkRm(((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyR@s			R9cBs)eZdZdZdZdZRS(s>A cache that can hold different representations of the same resource.

    If a resource has two representations with two media types,
    FileCache will only store the most recently fetched
    representation. This cache can keep track of multiple
    representations of the same resource.

    This class works on the assumption that outside calling code sets
    an instance's request_media_type attribute to the value of the
    'Accept' header before initiating the request.

    This class is very much not thread-safe, but FileCache isn't
    thread-safe anyway.
    cCs)tt|j||jd|_dS(s>Tell FileCache to call append_media_type when generating keys.N(R#R9R$tappend_media_typeR&trequest_media_type(R(R)((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyR$s
cCs-|jdk	r#|d|j}nt|S(sAppend the request media type to the cache key.

        This ensures that representations of the same resource will be
        cached separately, so long as they're served as different
        media types.
        t-N(RoR&R"(R(RO((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyRnsc
Cs~t|\}}}}|j|}|d}|dk	rzx:t|D])}	|	j|rJ|	t|jSqJWndS(s+Retrieve a cached value for an HTTP header.t:N(RRZR&RRLRtstrip(
R(R;R<tschemet	authorityR3R8tcached_valuetheader_starttline((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyR:s
(R=R>R?R$RnR:(((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyR9s		cBseZdZeZdZd
d
d
d
edZdZd
ddd
dZ	d
e
dZdZd	Z
d
d
ZdZd
dZRS(s6A class for making calls to lazr.restful web services.icCs}|dkr.tj}tjtj|nt|trLt	|}n|j
|||||_||_||_
dS(sInitialize, possibly creating a cache.

        If no cache is provided, a temporary directory will be used as
        a cache. The temporary directory will be automatically removed
        when the Python process exits.
        N(R&R_tmkdtemptatexittregistertshutiltrmtreeRt
basestringR9thttpFactoryt_connectiont
user_agenttmax_retries(R(tservice_roottcredentialsR)R*R+RR((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyR$s	c		Csxtd|jdD]p}|jj|d|d|d|\}}|jd	kr||jkrtd|d}t|qPqW||fS(
NiiR4R5R6iii(ii(trangeRRtrequesttstatustintR(	R(turlR4R5R6tretry_counttresponsetcontentt	sleep_for((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyt_request_and_retry/s	!
tGETsapplication/jsonc
CsJ|dkrtdni|d6}|jd
k	rG|j|d<nt|jjtrn||jj_n|d
k	r|j|n|j	t
|d|d|d|\}}|jdkr|d	kr	d
|ksd|kr||jfSt
||n	d|_||fSt||}	|	d
k	r@|	n||fS(s'Create an authenticated request object.stag:launchpad.net:2008:redactedsUYou tried to access a resource that you don't have the server-side permission to see.tAccepts
User-AgentR4R5R6i0Rs
If-None-MatchsIf-Modified-SinceiN(RNRR&RRR)R9RotupdateRRRtNOT_MODIFIEDR
R	(
R(RtdataR4t
media_typet
extra_headersR6RRterror((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyR/>s.
'
		
	cCsht|ttfr|}n|jd}|j}|j|d|\}}|rd||fS|S(s2GET a representation of the given resource or URI.RZR(RR}Rt
get_methodtbuild_request_urlR/(R(tresource_or_uriR6treturn_responseRR4RR((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyRZ{s	
cCs4d}|j|d|\}}tt||S(s?GET a WADL representation of the resource at the requested url.sapplication/vnd.sun.wadl+xmlR(R/RR(R(Rt	wadl_typeRR((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pytget_wadl_applicationscKs)||d<t|}|j||dS(s"POST a request to the web service.sws.optPOST(RR/(R(Rtmethod_nametkwsR((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pytposts
cCsBi|d6}|dk	r)|j|n|j||dd|S(s(PUT the given representation to the URL.sContent-TypetPUTRN(R&RR/(R(RtrepresentationRR6R((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pytputs

cCs|j|dddS(s%DELETE the resource at the given URL.R4tDELETEN(R/R&(R(R((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyRmscCsidd6}|dk	r)|j|n|jjt|d}|dk	rj|jjrj||d<n|j|tj|dt	dd|S(	s8PATCH the object at url with the updated representation.sapplication/jsonsContent-TypetetagsIf-MatchtclstPATCHRN(
R&RRR:Rtignore_etagR/t
simplejsontdumpsR(R(RRR6Rtcached_etag((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pytpatchs

N(R=R>R?tobjectRtMAX_RETRIESR&R$RR/tFalseRZRRRRmR(((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyRs		<			((&R?ttypet
__metaclass__t__all__RyRHRAR{RcR_ttimeRthttplib2RRRt	cStringIORturllibRtwadllib.applicationRtlazr.uriRterrorsR	R
t_jsonRRR
RR"RRR@R9R(((s?/usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyt<module>s0		+*\-