
    };e̷                        d Z eZg dZddlmZ ddlmZ ddlm	Z	m
Z
 	 ddlmZmZmZmZmZ dd	lZej(                  d   d
k\  reZeZneZeZddlmZ ddlmZ ddlm Z m!Z! ddl"m#Z# ddl$m%Z% ddl&m'Z'  e(       Z) G d d      Z* G d d      Z+ G d de+      Z G d de      Z, G d de      Z- G d de      Z. G d de+      Z/ G d d e      Z0 G d! d"e      Z1 G d# d$e1      Z2 G d% d&e      Z3y	# e$ r ddlmZmZmZ ddlmZmZ Y w xY w)'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                   $    e Zd ZdZd ZddZd Zy)HeaderDictionarya  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                     || _         y N)wrapped_dictionary)selfr   s     =/usr/lib/python3/dist-packages/lazr/restfulclient/resource.py__init__zHeaderDictionary.__init__K   s
    "4    Nc                 T    | j                   j                  |j                               S z2Retrieve a value, converting the key to lowercase.)r   getlower)r   keydefaults      r   r$   zHeaderDictionary.getN   s    &&**399;77r!   c                 X    | j                  |t              }|t        u rt        |      |S r#   )r$   missingKeyError)r   r&   values      r   __getitem__zHeaderDictionary.__getitem__R   s)    g&G3-r!   r   )__name__
__module____qualname____doc__r    r$   r,    r!   r   r   r   ?   s    	58r!   r   c                        e Zd ZdZdZd Zd Zy)RestfulBasez=Base class for classes that know about lazr.restful services.application/jsonc                     i }|j                         D ]5  \  }}t        |t              r|j                  }||| j	                  |      <   7 |S r   )items
isinstancer   	self_link_get_external_param_name)r   
dictionarynew_dictionaryr&   r+   s        r   _transform_resources_to_linksz)RestfulBase._transform_resources_to_links_   sU    $**, 	GJC%*AFN488=>	G r!   c                     |S )a  Turn 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.
        r1   r   
param_names     r   r9   z$RestfulBase._get_external_param_nameg   s
     r!   N)r-   r.   r/   r0   JSON_MEDIA_TYPEr<   r9   r1   r!   r   r3   r3   Z   s    G(Or!   r3   c                       e Zd ZdZd Z e       Z e       Z e       Ze	d        Z
e	d        Ze	d        Ze	d        Ze	d        ZeZd Zd	 Zd
 Zd Ze	 	 	 	 	 dd       ZddZd Zd Zd Zd Zd Zy)r   z+Base class for lazr.restful HTTP resources.c                 H    || }|| j                   d<   || j                   d<   y)z5Initialize with respect to a wadllib Resource object.N_root_wadl_resource)__dict__)r   rootwadl_resources      r   r    zResource.__init__u   s+    <D "&g*7&'r!   c                 8    | j                  | j                        S )z,Name the collections this resource links to.)_get_parameter_namesFIND_COLLECTIONSr   s    r   lp_collectionszResource.lp_collections   s     (()>)>??r!   c                 8    | j                  | j                        S )z(Name the entries this resource links to.)rI   FIND_ENTRIESrK   s    r   
