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.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�s6t|tttf�s$td��t|t�r�tjd|�t|�\}}	|	jd|�}|	jd|�}|	jd|�}|	jd|�}d|	kr�|	ds�|	dr�|s�td|	d|f��|p�|	d}|d	k	r|d
krt	d��|r<t
|t�s3td��|}
nt}
|d	krZt
j�}t|ttf�r%|\}}tjd
|�t
jt||dtd|d|�|d|�Id	H\}
}|jjd�}|d	k	r|jtjtjd�|j�}t|d	d��}nutjd|�t
jt|d|dtd|�|d|�Id	H\}
}|jjd�}|d	k	r�|j�}|
|
|d|d|d|d|�}y@|d	k	r�|j|�Id	H|d	k	r|j|�Id	HWn.tk
r1|j�|j�Id	H�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.
    ztuple or str expectedzParsing Redis URI %rr&r'r)r,r(zConflicting ssl options are setNrz1Timeout has to be None or a number greater than 0z9connection_class does not meet the AbcConnection contractzCreating tcp connection to %r�limitr+�socketr�zCreating unix connection to %r�addressr*)�
isinstance�tuple�list�str�AssertionErrorr�debugr�
setdefault�
ValueError�
issubclassrr�asyncio�get_event_loop�wait_forr�MAX_CHUNK_SIZE�	transportZget_extra_info�
setsockoptr/�IPPROTO_TCP�TCP_NODELAY�getpeernamer�auth�select�	Exception�close�wait_closed)r1r&r'r(r)r*r+r,r-�options�cls�host�port�reader�writer�sockZconn�rP�D/opt/alt/python35/lib/python3.5/site-packages/aioredis/connection.pyr/sh$					

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+cCsA|dkrtj�}|dkr*t}t|�sHtd|f��||_||_||_||_t	�|_
|jj|dtdt
��tj|j�d|j�|_d|_d|_d|_d|_|j�|_|jj|jj�d|_d|_d|_t�|_t�|_||_dS)NzParser argument is not callableZ
protocolErrorZ
replyErrorr+rF) r;r<r
�callabler6�_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)�selfrMrNr1r)r*r+rPrPrQ�__init__�s6												zRedisConnection.__init__cCsdj|j�S)Nz<RedisConnection [db:{}]>)�formatr[)rfrPrPrQ�__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)rrS�at_eofZreadobjr;ZCancelledError�RuntimeErrorrr`rarFrr7r2rrb�_process_pubsub�
_process_datar\rV�	call_soon�	_do_close)rfZ
last_error�obj�excrPrPrQrX�s:		'			zRedisConnection._read_datacCs�t|j�dks-tt|�|f��|jj�\}}}t|t�r�t|t�r�|jdj	d�r�t
|jd�}t||�|jdk	r�||_
n�|dk	ryt||�}Wn6tk
r}zt||�dSWYdd}~XnX|dk	ray||�}Wn6tk
r`}zt||�dSWYdd}~XnXt||�|jdk	r�|jj||f�dS)zProcesses command results.rZREADONLYN)�lenrWr6�type�popleftr2rr�args�
startswithrr	r`rarrFr�append)rfrq�waiterr)�cbrrrPrPrQrn�s0-



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)r|r})r~r)
rbrWrnrc�poprGrdZ
put_nowaitr�warning)	rfrqr{�kindrv�dataZchan�ch�patternrPrPrQrm�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�SELECTrv�MULTI�MULTI�EXEC�EXEC�discardF�DISCARD�DISCARDT)r�r�)r�r�)r�r�)r�r�)r�r�)rSrkrZr�	TypeError�upper�strip�_PUBSUB_COMMANDSrbrrr��execute_pubsubr�_set_db�_start_transaction�_end_transactionrrerVr^rT�writerrWrx)	rf�commandr)rv�msgZ	is_pubsubZis_pingrz�futrPrPrQ�executes<	
	zRedisConnection.executecs�|j�j�}|tks0td|f��|jdksN|jj�rZtd��dt|�krxtd��t	|�s�td��t	|�dk�t
td�d	|j���fd
d�|D�}t
�fdd
�|D��s
tdj||���t|dd
�|D��}g}xX|D]P}|jj�}|j|�t
|jd|�}|jj|d|f�q6W|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.
        zPub/Sub command expectedNzConnection closed or corruptedzargs must not contain NonezNo channels/patterns supplied�
��
is_patternr+cs1g|]'}t|t�r!|n	�|��qSrP)r2r)�.0r�)�	mkchannelrPrQ�
<listcomp>Os	z2RedisConnection.execute_pubsub.<locals>.<listcomp>c3s|]}|j�kVqdS)N)r�)r�r�)r�rPrQ�	<genexpr>Qsz1RedisConnection.execute_pubsub.<locals>.<genexpr>z$Not all channels {} match command {}css|]}|jVqdS)N)�name)r�r�rPrPrQr�Tsr�)r�r�)r�r�r�r6rSrkr�setr�rsrrrV�allr9rhrr^rx�_update_pubsubrWrTr�r;Zgather)rfr�Zchannels�cmd�resr�r�rzrP)r�r�rQr�>s2
	

zRedisConnection.execute_pubsubcCs|jt��dS)zClose connection.N)rpr)rfrPrPrQrG^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)r]r\rTr?rGrY�cancelrSr5rZrWrurr7r	rrc�popitemrd)rfrrryZspam�_r�rPrPrQrpbs2			
			zRedisConnection._do_closecCsX|jp|j}|rT|jrT|jj�rTd|_}|jj|jd�|S)zTrue if connection is closed.TN)r\r]rSrkrVrorp)rf�closedrPrPrQr��s

zRedisConnection.closedc�s"tj|jd|j�IdHdS)z-Coroutine waiting until connection is closed.r+N)r;Zshieldr_rV)rfrPrPrQrH�szRedisConnection.wait_closedcCs|jS)zCurrently selected db index.)r[)rfrPrPrQr&�szRedisConnection.dbcCs|jS)zCurrent set codec or None.)re)rfrPrPrQr)�szRedisConnection.encodingcCs|jS)z4Redis server address, either host-port tuple or str.)rU)rfrPrPrQr1�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�rhr9r�r)rfr&r�rPrPrQrE�s	zRedisConnection.selectcCs/|dkstd|f��|d|_|S)N�OK�OKzUnexpected result of SELECTr>r�r�)r6r[)rf�okrvrPrPrQr��s
zRedisConnection._set_dbcCs=|jdks$td|jf��t�|_d|_|S)Nz$Connection is already in transaction)r`r6rra)rfr�rPrPrQr��s
	z"RedisConnection._start_transactionc	Cs�|jdk	s!td|f��d|_|jd}|_|j�|rQ|St|t�s�|dkrs|s�td||f��|dkr�td�}|gt|�}t|�t|�ks�td||f��g}x�t||�D]�\}\}}t|t	�szy+|r/t
