
    (h̷                        S r \r/ SQrSSKJr  SSKJr  SSKJ	r	J
r
   SSKJrJrJrJrJr  SS	Kr\R(                  S   S
:  a  \r\rO\r\rSSKJr  SSKJr  SSKJ r J!r!  SSK"J#r#  SSK$J%r%  SSK&J'r'  \(" 5       r) " S S5      r* " S S5      r+ " S S\+5      r " S S\5      r, " S S\5      r- " S S\5      r. " S S\+5      r/ " S S \5      r0 " S! S"\5      r1 " S# S$\15      r2 " S% S&\5      r3g	! \ a    SSKJrJrJr  SSKJrJr   Nf = f)'z)Common support for web service resources.)
CollectionCollectionWithKeyBasedLookupEntryNamedOperationResourceServiceRoot    )Message)BytesIO)dumpsloads)parse_qsunquote	urlencodeurljoinurlparse)r   r   r   )r   r   N   )r   )__version__)BrowserRestfulHttp)DatetimeJSONEncoder)	HTTPError)URIc                   .    \ rS rSrSrS rSS jrS rSrg)	HeaderDictionary?   a  A 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                     Xl         g Nwrapped_dictionary)selfr   s     =/usr/lib/python3/dist-packages/lazr/restfulclient/resource.py__init__HeaderDictionary.__init__K   s    "4    Nc                 T    U R                   R                  UR                  5       5      $ z2Retrieve a value, converting the key to lowercase.)r   getlower)r    keydefaults      r!   r'   HeaderDictionary.getN   s    &&**399;77r$   c                 Z    U R                  U[        5      nU[        L a  [        U5      eU$ r&   )r'   missingKeyError)r    r)   values      r!   __getitem__HeaderDictionary.__getitem__R   s)    g&G3-r$   r   r   )	__name__
__module____qualname____firstlineno____doc__r"   r'   r0   __static_attributes__ r$   r!   r   r   ?   s    	58r$   r   c                   (    \ rS rSrSrSrS rS rSrg)RestfulBaseZ   z=Base class for classes that know about lazr.restful services.application/jsonc                     0 nUR                  5        H9  u  p4[        U[        5      (       a  UR                  nXBU R	                  U5      '   M;     U$ r   )items
isinstancer   	self_link_get_external_param_name)r    
dictionarynew_dictionaryr)   r/   s        r!   _transform_resources_to_links)RestfulBase._transform_resources_to_links_   sM    $**,JC%**AF488=> - r$   c                     U$ )zTurn 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.
r8   r    
param_names     r!   rA   $RestfulBase._get_external_param_nameg   s
     r$   r8   N)	r2   r3   r4   r5   r6   JSON_MEDIA_TYPErD   rA   r7   r8   r$   r!   r:   r:   Z   s    G(Or$   r:   c                       \ rS rSrSrS r\" 5       r\" 5       r\" 5       r	\
S 5       r\
S 5       r\
S 5       r\
S 5       r\
S 5       r\rS	 rS
 rS rS r\     SS j5       rSS jrS rS rS rS rS rSrg)r   r   z+Base class for lazr.restful HTTP resources.c                 F    Uc  U nXR                   S'   X R                   S'   g)z5Initialize with respect to a wadllib Resource object.N_root_wadl_resource)__dict__)r    rootwadl_resources      r!   r"   Resource.__init__u   s'    <D "&g*7&'r$   c                 8    U R                  U R                  5      $ )z,Name the collections this resource links to.)_get_parameter_namesFIND_COLLECTIONSr    s    r!   lp_collectionsResource.lp_collections   s     (()>)>??r$   c                 8    U R                  U R                  5      $ )z(Name the entries this resource links to.)rU   FIND_ENTRIESrW   s    r!   