lp_entrieszResource.lp_entries   s     (():):;;r!   c                 8    | j                  | j                        S )z'Name this resource's scalar attributes.)rI   FIND_ATTRIBUTESrK   s    r   lp_attributeszResource.lp_attributes   s     (()=)=>>r!   c                    g }| j                   j                  D ]  }|j                  j                         }|dk(  r|j                  j                  ddg      }nV|dk(  rQdD ]1  }|j                  j                  |      }|!|j                         } n j                  | j                         }D ].  }|j                  dk(  s|j                  |j                            |S )z.Name all of this resource's custom operations.r$   queryplainpost)!application/x-www-form-urlencodedmultipart/form-dataws.op)
rD   method_iternamer%   requestparamsget_representation_definitionresolve_definitionappendfixed_value)r   namesmethodr[   r]   
media_type
definitionparams           r   lp_operationszResource.lp_operations   s    
 ))55 	F;;$$&Du}../AB# 	J "(!M!M""J "-%/%B%B%D
	 $**4+>+>? ::(LL!2!23!	( r!   c                 d    | j                  | j                  | j                  | j                        S )z;A hook into dir() that returns web service-derived members.)rI   rJ   rN   rQ   rK   s    r   __members__zResource.__members__   s0     ((!!4#4#4d6J6J
 	
r!   c                    g }| j                   j                  | j                        D ]  }|j                  }|j                  }|dk7  rg|e|j
                  rY|j                  d      r$| j                  |v sN|j                  |dd        c| j                  |v sr|j                  |dd        | j                  |v s|j                  |        |S )z2Retrieve some subset of the resource's parameters.r8   N_collection_linki)rD   
parametersr@   r[   link
can_followendswithrJ   r`   rN   rQ   )r   kindsrb   	parameterr[   rn   s         r   rI   zResource._get_parameter_names   s    ,,778L8LM 	'I>>D>>D{"t'7DOO ==!34,,5T$3Z0 ((E1T#2Y/ ''50LL&9	': r!   c                 (    | j                  |      duS )z8Does this resource have a parameter with the given name?N)r9   r>   s     r   lp_has_parameterzResource.lp_has_parameter   s    ,,Z8DDr!   c                    | j                          dD ]{  }| j                  j                  ||z         }|$	 |j                          |j                          y|j
                  }| j                  | j                  ||j                        c S  | j                  j                  |      }|t	        d|z        |j                         S # t        $ r Y w xY w)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.
        )_linkrk   N)r?   zNo such parameter: %s)	_ensure_representationrD   get_parameter	get_valuer*   linked_resource_create_bound_resourcerC   r[   )r   r?   suffixrf   rz   s        r   lp_get_parameterzResource.lp_get_parameter   s     	##%3 	F''55j66IJE OO%
 ??$,  "'"7"722JJEJJ 3  	$ ##11*==2Z?@@  !    s   C	CCc                     d|i}| j                   j                  d|      }|| j                   j                  d|      }|t        d|z        t        | 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.
        rY   r$   )query_paramsrV   )representation_paramszNo operation with name: %s)rD   
get_methodr*   r   rC   )r   operation_namer]   rc   s       r   lp_get_named_operationzResource.lp_get_named_operation   sy     >*$$//F/K>((33f 4 F >7.HIIdjj$77r!   Nc                    |j                   }t        |      d   }	t        }
|j                  d      s||j                  d      rt        }
|j
                  j                  |	|
      }||j                  ||||      }n	  |||      S )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-pagerk   )representation_definition)type_urlr   r   rp   r   RESOURCE_TYPE_CLASSESr$   bind)clsrF   resourcerepresentationrepresentation_media_typerepresentation_needs_processingr   r?   r   resource_typer'   r_classs               r   r{   zResource._create_bound_resource  s    H $$ *2.W%"z':':;M'N G,,00H%  }})/*C	 % H tX&&r!   c                 L   ||| j                   _        i }|||d<   | j                  j                  j	                  | j                   |      }|| j                  j                  j
                  k(  ry| j                   j                  || j                        | j                  d<   y)&Update this resource's representation.NzIf-None-Match)headersrD   )	rD   _urlrC   _browserr$   NOT_MODIFIEDr   r@   rE   )r   new_urletagr   r   s        r   
lp_refreshzResource.lp_refreshH  s    '.D$'+GO$,,00 1 
 TZZ00=== +/*=*=*B*BD00+
&'r!   c                     	 | j                  |      S # t        $ r Y nw xY w	 | j                  |      S # t        $ r t        | d|d      w xY w)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__zResource.__getattr__[  sd    	..t44 			((.. 	 594@ 	s    	4 Ac                     | j                   j                  || j                        }|j                  }t	        |      dkD  r|D cg c]  }|j
                   c}S yc c}w )z0Find the set of possible values for a parameter.r   N)rD   rx   r@   optionslenr+   )r   r?   rr   r   options        r   lp_values_forzResource.lp_values_forh  sY    ''55,,
	 ##w<!/67VFLL77 8s   Ac                 Z    dD ]&  }||z   }| j                   j                  |      s$|c S  y)z>What's this parameter's name in the underlying representation?)rv   rk    N)rD   rx   )r   r?   r|   r[   s       r   r9   z!Resource._get_external_param_namer  s9    7 	F&D""006	 r!   c                 4   | j                   j                  | j                  j                  j	                  | j                         }t        |t              r|j                  d      }t        |      }t        |t              r`|d   }|Y|| j                   j                  k7  r@| j                  j                  j                  |      }|j                  | j                   _        | j                   j                  || j                  d      | j                   d<   yy)z5Make sure this resource has a representation fetched.Nutf-8resource_type_linkFr   rD   )rD   r   rC   r   r$   r7   binary_typedecoder   dictr   _wadlget_resource_typetagr   r@   rE   )r   r   	type_linkr   s       r   rw   zResource._ensure_representationz  s    --5!ZZ0044T5H5HIN.+6!/!6!6w!?">2N .$/*+?@	)!T%8%8%A%AA$(JJ$4$4$F$F!%M /<.?.?D''+.2.A.A.F.F$$05 /G /DMM*+3 6r!   c                     | |k(   S )zInequality operator.r1   r   others     r   __ne__zResource.__ne__  s    5=  r!   )Nr4   TNNNN)r-   r.   r/   r0   r    objectrJ   rN   rQ   propertyrL   rO   rR   rg   ri   __methods__rI   rt   r}   r   classmethodr{   r   r   r   r9   rw   r   r1   r!   r   r   r   r   s    58 x8LhO@ @ < < ? ?  6 
 
  K DE!>8  
 "4(,"&9' 9'v
