
    B>qjp)                        d dl Z 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mZ	 d dl
mZ d dlmZ d dlmZ  e j"                  d      Z G d	 d
e      Z G d de      Z G d de      Z G d de      ZeZ e       Z e       Z e       Z G d de      Z G d de      Z G d de      Z  G d de       Z!y# e$ r	 dxZ	xZZY w xY w)    N)QueueEvent)Lock)Thread)Greenlet)SqliteDatabasezpeewee.sqliteqc                       e Zd Zy)ResultTimeoutN__name__
__module____qualname__     P/opt/rentech/trading_bot/.venv/lib/python3.12/site-packages/playhouse/sqliteq.pyr   r          r   r   c                       e Zd Zy)WriterPausedNr   r   r   r   r   r      r   r   r   c                       e Zd Zy)ShutdownExceptionNr   r   r   r   r   r      r   r   r   c                   x    e Zd ZdZd ZddZddZd Zd ZeZ	e
d        Ze
d	        Ze
d
        Zd Zd Zd Zy)AsyncCursor)	sqlparamstimeout_event_cursor_exc_idx_rows_readyc                     || _         || _        || _        || _        d x| _        x| _        x| _        | _        d| _        y NF)	r   r   r   r   r   r   r    r!   r"   )selfeventr   r   r   s        r   __init__zAsyncCursor.__init__#   sA    <@@@ty@49tzr   Nc                     || _         || _        d| _        ||j                         ng | _        | j
                  j                          | S )Nr   )r   r   r    fetchallr!   r   set)r%   cursorexcs      r   
set_resultzAsyncCursor.set_result+   s>    		*-+V__&2
r   c                     ||n| j                   }| j                  j                  |      s|t        d      | j                  | j                  d| _        y )N)r   zresults not ready, timed out.T)r   r   waitr   r   r"   )r%   r   s     r   _waitzAsyncCursor._wait3   sR    $0'dll{{0W5H ?@@99 ))Or   c                 n    | j                   s| j                          | j                  | j                  | S N)r"   r0   r   r%   s    r   __iter__zAsyncCursor.__iter__;   s*    {{JJL99 ))Or   c                     | j                   s| j                          	 | j                  | j                     }| xj                  dz  c_        |S # t        $ r t
        w xY w)N   )r"   r0   r!   r    
IndexErrorStopIteration)r%   objs     r   nextzAsyncCursor.nextB   sT    {{JJL	**TYY'C IINIJ	  	 	 s   A Ac                 f    | j                   s| j                          | j                  j                  S r2   )r"   r0   r   	lastrowidr3   s    r   r<   zAsyncCursor.lastrowidN   s"    {{JJL||%%%r   c                 f    | j                   s| j                          | j                  j                  S r2   )r"   r0   r   rowcountr3   s    r   r>   zAsyncCursor.rowcountT   s"    {{JJL||$$$r   c                 f    | j                   s| j                          | j                  j                  S r2   )r"   r0   r   descriptionr3   s    r   r@   zAsyncCursor.descriptionZ   s"    {{JJL||'''r   c                 `    | j                   "| j                   j                          d | _         y y r2   )r   closer3   s    r   rB   zAsyncCursor.close`   s(    <<#LL DL $r   c                     t        |       S r2   )listr3   s    r   r)   zAsyncCursor.fetchalle   s    Dzr   c                 p    | j                   s| j                          	 t        |       S # t        $ r Y y w xY wr2   )r"   r0   r:   r8   r3   s    r   fetchonezAsyncCursor.fetchoneh   s3    {{JJL	: 		s   
) 	55r2   )r   r   r   	__slots__r'   r-   r0   r4   r:   __next__propertyr<   r>   r@   rB   r)   rF   r   r   r   r   r      ss    II	 H& &
 % %
 ( (
 
r   r   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)Writerdatabasequeuec                      || _         || _        y r2   rL   )r%   rM   rN   s      r   r'   zWriter.__init__y   s     
r   c                    | j                   j                         }	 	 	 |+| j                         r,| j                   j                         }n| j                  |      }@# t        $ rZ t
        j                  d       Y |@| j                   j                  |       | j                   j                  j                          y y w xY w# |@| j                   j                  |       | j                   j                  j                          w w xY w)Nz*writer received shutdown request, exiting.)
