o
    'h1)                     @  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abspathdnamer   
_dnamePathisdircreateDirectoryglobchildremoveexistsrename)selfr   freplacementsold r!   U/var/www/Befach/backend/venv/lib/python3.10/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.
           
   _   /   -)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'   )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)r2   writeflush)r   r   r6   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
        )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   siblingExtensionr:   BaseExceptionr   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
        rA   )	rB   r   rC   r   r   r-   r5   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
        rA   N)
rB   r   rC   r-   r   r   r   rI   rJ   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).
        )listmapr/   r   asBytesModer=   r>   r!   r!   r"   keys   r0   zDirDBM.keysc                 C  s(   g }|   }|D ]	}|| |  q|S )z?
        @return: a L{list} of file-contents (values).
        rQ   append)r   valsrQ   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.
        rR   )r   itemsrQ   rU   r!   r!   r"   rX      s
   zDirDBM.itemsrU   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.
        rA   )rB   r   rC   r-   r   r   isfiler   rU   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.
        r!   )r   rU   r]   r!   r!   r"   
setdefault   s   zDirDBM.setdefaultc                 C     d S Nr!   r[   r!   r!   r"   get      z
DirDBM.getdefaultr	   
bytes | _Tc                 C  r_   r`   r!   r   rU   rc   r!   r!   r"   ra      rb   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)}
        r!   re   r!   r!   r"   ra      s   	c                 C  s
   |  |S )z)
        @see: L{DirDBM.has_key}
        )r\   r[   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)rX   )r   ri   rU   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.
        )r   r   	__class__r   clearrQ   )r   r   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)rQ   r,   r!   r!   r"   rn     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
        rA   )	rB   r   rC   r   r   r-   rZ   getModificationTimerJ   )r   rU   r   r!   r!   r"   rt   )  s   zDirDBM.getModificationTime)r   r   r   r   )r$   r   r   r   )r   r1   r   r   )r   r1   r6   r   r   r   )r   r;   )r$   r   r@   r   r   r   )r$   r   r   r   )r   rM   )r   rW   )rU   r   r   rY   )rU   r   r]   r   r   r   )rU   r   r   r   )rU   r   rc   r	   r   rd   r`   )rU   r   rc   rf   r   rg   )ri   rj   r   r   )r   r   r   r   )r   r   )rU   r   r   rs   )__name__
__module____qualname____doc__r#   r-   r/   r5   r:   r?   rH   rK   rL   rQ   rV   rX   r\   r^   r   ra   rh   rl   rq   rn   rr   rt   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   rH   )r   r$   r@   r!   r!   r"   rH   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
        )rz   loadsr   rK   r,   r!   r!   r"   rK   N  s   zShelf.__getitem__N)ru   rv   rw   rx   rH   rK   r!   r!   r!   r"   ry   9  s    ry   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
    )r   )fileflagmoder!   r!   r"   r2   \  s   	r2   )r2   r   ry   )NN)rx   
__future__r   r)   r   r   rz   typingr   r   r   r   r   twisted.python.filepathr   r	   r   ry   r2   __all__r!   r!   r!   r"   <module>   s     
#