&B!r!   r   c                        e Zd ZdZed        Zy)ScalarValuez.A resource representing a single scalar value.c                 N    | j                          | j                  j                  S )zReturn the scalar value.)rw   rD   r   rK   s    r   r+   zScalarValue.value  s"     	##%""111r!   N)r-   r.   r/   r0   r   r+   r1   r!   r   r   r     s    82 2r!   r   c                   *    e Zd ZdZddZd Zd Zd Zy)
HostedFilezAA resource representing a file managed by a lazr.restful service.Nc                 <    |dv rt        | |||      S t        d      )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   openzHostedFile.open  s'    :#D$hGGFGGr!   c                 v    | j                   j                  j                  | j                  j                         y)z Delete the file from the server.N)rC   r   deleterD   urlrK   s    r   r   zHostedFile.delete  s&    

""4#6#6#:#:;r!   c                     g S )z4HostedFile objects define no web service parameters.r1   )r   rq   s     r   rI   zHostedFile._get_parameter_names  s    	r!   c                 h    |duxr- | j                   j                  |j                   j                  k(  S )a  Equality 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)rD   r   r   s     r   __eq__zHostedFile.__eq__  s7      D##''5+?+?+C+CC	
r!   )r   NN)r-   r.   r/   r0   r   r   rI   r   r1   r!   r   r   r     s    KH<
r!   r   c                   j     e Zd ZdZeedZdddddej                  f fd	Z	e
d        Zd Zd Z xZS )	r   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	           	         |(|d   dk7  r|dz  }|t        |      z  }|d   dk7  r|dz  }t        |      | _        || _        || _        t        | ||||| j                  |      | _        | j                  j                  | j                        | _	        | j                  j                  d      }	|	j                  | j                  j                  |	      d      }
t        t        | ?  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   /r   r4   )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    zServiceRoot.__init__  s    " B3&#CL(LB3&#\* "2 &
 ]]77G
 

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

 	k4)$