lp_entriesResource.lp_entries   s     (():):;;r$   c                 8    U R                  U R                  5      $ )z'Name this resource's scalar attributes.)rU   FIND_ATTRIBUTESrW   s    r!   lp_attributesResource.lp_attributes   s     (()=)=>>r$   c                    / nU R                   R                   H  nUR                  R                  5       nUS:X  a  UR                  R                  SS/5      nOZUS:X  aT  S H3  nUR                  R                  U5      nUc  M#  UR                  5       n  O   WR                  U R                   5      nW H1  nUR                  S:X  d  M  UR                  UR                  5          M     M     U$ )z.Name all of this resource's custom operations.r'   queryplainpost)!application/x-www-form-urlencodedmultipart/form-dataws.op)
rO   method_iternamer(   requestparamsget_representation_definitionresolve_definitionappendfixed_value)r    namesmethodrj   rl   
media_type
definitionparams           r!   lp_operationsResource.lp_operations   s    
 ))55F;;$$&Du}../AB#J "(!M!M""J "-%/%B%B%D
# $**4+>+>?::(LL!2!23  ! 6( r$   c                 d    U R                  U R                  U R                  U R                  5      $ )z;A hook into dir() that returns web service-derived members.)rU   rV   r[   r_   rW   s    r!   __members__Resource.__members__   s0     ((!!4#4#4d6J6J
 	
r$   c                    / nU R                   R                  U R                  5       H  nUR                  nUR                  nUS:w  az  Ubw  UR
                  (       af  UR                  S5      (       a(  U R                  U;   a  UR                  USS 5        Mq  Ms  U R                  U;   a  UR                  USS 5        M  M  U R                  U;   d  M  UR                  U5        M     U$ )z2Retrieve some subset of the resource's parameters.r@   N_collection_linki)rO   
parametersrJ   rj   link
can_followendswithrV   ro   r[   r_   )r    kindsrq   	parameterrj   r   s         r!   rU   Resource._get_parameter_names   s    ,,778L8LMI>>D>>D{"t'7DOO ==!344,,5T$3Z0 6 ((E1T#2Y/ 2 ''50LL&9 N: r$   c                 (    U R                  U5      SL$ )z8Does this resource have a parameter with the given name?N)rA   rG   s     r!   lp_has_parameterResource.lp_has_parameter   s    ,,Z8DDr$   c                    U R                  5         S Hy  nU R                  R                  X-   5      nUc  M%   UR                  5         UR                  5       c    gUR
                  nU R                  U R                  XCR                  S9s  $    U R                  R                  U5      nUc  [	        SU-  5      eUR                  5       $ ! [         a     M  f = f)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.
)_linkr|   N)rH   zNo such parameter: %s)	_ensure_representationrO   get_parameter	get_valuer.   linked_resource_create_bound_resourcerN   rj   )r    rH   suffixru   r   s        r!   lp_get_parameterResource.lp_get_parameter   s     	##%3F''55j6IJE OO%
 ??$,  "'"7"722JJJJ 3   4$ ##11*==2Z?@@  !    s   C
