
    X-Ph                        d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	 ddl
mZmZ erddlZ G d d          ZddZ G d de          ZdZddZ G d de          ZdS )a:  Interfaces for accessing metadata.

We provide two implementations.
 * The "classic" file system implementation, which uses a directory
   structure of files.
 * A hokey sqlite backed implementation, which basically simulates
   the file system in an effort to work around poor file system performance
   on OS X.
    )annotationsN)abstractmethod)Iterable)TYPE_CHECKINGAnyc                      e Zd ZdZedd            Zedd            Zeddd            Zedd            Zedd            Z	edd            Z
d	S )MetadataStorez'Generic interface for metadata storage.namestrreturnfloatc                    dS )zfRead the mtime of a metadata entry.

        Raises FileNotFound if the entry does not exist.
        N selfr
   s     N/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/mypy/metastore.pygetmtimezMetadataStore.getmtime             bytesc                    dS )ziRead the contents of a metadata entry.

        Raises FileNotFound if the entry does not exist.
        Nr   r   s     r   readzMetadataStore.read$   r   r   Ndatamtimefloat | Noneboolc                    dS )zWrite a metadata entry.

        If mtime is specified, set it as the mtime of the entry. Otherwise,
        the current time is used.

        Returns True if the entry is successfully written, False otherwise.
        Nr   )r   r
   r   r   s       r   writezMetadataStore.write+   r   r   Nonec                    dS )zDelete a metadata entryNr   r   s     r   removezMetadataStore.remove5   r   r   c                    dS )zIf the backing store requires a commit, do it.

        But N.B. that this is not *guaranteed* to do anything, and
        there is no guarantee that changes are not made until it is
        called.
        Nr   r   s    r   commitzMetadataStore.commit9   r   r   Iterable[str]c                    d S Nr   r#   s    r   list_allzMetadataStore.list_allB   s    ),r   r
   r   r   r   r
   r   r   r   r'   r
   r   r   r   r   r   r   r   r
   r   r   r   r   r   r   r%   )__name__
__module____qualname____doc__r   r   r   r   r!   r$   r(   r   r   r   r	   r	      s        11   ^    ^     ^ & & & ^&    ^ ,,, ^,,,r   r	   r   r   c                 t    t          j        t          j        d                                        d          S )N   ascii)binasciihexlifyosurandomdecoder   r   r   random_stringr;   F   s)    BJqMM**11':::r   c                  H    e Zd ZddZddZdd
ZdddZddZddZddZ	dS )FilesystemMetadataStorecache_dir_prefixr   r   r   c                d    |                     t          j                  r	d | _        d S || _        d S r'   )
startswithr8   devnullr>   r   r>   s     r   __init__z FilesystemMetadataStore.__init__K   s8     &&rz22 	5$(D!!!$4D!!!r   r
   r   c                    | j         st                      t          t          j                            t          j                            | j         |                              S r'   )r>   FileNotFoundErrorintr8   pathr   joinr   s     r   r   z FilesystemMetadataStore.getmtimeT   sJ    $ 	&#%%%27##BGLL1F$M$MNNOOOr   r   c                   t           j                            |          t           j                            |          k    s
J d            | j        st                      t          t           j                            | j        |          d          5 }|                                cd d d            S # 1 swxY w Y   d S )NDon't use absolute paths!rb)	r8   rG   normpathabspathr>   rE   openrH   r   )r   r
   fs      r   r   zFilesystemMetadataStore.readZ   s    w%%)>)>>>>@[>>>$ 	&#%%%"',,t4d;;TBB 	a6688	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   B44B8;B8Nr   r   r   r   c                   t           j                            |          t           j                            |          k    s
J d            | j        sdS t           j                            | j        |          }|dz   t                      z   }	 t          j        t           j                            |          d           t          |d          5 }|
                    |           d d d            n# 1 swxY w Y   t          j        ||           |t          j        |||f           n# t          $ r Y dS w xY wdS )NrJ   F.Texist_okwb)times)r8   rG   rL   rM   r>   rH   r;   makedirsdirnamerN   r   replaceutimeOSError)r   r
   r   r   rG   tmp_filenamerO   s          r   r   zFilesystemMetadataStore.writec   sf   w%%)>)>>>>@[>>>$ 	5w||D1488czMOO3		K--====lD)) Q              J|T*** eU^4444 	 	 	55	ts7   AD. C3'D. 3C77D. :C7;2D. .