;r!   c                    dt         z  }| j                  dk7  r| j                  dz   |z   dz   }t               }||d<   | j                  >| j                  j                  }t        |      D ]  }||   }|j                  ||d        |d   S )a  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   zServiceRoot._user_agent  s     /<!!R'11D8<G#ML) ,' $ 0 0 B B/0 <)#.!!#ul;< |$$r!   c                     t        ||||      S r   )r   )r   r   r   r   r   s        r   httpFactoryzServiceRoot.httpFactory!  s    :ugzBBr!   c                    t        |      }|j                  dk(  r1|dd dk(  r|dd }t        | j                  j	                  |            }| j
                  j                  |      }t        |t              r|j                  d      }	 t        |      }|j                  d      }|t        d|z        | j                  j                  j                  |      }t        | j                  j                  ||j                         }| j#                  | j                  ||d	d
      S # t        $ r t        d|z        w xY w)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.r4   Fr   )r   schemer   r   r`   r   r$   r7   r   r   r   r   rC   r   r   WadlResourcer   r{   )r   r   parseddocumentr   r   r   rG   s           r   loadzServiceRoot.load$  s<   #==B 2Aw#~!"gdnn++C01C==$$S)h,w/H	H"8_N #&&';<	=C  

((::9E$TZZ%5%5sM<M<MN**JJ,1 + 
 	
  	H@3FGG	Hs   	D% %D=)r-   r.   r/   r0   r   r   r   r   MAX_RETRIESr    r   r   r   r   __classcell__r   s   @r   r   r     sW     !" ''.<` % %0C
r!   r   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)r   z=A class for a named operation to be invoked with GET or POST.c                 .    || _         || _        || _        y)z/Initialize with respect to a WADL Method objectN)rF   r   wadl_method)r   rF   r   r   s       r   r    zNamedOperation.__init__G  s    	 &r!   c                    t        |      dkD  rt        d      | j                  j                  }| j	                  |      }| j                  j
                  }|dv r|j                  }nR|j                  d      }||j                  d      }|J d       |j                  | j                  j                        }|D ch c]5  }|j                  dk(  st        |j                        dkD  r|j                  7 }}|j                         D ]  \  }	}
|	|vst        |
t              ||	<     |dv r! | j                  j                   di |}d	}i }n=| j                  j!                         } | j                  j"                  di |\  }}d
|i}| j$                  j&                  j)                  |||j+                         |      \  }}|j,                  dk(  r| j/                  |||      S |dk(  rW|j,                  dk(  r.|d   }| j$                  j&                  j)                  |      \  }}n| j                  j1                          | j3                  |||      S c c}w )z)Invoke the method and process the result.r   z(Method must be called with keyword args.)r$   headr   rX   rW   zYA POST named operation must define a multipart or form-urlencoded request representation.binary)r   r   zContent-type)extra_headers   rV   -  locationr1   )r   	TypeErrorr   r[   r<   r\   r   r^   r]   r   rD   typer   r6   r   r   build_request_urlbuild_representationrF   r   _requestupperstatus_handle_201_responser   _handle_200_response)r   argskwargshttp_methodr\   r]   re   rf   send_as_is_paramsr&   r+   r   in_representationr   rd   responsecontents                    r   __call__zNamedOperation.__call__M  sy   t9q=FGG&&++11&9""**33))F >>%J !$BB7
 ) :)  &&t}}'C'CDF  
zzX%U]]);a)? JJ
 

 **, 	BJC ++!%-@AS		B 334$""44<t<C "M""446C 6  55==!+Z8M !II..77'	 8 
' ??c!,,S(GDDf$ ??c)":.C(,		(:(:(C(CC(H%HgMM,,.,,S(GDDc
s   >:Ic                 F   | j                   j                  j                  t        |            }|j	                  d      }|j
                  }| j                  j                  j                  |j                        \  }}t        j                  | j                  |||d         S )z5Handle the creation of a new resource by fetching it.Locationcontent-type)r   r  r   r   rx   rz   rF   r   r  r   r   r{   )r   r   r  r  wadl_responsewadl_parameterrG   s          r   r	  z#NamedOperation._handle_201_response  s    ((1166X&
 '44Z@&66 II..778I8IJ' ..II}gx/G
 	
