};e̷dZeZgdZddlmZddlmZddlm Z m Z  ddl m Z m Z mZmZmZdd lZej(dd k\reZeZneZeZdd lmZdd lmZdd lm Z m!Z!ddl"m#Z#ddl$m%Z%ddl&m'Z'e(Z)GddZ*GddZ+Gdde+ZGddeZ,GddeZ-GddeZ.Gdde+Z/Gdd eZ0Gd!d"eZ1Gd#d$e1Z2Gd%d&eZ3y #e$rddlmZmZm Z ddlm Z mZYwxYw)'z)Common support for web service resources.) CollectionCollectionWithKeyBasedLookupEntryNamedOperationResource ServiceRoot)Message)BytesIO)dumpsloads)parse_qsunquote urlencodeurljoinurlparse)rrr )rrN)r) __version__)Browser RestfulHttp)DatetimeJSONEncoder) HTTPError)URIc$eZdZdZdZddZdZy)HeaderDictionaryaA dictionary that bridges httplib2's and wadllib's expectations. httplib2 expects all header dictionary access to give lowercase header names. wadllib expects to access the header exactly as it's specified in the WADL file, which means the official HTTP header name. This class transforms keys to lowercase before doing a lookup on the underlying dictionary. That way wadllib can pass in the official header name and httplib2 will get the lowercased name. c||_yN)wrapped_dictionary)selfrs =/usr/lib/python3/dist-packages/lazr/restfulclient/resource.py__init__zHeaderDictionary.__init__Ks "4NcT|jj|jSz2Retrieve a value, converting the key to lowercase.)rgetlower)rkeydefaults rr$zHeaderDictionary.getNs&&**399;77r!cX|j|t}|tur t||Sr#)r$missingKeyError)rr&values r __getitem__zHeaderDictionary.__getitem__Rs)g& G 3-  r!r)__name__ __module__ __qualname____doc__r r$r,r!rrr?s 58r!rc eZdZdZdZdZdZy) RestfulBasez=Base class for classes that know about lazr.restful services.application/jsonci}|jD]5\}}t|tr |j}|||j |<7|Sr)items isinstancer self_link_get_external_param_name)r dictionarynew_dictionaryr&r+s r_transform_resources_to_linksz)RestfulBase._transform_resources_to_links_sU$**, GJC%*AFN488= > Gr!c|S)aTurn a lazr.restful name into something to be sent over HTTP. For resources this may involve sticking '_link' or '_collection_link' on the end of the parameter name. For arguments to named operations, the parameter name is returned as is. r1r param_names rr9z$RestfulBase._get_external_param_namegs r!N)r-r.r/r0JSON_MEDIA_TYPEr<r9r1r!rr3r3ZsG(Or!r3ceZdZdZdZeZeZeZe dZ e dZ e dZ e dZ e dZe ZdZd Zd Zd Ze dd ZddZdZdZdZdZdZy )rz+Base class for lazr.restful HTTP resources.cH||}||jd<||jd<y)z5Initialize with respect to a wadllib Resource object.N_root_wadl_resource)__dict__)rroot wadl_resources rr zResource.__init__us+ <D"& g*7 &'r!c8|j|jS)z,Name the collections this resource links to.)_get_parameter_namesFIND_COLLECTIONSrs rlp_collectionszResource.lp_collectionss(()>)>??r!c8|j|jS)z(Name the entries this resource links to.)rI FIND_ENTRIESrKs r lp_entrieszResource.lp_entriess(():):;;r!c8|j|jS)z'Name this resource's scalar attributes.)rIFIND_ATTRIBUTESrKs r lp_attributeszResource.lp_attributess(()=)=>>r!cg}|jjD]}|jj}|dk(r|jj ddg}nV|dk(rQdD]1}|jj |}|!|j}nj |j}D].}|jdk(s|j|j|S)z.Name all of this resource's custom operations.r$queryplainpost)!application/x-www-form-urlencodedmultipart/form-dataws.op) rD method_iternamer%requestparamsget_representation_definitionresolve_definitionappend fixed_value)rnamesmethodr[r] media_type definitionparams r lp_operationszResource.lp_operationss ))55 F;;$$&Du}../AB# J"(!M!M""J"-%/%B%B%D  $**4+>+>? ::(LL!2!23 ! ( r!cd|j|j|j|jS)z;A hook into dir() that returns web service-derived members.)rIrJrNrQrKs r __members__zResource.__members__s0((  ! !4#4#4d6J6J  r!cg}|jj|jD]}|j}|j}|dk7rg|e|j rY|j dr$|j|vsN|j|ddc|j|vsr|j|dd|j|vs|j||S)z2Retrieve some subset of the resource's parameters.r8N_collection_linki) rD parametersr@r[link can_followendswithrJr`rNrQ)rkindsrb parameterr[rns rrIzResource._get_parameter_namess,,778L8LM 'I>>D>>D{"t'7DOO==!34,,5 T$3Z0((E1 T#2Y/''50LL&9 ': r!c(|j|duS)z8Does this resource have a parameter with the given name?N)r9r>s rlp_has_parameterzResource.lp_has_parameters,,Z8DDr!c|jdD]{}|jj||z}|$ |j|jy|j }|j |j||jcS|jj|}|t d|z|jS#t$rYwxYw)zGet the value of one of the resource's parameters. :return: A scalar value if the parameter is not a link. A new Resource object, whose resource is bound to a representation, if the parameter is a link. )_linkrkN)r?zNo such parameter: %s) _ensure_representationrD get_parameter get_valuer*linked_resource_create_bound_resourcerCr[)rr?suffixrfrzs rlp_get_parameterzResource.lp_get_parameters ##%3 F''55j66IJE OO% ??$, "'"7"722JJEJJ3 $##11*= =2Z?@ @  ! sC  CCcd|i}|jjd|}||jjd|}|td|zt|j||S)zGet a custom operation with the given name. :return: A NamedOperation instance that can be called with appropriate arguments to invoke the operation. rYr$) query_paramsrV)representation_paramszNo operation with name: %s)rD get_methodr*rrC)roperation_namer]rcs rlp_get_named_operationzResource.lp_get_named_operationsy >*$$//F/K >((33f4F >7.HI Idjj$77r!Nc|j}t|d} t} |jds||jdrt} |j j | | } ||j||||}n | ||S)aCreate a lazr.restful Resource subclass from a wadllib Resource. :param resource: The wadllib Resource to wrap. :param representation: A previously fetched representation of this resource, to be reused. If not provided, this method will act just like the Resource constructor. :param representation_media_type: The media type of any previously fetched representation. :param representation_needs_processing: Set to False if the 'representation' parameter should be used as is. :param representation_definition: A wadllib RepresentationDefinition object describing the structure of this representation. Used in cases when the representation isn't the result of sending a standard GET to the resource. :param param_name: The name of the link that was followed to get to this resource. :return: An instance of the appropriate lazr.restful Resource subclass. z-pagerk)representation_definition)type_urlrrrprRESOURCE_TYPE_CLASSESr$bind) clsrFresourcerepresentationrepresentation_media_typerepresentation_needs_processingrr?r resource_typer'r_classs rr{zResource._create_bound_resource sH$$ *2.    W %  "z':':;M'N G,,00H  % }})/*C %H tX&&r!cL|||j_i}|||d<|jjj |j|}||jjj k(ry|jj ||j|jd<y)&Update this resource's representation.Nz If-None-Match)headersrD) rD_urlrC_browserr$ NOT_MODIFIEDrr@rE)rnew_urletagrrs r lp_refreshzResource.lp_refreshHs  '.D   $  '+GO $,,00   1  TZZ00== = +/*=*=*B*B D00+  &'r!c |j|S#t$rYnwxYw |j|S#t$rt|d|dwxYw)z@Try to retrive a named operation or parameter of the given name.z object has no attribute '')rr*r}AttributeError)rattrs r __getattr__zResource.__getattr__[sd ..t4 4    ((. .  594@  s 4Ac|jj||j}|j}t |dkDr|Dcgc]}|j c}Sycc}w)z0Find the set of possible values for a parameter.rN)rDrxr@optionslenr+)rr?rrroptions r lp_values_forzResource.lp_values_forhsY''55 ,, ## wWhat's this parameter's name in the underlying representation?)rvrkN)rDrx)rr?r|r[s rr9z!Resource._get_external_param_namers97 F&D""006  r!c4|jj|jjj |j}t |t r|jd}t|}t |tr`|d}|Y||jjk7r@|jjj|}|j|j_ |jj||jd|j d<yy)z5Make sure this resource has a representation fetched.Nutf-8resource_type_linkFrrD)rDrrCrr$r7 binary_typedecoder dictr_wadlget_resource_typetagrr@rE)rr type_linkrs rrwzResource._ensure_representationzs    - - 5!ZZ0044T5H5HIN.+6!/!6!6w!?">2N.$/*+?@ )!T%8%8%A%AA$(JJ$4$4$F$F!%M/<.?.?D''+.2.A.A.F.F$$05/G/DMM* +3 6r!c||k( S)zInequality operator.r1rothers r__ne__zResource.__ne__s5=  r!)Nr4TNNNN)r-r.r/r0r objectrJrNrQpropertyrLrOrRrgri __methods__rIrtr}r classmethodr{rrrr9rwrr1r!rrrrs58x8LhO @@<<??6   K DE!>8  "4(,"&9'9'v & B!r!rc eZdZdZedZy) ScalarValuez.A resource representing a single scalar value.cN|j|jjS)zReturn the scalar value.)rwrDrrKs rr+zScalarValue.values" ##%""111r!N)r-r.r/r0rr+r1r!rrrs8 22r!rc*eZdZdZddZdZdZdZy) HostedFilezAA resource representing a file managed by a lazr.restful service.Nc<|dvrt||||Std)z5Open the file on the server for read or write access.)rw'Invalid mode. Supported modes are: r, w)HostedFileBuffer ValueError)rmode content_typefilenames ropenzHostedFile.opens' : #D$ hG GFG Gr!cv|jjj|jjy)z Delete the file from the server.N)rCrdeleterDurlrKs rrzHostedFile.deletes& ""4#6#6#:#:;r!cgS)z4HostedFile objects define no web service parameters.r1)rrqs rrIzHostedFile._get_parameter_namess r!ch|duxr-|jj|jjk(S)aEquality comparison. Two hosted files are the same if they have the same URL. There is no need to check the contents because the only way to retrieve or modify the hosted file contents is to open a filehandle, which goes direct to the server. N)rDrrs r__eq__zHostedFile.__eq__s7   D##''5+?+?+C+CC r!)rNN)r-r.r/r0rrrIrr1r!rrrsKH<  r!rcjeZdZdZeedZdddddejffd Z e dZ dZ dZ xZS) rzEntry point to the service. Subclass this for a service-specific client. :ivar credentials: The credentials instance used to access Launchpad. )rrNrc |(|ddk7r|dz }|t|z }|ddk7r|dz }t||_||_||_t ||||||j ||_|jj|j|_ |jjd} | j|jj| d} tt|?d| y)zRoot access to a lazr.restful API. :param credentials: The credentials used to access the service. :param service_root: The URL to the root of the web service. :type service_root: string Nr/rr4)strr _root_uri_base_client_name credentialsr _user_agentrget_wadl_applicationrget_resource_by_pathrr$superrr ) r authorizer service_rootcachetimeout proxy_infoversionbase_client_name max_retries root_resource bound_root __class__s rr zServiceRoot.__init__s"  B3&# CL (LB3&# \*"2&           ]]77G  77; "'' MM  m ,.@  k4)$ ;r!cdtz}|jdk7r|jdz|zdz}t}||d<|j>|jj}t |D]}||}|j ||d|dS)aThe value for the User-Agent header. This will be something like: launchpadlib 1.6.1, lazr.restfulclient 1.0.0; application=apport That is, a string describing lazr.restfulclient and an optional custom client built on top, and parameters containing any authorization-specific information that identifies the user agent (such as the application name). zlazr.restfulclient %srz ()z User-Agent)rrr ruser_agent_paramssorted set_param)r base_portionmessagerr&r+s rrzServiceRoot._user_agents/<  ! !R '11D8<G#ML) ,     ' $ 0 0 B B /0 <)#.!!#ul; <|$$r!ct||||Sr)r)rrrrrs r httpFactoryzServiceRoot.httpFactory!s:ugzBBr!ct|}|jdk(r1|dddk(r|dd}t|jj |}|j j |}t|tr|jd} t|}|j d}|td|z|jjj|}t|jj||j }|j#|j||d d S#t$rtd|zwxYw) zLoad a resource given its URL.rNrrz!%s doesn't serve a JSON document.rz+Couldn't determine the resource type of %s.r4Fr)rschemerrr`rr$r7rrr rrCrr WadlResourcerr{)rrparseddocumentrrrrGs rloadzServiceRoot.load$s<# ==B 2Aw#~!"gdnn++C01C==$$S) h ,w/H H"8_N#&&';<  =C  ((::9E $TZZ%5%5sM>%J!$BB7 ) : ) &&t}}'C'CDF  zzX%U]]);a)? JJ  **, BJC ++!%-@AS  B 3 34$""44:IcF|jjjt|}|j d}|j }|j jj|j\}}tj|j |||dS)z5Handle the creation of a new resource by fetching it.Location content-type) rrrrrxrzrFrrrrr{)rrrr wadl_responsewadl_parameterrGs rr z#NamedOperation._handle_201_responses((1166 X & '44Z@&66  II..77 8I8IJ'.. II}gx/G  r!c|d}|jj}|j|}|=||jk(r,t |t r|j d}t|S|St |t r|j d}t|}||Sd|vr]d|vrY|d}|jjj|d}t|jj||j} n;|j}t|jj||j} tj|j| ||d|S)z)Process the return value of an operation.rrr8rF)rr)rrr^r@r7rrr rFrrrrr_rr{) rrrrrresponse_definitionrrrrGs rr z#NamedOperation._handle_200_responsesa/ "..77  = =l K " % ,t333g{3%nnW5GW~%N g{ +nnW-G>  O ( "';x'G ;'C IIOO==-.M) m&7&7M*<<> &) &?&C&CM.. II   ,1&? /  r!c|S)z/Named operation parameter names are sent as is.r1r>s rr9z'NamedOperation._get_external_param_namesr!N) r-r.r/r0r rr r r9r1r!rrrDs"G' HET  < |r!rc\eZdZdZfdZdZdZdZfdZdZ dZ d fd Z d Z xZ S) rzBA class for an entry-type resource that can be updated with PATCH.cttt| ||i|jd<tt| ||y)N_dirty_attributes)rrr rErrFrGrs rr zEntry.__init__s6 eT#D-8.0 )* eT#D-8r!cbdt|jjd|jdS)z:Return the WADL resource type and the URL to the resource.)rrfragmentr8rKs r__repr__zEntry.__repr__s) '' ( 1 1 NN  r!cr|jjjt|jS)zDelete the resource.)rCrrrr8rKs r lp_deletezEntry.lp_deletes&zz""))#dnn*=>>r!c|jS)zReturn the URL to the resource.)r8rKs r__str__z Entry.__str__s ~~r!cn|dk7r||jvr|j|Stt||S)z-Try to retrive a parameter of the given name.r)rrrr)rr[rs rrzEntry.__getattr__ s= & &t-----d33UD-d33r!c|j|s&td|jjd|d||j|<y)z$Set the parameter of the given name.rz' object has no attribute 'N)rtrrr-r)rr[r+s r __setattr__zEntry.__setattr__sC$$T* >>**D2 (-t$r!c|duxrO|j|jk(xr4|j|jk(xr|j|jk(S)zEquality operator. Two entries are the same if their self_link and http_etag attributes are the same, and if their dirty attribute dicts contain the same values. N)r8 http_etagrrs rrz Entry.__eq__sY   B%//1 B%//1 B&&%*A*AA  r!c|t|dd}tt||||jj y)rr,N)getattrrrrrclear)rrrrs rrzEntry.lp_refresh(s4t[$/ eT%gt4 $$&r!c@|j|j}i}t|dd}|||d<|jjj t |j||\}}|jdk(r|j|d|jj|d}|jdk(r_||jk(rOt|tr|jd}t|}||j _||j _yyy) zSave changes to the entry.r,NzIf-Matchrrrr)r<rr.rCrpatchrr8rrr/r@r7rrr rDrrd)rrrrrrrnew_representations rlp_savez Entry.lp_save.s;;  " "  t[$/  "&GJ  !JJ//55   ' ??c ! OOHZ0 1 $$&/ ??c !ld6J6J&J';/!..1!&w 1CD   .-9D   *'K !r!r)r-r.r/r0r r#r%r'rr*rrr4rrs@rrrs3L 9 ?4-  ' :r!rcFeZdZdZfdZdZdZdZdZdZ dZ xZ S) rz4A collection-type resource that supports pagination.c.tt| ||y)zCreate a collection object.N)rrr rs rr zCollection.__init__Ss j$(}=r!c|j}t|tr|St|tr |jSt d)znThe number of items in the collection. :return: length of the collection :rtype: int z collection size is not available) total_sizer7intrr+r)rr8s r__len__zCollection.__len__WsC __ j# &   K 0## #>? ?r!c#K|j|jj} |j|j diD]}||j d}|y|j j j t|}t|tr|jd}t|}w)zqIterate over the items in the collection. :return: iterator :rtype: sequence of `Entry` entriesnext_collection_linkNr) rwrDr_convert_dicts_to_entriesr$rCrrr7rrr )r current_pager next_linknext_gets r__iter__zCollection.__iter__is ##%**99  ::  B/  %(()?@I zz**..s9~>H(K0#??73 ?LsCCct|tr|j|S|jt||dz}t|dk7r t d|dS)acLook up a slice, or a subordinate resource by index. To discourage situations where a lazr.restful client fetches all of an enormous list, all collection slices must have a definitive end point. For performance reasons, all collection slices must be indexed from the start of the list rather than the end. rzlist index out of ranger)r7slice _get_slicer IndexError)rr& found_slices rr,zCollection.__getitem__~s[ c5 !??3' '//%S1W*=>K;1$ !:;;q> !r!c|jxsd}|j}|dkr td|dkr td|jj}|8|t |dkr'|d}t |}|||}|j d}n+d}g}|j|jjd|}||z } | t |z } | dkDr||jjj |} t| tr| jd} t| } | d} || d| z }| t |z } | j d}|n2| t | }| dkDr| |kr|j|d | }| dkDr||j|dd|j}|j!|Dcgc]}|c}Scc}w) z!Retrieve a slice of a collection.rz6Collection slices must have a nonnegative start point.z>Collection slices must have a definite, nonnegative end point.Nr<r=zws.startrzws.size)startstoprrDrrr$_with_url_query_variable_setrrCrr7rrr stepr>)rrDrIrJexisting_representation entry_pagefirst_page_size entry_dictspage_url desired_size more_neededpage_getrcurrent_page_entriesrs rrEzCollection._get_slices  qzz 19K  !8)  #'"5"5"D"D " .53 #I .< 4 &1;J!*oO$U40K.223IJH#OK88##''UHe| "S%55 Ao("6zz**..x8H(K0#??73"8_N#1)#< / = =K&[)99K%))*@AH&"%&:";Q;#@ <<i1Ao("68 :: !%mm4K !::;G     s5 Gc#0K|D]}|d}|d}|jj}|j|}t|jj||j}t j |j|||jdyw)aConvert dictionaries describing entries to Entry objects. The dictionaries come from the 'entries' field of the JSON dictionary you get when you GET a page of a collection. Each dictionary is the same as you'd get if you sent a GET request to the corresponding entry resource. So each of these dictionaries can be treated as a preprocessed representation of an entry resource, and turned into an Entry instance. :yield: A sequence of Entry instances. r8rFN) rD applicationrrrrr{rCr@)rr< entry_dict resource_urlrwadl_applicationrrs rr>z$Collection._convert_dicts_to_entriess" J%k2L!+,@!A #22>> ,>>"M$##//!!H 11 Hj$2F2F  sBBct|}|ji}nt|j}t|||<t |d|_t|S)z1A helper method to set a query variable in a URL.T)rrTr rr)rrvariable new_valueurir]s rrKz'Collection._with_url_query_variable_set sM#h 99 Fcii(Fy>xfd+ 3xr!) r-r.r/r0r r:rBr,rEr>rKrrs@rrrPs,>>@$+*"$[ z8 r!rc2eZdZdZfdZdZdZdZxZS)rzA collection-type resource that supports key-based lookup. This collection can be sliced, but any single index passed into __getitem__ will be treated as a custom lookup key. cZt|trtt||S |j |}| t|||} |j|S#t $r tdwxYw#t$r*}|jjdk(r t|d}~wwxYw)z8Look up a slice, or a subordinate resource by unique ID.unsubscriptable objectN) r7rDrrr,_get_url_from_idNotImplementedErrorrr*rwrrr)rr&r shim_resourceers rr,z(CollectionWithKeyBasedLookup.__getitem__s c5 !5tHM M 6'',C ;3- S    0 0 2 # 645 5 6 zz  C'sm#  s#A A7A47 B*%B%%B*c |j|}| t||j:d}t |j jjd|jz}nW |j jj|}t|tr|jd}t|}|d}t'|j j||}|j)|j ||dS#t$r tdwxYw#t$r*}|j j"dk(r t%|d}~wwxYw) z=Retrieve a member from this collection without looking it up.raN#rrbrF)rr)rcrdrr collection_ofrrCr markup_urlrr$r7rrr rrrr*rr{)rr&rrrurl_geterrorrs rrz%CollectionWithKeyBasedLookup.__call__2sC 6'',C ;S/ !    ) "N!(   ++S43E3E-E"  **--11#6g{3%nnW5G!&w"00D!E  0 0#7IJ** JJ ),1 +  M# 645 5 68 >>((C/"3-'  s$C='AD=D E%EENct)z2Transform the unique ID of an object into its URL.)rd)rr&s rrcz-CollectionWithKeyBasedLookup._get_url_from_idhs !##r!) r-r.r/r0r,rrircrrs@rrrs ,0 hM$r!rc$eZdZdZddZdZdZy)rz8The contents of a file hosted by a lazr.restful service.Nc>|jj|_|dk(r| td| td|jjj |jd\}}|d}|j d}|d}t |d } t| jd d }n/|d k(rd }| td| tdd}n td||_ ||_ ||_ ||_ ||_ tj||y)Nrz8Files opened for read access can't specify content_type.z4Files opened for read access can't specify filename.T)return_responserz last-modifiedzcontent-locationrrrr!z8Files opened for write access must specify content_type.z4Files opened for write access must specify filename.r)rDrrrCrr$rrsplit hosted_filerrr last_modifiedr r ) rrsrrrrr+rtcontent_locationpaths rr zHostedFileBuffer.__init__psP--11 3;' ,# M*//88<<$=OHe$N3L$LL9M ((:; ,-a0Dtzz#r23H S[E# , M!MFG G& (  *u%r!c|jdk(red|jz}|jjjj |j |j|jd|itj|y)Nrzattachment; filename="%s"zContent-Disposition) rrrsrCrputrgetvaluerr close)r dispositions rrzzHostedFileBuffer.closesk 99 5 EK    " " + + / / !!& 4    dr!c0tj||yr)r write)rbs rr}zHostedFileBuffer.writes dAr!r)r-r.r/r0r rzr}r1r!rrrmsB-&^ r!r)4r0r __metaclass____all__ email.messager ior jsonr r urllib.parser rrrr ImportErrorurllibsys version_infor text_typebytesrunicodewadllib.applicationrrlazr.restfulclientrlazr.restfulclient._browserrrlazr.restfulclient._jsonrlazr.restfulclient.errorsrlazr.urirrr)rr3rrrrrrrrr1r!rrs"&0  "*LL  A!IKIK8*<8/ (60k!{k!\ 2(2  Bv (v rb[bJd:Hd:NBBJU$:U$p>w>M"*44))*sC00D  D