rM   
connectionwait_unpauseloopr   loggerinfo_close_statereset)r%   conns     r   runz
Writer.run}   s    }}'')	-|,,.#'==#;#;#=D#yy  ) KK LM$$T*$$**,  	 $$T*$$**,  s/   C >A C C ;C ?C  C ADc                 z   | j                   j                         \  }}|t        u r&t        j	                  d       |j                          y|t        u r
t               |t        u r&t        j                  d       |j                          y |j                  d t                      t        j                  d|       y )Nz+writer unpaused - reconnecting to database.Tz-writer received pause, but is already paused.zwriter paused, not handling %s)rN   getUNPAUSErT   rU   r*   SHUTDOWNr   PAUSEerrorr-   r   warning)r%   opr9   s      r   rR   zWriter.wait_unpause   s    **.."C=KKEFGGI8^#%%5[LLHIGGINN40NN;SAr   c                    | j                   j                         \  }}|t        u r| j                  |       |S |t        u ret
        j                  d       | j                  j                  |       | j                  j                  j                          |j                          y |t        u r't
        j                  d       |j                          |S |t        u r
t               t
        j                  d|       |S )Nz,writer paused - closing database connection.z0writer received unpause, but is already running.z&writer received unsupported object: %s)rN   r\   QUERYexecuter_   rT   rU   rM   rV   rW   rX   r*   r]   r`   r^   r   )r%   rY   rb   r9   s       r   rS   zWriter.loop   s    **.."C;LL  5[KKFGMM  &MM  &&(GGI7]LLKLGGI
 	 8^#%%LLA3Gr   c                     t         j                  d|j                         	 | j                  j	                  |j                  |j
                        }d }|j                  ||      S # t        $ r}d }|}Y d }~$d }~ww xY w)Nzreceived query %s)rT   debugr   rM   _executer   	Exceptionr-   )r%   r9   r+   r,   execute_errs        r   re   zWriter.execute   sl    (#''2	]]++CGGSZZ@F
 C~~fc**  	FC	s   0A& &	A=/A88A=N)	r   r   r   rG   r'   rZ   rR   rS   re   r   r   r   rK   rK   v   s!    %I-$B&	+r   rK   c                   z     e Zd ZdZ	 	 d fd	Zd ZddZd Zd ZddZ	d Z
d	 Zd
 Zd Zd Zd ZexZxZZ xZS )SqliteQueueDatabasezSQLite 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                    d|d<   t               | _        | j                  |j                  dd             }t	        t
        |       }	|	j                  | _         |	j                  |g|d|i| || _	        || _
        d| _         | j                  |      |      | _        | j                          | j                  r| j                          y y )NFcheck_same_threadpragmasT)r   _qlock_validate_journal_modepopsuperrl   execute_sqlrh   r'   
_autostart_results_timeout_is_stoppedget_thread_impl_thread_helper_create_write_queuestart)r%   rM   
use_gevent	autostartqueue_max_sizeresults_timeoutargskwargsro   Parent	__class__s             r   r'   zSqliteQueueDatabase.__init__   s    &+"# f --fjjD.IJ
 *D1** 	CDC'CFC# / ?d22:>~N 	  "??JJL r   c                     |rt         S t        S r2   )GreenletHelperThreadHelper)r%   r|   s     r   rx   z#SqliteQueueDatabase.get_thread_impl   s    !+~==r   c                     |sddiS t        |t              st        d |D              }|j                  dd      j                         dk7  rt	        | j
                        d|d<   |S )Njournal_modewalc              3   F   K   | ]  \  }}|j                         |f  y wr2   )lower).0kvs      r   	<genexpr>z=SqliteQueueDatabase._validate_journal_mode.<locals>.<genexpr>   s     @fq!AGGIq>@s   !)
isinstancedictr\   r   
ValueErrorWAL_MODE_ERROR_MESSAGE)r%   ro   s     r   rq   z*SqliteQueueDatabase._validate_journal_mode   sf    "E**'4(@@@G;;~u-335>T8899"'r   c                 B    | j                   j                         | _        y r2   )ry   rN   _write_queuer3   s    r   rz   z'SqliteQueueDatabase._create_write_queue   s     //557r   c                 6    | j                   j                         S r2   )r   qsizer3   s    r   
queue_sizezSqliteQueueDatabase.queue_size   s      &&((r   c                    |j                         j                  d      r| j                  ||      S t        | j                  j                         |||| j                  n|      }| j                  j                  t        |f       |S )Nselect)r&   r   r   r   )
r   
startswithrh   r   ry   r&   rv   r   putrd   )r%   r   r   r   r+   s        r   rt   zSqliteQueueDatabase.execute_sql   sx    99;!!(+==f--%%++--4_D))'	K
 	ufo.r   c                      j                   5   j                  s
	 d d d        y fd} j                  j                  |       _         j                  j                          d _        	 d d d        y# 1 sw Y   y xY w)NFc                  R    t        j                        } | j                          y r2   )rK   r   rZ   )writerr%   s    r   rZ   z&SqliteQueueDatabase.start.<locals>.run	  s    d&7&78

r   T)rp   rw   ry   thread_writerr{   )r%   rZ   s   ` r   r{   zSqliteQueueDatabase.start  so    [[ 
	##
	 
	  ..55c:DLLL $D
	 
	 
	s   A5AA55A>c                 N   t         j                  d       | j                  5  | j                  r
	 d d d        y| j                  j                  t        d f       | j                  }d| _        d d d        j                          | j                  j                         s{| j                  j                         \  }}|t        u s|t        u r|j                          n"|t        u r|j                  d t!                      | j                  j                         s{y# 1 sw Y   xY w)Nzenvironment stop requested.FT)rT   rg   rp   rw   r   r   r^   r   joinemptyr\   r_   r]   r*   rd   r-   r   )r%   r   rb   r9   s       r   stopzSqliteQueueDatabase.stop  s    23[[ 	$	$ 	$ !!8T"23\\F#D	$ 	 ##))+''++-GBU{bGm	ut%6%89 ##))+ %	$ 	$s   D4DD$c                 ^    | j                   5  | j                  cd d d        S # 1 sw Y   y xY wr2   )rp   rw   r3   s    r   
is_stoppedzSqliteQueueDatabase.is_stopped(  s'    [[ 	$##	$ 	$ 	$s   #,c                    | j                   5  | j                  r
	 d d d        y| j                  j                         }| j                  j                  t        |f       d d d        j                          y # 1 sw Y   xY wr$   )rp   rw   ry   r&   r   r   r_   r/   r%   evts     r   pausezSqliteQueueDatabase.pause,  sm    [[ 	0	0 	0 %%++-C!!5#,/	0 	
	0 	0   A8;A88Bc                    | j                   5  | j                  r
	 d d d        y| j                  j                         }| j                  j                  t        |f       d d d        j                          y # 1 sw Y   xY wr$   )rp   rw   ry   r&   r   r   r]   r/   r   s     r   unpausezSqliteQueueDatabase.unpause6  sm    [[ 	2	2 	2 %%++-C!!7C.1	2 	
	2 	2r   c                 0    t        dt        |       z        )Nz#This method is not supported by %r.)r   type)r%   r   r   s      r   __unsupported__z#SqliteQueueDatabase.__unsupported__@  s    >dKLLr   )FTNNr2   )NN)r   r   r   r   r'   rx   rq   rz   r   rt   r{   r   r   r   r   r   atomictransaction	savepoint__classcell__)r   s   @r   rl   rl      sa    * >B6:@>