r!   c                    |d   }| j                   j                  }|j                  |      }|=|| j                  k(  r,t	        |t
              r|j                  d      }t        |      S |S t	        |t
              r|j                  d      }t        |      }||S d|v r]d|v rY|d   }| j                  j                  j                  |d         }t        | j                  j                  ||j                        }	n;|j                         }t        | j                  j                  ||j                        }	t        j                  | j                  |	||d|      S )z)Process the return value of an operation.r  r   r8   r   F)r   r   )r   r  r^   r@   r7   r   r   r   rF   r   r   r   r   r_   r   r{   )
r   r   r  r  r   response_definitionr   r   r   rG   s
             r   r
  z#NamedOperation._handle_200_response  sa   /"..77==lK 	" %, t333g{3%nnW5GW~%N g{+nnW-G>O("';x'G
 ;'C IIOO==-.M )		m&7&7M *<<> & )		&?&C&CM ..II,1&? / 
 	
r!   c                     |S )z/Named operation parameter names are sent as is.r1   r>   s     r   r9   z'NamedOperation._get_external_param_name  s    r!   N)	r-   r.   r/   r0   r    r  r	  r
  r9   r1   r!   r   r   r   D  s"    G'HET
<
|r!   r   c                   \     e Zd ZdZ fdZd Zd Zd Z fdZd Z	d Z
d fd		Zd
 Z xZS )r   zBA class for an entry-type resource that can be updated with PATCH.c                 t    t         t        |   ||       i | j                  d<   t         t        |   ||       y )N_dirty_attributes)r   r   r    rE   r   rF   rG   r   s      r   r    zEntry.__init__  s6    eT#D-8 .0)*eT#D-8r!   c                 b    dt        | j                        j                  d| j                  dS )z:Return the WADL resource type and the URL to the resource.<z at >)r   r   fragmentr8   rK   s    r   __repr__zEntry.__repr__  s)     ''(11NN
 	
r!   c                 r    | j                   j                  j                  t        | j                              S )zDelete the resource.)rC   r   r   r   r8   rK   s    r   	lp_deletezEntry.lp_delete  s&    zz""))#dnn*=>>r!   c                     | j                   S )zReturn the URL to the resource.)r8   rK   s    r   __str__zEntry.__str__  s    ~~r!   c                 n    |dk7  r|| j                   v r| j                   |   S t        t        |   |      S )z-Try to retrive a parameter of the given name.r  )r  r   r   r   )r   r[   r   s     r   r   zEntry.__getattr__
  s=    &&t-----d33UD-d33r!   c                     | j                  |      s&t        d| j                  j                  d|d      || j                  |<   y)z$Set the parameter of the given name.r   z' object has no attribute 'N)rt   r   r   r-   r  )r   r[   r+   s      r   __setattr__zEntry.__setattr__  sC    $$T* >>**D2  (-t$r!   c                     |duxrO | j                   |j                   k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  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_etagr  r   s     r   r   zEntry.__eq__  sY      B%//1B%//1B &&%*A*AA		
r!   c                 |    t        | dd      }t        t        |   ||       | j                  j                          y)r   r,  N)getattrr   r   r   r  clear)r   r   r   r   s      r   r   zEntry.lp_refresh(  s4    t[$/eT%gt4$$&r!   c                 @   | j                  | j                        }i }t        | dd      }|||d<   | j                  j                  j                  t        | j                        ||      \  }}|j                  dk(  r| j                  |d          | j                  j                          |d   }|j                  dk(  r_|| j                  k(  rOt        |t              r|j                  d      }t        |      }|| j                   _        || j                   _        yyy)	zSave changes to the entry.r,  NzIf-Matchr   r  r     r   )r<   r  r.  rC   r   patchr   r8   r  r   r/  r@   r7   r   r   r   rD   r   rd   )r   r   r   r   r  r  r   new_representations           r   lp_savezEntry.lp_save.  s   ;;""
 t[$/"&GJ
 !JJ//55
' ??c!OOHZ01$$&/??c!ld6J6J&J ';/!..1!&w1CD.-9D* 'K!r!   r   )r-   r.   r/   r0   r    r#  r%  r'  r   r*  r   r   r4  r   r   s   @r   r   r     s3    L9
?4-
':r!   r   c                   F     e Zd ZdZ fdZd Zd Zd Zd Zd Z	d Z
 xZS )	r   z4A collection-type resource that supports pagination.c                 .    t         t        |   ||       y)zCreate a collection object.N)r   r   r    r  s      r   r    zCollection.__init__S  s    j$(}=r!   c                     | j                   }t        |t              r|S t        |t              r|j                  S t        d      )znThe number of items in the collection.

        :return: length of the collection
        :rtype: int
        z collection size is not available)
