o
    Óó·i1)  ã                   @  s˜   d 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mZm	Z	m
Z
mZmZ ddlmZ eddd�ZG dd	„ d	ƒZG d
d„ deƒZddd„Zg d¢ZdS )aw  
A L{DirDBM} is a L{dbm}-style interface to a directory.

Each key is stored as a single file.  This is not expected to be very fast or
efficient, but it's good for easy debugging.

L{DirDBM}s are *not* thread-safe, they should only be accessed by one thread at
a time.

No files should be placed in the working directory of a DirDBM save those
created by the DirDBM itself!
é    )ÚannotationsN)ÚAnyStrÚIterableÚMappingÚTypeVarÚoverload)ÚFilePathÚ_T)Úboundc                   @  s   e Zd ZdZdFdd„ZdGd	d
„ZdGdd„ZdHdd„ZdIdd„ZdJdd„Z	dKdd„Z
dGdd„ZdLdd„ZdMdd „ZdMd!d"„ZdNd$d%„ZdOd(d)„ZdPd+d,„ZedQd-d.„ƒZedRd2d.„ƒZdSdTd6d.„ZdOd7d8„ZdUd;d<„ZdVd=d>„ZdWd?d@„ZdWdAdB„ZdXdDdE„Zd3S )YÚDirDBMz®
    A directory with a DBM interface.

    This class presents a hash-like interface to a directory of small,
    flat files. It can only use strings as keys or values.
    ÚnameÚbytesÚreturnÚNonec                 C  s¦   t j |¡| _t|ƒ| _| j ¡ s| j ¡  dS t | j 	d¡j¡D ]}t  
|¡ q"t | j 	d¡j¡}|D ]}|dd… }t j |¡rJt  
|¡ q6t  ||¡ q6dS )zJ
        @param name: Base path to use for the directory storage.
        z*.newz*.rplNéüÿÿÿ)ÚosÚpathÚabspathZdnamer   Ú
_dnamePathÚisdirZcreateDirectoryÚglobÚchildÚremoveÚexistsÚrename)Úselfr   ÚfÚreplacementsÚold© r   ú^C:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/persisted/dirdbm.pyÚ__init__(   s   


ûzDirDBM.__init__Úkc                 C  s   t  |¡ dd¡ dd¡S )z?
        Encode a key so it can be used as a filename.
        ó   
ó   _ó   /ó   -N)Úbase64ÚencodebytesÚreplace©r   r"   r   r   r    Ú_encodeC   s   zDirDBM._encodec                 C  s   t  | dd¡ dd¡¡S )z3
        Decode a filename to get the key.
        r$   r#   r&   r%   N)r'   Údecodebytesr)   r*   r   r   r    Ú_decodeJ   ó   zDirDBM._decoder   úFilePath[AnyStr]c                 C  s6   |  ¡ �}| ¡ }W d  ƒ |S 1 sw   Y  |S )z�
        Read in the contents of a file.

        Override in subclasses to e.g. provide transparently encrypted dirdbm.
        N)ÚopenÚread)r   r   r   Úsr   r   r    Ú	_readFileP   s   


ÿþzDirDBM._readFileÚdatac                 C  sB   |  d¡�}| |¡ | ¡  W d  ƒ dS 1 sw   Y  dS )zw
        Write data to a file.

        Override in subclasses to e.g. provide transparently encrypted dirdbm.
        ÚwN)r0   ÚwriteÚflush)r   r   r4   r   r   r   r    Ú
_writeFileZ   s   

"þzDirDBM._writeFileÚintc                 C  s   t | j ¡ ƒS )zF
        @return: The number of key/value pairs in this Shelf
        N)Úlenr   Úlistdir©r   r   r   r    Ú__len__d   s   zDirDBM.__len__Úvc                 C  s¤   t |ƒtks
