
qJ[c           @   s"  d  Z  d d l Z d d l Z d d l Z d d l m Z 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 d d l m Z d d l m Z d d l m Z m Z m Z m Z m Z m Z d d l m  Z  m! Z! m" Z" m# Z# m$ Z$ m% Z% m& Z& m' Z' m( Z( m) Z) d d l* m+ Z+ m, Z, m- Z- m. Z. m/ Z/ m0 Z0 m1 Z1 m2 Z2 m3 Z3 m4 Z4 m5 Z5 m6 Z6 m7 Z7 d Z8 d Z9 e j: e;  Z< d e= f d     YZ> d e= f d     YZ? d e? f d     YZ@ d e> e? f d     YZA d e= f d     YZB d S(   s`   
requests.models
~~~~~~~~~~~~~~~

This module contains the primary objects that power Requests.
iN(   t   BytesIOt   UnsupportedOperationi   (   t   default_hooks(   t   CaseInsensitiveDict(   t   HTTPBasicAuth(   t   cookiejar_from_dictt   get_cookie_header(   t   RequestField(   t   encode_multipart_formdata(   t	   parse_url(   t   DecodeError(   t	   HTTPErrort   RequestExceptiont   MissingSchemat
   InvalidURLt   ChunkedEncodingErrort   ContentDecodingError(
   t   guess_filenamet   get_auth_from_urlt   requote_urit   stream_decode_response_unicodet   to_key_val_listt   parse_header_linkst   iter_slicest   guess_json_utft	   super_lent   to_native_string(   t	   cookielibt
   urlunparset   urlsplitt	   urlencodet   strt   bytest   StringIOt   is_py2t   chardett   jsont   builtin_strt
   basestringt   IncompleteReadi
   i   i   t   RequestEncodingMixinc           B   s5   e  Z e d     Z e d    Z e d    Z RS(   c         C   ss   g  } t  |  j  } | j } | s- d } n  | j |  | j } | rf | j d  | j |  n  d j |  S(   s   Build the path URL to use.t   /t   ?t    (   R   t   urlt   patht   appendt   queryt   join(   t   selfR,   t   pR-   R/   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyt   path_url*   s    			c         C   s  t  |  t t f  r |  St |  d  r, |  St |  d  rg  } x t |   D] \ } } t  | t  sy t | d  r | g } n  xl | D]d } | d k	 r | j t  | t  r | j d  n | t  | t  r | j d  n | f  q q WqN Wt	 | d t
 S|  Sd S(   s   Encode parameters in a piece of data.

        Will successfully encode parameters when passed as a dict or a list of
        2-tuples. Order is retained if data is a list of 2-tuples but arbitrary
        if parameters are supplied as a dict.
        t   readt   __iter__s   utf-8t   doseqN(   t
   isinstanceR   R    t   hasattrR   R&   t   NoneR.   t   encodeR   t   True(   t   datat   resultt   kt   vst   v(    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyt   _encode_params?   s     	!3c         C   sr  |  s t  d   n t | t  r3 t  d   n  g  } t | pE i   } t |  pW i   }  x | D] \ } } t | t  s t | d  r | g } n  x | D] } | d k	 r t | t  s t |  } n  | j t | t  r | j	 d  n | t | t  r| j
 d  n | f  q q Wqd Wx'|  D]\ } } d } d }	 t | t t f  rt |  d k r| \ }
 } qt |  d k r| \ }
 } } q| \ }
 } } }	 n t |  p| }
 | } t | t  rt |  } n  t | t  rt |  } n  t d | d | j   d	 |
 d
 |	  } | j d |  | j |  q3Wt |  \ } } | | f S(   s  Build the body for a multipart/form-data request.

        Will successfully encode files when passed as a dict or a list of
        2-tuples. Order is retained if data is a list of 2-tuples but arbitrary
        if parameters are supplied as a dict.

        s   Files must be provided.s   Data must not be a string.R5   s   utf-8i   i   t   nameR<   t   filenamet   headerst   content_typeN(   t
   ValueErrorR7   R&   R   R8   R9   R    R   R.   t   decodeR:   t   tuplet   listt   lenR   R!   R    R   R4   t   make_multipartR   (   t   filesR<   t
   new_fieldst   fieldst   fieldt   valR@   R>   t   ftt   fht   fnt   fpt   rft   bodyRE   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyt   _encode_filesZ   sL    	!3(   t   __name__t
   __module__t   propertyR3   t   staticmethodRA   RW   (    (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyR(   )   s   t   RequestHooksMixinc           B   s   e  Z d    Z d   Z RS(   c         C   s   | |  j  k r" t d |   n  t | t j  rK |  j  | j |  n0 t | d  r{ |  j  | j d   | D  n  d S(   s   Properly register a hook.s1   Unsupported event specified, with event name "%s"R5   c         s   s'   |  ] } t  | t j  r | Vq d  S(   N(   R7   t   collectionst   Callable(   t   .0t   h(    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pys	   <genexpr>   s    N(   t   hooksRF   R7   R]   R^   R.   R8   t   extend(   R1   t   eventt   hook(    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyt   register_hook   s    c         C   s5   y |  j  | j |  t SWn t k
 r0 t SXd S(   si   Deregister a previously registered hook.
        Returns True if the hook existed, False if not.
        N(   Ra   t   removeR;   RF   t   False(   R1   Rc   Rd   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyt   deregister_hook   s
    (   RX   RY   Re   Rh   (    (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyR\      s   	t   Requestc        
   B   sD   e  Z d  Z d d d d d d d d d d 	 Z d   Z d   Z RS(   s  A user-created :class:`Request <Request>` object.

    Used to prepare a :class:`PreparedRequest <PreparedRequest>`, which is sent to the server.

    :param method: HTTP method to use.
    :param url: URL to send.
    :param headers: dictionary of headers to send.
    :param files: dictionary of {filename: fileobject} files to multipart upload.
    :param data: the body to attach the request. If a dictionary is provided, form-encoding will take place.
    :param params: dictionary of URL parameters to append to the URL.
    :param auth: Auth handler or (user, pass) tuple.
    :param cookies: dictionary or CookieJar of cookies to attach to this request.
    :param hooks: dictionary of callback hooks, for internal usage.

    Usage::

      >>> import requests
      >>> req = requests.Request('GET', 'http://httpbin.org/get')
      >>> req.prepare()
      <PreparedRequest [GET]>

    c
         C   s	  | d  k r g  n | } | d  k r* g  n | } | d  k rB i  n | } | d  k rZ i  n | } |	 d  k rr i  n |	 }	 t   |  _ x6 t |	 j    D]" \ }
 } |  j d |
 d |  q W| |  _ | |  _ | |  _ | |  _	 | |  _
 | |  _ | |  _ | |  _ d  S(   NRc   Rd   (   R9   R   Ra   RI   t   itemsRe   t   methodR,   RD   RL   R<   t   paramst   autht   cookies(   R1   Rk   R,   RD   RL   R<   Rl   Rm   Rn   Ra   R>   R@   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyt   __init__   s     							c         C   s   d |  j  S(   Ns   <Request [%s]>(   Rk   (   R1   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyt   __repr__   s    c         C   sh   t    } | j d |  j d |  j d |  j d |  j d |  j d |  j d |  j d |  j	 d	 |  j
  	| S(
   sX   Constructs a :class:`PreparedRequest <PreparedRequest>` for transmission and returns it.Rk   R,   RD   RL   R<   Rl   Rm   Rn   Ra   (   t   PreparedRequestt   prepareRk   R,   RD   RL   R<   Rl   Rm   Rn   Ra   (   R1   R2   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyRr      s    										
N(   RX   RY   t   __doc__R9   Ro   Rp   Rr   (    (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyRi      s   	Rq   c        
   B   s   e  Z d  Z d   Z d d d d d d d d d d 	 Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d	   Z d
 d  Z d   Z d   Z RS(   s  The fully mutable :class:`PreparedRequest <PreparedRequest>` object,
    containing the exact bytes that will be sent to the server.

    Generated from either a :class:`Request <Request>` object or manually.

    Usage::

      >>> import requests
      >>> req = requests.Request('GET', 'http://httpbin.org/get')
      >>> r = req.prepare()
      <PreparedRequest [GET]>

      >>> s = requests.Session()
      >>> s.send(r)
      <Response [200]>

    c         C   s=   d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ t   |  _ d  S(   N(   R9   Rk   R,   RD   t   _cookiesRV   R   Ra   (   R1   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyRo     s    					c
   
      C   sh   |  j  |  |  j | |  |  j |  |  j |  |  j | |  |  j | |  |  j |	  d S(   s6   Prepares the entire request with the given parameters.N(   t   prepare_methodt   prepare_urlt   prepare_headerst   prepare_cookiest   prepare_bodyt   prepare_autht   prepare_hooks(
   R1   Rk   R,   RD   RL   R<   Rl   Rm   Rn   Ra   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyRr     s    c         C   s   d |  j  S(   Ns   <PreparedRequest [%s]>(   Rk   (   R1   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyRp   *  s    c         C   sa   t    } |  j | _ |  j | _ |  j j   | _ |  j j   | _ |  j | _ |  j | _ | S(   N(   Rq   Rk   R,   RD   t   copyRt   RV   Ra   (   R1   R2   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyR|   -  s    	c         C   s1   | |  _  |  j  d k	 r- |  j  j   |  _  n  d S(   s   Prepares the given HTTP method.N(   Rk   R9   t   upper(   R1   Rk   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyRu   7  s    	c      
   C   sn  y t  |  } Wn- t k
 r/ t |  } n t k
 r? n Xd | k ro | j   j d  ro | |  _ d St |  \ } } } } } } }	 | s t d j	 |    n  | s t
 d |   n  y | j d  j d  } Wn t k
 rt
 d   n X| pd	 }
 |
 r"|
 d
 7}
 n  |
 | 7}
 | rI|
 d t |  7}
 n  | sXd } n  t rt | t  r| j d  } n  t |
 t  r|
 j d  }
 n  t | t  r| j d  } n  t | t  r| j d  } n  t |	 t  r|	 j d  }	 qn  |  j |  } | r=| r4d | | f } q=| } n  t t | |
 | d | |	 g   } | |  _ d S(   s   Prepares the given HTTP URL.t   :t   httpNsD   Invalid URL {0!r}: No schema supplied. Perhaps you meant http://{0}?s    Invalid URL %r: No host suppliedt   idnas   utf-8s   URL has an invalid label.R+   t   @R)   s   %s&%s(   t   unicodet	   NameErrorR   t   UnicodeDecodeErrort   lowert
   startswithR,   R	   R   t   formatR   R:   RG   t   UnicodeErrorR"   R7   RA   R   R   R9   (   R1   R,   Rl   t   schemeRm   t   hostt   portR-   R/   t   fragmentt   netloct
   enc_params(    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyRv   =  sZ    "	!	
		$c         C   s8   | r( t  d   | j   D  |  _ n t    |  _ d S(   s    Prepares the given HTTP headers.c         s   s'   |  ] \ } } t  |  | f Vq d  S(   N(   R   (   R_   RB   t   value(    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pys	   <genexpr>  s    N(   R   Rj   RD   (   R1   RD   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyRw     s    "c         C   s  d
 } d
 } d
 } t t | d  t | t  t | t  t | t  g  } y t |  } Wn  t t	 t
 f k
 r d
 } n X| r | } | r t d   n  | d
 k	 r t |  |  j d <q~d |  j d <n | r |  j | |  \ } } nT | rL|  j |  } t | t  s:t | t  s:t | d  rCd
 } qLd } n  |  j |  | r~d |  j k r~| |  j d	 <n  | |  _ d
 S(   s"   Prepares the given HTTP body data.R5   s1   Streamed bodies and files are mutually exclusive.s   Content-Lengtht   chunkeds   Transfer-EncodingR4   s!   application/x-www-form-urlencodeds   content-types   Content-TypeN(   R9   t   allR8   R7   R&   RI   t   dictR   t	   TypeErrort   AttributeErrorR   t   NotImplementedErrorR%   RD   RW   RA   R   t   prepare_content_lengthRV   (   R1   R<   RL   RV   RE   t   lengtht	   is_stream(    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyRy     s<    
-		c         C   s   t  | d  rZ t  | d  rZ | j d d  t | j    |  j d <| j d d  nV | d  k	 r t |  } | r t |  |  j d <q n |  j d	 k r d |  j d <n  d  S(
   Nt   seekt   telli    i   s   Content-Lengtht   GETt   HEADt   0(   R   R   (   R8   R   R%   R   RD   R9   R   Rk   (   R1   RV   t   l(    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyR     s    R+   c         C   s   | d k r6 t |  j  } t |  r- | n d } n  | r t | t  rl t |  d k rl t |   } n  | |   } |  j j	 | j  |  j
 |  j  n  d S(   s"   Prepares the given HTTP auth data.i   N(   R9   R   R,   t   anyR7   RH   RJ   R   t   __dict__t   updateR   RV   (   R1   Rm   R,   t   url_autht   r(    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyRz     s    !c         C   s_   t  | t j  r | |  _ n t |  |  _ t |  j |   } | d k	 r[ | |  j d <n  d S(   s$   Prepares the given HTTP cookie data.t   CookieN(   R7   R   t	   CookieJarRt   R   R   R9   RD   (   R1   Rn   t   cookie_header(    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyRx     s    c         C   s)   x" | D] } |  j  | | |  q Wd S(   s   Prepares the given hooks.N(   Re   (   R1   Ra   Rc   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyR{     s    N(   RX   RY   Rs   Ro   R9   Rr   Rp   R|   Ru   Rv   Rw   Ry   R   Rz   Rx   R{   (    (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyRq      s   			
		B		5		t   Responsec        
   B   s   e  Z d  Z d d d d d d d d d	 d
 g
 Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 e d    Z e d    Z d e d  Z e d d  Z e d    Z e d    Z d   Z e d    Z d   Z d   Z RS(   sh   The :class:`Response <Response>` object, which contains a
    server's response to an HTTP request.
    t   _contentt   status_codeRD   R,   t   historyt   encodingt   reasonRn   t   elapsedt   requestc         C   s   t  t |   j   t |  _ t |  _ d  |  _ t   |  _	 d  |  _
 d  |  _ d  |  _ g  |  _ d  |  _ t i   |  _ t j d  |  _ d  S(   Ni    (   t   superR   Ro   Rg   R   t   _content_consumedR9   R   R   RD   t   rawR,   R   R   R   R   Rn   t   datetimet	   timedeltaR   (   R1   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyRo     s    								c            s0     j  s   j n  t   f d     j D  S(   Nc         3   s'   |  ] } | t    | d   f Vq d  S(   N(   t   getattrR9   (   R_   t   attr(   R1   (    s3   /usr/lib/python2.7/dist-packages/requests/models.pys	   <genexpr>0  s   (   R   t   contentR   t	   __attrs__(   R1   (    (   R1   s3   /usr/lib/python2.7/dist-packages/requests/models.pyt   __getstate__)  s
    	
c         C   sA   x* | j    D] \ } } t |  | |  q Wt |  d t  d  S(   NR   (   Rj   t   setattrR;   (   R1   t   stateRB   R   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyt   __setstate__4  s    c         C   s   d |  j  S(   Ns   <Response [%s]>(   R   (   R1   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyRp   ;  s    c         C   s   |  j  S(   s,   Returns true if :attr:`status_code` is 'OK'.(   t   ok(   R1   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyt   __bool__>  s    c         C   s   |  j  S(   s,   Returns true if :attr:`status_code` is 'OK'.(   R   (   R1   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyt   __nonzero__B  s    c         C   s   |  j  d  S(   s,   Allows you to use a response as an iterator.i   (   t   iter_content(   R1   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyR5   F  s    c         C   s'   y |  j    Wn t k
 r" t SXt S(   N(   t   raise_for_statusR   Rg   R;   (   R1   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyR   J  s
    c         C   s   t  j |  j  d S(   sU   The apparent encoding, provided by the lovely Charade library
        (Thanks, Ian!).R   (   R#   t   detectR   (   R1   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyt   apparent_encodingR  s    i   c            sP    j  r t  j    S   f d   } |   } | rL t |   } n  | S(   sT  Iterates over the response data.  When stream=True is set on the
        request, this avoids reading the content at once into memory for
        large responses.  The chunk size is the number of bytes it should
        read into memory.  This is not necessarily the length of each item
        returned as decoding can take place.
        c          3   s   yp y, x%  j  j   d t D] }  |  Vq WWn= t k
 rP } t |   n t k
 rn } t |   n XWn? t k
 r x/ t r  j  j    }  |  s Pn  |  Vq Wn Xt  _	 d  S(   Nt   decode_content(
   R   t   streamR;   R'   R   R
   R   R   R4   R   (   t   chunkt   e(   t
   chunk_sizeR1   (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyt   generatec  s     	(   R   R   R   R   (   R1   R   t   decode_unicodeR   t   gen(    (   R   R1   s3   /usr/lib/python2.7/dist-packages/requests/models.pyR   X  s    		c         c   s   d } x |  j d | d |  D] } | d k	 r> | | } n  | j   } | r | d r | r | d d | d k r | j   } n d } x | D] } | Vq Wq W| d k	 r | Vn  d S(   s   Iterates over the response data, one line at a time.  When
        stream=True is set on the request, this avoids reading the
        content at once into memory for large responses.
        R   R   iN(   R9   R   t
   splitlinest   pop(   R1   R   R   t   pendingR   t   linest   line(    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyt
   iter_lines  s    .c         C   s   |  j  t k r y^ |  j r* t d   n  |  j d k rE d |  _  n' t   j |  j t	   pf t   |  _  Wq t
 k
 r d |  _  q Xn  t |  _ |  j  S(   s"   Content of the response, in bytes.s2   The content for this response was already consumedi    N(   R   Rg   R   t   RuntimeErrorR   R9   R    R0   R   t   CONTENT_CHUNK_SIZER   R;   (   R1   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyR     s    	+	c         C   s   d } |  j } |  j s" t d  S|  j d k r= |  j } n  y t |  j | d d } Wn, t t f k
 r t |  j d d } n X| S(   s  Content of the response, in unicode.

        If Response.encoding is None, encoding will be guessed using
        ``chardet``.

        The encoding of the response content is determined based soley on HTTP
        headers, following RFC 2616 to the letter. If you can take advantage of
        non-HTTP knowledge to make a better guess at the encoding, you should
        set ``r.encoding`` appropriately before accessing this property.
        R+   t   errorst   replaceN(   R9   R   R   R   R   t   LookupErrorR   (   R1   R   R   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyt   text  s    		
c         K   sl   |  j  rY t |  j  d k rY t |  j  } | d k	 rY t j |  j j |  |  Sn  t j |  j |  S(   s   Returns the json-encoded content of a response, if any.

        :param \*\*kwargs: Optional arguments that ``json.loads`` takes.
        i   N(	   R   RJ   R   R   R9   R$   t   loadsRG   R   (   R1   t   kwargsR   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyR$     s
    c         C   sj   |  j  j d  } i  } | rf t |  } x9 | D]. } | j d  pR | j d  } | | | <q1 Wn  | S(   s8   Returns the parsed header links of the response, if any.t   linkt   relR,   (   RD   t   getR   (   R1   t   headerR   t   linksR   t   key(    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyR     s    c         C   s   d } d |  j  k o  d k  n r> d |  j  |  j f } n8 d |  j  k oX d k  n rv d |  j  |  j f } n  | r t | d |   n  d S(	   s2   Raises stored :class:`HTTPError`, if one occurred.R+   i  i  s   %s Client Error: %siX  s   %s Server Error: %st   responseN(   R   R   R   (   R1   t   http_error_msg(    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyR     s    c         C   s   |  j  j   S(   s   Closes the underlying file descriptor and releases the connection
        back to the pool.

        *Note: Should not normally need to be called explicitly.*
        (   R   t   release_conn(   R1   (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyt   close  s    N(   RX   RY   Rs   R   Ro   R   R   Rp   R   R   R5   RZ   R   R   Rg   R   t   ITER_CHUNK_SIZER9   R   R   R   R$   R   R   R   (    (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyR     s8   		'						'&		i (  (C   Rs   R]   t   loggingR   t   ioR    R   Ra   R   t
   structuresR   Rm   R   Rn   R   R   t   urllib3.fieldsR   t   urllib3.filepostR   t   urllib3.utilR	   t   urllib3.exceptionsR
   t
   exceptionsR   R   R   R   R   R   t   utilsR   R   R   R   R   R   R   R   R   R   t   compatR   R   R   R   R   R    R!   R"   R#   R$   R%   R&   R'   R   R   t	   getLoggerRX   t   logt   objectR(   R\   Ri   Rq   R   (    (    (    s3   /usr/lib/python2.7/dist-packages/requests/models.pyt   <module>   s.   .FXmJ