total_sizer7   intr   r+   r  )r   r8  s     r   __len__zCollection.__len__W  sC     __
j#& 
K0 ###>??r!   c              #     K   | j                          | j                  j                  }	 | j                  |j	                  di             D ]  }|  |j	                  d      }|y| j
                  j                  j	                  t        |            }t        |t              r|j                  d      }t        |      }w)zqIterate over the items in the collection.

        :return: iterator
        :rtype: sequence of `Entry`
        entriesnext_collection_linkNr   )rw   rD   r   _convert_dicts_to_entriesr$   rC   r   r   r7   r   r   r   )r   current_pager   	next_linknext_gets        r   __iter__zCollection.__iter__i  s      	##%**99 ::  B/   %(()?@I zz**..s9~>H(K0#??73 ?L s   CCc                     t        |t              r| j                  |      S | j                  t        ||dz               }t        |      dk7  rt	        d      |d   S )ac  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   )r7   slice
_get_slicer   
IndexError)r   r&   found_slices      r   r,   zCollection.__getitem__~  s[     c5!??3'' //%S1W*=>K;1$ !:;;q>!r!   c                    |j                   xs d}|j                  }|dk  rt        d      |dk  rt        d      | j                  j                  }|8|t        |d         k  r'|d   }t        |      }||| }|j                  d      }n+d}g }| j                  | j                  j                  d|      }||z
  }	|	t        |      z
  }
|
dkD  r|| j                  j                  j                  |      }t        |t              r|j                  d      }t        |      }|d   }||d|
 z  }|	t        |      z
  }
|j                  d      }|n2|t        |      }|
dkD  r|
|k  r| j                  |d	|
      }|
dkD  r||j                  |dd|j                     }| j!                  |      D cg c]  }| c}S c c}w )
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.Nr<  r=  zws.startr   zws.size)startstopr   rD   r   r   r$   _with_url_query_variable_setr   rC   r   r7   r   r   r   stepr>  )r   rD  rI  rJ  existing_representation
entry_pagefirst_page_sizeentry_dictspage_urldesired_sizemore_neededpage_getr   current_page_entriesr   s                  r   rE  zCollection._get_slice  s    qzz19K  !8) 
 #'"5"5"D"D".53#I.<
 4
& 1;J!*oO$U40K.223IJH #OK88##''UH e|"S%55 Ao("6zz**..x8H(K0#??73"8_N#1)#< /==K&[)99K%))*@AH &"%&:";Q;#@  <<i1 Ao("68 ::!%mm4K
 !::;G
 
 	
 
s   5	Gc              #   0  K   |D ]  }|d   }|d   }| j                   j                  }|j                  |      }t        | j                   j                  ||j                        }t
        j                  | j                  ||| j                  d        yw)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.
        r8   r   FN)	rD   applicationr   r   r   r   r{   rC   r@   )r   r<  
entry_dictresource_urlr   wadl_applicationr   r   s           r   r>  z$Collection._convert_dicts_to_entries  s      " 	J%k2L!+,@!A#22>>,>>"M $##//!!H
 11