D<;D<c                    | j         st                      t          j        t          j                            | j         |                     d S r'   )r>   rE   r8   r!   rG   rH   r   s     r   r!   zFilesystemMetadataStore.removew   sB    $ 	&#%%%
	"',,t4d;;<<<<<r   c                    d S r'   r   r#   s    r   r$   zFilesystemMetadataStore.commit}   s    r   r%   c              #  .  K   | j         sd S t          j        | j                   D ]o\  }}}t          j                            || j                   }|D ]A}t          j                            t          j                            ||                    V  Bpd S r'   )r>   r8   walkrG   relpathrL   rH   )r   dir_filesfiles        r   r(   z FilesystemMetadataStore.list_all   s      $ 	FWT%:;; 	@ 	@MCE'//#t'<==C @ @g&&rw||C'>'>??????@	@ 	@r   r>   r   r   r   r)   r*   r'   r+   r,   r-   r.   )
r/   r0   r1   rC   r   r   r   r!   r$   r(   r   r   r   r=   r=   J   s        5 5 5 5P P P P       (= = = =   @ @ @ @ @ @r   r=   z
CREATE TABLE IF NOT EXISTS files2 (
    path TEXT UNIQUE NOT NULL,
    mtime REAL,
    data BLOB
);
CREATE INDEX IF NOT EXISTS path_idx on files2(path);
db_filesqlite3.Connectionc                v    dd l }|j                            |           }|                    t                     |S )Nr   )sqlite3.dbapi2dbapi2connectexecutescriptSCHEMA)rf   sqlite3dbs      r   
connect_dbrp      s<    				(	(BVIr   c                  P    e Z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
dS )!SqliteMetadataStorer>   r   r   r   c                    |                     t          j                  r	d | _        d S t          j        |d           t          t          j                            |d                    | _        d S )NTrR   zcache.db)r@   r8   rA   ro   rV   rp   rG   rH   rB   s     r   rC   zSqliteMetadataStore.__init__   sb     &&rz22 	DGF
$t4444RW\\*:JGGHHr   r
   fieldr   c                    | j         st                      | j                             d| d|f          }|                                }|st                      t	          |          dk    sJ |d         d         S )NzSELECT z FROM files2 WHERE path = ?   r   )ro   rE   executefetchalllen)r   r
   rt   curresultss        r   _queryzSqliteMetadataStore._query   s    w 	&#%%%gooJJJJTGTT,,.. 	&#%%%7||q    qz!}r   r   c                `    |                      |d          }t          |t                    sJ |S )Nr   )r|   
isinstancer   )r   r
   r   s      r   r   zSqliteMetadataStore.getmtime   s0    D'**%'''''r   r   c                `    |                      |d          }t          |t                    sJ |S )Nr   )r|   r~   r   )r   r
   r   s      r   r   zSqliteMetadataStore.read   s0    {{4(($&&&&&r   Nr   r   r   r   c                    dd l }| j        sdS 	 |t          j                    }| j                            d|||f           n# |j        $ r Y dS w xY wdS )Nr   Fz@INSERT OR REPLACE INTO files2(path, mtime, data) VALUES(?, ?, ?)T)rn   ro   timerw   OperationalError)r   r
   r   r   rn   s        r   r   zSqliteMetadataStore.write   s    w 	5	}	GOORud#    ' 	 	 	55	ts   3A 
AAc                h    | j         st                      | j                             d|f           d S )Nz!DELETE FROM files2 WHERE path = ?)ro   rE   rw   r   s     r   r!   zSqliteMetadataStore.remove   s7    w 	&#%%%;dWEEEEEr   c                J    | j         r| j                                          d S d S r'   )ro   r$   r#   s    r   r$   zSqliteMetadataStore.commit   s.    7 	GNN	 	r   r%   c              #  j   K   | j         r'| j                             d          D ]}|d         V  d S d S )NzSELECT path FROM files2r   )ro   rw   )r   rows     r   r(   zSqliteMetadataStore.list_all   sQ      7 	w'@AA  !f	 	 r   re   )r
   r   rt   r   r   r   r)   r*   r'   r+   r,   r-   r.   )r/   r0   r1   rC   r|   r   r   r   r!   r$   r(   r   r   r   rr   rr      s        	I 	I 	I 	I
 
 
 
   
   
     F F F F        r   rr   )r   r   )rf   r   r   rg   )r2   
__future__r   r6   r8   r   abcr   collections.abcr   typingr   r   rn   r	   r;   r=   rm   rp   rr   r   r   r   <module>r      sL    # " " " " "  				        $ $ $ $ $ $ % % % % % % % %  NNN)- )- )- )- )- )- )- )-X; ; ; ;=@ =@ =@ =@ =@m =@ =@ =@@
   ? ? ? ? ?- ? ? ? ? ?r   