tdƒ‚t |ƒtkstdƒ‚|  |¡}| j |¡}| ¡ r)| d¡}n| d¡}z|  ||¡ W n t	yB   | 
¡  ‚ w | ¡ rK| 
¡  | |¡ dS )zÔ
        C{dirdbm[k] = v}
        Create or modify a textfile in this directory

        @type k: bytes
        @param k: key to set

        @type v: bytes
        @param v: value to associate with C{k}
        úDirDBM key must be byteszDirDBM value must be bytesz.rplz.newN)Útyper   Ú	TypeErrorr+   r   r   r   ZsiblingExtensionr8   ÚBaseExceptionr   ZmoveTo)r   r"   r>   r   Únewr   r   r    Ú__setitem__j   s$   

þzDirDBM.__setitem__c                 C  sJ   t |ƒtks
tdƒ‚| j |  |¡¡}z|  |¡W S  ty$   t|ƒ‚w )zå
        C{dirdbm[k]} Get the contents of a file in this directory as a string.

        @param k: key to lookup

        @return: The value associated with C{k}

        @raise KeyError: Raised when there is no such key
        r?   N)	r@   r   rA   r   r   r+   r3   ÚOSErrorÚKeyError)r   r"   r   r   r   r    Ú__getitem__Œ   s   
ÿzDirDBM.__getitem__c                 C  sR   t |ƒtks
tdƒ‚|  |¡}z| j |¡ ¡  W dS  ty(   t|  	|¡ƒ‚w )zÀ
        C{del dirdbm[foo]}
        Delete a file in this directory.

        @type k: bytes
        @param k: key to delete

        @raise KeyError: Raised when there is no such key
        r?   N)
r@   r   rA   r+   r   r   r   rE   rF   r-   r*   r   r   r    Ú__delitem__ž   s   

ÿzDirDBM.__delitem__úIterable[bytes]c                 C  s   t t| j| j ¡  ¡ ƒƒS )z9
        @return: a L{list} of filenames (keys).
        N)ÚlistÚmapr-   r   ZasBytesModer;   r<   r   r   r    Úkeys°   r.   zDirDBM.keysc                 C  s(   g }|   ¡ }|D ]	}| | | ¡ q|S )z?
        @return: a L{list} of file-contents (values).
        N©rL   Úappend)r   ÚvalsrL   Úkeyr   r   r    Úvalues¶   s
   zDirDBM.valuesúIterable[tuple[bytes, bytes]]c                 C  s,   g }|   ¡ }|D ]}| || | f¡ q|S )zL
        @return: a L{list} of 2-tuples containing key/value pairs.
        NrM   )r   ÚitemsrL   rP   r   r   r    rS   À   s
   zDirDBM.itemsrP   Úboolc                 C  s.   t |ƒtks
tdƒ‚|  |¡}| j |¡ ¡ S )z¬
        @type key: bytes
        @param key: The key to test

        @return: A true value if this dirdbm has the specified key, a false
        value otherwise.
        r?   N)r@   r   rA   r+   r   r   Úisfile©r   rP   r   r   r    Úhas_keyÊ   s   
zDirDBM.has_keyÚvaluec                 C  s   || vr
|| |< |S | | S )z¶
        @type key: bytes
        @param key: The key to lookup

        @param value: The value to associate with key if key is not already
        associated with a value.
        Nr   )r   rP   rX   r   r   r    Ú
