
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
U
    S¤ðW[!  ã                   @   s  d dl Z d dlZd dlmZ d dlmZ zd dlmZ W n  ek
rX   d dlmZ Y nX z0d dlZd dlm	Z
 d dlmZ d dlmZ W n  ek
rª   d Z
 ZZY nX d dlmZ e  d¡ZG dd	„ d	eƒZG d
d„ deƒZdZdZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )é    N©ÚEvent)ÚThread)ÚQueue)ÚGreenlet)ÚSqliteExtDatabasezpeewee.sqliteqc                   @   s   e Zd ZdS )ÚResultTimeoutN)Ú__name__Ú
__module__Ú__qualname__© r   r   úD/opt/alt/python38/lib64/python3.8/site-packages/playhouse/sqliteq.pyr      s   r   c                   @   s|   e Zd ZdZdd„ Zddd„Zddd„Zd	d
„ Zdd„ ZeZ	e
dd„ ƒZe
dd„ ƒZe
dd„ ƒZdd„ Zdd„ Zdd„ ZdS )ÚAsyncCursor)	ÚsqlÚparamsÚcommitÚtimeoutÚ_eventÚ_cursorÚ_excÚ_idxÚ_rowsc                 C   s:   || _ || _|| _|| _|| _d  | _ | _ | _| _d S ©N)	r   r   r   r   r   r   r   r   r   )ÚselfÚeventr   r   r   r   r   r   r   Ú__init__    s    zAsyncCursor.__init__Nc                 C   s6   || _ || _d| _|d kr"| ¡ ng | _| j ¡  | S )Nr   )r   r   r   Úfetchallr   r   Úset)r   ÚcursorÚexcr   r   r   Ú
set_result(   s    
zAsyncCursor.set_resultc                 C   s@   |d k	r|n| j }| jj|d�s,|r,tdƒ‚| jd k	r<| j‚d S )N)r   zresults not ready, timed out.)r   r   Úwaitr   r   )r   r   r   r   r   Ú_wait0   s
    
zAsyncCursor._waitc                 C   s   |   ¡  | jd k	r| j‚| S r   )r"   r   Ú_exec©r   r   r   r   Ú__iter__7   s    
zAsyncCursor.__iter__c                 C   s@   z| j | j }W n tk
r(   t‚Y nX |  jd7  _|S d S )Né   )r   r   Ú
IndexErrorÚStopIteration)r   Úobjr   r   r   Únext=   s    
zAsyncCursor.nextc                 C   s   |   ¡  | jjS r   )r"   r   Ú	lastrowidr$   r   r   r   r+   G   s    zAsyncCursor.lastrowidc                 C   s   |   ¡  | jjS r   )r"   r   Úrowcountr$   r   r   r   r,   L   s    zAsyncCursor.rowcountc                 C   s   | j jS r   )r   Údescriptionr$   r   r   r   r-   Q   s    zAsyncCursor.descriptionc                 C   s   | j  ¡  d S r   )r   Úcloser$   r   r   r   r.   U   s    zAsyncCursor.closec                 C   s   t | ƒS r   )Úlistr$   r   r   r   r   X   s    zAsyncCursor.fetchallc                 C   s.   |   ¡  z
t| ƒW S  tk
r(   Y d S X d S r   )r"   r*   r(   r$   r   r   r   Úfetchone[   s
    
zAsyncCursor.fetchone)N)N)r	   r
   r   Ú	__slots__r   r    r"   r%   r*   Ú__next__Úpropertyr+   r,   r-   r.   r   r0   r   r   r   r   r      s    




