Your IP : 216.73.216.213


Current Path : /opt/alt/python35/lib/python3.5/site-packages/aioredis/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib/python3.5/site-packages/aioredis/__pycache__/connection.cpython-35.opt-1.pyc



�D�ZZI�@s�ddlZddlZddlZddlmZddlmZddlmZm	Z	m
Z
mZmZm
Z
mZmZddlmZddlmZmZddlmZmZmZmZmZmZmZmZdd	lmZdd
l m!Z!ddl m"Z"ddl#m$Z$d
dgZ%dZ&d"Z'ddddddddddddddddd d
�Z(Gd!d�de"�Z)dS)#�N)�partial)�deque�)�encode_command�wait_ok�_NOTSET�_set_result�_set_exception�coerced_keys_dict�decode�	parse_url)�Reader)�open_connection�open_unix_connection)�ConnectionClosedError�ConnectionForcedCloseError�
RedisError�
ProtocolError�
ReplyError�WatchVariableError�
ReadOnlyError�MaxClientsError)�Channel)�
AbcChannel)�
AbcConnection)�logger�create_connection�RedisConnectioni�	SUBSCRIBE�	SUBSCRIBE�
PSUBSCRIBE�
PSUBSCRIBE�UNSUBSCRIBE�UNSUBSCRIBE�PUNSUBSCRIBE�PUNSUBSCRIBE�db�password�ssl�encoding�parser�loop�timeout�connection_clsc�s�t|t�r�tjd|�t|�\}}	|	jd|�}|	jd|�}|	jd|�}|	jd|�}d|	kr�|p�|	d}|dk	r�|dkr�td	��|r�|}
nt}
|dkr�tj	�}t|t
tf�r�|\}}tjd
|�tjt
||dtd|d|�|d|�IdH\}
}|jjd
�}|dk	r�|jtjtjd�|j�}t|dd��}nutjd|�tjt|d|dtd|�|d|�IdH\}
}|jjd
�}|dk	r&|j�}|
|
|d|d|d|d|�}y@|dk	rn|j|�IdH|dk	r�|j|�IdHWn.tk
r�|j�|j�IdH�YnX|S)a�Creates redis connection.

    Opens connection to Redis server specified by address argument.
    Address argument can be one of the following:
    * A tuple representing (host, port) pair for TCP connections;
    * A string representing either Redis URI or unix domain socket path.

    SSL argument is passed through to asyncio.create_connection.
    By default SSL/TLS is not used.

    By default any timeout is applied at the connection stage, however
    you can set a limitted time used trying to open a connection via
    the `timeout` Kw.

    Encoding argument can be used to decode byte-replies to strings.
    By default no decoding is done.

    Parser parameter can be used to pass custom Redis protocol parser class.
    By default hiredis.Reader is used (unless it is missing or platform
    is not CPython).

    Return value is RedisConnection instance or a connection_cls if it is
    given.

    This function is a coroutine.
    zParsing Redis URI %rr&r'r)r,r(Nrz1Timeout has to be None or a number greater than 0zCreating tcp connection to %r�limitr+�socketr�zCreating unix connection to %r�addressr*)�
isinstance�strr�debugr�
setdefault�
ValueErrorr�asyncio�get_event_loop�list�tupleZwait_forr�MAX_CHUNK_SIZE�	transportZget_extra_infoZ
setsockoptr/ZIPPROTO_TCPZTCP_NODELAYZgetpeernamer�auth�select�	Exception�close�wait_closed)r1r&r'r(r)r*r+r,r-�options�cls�hostZport�reader�writerZsockZconn�rG�D/opt/alt/python35/lib/python3.5/site-packages/aioredis/connection.pyr/s^				