8)
,$M'66F6[9r   rl   c                   ,    e Zd ZdZddZd ZddZd Zy)r   r~   Nc                     || _         y r2   r   )r%   r~   s     r   r'   zThreadHelper.__init__H  s
    ,r   c                     t               S r2   r   r3   s    r   r&   zThreadHelper.eventK  s    EG^r   c                 B    ||n| j                   }t        |xs d      S Nr   )maxsize)r~   r   r%   max_sizes     r   rN   zThreadHelper.queueM  s#    '389L9LX]++r   c                 0    t        |||      }d|_        |S )N)targetr   r   T)r   daemon)r%   fnr   r   r   s        r   r   zThreadHelper.threadQ  s    rV<r   r2   )r   r   r   rG   r'   r&   rN   r   r   r   r   r   r   E  s    #I- $,r   r   c                   $    e Zd ZdZd ZddZd Zy)r   r   c                     t               S r2   )GEventr3   s    r   r&   zGreenletHelper.eventZ  s    FH_r   Nc                 B    ||n| j                   }t        |xs d      S r   )r~   GQueuer   s     r   rN   zGreenletHelper.queue\  s#    '389L9Lhm!,,r   c                 ,    fd}t        |g|i |S )Nc                  <    t        j                           | i |S r2   )geventsleep)ar   r   s     r   wrapz#GreenletHelper.thread.<locals>.wrapa  s    LLNq;A;r   )GThread)r%   r   r   r   r   s    `   r   r   zGreenletHelper.thread`  s    	 t-d-f--r   r2   )r   r   r   rG   r&   rN   r   r   r   r   r   r   W  s    I$-.r   r   )"loggingrN   r   	threadingr   r   r   r   r   r   gevent.eventr   gevent.queuer   ImportErrorpeeweer	   	getLoggerrT   ri   r   r   r   objectr   r8   r^   rd   r_   r]   rK   rl   r   r   r   r   r   <module>r      s        %*,, " 
		+	,	I 		9 			 	O& Ob 
(D+V D+NE7. E7P6 $.\ .W
  % $$G$fv%s   B< <C
	C