setdefault×   s   zDirDBM.setdefaultc                 C  ó   d S ©Nr   rV   r   r   r    Úgetä   ó   z
DirDBM.getÚdefaultr	   ú
bytes | _Tc                 C  rZ   r[   r   ©r   rP   r^   r   r   r    r\   è   r]   Nú	_T | Noneúbytes | _T | Nonec                 C  s   || v r| | S |S )zç
        @param key: The key to lookup

        @param default: The value to return if the given key does not exist

        @return: The value associated with C{key} or C{default} if not
            L{DirDBM.has_key(key)}
        Nr   r`   r   r   r    r\   ì   s   	c                 C  s
   |   |¡S )z)
        @see: L{DirDBM.has_key}
        N)rW   rV   r   r   r    Ú__contains__ú   s   
zDirDBM.__contains__ÚotherúMapping[bytes, bytes]c                 C  s   |  ¡ D ]\}}|| |< qdS )zâ
        Add all the key/value pairs in L{dict} to this dirdbm.  Any conflicting
        keys will be overwritten with the values from L{dict}.

        @param dict: A mapping of key/value pairs to add to this dirdbm.
        N)rS   )r   rd   rP   Úvalr   r   r    Úupdate   s   
ÿzDirDBM.updatec                 C  sH   t |ƒ}|| jksJ ‚|  |j¡}| ¡  |  ¡ D ]}| | ||< q|S )a!  
        Copy the contents of this dirdbm to the dirdbm at C{path}.

        @param path: The path of the dirdbm to copy to.  If a dirdbm
        exists at the destination path, it is cleared first.

        @rtype: C{DirDBM}
        @return: The dirdbm this dirdbm was copied to.
        N)r   r   Ú	__class__r   ÚclearrL   )r   r   ZfpathÚdr"   r   r   r    ÚcopyTo
  s   
zDirDBM.copyToc                 C  s   |   ¡ D ]}| |= qdS )z<
        Delete all key/value pairs in this dirdbm.
        N)rL   r*   r   r   r    ri     s   ÿzDirDBM.clearc                 C  s   dS )zL
        Close this dbm: no-op, for dbm-style interface compliance.
        Nr   r<   r   r   r    Úclose$  s    zDirDBM.closeÚfloatc                 C  s>   t |ƒtks
tdƒ‚| j |  |¡¡}| ¡ r| ¡ S t|ƒ‚)zÁ
        Returns modification time of an entry.

        @return: Last modification date (seconds since epoch) of entry C{key}
        @raise KeyError: Raised when there is no such key
        r?   N)	r@   r   rA   r   r   r+   rU   ÚgetModificationTimerF   )r   rP   r   r   r   r    rn   )  s   zDirDBM.getModificationTime)r   r   r   r   )r"   r   r   r   )r   r/   r   r   )r   r/   r4   r   r   r   )r   r9   )r"   r   r>   r   r   r   )r"   r   r   r   )r   rI   )r   rR   )rP   r   r   rT   )rP   r   rX   r   r   r   )rP   r   r   r   )rP   r   r^   r	   r   r_   r[   )rP   r   r^   ra   r   rb   )rd   re   r   r   )r   r   r   r   )r   r   )rP   r   r   rm   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r!   r+   r-   r3   r8   r=   rD   rG   rH   rL   rQ   rS   rW   rY   r   r\   rc   rg   rk   ri   rl   rn   r   r   r   r    r       s6    









"













r   c                   @  s    e Zd ZdZdd„ Zdd„ ZdS )ÚShelfzÃ
    A directory with a DBM shelf interface.

    This class presents a hash-like interface to a directory of small,
    flat files. Keys must be strings, but values can be any given object.
    c                 C  s   t  |¡}t | ||¡ dS )zÉ
        C{shelf[foo] = bar}
        Create or modify a textfile in this directory.

        @type k: str
        @param k: The key to set

        @param v: The value to associate with C{key}
        N)ÚpickleÚdumpsr   rD   )r   r"   r>   r   r   r    rD   A  s   

zShelf.__setitem__c                 C  s   t  t | |¡¡S )a  
        C{dirdbm[foo]}
        Get and unpickle the contents of a file in this directory.

        @type k: bytes
        @param k: The key to lookup

        @return: The value associated with the given key
        @raise KeyError: Raised if the given key does not exist
        N)rt   Úloadsr   rG   r*   r   r   r    rG   N  s   zShelf.__getitem__N)ro   rp   rq   rr   rD   rG   r   r   r   r    rs   9  s    rs   c                 C  s   t | ƒS )z£
    This is for 'anydbm' compatibility.

    @param file: The parameter to pass to the DirDBM constructor.

    @param flag: ignored
    @param mode: ignored
    N)r   )ÚfileÚflagÚmoder   r   r    r0   \  s   	r0   )r0   r   rs   )NN)rr   Ú
__future__r   r'   r   r   rt   Útypingr   r   r   r   r   Ztwisted.python.filepathr   r	   r   rs   r0   Ú__all__r   r   r   r    Ú<module>   s     
#