CCc                     SU0nU R                   R                  SUS9nUc  U R                   R                  SUS9nUc  [        SU-  5      e[        U R                  X5      $ )zGet a custom operation with the given name.

:return: A NamedOperation instance that can be called with
         appropriate arguments to invoke the operation.
rh   r'   )query_paramsre   )representation_paramszNo operation with name: %s)rO   
get_methodr.   r   rN   )r    operation_namerl   rr   s       r!   lp_get_named_operationResource.lp_get_named_operation   sw     >*$$//F/K>((33f 4 F >7.HIIdjj$77r$   Nc                     UR                   n[        U5      S   n	[        n
UR                  S5      (       d  Ub  UR                  S5      (       a  [        n
UR
                  R                  X5      nUb  UR                  UUUUS9nO U" X5      $ )a  Create 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-pager|   )representation_definition)type_urlr   r   r   r   RESOURCE_TYPE_CLASSESr'   bind)clsrQ   resourcerepresentationrepresentation_media_typerepresentation_needs_processingr   rH   r   resource_typer*   r_classs               r!   r   Resource._create_bound_resource  s    H $$ *2.W%%"z':':;M'N'N G,,00H%  }})/*C	 % H t&&r$   c                 D   Ub  XR                   l        0 nUb  X#S'   U R                  R                  R	                  U R                   US9nX@R                  R                  R
                  :X  a  gU R                   R                  X@R                  5      U R                  S'   g)&Update this resource's representation.NzIf-None-Match)headersrO   )	rO   _urlrN   _browserr'   NOT_MODIFIEDr   rJ   rP   )r    new_urletagr   r   s        r!   
lp_refreshResource.lp_refreshH  s    '.$'+O$,,00 1 
 ZZ00=== +/*=*=*B*B00+
&'r$   c                      U R                  U5      $ ! [         a     Of = f U R                  U5      $ ! [         a    [        U < SU< S35      ef = f)z@Try to retrive a named operation or parameter of the given name.z object has no attribute '')r   r.   r   AttributeError)r    attrs     r!   __getattr__Resource.__getattr__[  sd    	..t44 			((.. 	 594@ 	s    
  5 Ac                     U R                   R                  XR                  5      nUR                  n[	        U5      S:  a  U Vs/ s H  oDR
                  PM     sn$ gs  snf )z0Find the set of possible values for a parameter.r   N)rO   r   rJ   optionslenr/   )r    rH   r   r   options        r!   lp_values_forResource.lp_values_forh  sY    ''55,,
	 ##w<!/67wVLLw77 8s   Ac                 f    S H+  nX-   nU R                   R                  U5      (       d  M)  Us  $    g)z>What's this parameter's name in the underlying representation?)r   r|    N)rO   r   )r    rH   r   rj   s       r!   rA   !Resource._get_external_param_namer  s5    7F&D""0066 8 r$   c                 F   U R                   R                  Gc	  U R                  R                  R	                  U R                   5      n[        U[        5      (       a  UR                  S5      n[        U5      n[        U[        5      (       aa  US   nUbY  X R                   R                  :w  a@  U R                  R                  R                  U5      nUR                  U R                   l        U R                   R                  UU R                  SS9U R                   S'   gg)z5Make sure this resource has a representation fetched.Nutf-8resource_type_linkFr   rO   )rO   r   rN   r   r'   r?   binary_typedecoder   dictr   _wadlget_resource_typetagr   rJ   rP   )r    r   	type_linkr   s       r!   r   Resource._ensure_representationz  s    --5!ZZ0044T5H5HIN.+66!/!6!6w!?">2N .$//*+?@	)!%8%8%A%AA$(JJ$4$4$F$F!%M /<.?.?D''+.2.A.A.F.F$$05 /G /DMM*+3 6r$   c                     X:X  + $ )zInequality operator.r8   r    others     r!   __ne__Resource.__ne__  s      r$   r8   )Nr<   TNNNN)r2   r3   r4   r5   r6   r"   objectrV   r[   r_   propertyrX   r\   r`   rv   ry   __methods__rU   r   r   r   classmethodr   r   r   r   rA   r   r   r7   r8   r$   r!   r   r   r   s    58 x8LhO@ @ < < ? ?  6 
 
  K DE!>8  
 "4(,"&9' 9'v
&B!r$   r   c                   (    \ rS rSrSr\S 5       rSrg)ScalarValuei  z.A resource representing a single scalar value.c                 N    U R                  5         U R                  R                  $ )zReturn the scalar value.)r   rO   r   rW   s    r!   r/   ScalarValue.value  s"     	##%""111r$   r8   N)r2   r3   r4   r5   r6   r   r/   r7   r8   r$   r!   r   r     s    82 2r$   r   c                   4    \ rS rSrSrS	S jrS rS rS rSr	g)

HostedFilei  zAA resource representing a file managed by a lazr.restful service.Nc                 <    US;   a  [        XX#5      $ [        S5      e)z5Open the file on the server for read or write access.)rw'Invalid mode. Supported modes are: r, w)HostedFileBuffer
ValueError)r    modecontent_typefilenames       r!   openHostedFile.open  s#    :#DGGFGGr$   c                 v    U R                   R                  R                  U R                  R                  5        g)z Delete the file from the server.N)rN   r   deleterO   urlrW   s    r!   r   HostedFile.delete  s&    

""4#6#6#:#:;r$   c                     / $ )z4HostedFile objects define no web service parameters.r8   )r    r   s     r!   rU   HostedFile._get_parameter_names  s    	r$   c                 r    USL=(       a-    U R                   R                  UR                   R                  :H  $ )zEquality 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)rO   r   r   s     r!   __eq__HostedFile.__eq__  s7      D##''5+?+?+C+CC	
r$   r8   )r   NN)
r2   r3   r4   r5   r6   r   r   rU   r   r7   r8   r$   r!   r   r     s    KH<
r$   r   c                   v   ^  \ rS rSrSr\\S.rSSSSS\R                  4U 4S jjr
\S 5       rS rS	 rS
rU =r$ )r   i  zEntry point to the service. Subclass this for a service-specific client.

:ivar credentials: The credentials instance used to access Launchpad.
)r   r   Nr   c	           	        > Ub*  US   S:w  a  US-  nU[        U5      -  nUS   S:w  a  US-  n[        U5      U l        Xpl        Xl        [        U UUUUU R                  U5      U l        U R                  R                  U R                  5      U l	        U R                  R                  S5      n	U	R                  U R                  R                  U	5      S5      n
[        [        U ]?  SU
5        g)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   /r   r<   )strr   	_root_uri_base_client_namecredentialsr   _user_agentr   get_wadl_applicationr   get_resource_by_pathr   r'   superr   r"   )r    
authorizerservice_rootcachetimeout
proxy_infoversionbase_client_namemax_retriesroot_resource
bound_root	__class__s              r!   r"   ServiceRoot.__init__  s    " B3&#CL(LB3&#\* "2 &
 ]]77G
 

77;"''MMm,.@

 	k4)$
;r$   c                    S[         -  nU R                  S:w  a  U R                  S-   U-   S-   n[        5       nXS'   U R                  b>  U R                  R                  n[        U5       H  nX4   nUR                  XES5        M     US   $ )aa  The 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).
zlazr.restfulclient %sr   z ()z
User-Agent)r   r   r	   r   user_agent_paramssorted	set_param)r    base_portionmessager	  r)   r/   s         r!   r   ServiceRoot._user_agent  s     /<!!R'11D8<G#ML) ,' $ 0 0 B B/0).!!#l; 1 |$$r$   c                     [        XX45      $ r   )r   )r    r   r   r   r   s        r!   httpFactoryServiceRoot.httpFactory!  s    :gBBr$   c                    [        U5      nUR                  S:X  a2  USS S:X  a  USS n[        U R                  R	                  U5      5      nU R
                  R                  U5      n[        U[        5      (       a  UR                  S5      n [        U5      nUR                  S5      nUc  [        SU-  5      eU R                  R                  R                  U5      n[        U R                  R                  XR                   5      nU R#                  U R                  UUS	S
S9$ ! [         a    [        SU-  5      ef = f)zLoad a resource given its URL.r   N   r   r   z!%s doesn't serve a JSON document.r   z+Couldn't determine the resource type of %s.r<   Fr   )r   schemer   r   ro   r   r'   r?   r   r   r   r   rN   r   r   WadlResourcer   r   )r    r   parseddocumentr   r   r   rR   s           r!   loadServiceRoot.load$  s=   #==B 2Aw#~!"gdnn++C01C==$$S)h,,w/H	H"8_N #&&';<	=C  

((::9E$TZZ%5%5s<M<MN**JJ,1 + 
 	
  	H@3FGG	Hs   D) )E)r   r   r   r   r   )r2   r3   r4   r5   r6   r   r   r   r   MAX_RETRIESr"   r   r   r  r  r7   __classcell__r  s   @r!   r   r     s\     !" ''.<` % %0C
 
r$   r   c                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
r   iD  z=A class for a named operation to be invoked with GET or POST.c                 (    Xl         X l        X0l        g)z/Initialize with respect to a WADL Method objectN)rQ   r   wadl_method)r    rQ   r   r  s       r!   r"   NamedOperation.__init__G  s    	 &r$   c                    [        U5      S:  a  [        S5      eU R                  R                  nU R	                  U5      nU R                  R
                  nUS;   a  UR                  nOTUR                  S5      nUc  UR                  S5      nUc   S5       eUR                  U R                  R                  5      nU Vs1 s H:  nUR                  S:X  d  [        UR                  5      S:  d  M.  UR                  iM<     nnUR                  5        H  u  pX;  d  M  [        U
[        S9X'   M     US;   a!  U R                  R                   " S0 UD6nS	n0 nO=U R                  R!                  5       nU R                  R"                  " S0 UD6u  nnS
U0nU R$                  R&                  R)                  UUUR+                  5       US9u  nnUR,                  S:X  a  U R/                  XU5      $ US:X  aX  UR,                  S:X  a.  US   nU R$                  R&                  R)                  U5      u  nnOU R                  R1                  5         U R3                  XU5      $ s  snf )z)Invoke the method and process the result.r   z(Method must be called with keyword args.)r'   headr   rg   rf   zYA POST named operation must define a multipart or form-urlencoded request representation.binary)r   r   zContent-type)extra_headers   re   -  locationr8   )r   	TypeErrorr  rj   rD   rk   r   rm   rl   r   rO   typer   r>   r   r   build_request_urlbuild_representationrQ   r   _requestupperstatus_handle_201_responser   _handle_200_response)r    argskwargshttp_methodrk   rl   rt   ru   send_as_is_paramsr)   r/   r   in_representationr$  rs   responsecontents                    r!   __call__NamedOperation.__call__M  sg   t9q=FGG&&++11&9""**33))F >>%J !$BB7
 ) :)  &&t}}'C'CDF  
zzX%U]]);a)? EJJ 	 

 **,JC +!%-@A	 ' 33""44<t<C "M""446C   55==!+Z8M !II..77'	 8 
' ??c!,,SGDDf$ ??c)":.C(,		(:(:(C(CC(H%HgMM,,.,,SGDDc
s   -I#4I#c                 B   U R                   R                  R                  [        U5      5      nUR	                  S5      nUR
                  nU R                  R                  R                  UR                  5      u  p#[        R                  U R                  XcUS   5      $ )z5Handle the creation of a new resource by fetching it.Locationcontent-type)r  r6  r   r   r   r   rQ   r   r,  r   r   r   )r    r   r6  r7  wadl_responsewadl_parameterrR   s          r!   r/  #NamedOperation._handle_201_response  s    ((1166X&
 '44Z@&66 II..778I8IJ ..II}x/G
 	
r$   c           	         US   nU R                   R                  nUR                  U5      nUcB  X@R                  :X  a1  [	        U[
        5      (       a  UR                  S5      n[        U5      $ U$ [	        U[
        5      (       a  UR                  S5      n[        U5      nUc  U$ SU;   a^  SU;   aX  US   nU R                  R                  R                  US   5      n[        U R                  R                  XR                  5      n	O:UR                  5       n[        U R                  R                  XR                  5      n	[        R                  U R                  U	UUSUS9$ )z)Process the return value of an operation.r<  r   r@   r   F)r   r   )r  r6  rm   rJ   r?   r   r   r   rQ   r   r   r  r   rn   r   r   )
r    r   r6  r7  r   response_definitionr   r  r   rR   s
             r!   r0  #NamedOperation._handle_200_response  s`   /"..77==lK 	" %, 333g{33%nnW5GW~%N g{++nnW-G>O("';x'G
 ;'C IIOO==-.M )		&7&7M *<<> & )		&C&CM ..II,1&? / 
 	
r$   c                     U$ )z/Named operation parameter names are sent as is.r8   rG   s     r!   rA   'NamedOperation._get_external_param_name  s    r$   )r   rQ   r  N)r2   r3   r4   r5   r6   r"   r8  r/  r0  rA   r7   r8   r$   r!   r   r   D  s"    G'HET
<
|r$   r   c                   l   ^  \ rS rSrSrU 4S jrS rS rS rU 4S jr	S r
S	 rSU 4S
 jjrS rSrU =r$ )r   i  zBA class for an entry-type resource that can be updated with PATCH.c                 p   > [         [        U ]  X5        0 U R                  S'   [         [        U ]  X5        g )N_dirty_attributes)r   r   r"   rP   r    rQ   rR   r  s      r!   r"   Entry.__init__  s2    eT#D8 .0)*eT#D8r$   c                 f    S[        U R                  5      R                  < SU R                  < S3$ )z:Return the WADL resource type and the URL to the resource.<z at >)r   r   fragmentr@   rW   s    r!   __repr__Entry.__repr__  s)     ''(11NN
 	
r$   c                 r    U R                   R                  R                  [        U R                  5      5      $ )zDelete the resource.)rN   r   r   r   r@   rW   s    r!   	lp_deleteEntry.lp_delete  s&    zz""))#dnn*=>>r$   c                     U R                   $ )zReturn the URL to the resource.)r@   rW   s    r!   __str__Entry.__str__  s    ~~r$   c                 r   > US:w  a  XR                   ;   a  U R                   U   $ [        [        U ]  U5      $ )z-Try to retrive a parameter of the given name.rG  )rG  r   r   r   )r    rj   r  s     r!   r   Entry.__getattr__
  s;    &&-----d33UD-d33r$   c                     U R                  U5      (       d(  [        SU R                  R                  < SU< S35      eX R                  U'   g)z$Set the parameter of the given name.r   z' object has no attribute 'N)r   r   r  r2   rG  )r    rj   r/   s      r!   __setattr__Entry.__setattr__  sD    $$T** >>**D2  (-t$r$   c                     USL=(       aY    U R                   UR                   :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ )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)r@   	http_etagrG  r   s     r!   r   Entry.__eq__  sY      B%//1B%//1B &&%*A*AA		
r$   c                 z   > [        U SS5      n[        [        U ]  X5        U R                  R                  5         g)r   r\  N)getattrr   r   r   rG  clear)r    r   r   r  s      r!   r   Entry.lp_refresh(  s2    t[$/eT%g4$$&r$   c                 F   U R                  U R                  5      n0 n[        U SS5      nUb  X2S'   U R                  R                  R                  [        U R                  5      X5      u  pEUR                  S:X  a  U R                  US   5        U R                  R                  5         US   nUR                  S:X  ab  X`R                  :X  aR  [        U[        5      (       a  UR                  S5      n[        U5      nXpR                   l        X`R                   l        ggg)	zSave changes to the entry.r\  NzIf-Matchr&  r'  r<     r   )rD   rG  r_  rN   r   patchr   r@   r.  r   r`  rJ   r?   r   r   r   rO   r   rs   )r    r   r   r   r6  r7  r   new_representations           r!   lp_saveEntry.lp_save.  s   ;;""
 t[$/"&J
 !JJ//55
 ??c!OOHZ01$$&/??c!l6J6J&J ';//!..1!&w1C.-9* 'K!r$   r8   r   )r2   r3   r4   r5   r6   r"   rN  rQ  rT  r   rY  r   r   rf  r7   r  r  s   @r!   r   r     s8    L9
?4-
': :r$   r   c                   P   ^  \ rS rSrSrU 4S jrS rS rS rS r	S r
S	 rS
rU =r$ )r   iP  z4A collection-type resource that supports pagination.c                 ,   > [         [        U ]  X5        g)zCreate a collection object.N)r   r   r"   rH  s      r!   r"   Collection.__init__S  s    j$(=r$   c                     U R                   n[        U[        5      (       a  U$ [        U[        5      (       a  UR                  $ [        S5      e)zVThe number of items in the collection.

:return: length of the collection
:rtype: int
z collection size is not available)
total_sizer?   intr   r/   r(  )r    rl  s     r!   __len__Collection.__len__W  sI     __
j#&& 
K00 ###>??r$   c              #     #    U R                  5         U R                  R                  n U R                  UR	                  S0 5      5       H  nUv   M	     UR	                  S5      nUc  gU R
                  R                  R	                  [        U5      5      n[        U[        5      (       a  UR                  S5      n[        U5      nM  7f)zYIterate over the items in the collection.

:return: iterator
:rtype: sequence of `Entry`
entriesnext_collection_linkNr   )r   rO   r   _convert_dicts_to_entriesr'   rN   r   r   r?   r   r   r   )r    current_pager   	next_linknext_gets        r!   __iter__Collection.__iter__i  s      	##%**99 ::  B/  %(()?@I zz**..s9~>H(K00#??73 ?L s   C
Cc                     [        U[        5      (       a  U R                  U5      $ U R                  [        XS-   5      5      n[        U5      S:w  a  [	        S5      eUS   $ )a3  Look 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.
r  zlist index out of ranger   )r?   slice
_get_slicer   
IndexError)r    r)   found_slices      r!   r0   Collection.__getitem__~  s\     c5!!??3'' //%1W*=>K;1$ !:;;q>!r$   c                    UR                   =(       d    SnUR                  nUS:  a  [        S5      eUS:  a  [        S5      eU R                  R                  nUb8  U[        US   5      :  a&  US   n[        U5      nXRU nUR                  S5      nO+Sn/ nU R                  U R                  R                  SU5      nX2-
  n	U	[        U5      -
  n
U
S:  a  Ub  U R                  R                  R                  U5      n[        U[        5      (       a  UR                  S5      n[        U5      nUS   nX}SU
 -  nU	[        U5      -
  n
UR                  S5      nUc  O7Uc  [        U5      nU
S:  a  X:  a  U R                  US	U
5      nU
S:  a  Ub  M  UR                  b  USSUR                  2   nU R!                  U5       Vs/ s H  nUPM     sn$ s  snf )
z!Retrieve a slice of a collection.r   z6Collection slices must have a nonnegative start point.z>Collection slices must have a definite, nonnegative end point.Nrq  rr  zws.startr   zws.size)startstopr   rO   r   r   r'   _with_url_query_variable_setr   rN   r   r?   r   r   r   steprs  )r    rz  r  r  existing_representation
entry_pagefirst_page_sizeentry_dictspage_urldesired_sizemore_neededpage_getr   current_page_entriesr   s                  r!   r{  Collection._get_slice  s    qzz19K  !8) 
 #'"5"5"D"D".53#I.<
 4
& 1;J!*oO$40K.223IJH #OK88##''UH |"S%55 Ao("6zz**..x8H(K00#??73"8_N#1)#< ==K&[)99K%))*@AH &"%&:";Q;#@  <<i1 Ao("68 ::!%mm4K
 !::;G
G G
 	
 
s   	Gc              #   2  #    U H  nUS   nUS   nU R                   R                  nUR                  U5      n[        U R                   R                  UUR                  5      n[
        R                  U R                  XrU R                  S5      v   M     g7f)a  Convert 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.
r@   r   FN)	rO   applicationr   r  r   r   r   rN   rJ   )r    rq  
entry_dictresource_urlr   wadl_applicationr   r   s           r!   rs  $Collection._convert_dicts_to_entries  s      "J%k2L!+,@!A#22>>,>>"M $##//!!H
 11

H$2F2F  "s   BBc                     [        U5      nUR                  c  0 nO[        UR                  5      n[        U5      XR'   [	        US5      Ul        [        U5      $ )z1A helper method to set a query variable in a URL.T)r   rc   r   r   r   )r    r   variable	new_valueurirl   s         r!   r  'Collection._with_url_query_variable_set	  sK    #h99Fcii(Fy>fd+	3xr$   r8   )r2   r3   r4   r5   r6   r"   rn  rw  r0   r{  rs  r  r7   r  r  s   @r!   r   r   P  s1    >>@$+*"$[
z8	 	r$   r   c                   <   ^  \ rS rSrSrU 4S jrS rSrS rSr	U =r
$ )r   i  zA 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.
c                 l  > [        U[        5      (       a  [        [        U ]  U5      $  U R                  U5      nUc  [        U5      eU " U5      n UR                  5         U$ ! [         a    [        S5      ef = f! [         a+  nUR                  R                  S:X  a  [        U5      ee SnAff = f)z8Look up a slice, or a subordinate resource by unique ID.unsubscriptable objectN  )r?   rz  r   r   r0   _get_url_from_idNotImplementedErrorr(  r.   r   r   r6  r.  )r    r)   r   shim_resourceer  s        r!   r0   (CollectionWithKeyBasedLookup.__getitem__  s    c5!!5tHMM	6'',C ;3-S		002  # 	6455	6  	zz  C'sm#		s#   A% A> %A;>
B3&B..B3c                     U R                  U5      nUc  [        U5      eU R                  b:  Sn[        U R                  R                  R                  SU R                  -   5      nO\ U R                  R                  R                  U5      n[        U[        5      (       a  UR                  S5      n[        U5      nUS   n['        U R                  R                  X$5      nU R)                  U R                  UUSS9$ ! [         a    [        S5      ef = f! [         a+  nUR                   R"                  S:X  a  [%        U5      ee SnAff = f)	z=Retrieve a member from this collection without looking it up.r  N#r   r  r   F)r   r   )r  r  r(  r   collection_ofr   rN   r   
markup_urlr   r'   r?   r   r   r   r   r6  r.  r.   r  r   )r    r)   r   r   r   url_geterrorr   s           r!   r8  %CollectionWithKeyBasedLookup.__call__2  sC   	6'',C ;S/!) "N!(

  ++S43E3E-E"	**--11#6g{33%nnW5G!&w "00D!E

 0 0#J**JJ),1	 + 
 	
M # 	6455	68  >>((C/"3-'	s$   D  )AD  D
E#&E		ENc                     [        5       e)z2Transform the unique ID of an object into its URL.)r  )r    r)   s     r!   r  -CollectionWithKeyBasedLookup._get_url_from_idh  s    !##r$   r8   )r2   r3   r4   r5   r6   r0   r8  r  r  r7   r  r  s   @r!   r   r     s$    ,0
h M$ $r$   r   c                   .    \ rS rSrSrSS jrS rS rSrg)	r   im  z8The contents of a file hosted by a lazr.restful service.Nc                 8   UR                   R                  U l        US:X  a  Ub  [        S5      eUb  [        S5      eUR                  R                  R                  U R                  SS9u  pVUS   nUR                  S5      nUS   n[        U5      S	   n	[        U	R                  S
5      S   5      nO2US:X  a!  SnUc  [        S5      eUc  [        S5      eS nO[        S5      eXl	        X l
        X0l        X@l        Xpl        [        R                  " X5        g )Nr   z8Files opened for read access can't specify content_type.z4Files opened for read access can't specify filename.T)return_responser<  zlast-modifiedzcontent-location   r   r   r   r$   z8Files opened for write access must specify content_type.z4Files opened for write access must specify filename.r   )rO   r   r   rN   r   r'   r   r   splithosted_filer   r   r   last_modifiedr
   r"   )
r    r  r   r   r   r6  r/   r  content_locationpaths
             r!   r"   HostedFileBuffer.__init__p  sB   --113;' ,  # M  *//88<<$ = OH $N3L$LL9M  ((:;,-a0Dtzz#r23HS[E# ,   M  !MFGG&	( *%r$   c                    U R                   S:X  ae  SU R                  -  nU R                  R                  R                  R                  U R                  U R                  5       U R                  SU05        [        R                  " U 5        g )Nr   zattachment; filename="%s"zContent-Disposition)r   r   r  rN   r   putr   getvaluer   r
   close)r    dispositions     r!   r  HostedFileBuffer.close  sk    995EK""++//!!&4	 	dr$   c                 0    [         R                  " X5        g r   )r
   write)r    bs     r!   r  HostedFileBuffer.write  s    dr$   )r   r   r  r  r   r   r   )	r2   r3   r4   r5   r6   r"   r  r  r7   r8   r$   r!   r   r   m  s    B-&^	r$   r   )4r6   r)  __metaclass____all__email.messager	   ior
   jsonr   r   urllib.parser   r   r   r   r   ImportErrorurllibsysversion_infor   	text_typebytesr   unicodewadllib.applicationr   r  lazr.restfulclientr   lazr.restfulclient._browserr   r   lazr.restfulclient._jsonr   lazr.restfulclient.errorsr   lazr.urir   r   r-   r   r:   r   r   r   r   r   r   r   r   r8   r$   r!   <module>r     s"  & 0 "  *LL
 A!IKIK 8 * < 8 / 
( 6 0k!{ k!\	2( 2
 
Bv
( v
rb[ bJd:H d:NB BJU$: U$p>w >M"  *44))*s   C1 1DD