Hj$2F2F 	s   BBc                     t        |      }|j                  i }nt        |j                        }t        |      ||<   t	        |d      |_        t        |      S )z1A helper method to set a query variable in a URL.T)r   rT   r   r   r   )r   r   variable	new_valueurir]   s         r   rK  z'Collection._with_url_query_variable_set	  sM    #h99Fcii(Fy>xfd+	3xr!   )r-   r.   r/   r0   r    r:  rB  r,   rE  r>  rK  r   r   s   @r   r   r   P  s,    >>@$+*"$[
z8	r!   r   c                   2     e Zd ZdZ fdZd ZdZd Z xZS )r   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                 Z   t        |t              rt        t        |   |      S 	 | j                  |      }|t        |       | |      }	 |j                          |S # t        $ r t        d      w xY w# t        $ r*}|j                  j                  dk(  rt        |       d}~ww xY w)z8Look up a slice, or a subordinate resource by unique ID.unsubscriptable objectN  )r7   rD  r   r   r,   _get_url_from_idNotImplementedErrorr  r*   rw   r   r  r  )r   r&   r   shim_resourceer   s        r   r,   z(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 A7 A47	B* %B%%B*c                    	 | j                  |      }|t        |      | j                  :d}t        | j                  j                  j                  d| j                  z         }nW	 | j                  j                  j                  |      }t        |t              r|j                  d      }t        |      }|d   }t'        | j                  j                  ||      }| j)                  | j                  ||d      S # t        $ r t        d      w xY w# t        $ r*}|j                   j"                  dk(  rt%        |       d}~ww xY w)	z=Retrieve a member from this collection without looking it up.ra  N#r   rb  r   F)r   r   )rc  rd  r  r   collection_ofr   rC   r   
markup_urlr   r$   r7   r   r   r   r   r  r  r*   r   r{   )r   r&   r   r   r   url_geterrorr   s           r   r  z%CollectionWithKeyBasedLookup.__call__2  sC   	6'',C ;S/!) "N!(

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

 0 0#7IJ**JJ),1	 + 
 	
M # 	6455	68  >>((C/"3-'	s$   C= 'AD =D	E%EENc                     t               )z2Transform the unique ID of an object into its URL.)rd  )r   r&   s     r   rc  z-CollectionWithKeyBasedLookup._get_url_from_idh  s    !##r!   )	r-   r.   r/   r0   r,   r  ri  rc  r   r   s   @r   r   r     s    ,0
h M$r!   r   c                   $    e Zd ZdZddZd Zd Zy)r   z8The contents of a file hosted by a lazr.restful service.Nc                 >   |j                   j                  | _        |dk(  r|t        d      |t        d      |j                  j                  j                  | j                  d      \  }}|d   }|j                  d      }|d   }t        |      d	   }	t        |	j                  d
      d         }n/|dk(  rd}|t        d      |t        d      d }nt        d      || _	        || _
        || _        || _        || _        t        j                  | |       y )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   )rD   r   r   rC   r   r$   r   r   splithosted_filer   r   r   last_modifiedr
   r    )
r   rs  r   r   r   r  r+   rt  content_locationpaths
             r   r    zHostedFileBuffer.__init__p  sP   --113;' ,  # M  *//88<<$ = OHe $N3L$LL9M  ((:;,-a0Dtzz#r23HS[E# ,   M  !MFGG&	( *u%r!   c                    | j                   dk(  red| j                  z  }| j                  j                  j                  j                  | j                  | j                         | j                  d|i       t        j                  |        y )Nr   zattachment; filename="%s"zContent-Disposition)r   r   rs  rC   r   putr   getvaluer   r
   close)r   dispositions     r   rz  zHostedFileBuffer.close  sk    995EK""++//!!&4	 	dr!   c                 0    t        j                  | |       y r   )r
   write)r   bs     r   r}  zHostedFileBuffer.write  s    dAr!   r   )r-   r.   r/   r0   r    rz  r}  r1   r!   r   r   r   m  s    B-&^	r!   r   )4r0   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   r3   r   r   r   r   r   r   r   r   r1   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   C0 0D
	D
