
    Be|                    t    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m	Z	 d dl
mZmZ  G d deeef         Zy)    )annotationsN)Iterator)quoteunquote)ZictBaselockedc                       e Zd ZU dZded<   ded<   ded<   ded	<   dd fd
ZddZedd       ZddZ	e	Z
edd       Ze	 	 	 	 	 	 dd       ZddZddZedd       ZddZ xZS )Filea  Mutable Mapping interface to a directory

    Keys must be strings, values must be buffers

    Note this shouldn't be used for interprocess persistence, as keys
    are cached in memory.

    Parameters
    ----------
    directory: str
        Directory to write to. If it already exists, existing files will be imported as
        mapping elements. If it doesn't exists, it will be created.
    memmap: bool (optional)
        If True, use `mmap` for reading. Defaults to False.

    Notes
    -----
    If you call methods of this class from multiple threads, access will be fast as long
    as atomic disk access such as ``open``, ``os.fstat``, and ``os.remove`` is fast.
    This is not always the case, e.g. in case of slow network mounts or spun-down
    magnetic drives.
    Bytes read/write in the files is not protected by locks; this could cause failures
    on Windows, NFS, and in general whenever it's not OK to delete a file while there
    are file descriptors open on it.

    Examples
    --------
    >>> z = File('myfile')  # doctest: +SKIP
    >>> z['x'] = b'123'  # doctest: +SKIP
    >>> z['x']  # doctest: +SKIP
    b'123'

    Also supports writing lists of bytes objects

    >>> z['y'] = [b'123', b'4567']  # doctest: +SKIP
    >>> z['y']  # doctest: +SKIP
    b'1234567'

    Or anything that can be used with file.write, like a memoryview

    >>> z['data'] = np.ones(5).data  # doctest: +SKIP
    str	directoryboolmemmapzdict[str, str]	filenamesint_incc                   t         |           t        |      | _        || _        i | _        d| _        t        j                  j                  | j                        s"t        j                  | j                  d       y t        j                  | j                        D ]5  }|| j
                  | j                  |      <   | xj                  dz  c_        7 y )Nr   T)exist_ok   )super__init__r   r   r   r   r   ospathexistsmakedirslistdir_unsafe_key)selfr   r   fn	__class__s       )lib/python3.12/site-packages/zict/file.pyr   zFile.__init__=   s    Y	ww~~dnn-KK6jj0 79t//34		Q	    c                j    t        |d      d| j                   z   }| xj                  dz  c_        |S )a8  Escape key so that it is usable on all filesystems.

        Append to the filenames a unique suffix that changes every time this method is
        called. This prevents race conditions when another thread accesses the same
        key, e.g. ``__setitem__`` on one thread and ``__getitem__`` on another.
         )safe#r   )r   r   r   keys     r    	_safe_keyzFile._safe_keyK   s1     Cb!a		{O3		Q	
r!   c                @    | j                  d      d   } t        |       S )z%Undo the escaping done by _safe_key()r%   r   )splitr   )r'   s    r    r   zFile._unsafe_keyW   s      iinQs|r!   c                :    d| j                    dt        |        dS )Nz<File: z, z
 elements>)r   lenr   s    r    __str__zFile.__str__]   s     (3t9+Z@@r!   c                X   t         j                  j                  | j                  | j                  |         }| j
                  rCt        |d      5 }t        t        j                  |j                         d            cd d d        S t        |d      5 }t        j                  |j                               j                  }t        |      }| j                         5  |j                  |      }d d d        |k(  sJ |cd d d        S # 1 sw Y   y xY w# 1 sw Y   'xY w# 1 sw Y   y xY w)Nzr+br   rb)r   r   joinr   r   r   open
memoryviewmmapfilenofstatst_size	bytearrayunlockreadinto)r   r'   r   fhsizebufnreads          r    __getitem__zFile.__getitem__b   s    WW\\$..$..*=> ;;b% =B!$))BIIK";<= = b$ 2xx		,44o[[] -KK,E-}$ = =- - s1   -DA	D D.D DD	D  D)c                L   | j                  |       | j                  |      }t        t        j                  j                  | j                  |      d      5 }| j                         5  t        |t        t        f      r|j                  |       n|j                  |       d d d        d d d        || j                  v r>t        j                  t        j                  j                  | j                  |             y || j                  |<   y # 1 sw Y   mxY w# 1 sw Y   qxY w)Nwb)discardr(   r2   r   r   r1   r   r9   
isinstancetuplelist
writelineswriter   remove)r   r'   valuer   r;   s        r    __setitem__zFile.__setitem__y   s     	S^^C "'',,t~~r2D9 	 R 	 %%/e$		  	  $..  IIbggll4>>267"$DNN3	  	  	  	 s$   D(:D"DD	DD#c                    || j                   v S N)r   r&   s     r    __contains__zFile.__contains__   s    dnn$$r!   c                ,    t        | j                        S rL   )iterr   r-   s    r    __iter__zFile.__iter__   s    DNN##r!   c                    | j                   j                  |      }t        j                  t        j                  j                  | j                  |             y rL   )r   popr   rH   r   r1   r   )r   r'   r   s      r    __delitem__zFile.__delitem__   s5    ^^$
		"'',,t~~r23r!   c                ,    t        | j                        S rL   )r,   r   r-   s    r    __len__zFile.__len__   s    4>>""r!   )F)r   zstr | pathlib.Pathr   r   )r'   r   returnr   )rV   r   )r'   r   rV   zbytearray | memoryview)r'   r   rI   zrbytes | bytearray | memoryview | list[bytes | bytearray | memoryview] | tuple[bytes | bytearray | memoryview, ...]rV   None)r'   objectrV   r   )rV   zIterator[str])r'   r   rV   rW   )rV   r   )__name__
__module____qualname____doc____annotations__r   r(   staticmethodr   r.   __repr__r   r?   rJ   rM   rP   rS   rU   __classcell__)r   s   @r    r
   r
      s    )V NL
I
  
A H , %%5% 
% %0%$ 4 4#r!   r
   )
__future__r   r4   r   pathlibcollections.abcr   urllib.parser   r   zict.commonr   r   r   bytesr
    r!   r    <module>rh      s1    "  	  $ ' (R#8CJ R#r!   