r   z×threadlocals cannot be set to True when using the Sqlite thread / queue database. All queries are serialized through a single connection, so allowing multiple threads to connect defeats the purpose of this database.z·SQLite must be configured to use the WAL journal mode when using this feature. WAL mode allows one or more readers to continue reading while another connection writes to the database.c                       sr   e Zd Zd‡ fdd„	Zdd„ Zddd	„Zd
d„ Zdd„ Zdd„ Zdd„ Z	ddd„Z
dd„ Zdd„ Zdd„ Z‡  ZS )ÚSqliteQueueDatabaseFr&   Nc                    s¬   |  d¡rttƒ‚d|d< d|d< |  | dd ¡| dd ¡¡}	tt| ƒ}
|
j| _|
j	|f|žd|	i|—Ž || _
|| _|| _d| _|  |¡|ƒ| _|  ¡  | j
r¨|  ¡  d S )NÚthreadlocalsFZcheck_same_threadÚjournal_modeÚpragmasT)ÚgetÚ
ValueErrorÚTHREADLOCAL_ERROR_MESSAGEÚ_validate_journal_modeÚpopÚsuperr4   Úexecute_sqlÚ!_SqliteQueueDatabase__execute_sqlr   Z
_autostartÚ_results_timeoutÚ_num_readersÚ_is_stoppedÚget_thread_implÚ_thread_helperÚ_create_queues_and_workersÚstart)r   ÚdatabaseÚ
use_geventZ	autostartZreadersÚqueue_max_sizeZresults_timeoutÚargsÚkwargsr7   ZParent©Ú	__class__r   r   r   o   s&    


þ
zSqliteQueueDatabase.__init__c                 C   s   |rt S tS r   )ÚGreenletHelperÚThreadHelper)r   rH   r   r   r   rC   �   s    z#SqliteQueueDatabase.get_thread_implc                 C   sh   |r|  ¡ dkrttƒ‚|r^tdd„ |D ƒƒ}| dd¡  ¡ dkrJttƒ‚dd„ |D ƒdg S dgS d S )NÚwalc                 s   s   | ]\}}|  ¡ |fV  qd S r   )Úlower©Ú.0ÚkÚvr   r   r   Ú	<genexpr>˜   s     z=SqliteQueueDatabase._validate_journal_mode.<locals>.<genexpr>r6   c                 S   s    g | ]\}}|d kr||f‘qS )r6   r   rR   r   r   r   Ú
<listcomp>œ   s    ÿz>SqliteQueueDatabase._validate_journal_mode.<locals>.<listcomp>)r6   rP   )rQ   r9   ÚWAL_MODE_ERROR_MESSAGEÚdictr8   )r   r6   r7   Zpdictr   r   r   r;   “   s    ÿz*SqliteQueueDatabase._validate_journal_modec                    sP   ˆ j  ¡ ˆ _ˆ j  ¡ ˆ _ˆ j‰ˆ j  ˆˆ j¡ˆ _‡ ‡fdd„tˆ jƒD ƒˆ _	d S )Nc                    s   g | ]}ˆ j  ˆˆ j¡‘qS r   )rD   ÚthreadÚ_read_queue)rS   Ú_©r   Útargetr   r   rW   §   s   ÿzBSqliteQueueDatabase._create_queues_and_workers.<locals>.<listcomp>)
rD   ÚqueueÚ_write_queuer[   Ú_run_worker_looprZ   Ú_writerÚrangerA   Ú_readersr$   r   r]   r   rE   ¡   s    ÿz.SqliteQueueDatabase._create_queues_and_workersc                 C   s<   |  ¡ }|tkrt d¡ d S t d|j¡ |  |¡ q d S )Nzworker shutting down.zreceived query %s)r8   r(   ÚloggerÚinfoÚdebugr   Ú_process_execution)r   r_   Úasync_cursorr   r   r   ra   ª   s    
z$SqliteQueueDatabase._run_worker_loopc              
   C   sP   z|   |j|j|j¡}W n& tk
