
&ˉTc           @   sv  d  Z  e Z d d g Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 m Z d d l m Z m Z d d l Z d d l m Z d d l m 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 m Z m Z d   Z  d e f d     YZ! d e" f d     YZ# d e# f d     YZ$ d d d     YZ% d S(   s   Browser object to make requests of lazr.restful web services.

The `Browser` class does some massage of HTTP requests and responses,
and handles custom caches. It is not part of the public
lazr.restfulclient API. (But maybe it should be?)
t   Browsert   RestfulHttpiN(   t   sleep(   t   Httpt   urlnorm(   t   StringIO(   t	   urlencode(   t   Application(   t   URI(   t	   error_fort	   HTTPError(   t   DatetimeJSONEncoder(   t   _md5t   re_url_schemet   re_slashc         C   s	  yU t  j |   rT t |  t  rB |  j d  }  |  j d  }  qT |  j d  }  n  Wn t k
 rh n Xt |  t  r |  j d  }  n  t |   j	   } t  j
 d |   }  t j
 d |   }  t j } | d d } t |   | k r |  |  }  n  d j |  | f  S(   s   Return a filename suitable for the cache.

    Strips dangerous and common characters to create a filename we
    can use to store the cache in.
    s   utf-8t   idnat    t   ,i    i   (   R   t   matcht
   isinstancet   strt   decodet   encodet   UnicodeErrort   unicodeR   t	   hexdigestt   subR   R   t   maximum_cache_filename_lengtht   lent   join(   t   filenamet   filemd5t   maximum_filename_lengtht   maximum_length_before_md5_sum(    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyt   safename9   s$    	c           B   s;   e  Z d  Z d Z d d d d d  Z d   Z d   Z RS(   s   An Http subclass with some custom behavior.

    This Http client uses the TE header instead of the Accept-Encoding
    header to ask for compressed representations. It also knows how to
    react when its cache is a MultipleRepresentationCache.
    i   c         C   sK   t  t |   j | | |  | |  _ |  j d  k	 rG |  j j |   n  d  S(   N(   t   superR   t   __init__t
   authorizert   Nonet   authorizeSession(   t   selfR%   t   cachet   timeoutt
   proxy_info(    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyR$   n   s    	c
   
   
   C   sr   | j  d  r | d =n  |  j d k	 rD |  j j | | | |  n  t t |   j | | | | | | | | |	 	 S(   s4   Use the authorizer to authorize an outgoing request.t   authorizationN(   t   has_keyR%   R&   t   authorizeRequestR#   R   t   _request(
   R(   t   connt   hostt   absolute_urit   request_urit   methodt   bodyt   headerst   redirectionst   cachekey(    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyR/   u   s    
	c         C   s)   t  |  j t  r% |  j j | |  Sd S(   s+   Retrieve a cached value for an HTTP header.N(   R   R)   t   MultipleRepresentationCachet   _getCachedHeaderR&   (   R(   t   urit   header(    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyR:      s    N(   t   __name__t
   __module__t   __doc__R   R&   R$   R/   R:   (    (    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyR   d   s   		t   AtomicFileCachec           B   sD   e  Z d  Z d Z e d  Z d   Z d   Z d   Z d   Z	 RS(   s   A FileCache that can be shared by multiple processes.

    Based on a patch found at
    <http://code.google.com/p/httplib2/issues/detail?id=125>.
    s   .tempc         C   sd   t  j j |  |  _ | |  _ y t  j |  j  Wn+ t k
 r_ } | j t j k r`   q` n Xd S(   sU  Construct an ``AtomicFileCache``.

        :param cache: The directory to use as a cache.
        :param safe: A function that takes a key and returns a name that's
            safe to use as a filename.  The key must never return a string
            that begins with ``TEMPFILE_PREFIX``.  By default uses
            ``safename``.
        N(	   t   ost   patht   normpatht
   _cache_dirt   _get_safe_namet   makedirst   OSErrort   errnot   EEXIST(   R(   R)   t   safet   e(    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyR$      s    		c         C   sM   |  j  |  } | j |  j  r7 t d |  j   n  t j j |  j |  S(   s0   Return the path on disk where ``key`` is stored.s    Cache key cannot start with '%s'(   RE   t
   startswitht   TEMPFILE_PREFIXt
   ValueErrorRA   RB   R   RD   (   R(   t   keyt   safe_key(    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyt   _get_key_path   s
    c         C   sv   |  j  |  } y/ t | d  } z | j   SWd | j   XWn1 t t f k
 rq } | j t j k rr   qr n Xd S(   s  Get the value of ``key`` if set.

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

        :param key: The key to retrieve.  Must be either bytes or unicode
            text.
        :return: The value of ``key`` if set, None otherwise.
        t   rbN(   RQ   t   opent   readt   closet   IOErrorRG   RH   t   ENOENT(   R(   RO   t   cache_full_patht   fRK   (    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyt   get   s    c         C   s   t  j d |  j d |  j  \ } } t j | d  } | j |  | j   |  j |  } t	 j
 d k r t j j |  r t j |  n  t j | |  d S(   s   Set ``key`` to ``value``.

        :param key: The key to set.  Must be either bytes or unicode text.
        :param value: The value to set ``key`` to.  Must be bytes.
        t   prefixt   dirt   wbt   win32N(   t   tempfilet   mkstempRM   RD   RA   t   fdopent   writeRU   RQ   t   syst   platformRB   t   existst   unlinkt   rename(   R(   RO   t   valuet   handlet	   path_nameRY   RX   (    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyt   set   s    	
!c         C   sR   |  j  |  } y t j |  Wn+ t k
 rM } | j t j k rN   qN n Xd S(   s   Delete ``key`` from the cache.

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

        :param key: The key to delete.  Must be either bytes or unicode text.
        N(   RQ   RA   t   removeRG   RH   RW   (   R(   RO   RX   RK   (    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyt   delete   s    (
   R=   R>   R?   RM   R"   R$   RQ   RZ   Rk   Rm   (    (    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyR@      s   			R9   c           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   s>  A cache that can hold different representations of the same resource.

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

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

    This class is very much not thread-safe, but FileCache isn't
    thread-safe anyway.
    c         C   s)   t  t |   j | |  j  d |  _ d S(   s>   Tell FileCache to call append_media_type when generating keys.N(   R#   R9   R$   t   append_media_typeR&   t   request_media_type(   R(   R)   (    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyR$      s    c         C   s-   |  j  d k	 r# | d |  j  } n  t |  S(   s   Append the request media type to the cache key.

        This ensures that representations of the same resource will be
        cached separately, so long as they're served as different
        media types.
        t   -N(   Ro   R&   R"   (   R(   RO   (    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyRn      s    c   
      C   s~   t  |  \ } } } } |  j |  } | d } | d k	 rz x: t |  D]) }	 |	 j |  rJ |	 t |  j   SqJ Wn  d S(   s+   Retrieve a cached value for an HTTP header.t   :N(   R   RZ   R&   R   RL   R   t   strip(
   R(   R;   R<   t   schemet	   authorityR3   R8   t   cached_valuet   header_startt   line(    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyR:     s    
(   R=   R>   R?   R$   Rn   R:   (    (    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyR9      s   		c           B   s   e  Z d  Z e   Z d Z d d d d e d  Z d   Z d d d d d  Z	 d e
 d  Z d   Z d	   Z d d
  Z d   Z d d  Z RS(   s6   A class for making calls to lazr.restful web services.i   c         C   s}   | d k r. t j   } t j t j |  n  t | t  rL t	 |  } n  | j
 | | | |  |  _ | |  _ | |  _ d S(   s   Initialize, possibly creating a cache.

        If no cache is provided, a temporary directory will be used as
        a cache. The temporary directory will be automatically removed
        when the Python process exits.
        N(   R&   R_   t   mkdtempt   atexitt   registert   shutilt   rmtreeR   t
   basestringR9   t   httpFactoryt   _connectiont
   user_agentt   max_retries(   R(   t   service_roott   credentialsR)   R*   R+   R   R   (    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyR$     s    	c   	   	   C   s   x t  d |  j d  D]p } |  j j | d | d | d | \ } } | j d	 k r | |  j k  r t d | d  } t |  q Pq W| | f S(
   Ni    i   R4   R5   R6   i  i  i   (   i  i  (   t   rangeR   R   t   requestt   statust   intR   (	   R(   t   urlR4   R5   R6   t   retry_countt   responset   contentt	   sleep_for(    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyt   _request_and_retry/  s    	!t   GETs   application/jsonc   
      C   sJ  | d k r t  d   n  i | d 6} |  j d k	 rG |  j | d <n  t |  j j t  rn | |  j j _ n  | d k	 r | j |  n  |  j	 t
 |  d | d | d | \ } } | j d k r| d	 k r	d
 | k s d | k r | |  j f St | |   n	 d | _ | | f St | |  }	 |	 d k	 r@|	  n  | | f S(   s'   Create an authenticated request object.s   tag:launchpad.net:2008:redactedsU   You tried to access a resource that you don't have the server-side permission to see.t   Accepts
   User-AgentR4   R5   R6   i0  R   s   If-None-Matchs   If-Modified-Sincei   N(   RN   R   R&   R   R   R)   R9   Ro   t   updateR   R   R   t   NOT_MODIFIEDR
   R	   (
   R(   R   t   dataR4   t
   media_typet   extra_headersR6   R   R   t   error(    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyR/   >  s.    '		
	c         C   sh   t  | t t f  r | } n | j d  } | j   } |  j | d | \ } } | rd | | f S| S(   s2   GET a representation of the given resource or URI.RZ   R   (   R   R}   R   t
   get_methodt   build_request_urlR/   (   R(   t   resource_or_uriR6   t   return_responseR   R4   R   R   (    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyRZ   {  s    	
c         C   s4   d } |  j  | d | \ } } t t |  |  S(   s?   GET a WADL representation of the resource at the requested url.s   application/vnd.sun.wadl+xmlR   (   R/   R   R   (   R(   R   t	   wadl_typeR   R   (    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyt   get_wadl_application  s    c         K   s)   | | d <t  |  } |  j | | d  S(   s"   POST a request to the web service.s   ws.opt   POST(   R   R/   (   R(   R   t   method_namet   kwsR   (    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyt   post  s    
c         C   sB   i | d 6} | d k	 r) | j |  n  |  j | | d d | S(   s(   PUT the given representation to the URL.s   Content-Typet   PUTR   N(   R&   R   R/   (   R(   R   t   representationR   R6   R   (    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyt   put  s
    c         C   s   |  j  | d d d S(   s%   DELETE the resource at the given URL.R4   t   DELETEN(   R/   R&   (   R(   R   (    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyRm     s    c         C   s   i d d 6} | d k	 r) | j |  n  |  j j t |  d  } | d k	 rj |  j j rj | | d <n  |  j | t j | d t	 d d | S(	   s8   PATCH the object at url with the updated representation.s   application/jsons   Content-Typet   etags   If-Matcht   clst   PATCHR   N(
   R&   R   R   R:   R   t   ignore_etagR/   t
   simplejsont   dumpsR   (   R(   R   R   R6   R   t   cached_etag(    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyt   patch  s    N(   R=   R>   R?   t   objectR   t   MAX_RETRIESR&   R$   R   R/   t   FalseRZ   R   R   R   Rm   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyR      s   		<			(    (&   R?   t   typet   __metaclass__t   __all__Ry   RH   RA   R{   Rc   R_   t   timeR   t   httplib2R   R   R   t	   cStringIOR   t   urllibR   t   wadllib.applicationR   t   lazr.uriR   t   errorsR	   R
   t   _jsonR   R   R   R   R"   R   R   R@   R9   R    (    (    (    s?   /usr/lib/python2.7/dist-packages/lazr/restfulclient/_browser.pyt   <module>   s0   		+*\-