c@s�eZdZdZdddddddd�Zdd	�Zd
d�Zdd
�Zdddd�Zde	dd�Z
dd�Zdd�Zdd�Z
edd��Zdd�Zedd��Zed d!��Zed"d#��Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zed.d/��Zed0d1��Zed2d3��Zed4d5��Zd6d7�ZdS)8rzRedis connection.r)Nr*r+cCs#|dkrtj�}|dkr*t}||_||_||_||_t�|_|jj	|dt
dt��tj|j
�d|j�|_d|_d|_d|_d|_|j�|_|jj|jj�d|_d|_d|_t�|_t�|_||_dS)NZ
protocolErrorZ
replyErrorr+rF)r7r8r
�_reader�_writer�_address�_loopr�_waitersZ
set_parserrrZ
ensure_future�
_read_data�_reader_task�
_close_msg�_db�_closing�_closed�
create_future�
_close_waiterZadd_done_callbackZ
set_result�_in_transaction�_transaction_error�
_in_pubsubr
�_pubsub_channels�_pubsub_patterns�	_encoding)�selfrErFr1r)r*r+rGrGrH�__init__�s2												zRedisConnection.__init__cCsdj|j�S)Nz<RedisConnection [db:{}]>)�formatrQ)r\rGrGrH�__repr__�szRedisConnection.__repr__c�sptd�}x>|jj�sLy|jj�IdH}Wn�tjk
rZtd�}PYqtk
r�}z$|jdk	r�||_	|}PWYdd}~Xqt
k
r�}z|}PWYdd}~XqX|dks�|dkr
|jj�r
tjd|�td�}Pt
|t�r#|}P|jr<|j|�q|j|�qWd|_|jj|j|�dS)zResponse reader task.z$Connection has been closed by serverNzthis is unexpected�z2Connection has been closed by server, response: %rzReader at end of fileT)rrI�at_eofZreadobjr7ZCancelledError�RuntimeErrorrrVrWr?rr4r2rrX�_process_pubsub�
_process_datarRrL�	call_soon�	_do_close)r\Z
last_error�obj�excrGrGrHrN�s:		'			zRedisConnection._read_datacCsj|jj�\}}}t|t�r�t|t�r_|jdjd�r_t|jd�}t||�|j	dk	rf||_
n�|dk	r�yt||�}Wn6tk
r�}zt||�dSWYdd}~XnX|dk	r4y||�}Wn6tk
r3}zt||�dSWYdd}~XnXt
||�|j	dk	rf|j	j||f�dS)zProcesses command results.rZREADONLYN)rM�popleftr2rr�args�
startswithrr	rVrWrr?r�append)r\rg�waiterr)�cbrhrGrGrHrd�s.



zRedisConnection._process_data�process_waitersTc	Cs�|^}}}|dkr�|\}|rI|jrI|jrI|j|�|dkrz|jj|d�}|rz|j�||_n"|dkr�|\}|r�|jr�|jr�|j|�|dkr�|jj|d�}|r�|j�||_n�|dkr)|\}|j|j|�n|dkr^|\}}|j|j||f�nJ|dkr�|r�|jr�|jr�|j|p�d	�ntj	d
|�dS)
zProcesses pubsub messages.�	subscribe�unsubscribeN�
psubscribe�punsubscribesmessagespmessagespongsPONGz"Unknown pubsub message received %r)rprq)rrrs)
rXrMrdrY�popr@rZZ
put_nowaitr�warning)	r\rgro�kindrj�dataZchan�ch�patternrGrGrHrc�s:	

	

	zRedisConnection._process_pubsubc	Gs�|jdks|jj�r9|jp*d}t|��|dkrQtd��d|kritd��|j�j�}|tk}|dk}|jr�|p�|r�t	d��n#|r�t
jd�|j||�S|dkr�t
|jd|�}nf|dkr|j}nN|dkr;t
|jdd�}n*|dkr_t
|jdd�}nd}|tkrz|j}|jj�}|jjt||��|jj|||f�|S)a+Executes redis command and returns Future waiting for the answer.

        Raises:
        * TypeError if any of args can not be encoded as bytes.
        * ReplyError on redis '-ERR' resonses.
        * ProtocolError when response can not be decoded meaning connection
          is broken.
        NzConnection closed or corruptedzcommand must not be Nonezargs must not contain None�PING�PINGzConnection in SUBSCRIBE modez0Deprecated. Use `execute_pubsub` method directly�SELECT�SELECTrj�MULTI�MULTI�EXEC�EXEC�discardF�DISCARD�DISCARDT)rzr{)r|r})r~r)r�r�)r�r�)rIrarPr�	TypeError�upper�strip�_PUBSUB_COMMANDSrXrrru�execute_pubsubr�_set_db�_start_transaction�_end_transactionrr[rLrTrJ�writerrMrl)	r\�commandr)rj�msgZ	is_pubsubZis_pingrn�futrGrGrH�executes<	
	zRedisConnection.executecs�|j�j�}|jdks0|jj�r<td��dt|�krZtd��t|�srtd��t|�dk�tt	d�d|j
���fd	d
�|D�}t�fdd�|D��s�td
j
||���t|dd�|D��}g}xX|D]P}|j
j�}|j|�t|jd|�}|jj|d|f�qW|jj|�tj|d|j
�S)z�Executes redis (p)subscribe/(p)unsubscribe commands.

        Returns asyncio.gather coroutine waiting for all channels/patterns
        to receive answers.
        NzConnection closed or corruptedzargs must not contain NonezNo channels/patterns supplied�
��
is_patternr+cs1g|]'}t|t�r!|n	�|��qSrG)r2r)�.0rx)�	mkchannelrGrH�
<listcomp>Os	z2RedisConnection.execute_pubsub.<locals>.<listcomp>c3s|]}|j�kVqdS)N)r�)r�rx)r�rGrH�	<genexpr>Qsz1RedisConnection.execute_pubsub.<locals>.<genexpr>z$Not all channels {} match command {}css|]}|jVqdS)N)�name)r�rxrGrGrHr�Tsrx)r�r�)r�r�rIrar�setr��lenrrrL�allr6r^rrTrl�_update_pubsubrMrJr�r7Zgather)r\r�Zchannels�cmd�resrxr�rnrG)r�r�rHr�>s.
	

zRedisConnection.execute_pubsubcCs|jt��dS)zClose connection.N)rfr)r\rGrGrHr@^szRedisConnection.closecCsa|jr
dSd|_d|_|jjj�|jj�d|_d|_d|_|dk	rrt|�|_	xd|j
r�|j
j�^}}tj
d||f�|dkr�t|t��qut||�quWx?|jr|jj�\}}tj
d|�|j|�q�Wx?|jr\|jj�\}}tj
d|�|j|�qWdS)NTFzCancelling waiter %rzClosing pubsub channel %rzClosing pubsub pattern %r)rSrRrJr<r@rOZcancelrIr3rPrMrirr4r	rrY�popitemrZ)r\rhrmZspam�_rxrGrGrHrfbs2			
			zRedisConnection._do_closecCsX|jp|j}|rT|jrT|jj�rTd|_}|jj|jd�|S)zTrue if connection is closed.TN)rRrSrIrarLrerf)r\�closedrGrGrHr��s

zRedisConnection.closedc�s"tj|jd|j�IdHdS)z-Coroutine waiting until connection is closed.r+N)r7ZshieldrUrL)r\rGrGrHrA�szRedisConnection.wait_closedcCs|jS)zCurrently selected db index.)rQ)r\rGrGrHr&�szRedisConnection.dbcCs|jS)zCurrent set codec or None.)r[)r\rGrGrHr)�szRedisConnection.encodingcCs|jS)z4Redis server address, either host-port tuple or str.)rK)r\rGrGrHr1�szRedisConnection.addresscCsat|t�s$tdj|���|dkrEtdj|���|jd|�}t|�S)z8Change the selected database for the current connection.z DB must be of int type, not {!r}rz'DB must be greater or equal 0, got {!r}r|)r2�intr�r^r6r�r)r\r&r�rGrGrHr>�s	zRedisConnection.selectcCs|d|_|S)Nr)rQ)r\�okrjrGrGrHr��s
zRedisConnection._set_dbcCst�|_d|_|S)N)rrVrW)r\r�rGrGrHr��s	z"RedisConnection._start_transactionc	Cs
d|_|jd}|_|j�|r0|S|dkr[td�}|gt|�}g}x�t||�D]�\}\}}t|t�s�y+|r�t||�}|r�||�}Wn5t	k
r�}z|j
|�wqWYdd}~XnX|j
|�qqW|S)NzWATCH variable has changed)rWrVrirr��zipr2rrr?rl)	r\rgr�Zrecall�errr��or)rnrGrGrHr��s*	
"
z RedisConnection._end_transactioncCs�|^}}}}||j|_}|dkrP||jkrP||j|<n(|dkrx||jkrx||j|<|s�|j|dd�|S)Ns	subscribes
psubscriberoF)rXrYrZrc)r\rgrxrvryZchannelZ
subscriptionsZ
was_in_pubsubrGrGrHr��s
zRedisConnection._update_pubsubcCs
|jdk	S)z*Set to True when MULTI command was issued.N)rV)r\rGrGrH�in_transaction�szRedisConnection.in_transactioncCs|jS)zkIndicates that connection is in PUB/SUB mode.

        Provides the number of subscribed channels.
        )rX)r\rGrGrH�	in_pubsub�szRedisConnection.in_pubsubcCstj|j�S)z Returns read-only channels dict.)�types�MappingProxyTyperY)r\rGrGrH�pubsub_channels�szRedisConnection.pubsub_channelscCstj|j�S)z Returns read-only patterns dict.)r�r�rZ)r\rGrGrH�pubsub_patterns�szRedisConnection.pubsub_patternscCs|jd|�}t|�S)zAuthenticate to server.ZAUTH)r�r)r\r'r�rGrGrHr=�szRedisConnection.auth)�__name__�
__module__�__qualname__�__doc__r]r_rNrdrcrr�r�r@rf�propertyr�rAr&r)r1r>r�r�r�r�r�r�r�r�r=rGrGrGrHr�s4	(!* 	
)rrr r!r"r#r$r%)*r�r7r/�	functoolsr�collectionsr�utilrrrrr	r
rrr*r
�streamrr�errorsrrrrrrrrZpubsubr�abcrr�logr�__all__r;r�rrrGrGrGrH�<module>s,:
:
Y