r> } zd }W 5 d }~X Y nX d }| ||¡S r   )r?   r   r   r   Ú	Exceptionr    )r   ri   r   r   r   r   r   rh   ´   s    ÿz&SqliteQueueDatabase._process_executionc                 C   s   | j  ¡ | j ¡ fS r   )r`   Zqsizer[   r$   r   r   r   Ú
queue_size¾   s    zSqliteQueueDatabase.queue_sizeTc                 C   sD   t | j ¡ ||||d kr| jn|d�}|r0| jn| j}| |¡ |S )N)r   r   r   r   r   )r   rD   r   r@   r`   r[   Úput)r   r   r   Úrequire_commitr   r   r_   r   r   r   r>   Á   s    û
zSqliteQueueDatabase.execute_sqlc              	   C   sb   | j �R | jsW 5 Q R £ dS | j ¡  | jD ]}| ¡  q,t d¡ d| _W 5 Q R £ dS Q R X d S )NFzworkers started.T)Ú
_conn_lockrB   rb   rF   rd   re   rf   )r   Úreaderr   r   r   rF   Ì   s    



zSqliteQueueDatabase.startc              	   C   s€   t  d¡ | j�f | jr&W 5 Q R £ dS | j t¡ | jD ]}| j t¡ q8| j	 
¡  | jD ]}| 
¡  qZW 5 Q R £ dS Q R X d S )Nzenvironment stop requested.FT)re   rg   rn   rB   r`   rl   r(   rd   r[   rb   Újoin)r   r\   ro   r   r   r   Ústop×   s    




zSqliteQueueDatabase.stopc              
   C   s$   | j � | jW  5 Q R £ S Q R X d S r   )rn   rB   r$   r   r   r   Ú
is_stoppedä   s    zSqliteQueueDatabase.is_stopped)FFr&   NN)NN)NTN)r	   r
   r   r   rC   r;   rE   ra   rh   rk   r>   rF   rq   rr   Ú__classcell__r   r   rL   r   r4   n   s       ÿ!
	


r4   c                   @   s4   e Zd ZdZddd„Zdd„ Zddd„Zd	d
„ ZdS )rO   ©rI   Nc                 C   s
   || _ d S r   rt   )r   rI   r   r   r   r   ì   s    zThreadHelper.__init__c                 C   s   t ƒ S r   r   r$   r   r   r   r   ï   ó    zThreadHelper.eventc                 C   s    |d k	r|n| j }t|pdd�S ©Nr   )Úmaxsize)rI   r   ©r   Zmax_sizer   r   r   r_   ñ   s    zThreadHelper.queuec                 O   s   t |||d�}d|_|S )N)r^   rJ   rK   T)r   Údaemon)r   ÚfnrJ   rK   rZ   r   r   r   rZ   õ   s    zThreadHelper.thread)N)N)r	   r
   r   r1   r   r   r_   rZ   r   r   r   r   rO   é   s
   

rO   c                   @   s*   e Zd ZdZdd„ Zd	dd„Zdd„ ZdS )
rN   rt   c                 C   s   t ƒ S r   )ÚGEventr$   r   r   r   r   þ   ru   zGreenletHelper.eventNc                 C   s    |d k	r|n| j }t|pdd�S rv   )rI   ÚGQueuerx   r   r   r   r_      s    zGreenletHelper.queuec                    s   ‡ fdd„}t |f|ž|ŽS )Nc                     s   t  ¡  ˆ | |ŽS r   )ÚgeventÚsleep)ÚarT   ©rz   r   r   Úwrap  s    z#GreenletHelper.thread.<locals>.wrap)ÚGThread)r   rz   rJ   rK   r�   r   r€   r   rZ     s    zGreenletHelper.thread)N)r	   r
   r   r1   r   r_   rZ   r   r   r   r   rN   û   s   
rN   )ÚloggingÚweakrefÚ	threadingr   r   r   ÚImportErrorr_   r}   r   r‚   Zgevent.eventr{   Zgevent.queuer|   Zplayhouse.sqlite_extr   Ú	getLoggerre   rj   r   Úobjectr   r:   rX   r4   rO   rN   r   r   r   r   Ú<module>   s.   
G{