
O'^c           @  sE  d  d l  m Z d  d l m Z d  d l m Z d  d l m Z d  d l Z d  d l Z d  d l	 Z	 d  d l
 Z
 d  d l Z y d  d l Z e Z Wn e k
 r e Z n Xd  d l m Z m Z m Z m Z m Z m Z m Z 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( m) Z) m* Z* m+ Z+ m, Z, m- Z- d  d l. m/ Z/ e/ rd  d l0 Z0 e e0 j1  Z2 e2 e d	  k Z3 e2 e d
  k Z4 e4 rd e0 j1 Z5 e j6 e5  n  e Z7 e4 pe	 j8 d d k oe	 j8 d d k  re Z7 n  n  e d  Z9 e d  Z: e d  Z; e d  Z< d Z= d e> f d     YZ? d e> f d     YZ@ d e> f d     YZA d e@ f d     YZB d e@ f d     YZC e/ reC ZD n eB ZD d e> f d      YZE d! eE f d"     YZF d# eE f d$     YZG d% e> f d&     YZH d' eH f d(     YZI d S()   i(   t   with_statement(   t   StrictVersion(   t   chain(   t   selectN(   t   bt   xranget   imapt   byte_to_chrt   unicodet   bytest   longt   BytesIOt	   nativestrt
   basestringt	   iteritemst	   LifoQueuet   Emptyt   Fullt   urlparset   parse_qst   unquote(
   t
   RedisErrort   ConnectionErrort   TimeoutErrort   BusyLoadingErrort   ResponseErrort   InvalidResponset   AuthenticationErrort   NoScriptErrort   ExecAbortErrort   ReadOnlyError(   t   HIREDIS_AVAILABLEs   0.1.3s   0.1.4s`   redis-py works best with hiredis >= 0.1.4. You're running hiredis %s. Please consider upgrading.i    i   i   i   t   *t   $s   
t    s   Connection closed by server.t   Tokenc           B  s)   e  Z d  Z d   Z d   Z d   Z RS(   s   
    Literal strings in Redis commands, such as the command names and any
    hard-coded arguments are wrapped in this class so we know not to apply
    and encoding rules on them.
    c         C  s(   t  | t  r | j } n  | |  _ d  S(   N(   t
   isinstanceR#   t   value(   t   selfR%   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyt   __init__E   s    c         C  s   |  j  S(   N(   R%   (   R&   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyt   __repr__J   s    c         C  s   |  j  S(   N(   R%   (   R&   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyt   __str__M   s    (   t   __name__t
   __module__t   __doc__R'   R(   R)   (    (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR#   ?   s   		t
   BaseParserc           B  sA   e  Z i i e d  6d 6e d 6e d 6e d 6e d 6Z d   Z RS(   s   max number of clients reachedt   ERRt	   EXECABORTt   LOADINGt   NOSCRIPTt   READONLYc         C  s{   | j  d  d } | |  j k rq | t |  d } |  j | } t | t  rg | j | t  } n  | |  St |  S(   s   Parse an error responset    i    i   (   t   splitt   EXCEPTION_CLASSESt   lenR$   t   dictt   getR   (   R&   t   responset
   error_codet   exception_class(    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyt   parse_error\   s    
(	   R*   R+   R   R   R   R   R   R5   R<   (    (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR-   Q   s   
t   SocketBufferc           B  sP   e  Z d    Z e d    Z d d  Z d   Z d   Z d   Z	 d   Z
 RS(   c         C  s4   | |  _  | |  _ t   |  _ d |  _ d |  _ d  S(   Ni    (   t   _sockt   socket_read_sizeR   t   _buffert   bytes_writtent
   bytes_read(   R&   t   socketR?   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR'   i   s
    			c         C  s   |  j  |  j S(   N(   RA   RB   (   R&   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyt   lengthr   s    c         C  s/  |  j  } |  j } | j |  j  d } y x t r |  j j |  } t | t  ry t	 |  d k ry t
 j t   n  | j |  t	 |  } |  j | 7_ | | 7} | d  k	 r | | k r q. n  Pq. WWnY t
 j k
 r t d   n: t
 j k
 r*t j   d } t d | j f   n Xd  S(   Ni    s   Timeout reading from socketi   s#   Error while reading from socket: %s(   R?   R@   t   seekRA   t   TrueR>   t   recvR$   R	   R6   RC   t   errort   SERVER_CLOSED_CONNECTION_ERRORt   writet   Nonet   timeoutR   t   syst   exc_infoR   t   args(   R&   RD   R?   t   buft   markert   datat   data_lengtht   e(    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyt   _read_from_socketv   s,    			!
	c         C  s   | d } | |  j  k r0 |  j | |  j   n  |  j j |  j  |  j j |  } |  j t |  7_ |  j |  j k r |  j   n  | d  S(   Ni   i(	   RD   RU   R@   RE   RB   t   readR6   RA   t   purge(   R&   RD   RR   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyRV      s    
c         C  s   |  j  } | j |  j  | j   } x9 | j t  s` |  j   | j |  j  | j   } q( W|  j t |  7_ |  j |  j k r |  j	   n  | d  S(   Ni(
   R@   RE   RB   t   readlinet   endswitht   SYM_CRLFRU   R6   RA   RW   (   R&   RP   RR   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyRX      s    	
c         C  s3   |  j  j d  |  j  j   d |  _ d |  _ d  S(   Ni    (   R@   RE   t   truncateRA   RB   (   R&   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyRW      s    	c         C  s;   y |  j    |  j j   Wn n Xd  |  _ d  |  _ d  S(   N(   RW   R@   t   closeRK   R>   (   R&   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR\      s    
	N(   R*   R+   R'   t   propertyRD   RK   RU   RV   RX   RW   R\   (    (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR=   h   s   					t   PythonParserc           B  sJ   e  Z d  Z d Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 RS(   s   Plain Python parsing classc         C  s   | |  _  d  |  _ d  |  _ d  S(   N(   R?   RK   R>   R@   (   R&   R?   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR'      s    		c         C  s&   y |  j    Wn t k
 r! n Xd  S(   N(   t   on_disconnectt	   Exception(   R&   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyt   __del__   s    c         C  s@   | j  |  _  t |  j  |  j  |  _ | j r< | j |  _ n  d S(   s   Called when the socket connectsN(   R>   R=   R?   R@   t   decode_responsest   encoding(   R&   t
   connection(    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyt
   on_connect   s    	c         C  s]   |  j  d k	 r( |  j  j   d |  _  n  |  j d k	 rP |  j j   d |  _ n  d |  _ d S(   s"   Called when the socket disconnectsN(   R>   RK   R\   R@   Rc   (   R&   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR_      s    c         C  s   |  j  o t |  j  j  S(   N(   R@   t   boolRD   (   R&   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyt   can_read   s    c         C  s  |  j  j   } | s$ t t   n  t | d  | d } } | d
 k rp t d t |  t |  f   n  | d k r t |  } |  j |  } t	 | t  r |  n  | S| d k r n | d k r t
 |  } n | d k rt |  } | d	 k rd  S|  j  j |  } nP | d k rjt |  } | d	 k rBd  Sg  t |  D] } |  j   ^ qO} n  t	 | t  r|  j r| j |  j  } n  | S(   Ni    i   t   -t   +t   :R!   R    s   Protocol Error: %s, %si(   Rh   Ri   Rj   R!   R    (   R@   RX   R   RI   R   R   t   strR   R<   R$   R
   t   intRK   RV   R   t   read_responseR	   Rc   t   decode(   R&   R9   t   byteRH   RD   t   i(    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyRm      s<    	(N(   R*   R+   R,   RK   Rc   R'   Ra   Re   R_   Rg   Rm   (    (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR^      s   				
	t   HiredisParserc           B  sD   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s*   Parser class for connections using Hiredisc         C  s:   t  s t d   n  | |  _ t r6 t |  |  _ n  d  S(   Ns   Hiredis is not installed(   R   R   R?   t   HIREDIS_USE_BYTE_BUFFERt	   bytearrayR@   (   R&   R?   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR'     s
    	c         C  s&   y |  j    Wn t k
 r! n Xd  S(   N(   R_   R`   (   R&   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyRa   &  s    c         C  sn   | j  |  _  i t d 6|  j d 6} t s6 t | d <n  | j rO | j | d <n  t j |   |  _	 t
 |  _ d  S(   Nt   protocolErrort
   replyErrorRc   (   R>   R   R<   t    HIREDIS_SUPPORTS_CALLABLE_ERRORSR   Rb   Rc   t   hiredist   Readert   _readert   Falset   _next_response(   R&   Rd   t   kwargs(    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyRe   ,  s    	c         C  s   d  |  _ d  |  _ t |  _ d  S(   N(   RK   R>   Ry   Rz   R{   (   R&   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR_   <  s    		c         C  sI   |  j  s t t   n  |  j t k r< |  j  j   |  _ n  |  j t k	 S(   N(   Ry   R   RI   R{   Rz   t   gets(   R&   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyRg   A  s
    	c         C  sV  |  j  s t t   n  |  j t k	 r= |  j } t |  _ | S|  j  j   } |  j } x3| t k ry t r |  j j	 |  j
  } | d k r t j t   q nF |  j j |  } t | t  s t |  d k r t j t   n  WnY t j k
 rt d   n: t j k
 rEt j   d } t d | j f   n Xt rh|  j  j |  j
 d |  n |  j  j |  |  j  j   } qX Wt st | t  r|  j | j d  } qt | t  r| rt | d t  r|  j | d j d  | d <qn  t | t  r|  n5 t | t  rR| rRt | d t  rR| d  n  | S(   Ni    s   Timeout reading from socketi   s#   Error while reading from socket: %s(   Ry   R   RI   R{   Rz   R}   R?   Rr   R>   t	   recv_intoR@   RC   RH   RG   R$   R	   R6   RL   R   RM   RN   RO   t   feedRv   R   R<   t   list(   R&   R9   R?   t   bufflent   bufferRT   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyRm   I  sN    				"$	(	   R*   R+   R,   R'   Ra   Re   R_   Rg   Rm   (    (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyRq     s   					t
   Connectionc           B  s   e  Z d  Z d Z d d d d d d e d e d d e e d d  Z d	   Z d
   Z	 d   Z
 d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d d  Z d   Z d   Z d   Z d   Z RS(   s4   Manages TCP communication to and from a Redis servers1   Connection<host=%(host)s,port=%(port)s,db=%(db)s>t	   localhosti  i    s   utf-8t   stricti   c         C  s   t  j   |  _ | |  _ t |  |  _ | |  _ | |  _ | |  _ | pK | |  _	 | |  _
 | pc i  |  _ |	 |  _ |
 |  _ | |  _ | |  _ d  |  _ | d |  |  _ i |  j d 6|  j d 6|  j d 6|  _ g  |  _ d  S(   NR?   t   hostt   portt   db(   t   ost   getpidt   pidR   Rl   R   R   t   passwordt   socket_timeoutt   socket_connect_timeoutt   socket_keepalivet   socket_keepalive_optionst   retry_on_timeoutRc   t   encoding_errorsRb   RK   R>   t   _parsert   _description_argst   _connect_callbacks(   R&   R   R   R   R   R   R   R   R   R   Rc   R   Rb   t   parser_classR?   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR'     s(    										

c         C  s   |  j  |  j S(   N(   t   description_formatR   (   R&   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR(     s    c         C  s&   y |  j    Wn t k
 r! n Xd  S(   N(   t
   disconnectR`   (   R&   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyRa     s    c         C  s   |  j  j |  d  S(   N(   R   t   append(   R&   t   callback(    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyt   register_connect_callback  s    c         C  s   g  |  _  d  S(   N(   R   (   R&   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyt   clear_connect_callbacks  s    c         C  s   |  j  r d Sy |  j   } Wn9 t j k
 rX t j   d } t |  j |    n X| |  _  y |  j   Wn t	 k
 r |  j
     n Xx |  j D] } | |   q Wd S(   s5   Connects to the Redis server if not already connectedNi   (   R>   t   _connectRC   RH   RM   RN   R   t   _error_messageRe   R   R   R   (   R&   t   sockRT   R   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyt   connect  s    		
c         C  sk  d } x:t j |  j |  j d t j  D]} | \ } } } } } d } y t j | | |  } | j t j t j d  |  j	 r | j t j
 t j d  x6 t |  j  D]" \ }	 }
 | j t j |	 |
  q Wn  | j |  j  | j |  | j |  j  | SWq( t j k
 r>} | } | d k	 r?| j   q?q( Xq( W| d k	 rX|  n  t j d   d S(   s   Create a TCP socket connectioni    i   s)   socket.getaddrinfo returned an empty listN(   RK   RC   t   getaddrinfoR   R   t   SOCK_STREAMt
   setsockoptt   IPPROTO_TCPt   TCP_NODELAYR   t
   SOL_SOCKETt   SO_KEEPALIVER   R   t   SOL_TCPt
   settimeoutR   R   R   RH   R\   (   R&   t   errt   rest   familyt   socktypet   protot	   canonnamet   socket_addressR   t   kt   vt   _(    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR     s.    		c         C  s_   t  | j  d k r3 d |  j |  j | j d f Sd | j d |  j |  j | j d f Sd  S(   Ni   s   Error connecting to %s:%s. %s.i    s!   Error %s connecting to %s:%s. %s.(   R6   RO   R   R   (   R&   t	   exception(    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR     s
    c         C  s   |  j  j |   |  j rV |  j d |  j  t |  j    d k rV t d   qV n  |  j r |  j d |  j  t |  j    d k r t d   q n  d S(   s=   Initialize the connection, authenticate and select a databaset   AUTHt   OKs   Invalid Passwordt   SELECTs   Invalid DatabaseN(	   R   Re   R   t   send_commandR   Rm   R   R   R   (   R&   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyRe     s    		c         C  sh   |  j  j   |  j d k r  d Sy$ |  j j t j  |  j j   Wn t j k
 rZ n Xd |  _ d S(   s!   Disconnects from the Redis serverN(	   R   R_   R>   RK   t   shutdownRC   t	   SHUT_RDWRR\   RH   (   R&   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR     s    c         C  s#  |  j  s |  j   n  y@ t | t  r4 | g } n  x | D] } |  j  j |  q; WWn t j k
 r |  j   t d   n t j	 k
 rt
 j   d } |  j   t | j  d k r d | j d } } n | j d } | j d } t d | | f   n |  j     n Xd S(   s2   Send an already packed command to the Redis servers   Timeout writing to socketi   t   UNKNOWNi    s%   Error %s while writing to socket. %s.N(   R>   R   R$   Rk   t   sendallRC   RL   R   R   RH   RM   RN   R6   RO   R   (   R&   t   commandt   itemRT   t   errnot   errmsg(    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyt   send_packed_command  s,    	


c         G  s   |  j  |  j |    d S(   s+   Pack and send a command to the Redis serverN(   R   t   pack_command(   R&   RO   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR   1  s    c         C  sT   |  j  } | s% |  j   |  j  } n  |  j j   pS t t | g g  g  |  d  S(   s8   Poll the socket to see if there's data that can be read.i    (   R>   R   R   Rg   Rf   R   (   R&   RL   R   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyRg   5  s    	
c         C  sF   y |  j  j   } Wn |  j     n Xt | t  rB |  n  | S(   s0   Read the response from a previously sent command(   R   Rm   R   R$   R   (   R&   R9   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyRm   >  s    
	c         C  s   t  | t  r t | j  St  | t  r/ | St  | t t f  rY t t |   } nB t  | t  r} t t	 |   } n t  | t
  s t |  } n  t  | t  r | j |  j |  j  } n  | S(   s/   Return a bytestring representation of the value(   R$   R#   R   R%   R	   Rl   R
   Rk   t   floatt   reprR   R   t   encodeRc   R   (   R&   R%   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR   I  s    c         G  sk  g  } | d } d | k rU t  g  | j d  D] } t |  ^ q/  | d } n t |  f | d } t j t t t t |    t	 f  } x t
 |  j |  D] } t |  d k s t |  d k r#t j | t t t t |    t	 f  } | j |  | j |  t	 } q t j | t t t t |    t	 | t	 f  } q W| j |  | S(   s2   Pack a series of arguments into the Redis protocoli    R3   i   ip  (   t   tupleR4   R#   t	   SYM_EMPTYt   joint   SYM_STARR   Rk   R6   RZ   R   R   t
   SYM_DOLLARR   (   R&   RO   t   outputR   t   st   bufft   arg(    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR   Y  s$    
9$$'	!c         C  s   g  } g  } d } xv | D]n } x4 |  j  |   D]# } | j |  | t |  7} q/ W| d k r | j t j |   d } g  } q q W| r | j t j |   n  | S(   s.   Pack multiple commands into the Redis protocoli    ip  (   R   R   R6   R   R   (   R&   t   commandsR   t   piecest   buffer_lengtht   cmdt   chunk(    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyt   pack_commandsy  s    N(   R*   R+   R,   R   RK   Rz   t   DefaultParserR'   R(   Ra   R   R   R   R   R   Re   R   R   R   Rg   Rm   R   R   R   (    (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR     s0   						(	
								 t   SSLConnectionc           B  s,   e  Z d  Z d d d d d  Z d   Z RS(   s4   SSLConnection<host=%(host)s,port=%(port)s,db=%(db)s>c         K  s   t  s t d   n  t t |   j |   | |  _ | |  _ | d  k rU t j	 } n_ t
 | t  r i t j	 d 6t j d 6t j d 6} | | k r t d |   n  | | } n  | |  _ | |  _ d  S(   Ns$   Python wasn't built with SSL supportt   nonet   optionalt   requireds-   Invalid SSL Certificate Requirements Flag: %s(   t   ssl_availableR   t   superR   R'   t   keyfilet   certfileRK   t   sslt	   CERT_NONER$   R   t   CERT_OPTIONALt   CERT_REQUIREDt	   cert_reqst   ca_certs(   R&   t   ssl_keyfilet   ssl_certfilet   ssl_cert_reqst   ssl_ca_certsR|   t	   CERT_REQS(    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR'     s&    		

	c      
   C  sL   t  t |   j   } t j | d |  j d |  j d |  j d |  j } | S(   s    Wrap the socket with SSL supportR   R   R   R   (	   R   R   R   R   t   wrap_socketR   R   R   R   (   R&   R   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR     s    			N(   R*   R+   R   RK   R'   R   (    (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR     s   	t   UnixDomainSocketConnectionc           B  sG   e  Z d  Z d d d	 d	 d d e e e d d 
 Z d   Z d   Z RS(
   s3   UnixDomainSocketConnection<path=%(path)s,db=%(db)s>R"   i    s   utf-8R   i   c         C  s   t  j   |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _	 | |  _
 d  |  _ |	 d |
  |  _ i |  j d 6|  j d 6|  _ g  |  _ d  S(   NR?   t   pathR   (   R   R   R   R   R   R   R   R   Rc   R   Rb   RK   R>   R   R   R   (   R&   R   R   R   R   Rc   R   Rb   R   R   R?   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR'     s    									
c         C  s<   t  j  t  j t  j  } | j |  j  | j |  j  | S(   s&   Create a Unix domain socket connection(   RC   t   AF_UNIXR   R   R   R   R   (   R&   R   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR     s    c         C  sS   t  | j  d k r- d |  j | j d f Sd | j d |  j | j d f Sd  S(   Ni   s(   Error connecting to unix socket: %s. %s.i    s+   Error %s connecting to unix socket: %s. %s.(   R6   RO   R   (   R&   R   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR     s
    N(	   R*   R+   R   RK   Rz   R   R'   R   R   (    (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR     s   		t   ConnectionPoolc           B  sq   e  Z d  Z e d
 e d   Z e d
 d  Z d   Z	 d   Z
 d   Z d   Z d   Z d   Z d	   Z RS(   s   Generic connection poolc         K  s  | } t  |  } d } d | j k rh | j rh | j j d d  d } t  | t |  d   } n	 | j } i  } xI t t |   D]5 \ } }	 |	 r t |	  d k r |	 d | | <q q W| r&| j r t | j  n d }
 | j r t | j  n d } | j
 rt | j
  n d } n | j }
 | j } | j
 } | j d k ru| j i |
 d 6| d 6t d 6 n | j i | d	 6t | j pd
  d 6|
 d 6 d | k r| ry  t | j d d   | d <Wqt t f k
 rqXn  | j d k rt | d <n  t | j d | p&d   | d <| j |  d | k rut j t d   | j d  | d <n  d | k rt j t d   | j d  | d <n  |  |   S(   s  
        Return a connection pool configured from the given URL.

        For example::

            redis://[:password]@localhost:6379/0
            rediss://[:password]@localhost:6379/0
            unix://[:password]@/path/to/socket.sock?db=0

        Three URL schemes are supported:
            redis:// creates a normal TCP socket connection
            rediss:// creates a SSL wrapped TCP socket connection
            unix:// creates a Unix Domain Socket connection

        There are several ways to specify a database number. The parse function
        will return the first specified option:
            1. A ``db`` querystring option, e.g. redis://localhost?db=0
            2. If using the redis:// scheme, the path argument of the url, e.g.
               redis://localhost/0
            3. The ``db`` argument to this function.

        If none of these options are specified, db=0 is used.

        The ``decode_components`` argument allows this function to work with
        percent-encoded URLs. If this argument is set to ``True`` all ``%xx``
        escapes will be replaced by their single-character equivalents after
        the URL has been parsed. This only applies to the ``hostname``,
        ``path``, and ``password`` components.

        Any additional querystring arguments and keyword arguments will be
        passed along to the ConnectionPool class's initializer. In the case
        of conflicting arguments, querystring arguments always win.
        R"   t   ?i   i    t   unixR   R   t   connection_classR   i  R   R   t   /t   redisst   charsets/   "charset" is deprecated. Use "encoding" insteadRc   t   errorss5   "errors" is deprecated. Use "encoding_errors" insteadR   N(   R   R   t   queryR4   R6   R   R   R   R   RK   t   hostnamet   schemet   updateR   Rl   R   t   replacet   AttributeErrort
   ValueErrorR   R8   t   warningst   warnt   DeprecationWarningt   pop(   t   clst   urlR   t   decode_componentsR|   t
   url_stringt   qst   url_optionst   nameR%   R   R   R   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyt   from_url  sZ    #	!					 "	
	
c         K  sf   | p	 d } t  | t t f  s. | d k  r= t d   n  | |  _ | |  _ | |  _ |  j   d S(   s  
        Create a connection pool. If max_connections is set, then this
        object raises redis.ConnectionError when the pool's limit is reached.

        By default, TCP connections are created connection_class is specified.
        Use redis.UnixDomainSocketConnection for unix sockets.

        Any additional keyword arguments are passed to the constructor of
        connection_class.
        i   i   i    s,   "max_connections" must be a positive integerNI       (   R$   Rl   R
   R   R   t   connection_kwargst   max_connectionst   reset(   R&   R   R	  R  (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR'   N  s    "			c         C  s$   d t  |   j |  j j |  j f S(   Ns   %s<%s>(   t   typeR*   R   R   R  (   R&   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR(   d  s    c         C  s@   t  j   |  _ d |  _ g  |  _ t   |  _ t j   |  _	 d  S(   Ni    (
   R   R   R   t   _created_connectionst   _available_connectionst   sett   _in_use_connectionst	   threadingt   Lockt   _check_lock(   R&   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR
  j  s
    		c         C  sY   |  j  t j   k rU |  j 2 |  j  t j   k r8 d  S|  j   |  j   Wd  QXn  d  S(   N(   R   R   R   R  R   R
  (   R&   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyt	   _checkpidq  s    

c         O  sQ   |  j    y |  j j   } Wn t k
 r< |  j   } n X|  j j |  | S(   s   Get a connection from the pool(   R  R  R   t
   IndexErrort   make_connectionR  t   add(   R&   t   command_namet   keyst   optionsRd   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyt   get_connection{  s    
c         C  s@   |  j  |  j k r! t d   n  |  j  d 7_  |  j |  j   S(   s   Create a new connections   Too many connectionsi   (   R  R	  R   R   R  (   R&   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR    s    c         C  sD   |  j    | j |  j k r  d S|  j j |  |  j j |  d S(   s(   Releases the connection back to the poolN(   R  R   R  t   removeR  R   (   R&   Rd   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyt   release  s
    
c         C  s4   t  |  j |  j  } x | D] } | j   q Wd S(   s'   Disconnects all connections in the poolN(   R   R  R  R   (   R&   t	   all_connsRd   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR     s    	N(   R*   R+   R,   t   classmethodRK   Rz   R  R   R'   R(   R
  R  R  R  R  R   (    (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR     s   k			
	
		t   BlockingConnectionPoolc           B  sP   e  Z d  Z d d e e d  Z d   Z d   Z d   Z d   Z	 d   Z
 RS(	   s6  
    Thread-safe blocking connection pool::

        >>> from redis.client import Redis
        >>> client = Redis(connection_pool=BlockingConnectionPool())

    It performs the same function as the default
    ``:py:class: ~redis.connection.ConnectionPool`` implementation, in that,
    it maintains a pool of reusable connections that can be shared by
    multiple redis clients (safely across threads if required).

    The difference is that, in the event that a client tries to get a
    connection from the pool when all of connections are in use, rather than
    raising a ``:py:class: ~redis.exceptions.ConnectionError`` (as the default
    ``:py:class: ~redis.connection.ConnectionPool`` implementation does), it
    makes the client wait ("blocks") for a specified number of seconds until
    a connection becomes available.

    Use ``max_connections`` to increase / decrease the pool size::

        >>> pool = BlockingConnectionPool(max_connections=10)

    Use ``timeout`` to tell it either how many seconds to wait for a connection
    to become available, or to block forever:

        # Block forever.
        >>> pool = BlockingConnectionPool(timeout=None)

        # Raise a ``ConnectionError`` after five seconds if a connection is
        # not available.
        >>> pool = BlockingConnectionPool(timeout=5)
    i2   i   c         K  s8   | |  _  | |  _ t t |   j d | d | |  d  S(   NR   R	  (   t   queue_classRL   R   R  R'   (   R&   R	  RL   R   R   R  (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR'     s    		c         C  sv   t  j   |  _ t j   |  _ |  j |  j  |  _ x3 t	 rh y |  j j
 d   Wq6 t k
 rd Pq6 Xq6 Wg  |  _ d  S(   N(   R   R   R   R  R  R  R   R	  t   poolRF   t
   put_nowaitRK   R   t   _connections(   R&   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR
    s    		c         C  s&   |  j  |  j   } |  j j |  | S(   s   Make a fresh connection.(   R   R  R#  R   (   R&   Rd   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR    s    c         O  sq   |  j    d } y" |  j j d t d |  j  } Wn t k
 rQ t d   n X| d k rm |  j   } n  | S(   s7  
        Get a connection, blocking for ``self.timeout`` until a connection
        is available from the pool.

        If the connection returned is ``None`` then creates a new connection.
        Because we use a last-in first-out queue, the existing connections
        (having been returned to the pool after the initial ``None`` values
        were added) will be returned before ``None`` values. This means we only
        create new connections when we need to, i.e.: the actual number of
        connections will only increase in response to demand.
        t   blockRL   s   No connection available.N(	   R  RK   R!  R8   RF   RL   R   R   R  (   R&   R  R  R  Rd   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR    s    
"c         C  sL   |  j    | j |  j k r  d Sy |  j j |  Wn t k
 rG n Xd S(   s)   Releases the connection back to the pool.N(   R  R   R!  R"  R   (   R&   Rd   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR    s    
c         C  s"   x |  j  D] } | j   q
 Wd S(   s(   Disconnects all connections in the pool.N(   R#  R   (   R&   Rd   (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR     s    (   R*   R+   R,   R   R   R'   R
  R  R  R  R   (    (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyR    s    
			 	(J   t
   __future__R    t   distutils.versionR   t	   itertoolsR   R   R   RC   RM   R  R   R   RF   R   t   ImportErrorRz   t   redis._compatR   R   R   R   R   R	   R
   R   R   R   R   R   R   R   R   R   R   t   redis.exceptionsR   R   R   R   R   R   R   R   R   R   t   redis.utilsR   Rw   t   __version__t   hiredis_versionRv   t   HIREDIS_SUPPORTS_BYTE_BUFFERt   msgR   Rr   t   version_infoR   R   RZ   R   RI   t   objectR#   R-   R=   R^   Rq   R   R   R   R   R   R  (    (    (    s2   /tmp/pip-unpacked-wheel-5e_doG/redis/connection.pyt   <module>   s`   

pF
&bRa	 '+