||�}|rA||�}Wn5tk
ry}z|j|�w�WYdd}~XnX|j|�q�W|S)Nz Connection is not in transactionzUnexpected MULTI/EXEC resultzWATCH variable has changedz*Wrong number of result items in mutli-exec)
r`r6rarur2r4rrs�ziprrrFrx)	rfrqr�Zrecall�errr��or)rzrPrPrQr��s6	
%"
z RedisConnection._end_transactioncCs�|^}}}}||j|_}|dkrP||jkrP||j|<n(|dkrx||jkrx||j|<|s�|j|dd�|S)Ns	subscribes
psubscriber{F)rbrcrdrm)rfrqr�r�r�ZchannelZ
subscriptionsZ
was_in_pubsubrPrPrQr��s
zRedisConnection._update_pubsubcCs
|jdk	S)z*Set to True when MULTI command was issued.N)r`)rfrPrPrQ�in_transaction�szRedisConnection.in_transactioncCs|jS)zkIndicates that connection is in PUB/SUB mode.

        Provides the number of subscribed channels.
        )rb)rfrPrPrQ�	in_pubsub�szRedisConnection.in_pubsubcCstj|j�S)z Returns read-only channels dict.)�types�MappingProxyTyperc)rfrPrPrQ�pubsub_channels�szRedisConnection.pubsub_channelscCstj|j�S)z Returns read-only patterns dict.)r�r�rd)rfrPrPrQ�pubsub_patterns�szRedisConnection.pubsub_patternscCs|jd|�}t|�S)zAuthenticate to server.ZAUTH)r�r)rfr'r�rPrPrQrD�szRedisConnection.auth)�__name__�
__module__�__qualname__�__doc__rgrirXrnrmrr�r�rGrp�propertyr�rHr&r)r1rEr�r�r�r�r�r�r�r�rDrPrPrPrQr�s4	(!* 	
)rrr r!r"r#r$r%)*r�r;r/�	functoolsr�collectionsr�utilrrrrr	r
rrr*r
�streamrr�errorsrrrrrrrrZpubsubr�abcrr�logr�__all__r>r�rrrPrPrPrQ�<module>s,:
:
Y