
    -iY0                         d Z ddlZddl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	dZ
n# e$ r dZ
Y nw xY w e            ZdefdZd Z G d	 d
          Ze
r G d d          ZdS  G d d          ZdS )zz
pickleDB - https://patx.github.io/pickledb
Harrison Erd - https://harrisonerd.com/
Licensed - BSD 3 Clause (see LICENSE)
    N)AnyTFreturnc                  P    	 t          j                     dS # t          $ r Y dS w xY w)z<Return True if we're currently running inside an event loop.TF)asyncioget_running_loopRuntimeError     F/var/www/html/speakWrite/venv/lib/python3.11/site-packages/pickledb.pyin_asyncr      s=     """t   uus    
%%c                       fd}|S )z
    Decorator that lets an async method be called in both sync and async code.

    - In async code: returns the coroutine (you must `await` it).
    - In sync code: runs the coroutine with asyncio.run() and returns the result.
    c                 d     | g|R i |}t                      r|S t          j        |          S N)r   r   run)selfargskwargscorofuncs       r   wrapperzdualmethod.<locals>.wrapper)   sC    tD*4***6**:: 	K{4   r
   r	   )r   r   s   ` r   
dualmethodr   "   s#    ! ! ! ! !
 Nr
   c                       e Zd ZdZdefdZd Zd Zd Zd Z	e
defd	            Ze
defd
            Ze
defd            Ze
dd            Ze
defd            Ze
d             Ze
defd            ZdS )PickleDBz
    A unified async/sync key-value store using orjson + aiofiles.

    All data is kept in-memory in `self.db` and serialized to disk as a single
    orjson-encoded file at `self.location`.
    locationc                     t           j                            |          | _        i | _        t          j                    | _        d S r   )ospath
expanduserr   dbr   Lock_lock)r   r   s     r   __init__zPickleDB.__init__9   s1    **844"$\^^


r
   c                 .    |                                   | S r   loadr   s    r   	__enter__zPickleDB.__enter__>   s    		r
   c                 6    ||                                   d S d S r   saver   exc_typeexc_valexc_tbs       r   __exit__zPickleDB.__exit__B   s!    IIKKKKK r
   c                 >   K   |                                   d {V  | S r   r$   r&   s    r   
__aenter__zPickleDB.__aenter__F   s+      iikkr
   c                 F   K   ||                                   d {V  d S d S r   r)   r+   s       r   	__aexit__zPickleDB.__aexit__J   s:      ))++ r
   r   c                   K   t           j                            | j                  rt           j                            | j                  dk    rst          j        | j        d          4 d{V }|                                 d{V }ddd          d{V  n# 1 d{V swxY w Y   t          j	        |          }ni }| j
        4 d{V  || _        ddd          d{V  n# 1 d{V swxY w Y   | S )z
        Load JSON database from disk into memory.

        Returns `self` to allow chaining:
            db = PickleDB("Example.json").load()
        r   rbN)r   r   existsr   getsizeaiofilesopenreadorjsonloadsr!   r   )r   fdatanew_dbs       r   r%   zPickleDB.loadN   s      7>>$-(( 	RW__T]-K-Ka-O-O}T]D99 & & & & & & &QVVXX~~~~~~& & & & & & & & & & & & & & & & & & & & & & & & & & &\$''FFF: 	 	 	 	 	 	 	 	DG	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s$   /B
B&)B&C++
C58C5c                   K   | j          d}| j        4 d{V  t          j        |d          4 d{V }|                    t          j        | j                             d{V  ddd          d{V  n# 1 d{V swxY w Y   t          j	        t          j        || j                    d{V  ddd          d{V  n# 1 d{V swxY w Y   dS )z
        Atomically save database to disk.

        Writes to `<location>.tmp` and then os.replace() over the original file.
        Returns True on success.
        z.tmpNwbT)r   r!   r8   r9   writer;   dumpsr   r   	to_threadr   replace)r   tempr=   s      r   r*   zPickleDB.savea   s      -%%%: 	E 	E 	E 	E 	E 	E 	E 	E}T400 5 5 5 5 5 5 5Aggfl47334444444445 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5#BJdmDDDDDDDDD	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E ts4   C	3A;)C	;
B	C	B		.C		
CCc                    K   | j         4 d{V  || j        t          |          <   ddd          d{V  n# 1 d{V swxY w Y   dS )z*Set a key-value pair. Always returns True.NT)r!   r   str)r   keyvalues      r   setzPickleDB.setp   s       : 	& 	& 	& 	& 	& 	& 	& 	& %DGCHH	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&ts   :
AANc                    K   | j         4 d{V  | j                            t          |          |          cddd          d{V  S # 1 d{V swxY w Y   dS )z+Get a key's value, or `default` if missing.N)r!   r   getrH   )r   rI   defaults      r   rM   zPickleDB.getw   s       : 	2 	2 	2 	2 	2 	2 	2 	27;;s3xx11	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2s   (A
AAc                    K   | j         4 d{V  | j                            t          |          d          ducddd          d{V  S # 1 d{V swxY w Y   dS )zERemove a key-value pair. Returns True if it existed, False otherwise.N)r!   r   poprH   )r   rI   s     r   removezPickleDB.remove}   s       : 	; 	; 	; 	; 	; 	; 	; 	;7;;s3xx..d:	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;s   *A
AAc                    K   | j         4 d{V  t          | j                                                  cddd          d{V  S # 1 d{V swxY w Y   dS )zReturn a list of all keys.N)r!   listr   keysr&   s    r   allzPickleDB.all   s       : 	( 	( 	( 	( 	( 	( 	( 	(''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(s   &A		
AAc                    K   | j         4 d{V  | j                                         ddd          d{V  n# 1 d{V swxY w Y   dS )zBRemove all key-value pairs from the database. Always returns True.NT)r!   r   clearr&   s    r   purgezPickleDB.purge   s       : 	 	 	 	 	 	 	 	GMMOOO	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	ts   <
A	Ar   )__name__
__module____qualname____doc__rH   r"   r'   r/   r1   r3   r   boolr%   r*   rK   rM   rQ   rU   rX   r	   r
   r   r   r   1   ss        $ $ $ $ $
         D    Z$ D    Z t    Z 2 2 2 Z2
 ;4 ; ; ; Z;
 ( ( Z(
 T    Z  r
   r   c                       e Zd ZdZ	 	 ddededdfdZd	edefd
ZdedefdZ	dedz  d	edefdZ
efdededefdZdedefdZdee         fdZdefdZddZdS )PickleDBSQLitea$  
        A unified async/sync key-value store backed by SQLite.

        Each key is stored as a row:

            CREATE TABLE kv (
                key   TEXT PRIMARY KEY,
                value BLOB NOT NULL
            )

        Values are stored as JSON-encoded bytes via orjson.
        pickledb.sqlite3kvsqlite_path
table_namer   Nc                    || _         || _        t          j        | j         d          | _        t          j        | j        _        | j                            d| j         d           | j                                         d S )NF)check_same_threadz,
                CREATE TABLE IF NOT EXISTS zy (
                    key   TEXT PRIMARY KEY,
                    value BLOB NOT NULL
                )
                )	rb   rc   sqlite3connect_connRowrow_factoryexecutecommit)r   rb   rc   s      r   r"   zPickleDBSQLite.__init__   s    
  +D(DO )9USSSDJ%,[DJ"J,0O     Jr
   rJ   c                 *    t          j        |          S )z2Serialize a Python object to orjson-encoded bytes.)r;   rC   )r   rJ   s     r   _dumpszPickleDBSQLite._dumps   s    <&&&r
   r>   c                 *    t          j        |          S )z;Deserialize orjson-encoded bytes back into a Python object.)r;   r<   )r   r>   s     r   _loadszPickleDBSQLite._loads   s    <%%%r
   rI   c                     t                      rdt          f fd} |            S                                }at          t          j                              } j                            d j         d||f            j                                         |S  j                            d j         dt                    |f            j                                         t                    S )z
            Set a key-value pair.

            If key is None, generate a new random UUID key and return it.

            In async code, returns a coroutine you must `await`.
            In sync code, returns the key string directly.
            r   c                    K   t          j        j                  4 d {V } t          j        | _                                      }ut          t          j	                              }| 
                    dj         d||f           d {V  |                                  d {V  |cd d d           d {V  S | 
                    dj         dt                    |f           d {V  |                                  d {V  t                    cd d d           d {V  S # 1 d {V swxY w Y   d S )NINSERT INTO  (key, value) VALUES (?, ?)z)
                            INSERT INTO z (key, value)
                            VALUES (?, ?)
                            ON CONFLICT(key) DO UPDATE SET value=excluded.value
                            )	aiosqliterg   rb   rf   ri   rj   rn   rH   uuiduuid4rk   rc   rl   )r   payloadnew_keyrI   r   rJ   s      r   _asetz!PickleDBSQLite.set.<locals>._aset   sI     (01ABB ( ( ( ( ( ( (b)0"&++e"4"4;&)$*,,&7&7G"$** [t [ [ [!(' 2# #        #%))++-------#*( ( ( ( ( ( ( ( ( ( ( ( ( ( !jj )-     
 !XXw/         !iikk)))))))"3xx-( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( (s   BD0AD00
D:=D:Nrs   rt   z
                INSERT INTO z (key, value)
                VALUES (?, ?)
                ON CONFLICT(key) DO UPDATE SET value=excluded.value
                )	r   rH   rn   rv   rw   rh   rk   rc   rl   )r   rI   rJ   rz   rx   ry   s   ```   r   rK   zPickleDBSQLite.set   s)    zz (S ( ( ( ( ( ( ( (2 uwwkk%((G{djll++
""O4?OOOg&   
!!###J!_  
 S7#   Js88Or
   rN   c                 P    t                      rdt          f fd} |            S  j                            d j         dt                    f          }|                                }|t          u rt                    S  	                    |d                   S )a9  
            Get the value for a key.

            If the key does not exist:
                - If default is MISSING, raises KeyError.
                - Otherwise returns default.

            In async code, returns a coroutine you must `await`.
            In sync code, returns the value directly.
            r   c                    K   t          j        j                  4 d {V } t          j        | _        |                     dj         dt                    f           d {V }|	                                 d {V }|
                                 d {V  |,t          u rt                    cd d d           d {V  S                     |d                   cd d d           d {V  S # 1 d {V swxY w Y   d S )NSELECT value FROM  WHERE key = ?rJ   )ru   rg   rb   rf   ri   rj   rk   rc   rH   fetchonecloseMISSINGKeyErrorrp   )r   cursorrowrN   rI   r   s      r   _agetz!PickleDBSQLite.get.<locals>._aget  s     (01ABB 9 9 9 9 9 9 9b)0')zzPPPP XXK( ( " " " " " " %+OO$5$5555555$llnn,,,,,,,;&'11&.smm 3#*9 9 9 9 9 9 9 9 9 9 9 9 9 9  ${{3w<889 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9s   BC7
C77
DDr}   r~   NrJ   )
r   r   rh   rk   rc   rH   r   r   r   rp   )r   rI   rN   r   r   r   s   ```   r   rM   zPickleDBSQLite.get   s     zz 9S 9 9 9 9 9 9 9 9" uwwZ''DT_DDDS F //##C{g%%"3--';;s7|,,,r
   c                      t                      rdt          f fd} |            S  j                            d j         dt                    f          } j                                         |j        dk    S )z
            Remove a key-value pair.

            Returns True if a row was deleted, False otherwise.

            In async code, returns a coroutine you must `await`.
            In sync code, returns a bool directly.
            r   c                  B  K   t          j        j                  4 d {V } |                     dj         dt                    f           d {V }|                                  d {V  |j        dk    cd d d           d {V  S # 1 d {V swxY w Y   d S )NDELETE FROM r~   r   )ru   rg   rb   rk   rc   rH   rl   rowcount)r   r   rI   r   s     r   _aremovez'PickleDBSQLite.remove.<locals>._aremove*  sN     (01ABB 3 3 3 3 3 3 3b')zzJ4?JJJ XXK( ( " " " " " " !iikk)))))))%23 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3s   AB
BBr   r~   r   )r   r]   rh   rk   rc   rH   rl   r   )r   rI   r   r   s   ``  r   rQ   zPickleDBSQLite.remove   s     zz 
"3 3 3 3 3 3 3 3  xzz!Z''>t>>>S F J?Q&&r
   c                      t                      r"dt          t                   f fd} |            S  j                            d j         d          }d |                                D             S )z
            Return a list of all keys in the database.

            In async code, returns a coroutine you must `await`.
            In sync code, returns the list directly.
            r   c                  |  K   t          j        j                  4 d {V } t          j        | _        |                     dj         d           d {V }|                                 d {V }|	                                 d {V  d |D             cd d d           d {V  S # 1 d {V swxY w Y   d S )NSELECT key FROM  ORDER BY keyc                     g | ]
}|d          S rI   r	   .0r   s     r   
<listcomp>z5PickleDBSQLite.all.<locals>._aall.<locals>.<listcomp>L  s    ;;;sE
;;;r
   )
ru   rg   rb   rf   ri   rj   rk   rc   fetchallr   )r   r   rowsr   s      r   _aallz!PickleDBSQLite.all.<locals>._aallD  st     (01ABB < < < < < < <b)0')zzMtMMM( ( " " " " " " &,__%6%6666666$llnn,,,,,,,;;d;;;< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <s   A5B++
B58B5r   r   c                     g | ]
}|d          S r   r	   r   s     r   r   z&PickleDBSQLite.all.<locals>.<listcomp>S  s    <<<3CJ<<<r
   )r   rS   rH   rh   rk   rc   r   )r   r   r   s   `  r   rU   zPickleDBSQLite.all<  s     zz <T#Y < < < < < < uwwZ''A4?AAA F =<&//*;*;<<<<r
   c                      t                      rdt          f fd} |            S  j                            d j                     j                                         dS )z
            Remove all key-value pairs from the database.

            Always returns True.

            In async code, returns a coroutine you must `await`.
            In sync code, returns True directly.
            r   c                    K   t          j        j                  4 d {V } |                     dj                    d {V  |                                  d {V  	 d d d           d {V  dS # 1 d {V swxY w Y   d S )Nr   T)ru   rg   rb   rk   rc   rl   )r   r   s    r   _apurgez%PickleDBSQLite.purge.<locals>._apurge_  s(     (01ABB $ $ $ $ $ $ $b jj)I)I)IJJJJJJJJJ iikk)))))))#$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $s   ?A55
A?A?r   T)r   r]   rh   rk   rc   rl   )r   r   s   ` r   rX   zPickleDBSQLite.purgeU  s~     zz !$t $ $ $ $ $ $ wyy J?do??@@@J4r
   c                 v     t                      rd fd} |            S  j                                         dS )z
            Close the underlying sync SQLite connection.

            In async code, returns a coroutine you must `await`.
            In sync code, closes immediately.
            r   Nc                  >   K    j                                          d S r   )rh   r   r&   s   r   _aclosez%PickleDBSQLite.close.<locals>._acloses  s!      J$$&&&&&r
   r   N)r   rh   r   )r   r   s   ` r   r   zPickleDBSQLite.closek  sU     zz !' ' ' ' ' 'wyy Jr
   )r`   ra   r   )rY   rZ   r[   r\   rH   r"   r   bytesrn   rp   rK   r   rM   r]   rQ   rS   rU   rX   r   r	   r
   r   r_   r_      sq       	 	  2"	  	 	  	  		  	  	  	 *	' 	' 	' 	' 	' 	'	&u 	& 	& 	& 	& 	&8	3: 8	c 8	c 8	 8	 8	 8	t 07 (	- (	-3 (	- (	-3 (	- (	- (	- (	-T	'c 	'd 	' 	' 	' 	'8	=c 	= 	= 	= 	=2	4 	 	 	 	,	 	 	 	 	 	r
   r_   c                   &    e Zd ZdZdededdfdZdS )r_   zy
        This class is only usable if `aiosqlite` is installed, e.g.:
            pip install "pickledb[sqlite]"
        r   r   r   Nc                      t          d          )NzUPickleDBSQLite requires `aiosqlite`. Install it via `pip install "pickledb[sqlite]"`.)r   )r   r   r   s      r   r"   zPickleDBSQLite.__init__  s    E  r
   )rY   rZ   r[   r\   r   r"   r	   r
   r   r_   r_   {  sG        	 	
	# 	 	 	 	 	 	 	 	r
   )r\   r   r   typingr   rv   r;   r8   rf   ru   sqlite_enableImportErrorobjectr   r]   r   r   r   r_   r	   r
   r   <module>r      sw     				         NNNMM   MMM &(($      ] ] ] ] ] ] ] ]~  uf f f f f f f f f fT
 
 
 
 
 
 
 
 
 
s   
) 33