
    \di'                         d Z ddlZddlZddlZddlZddlmZ 	 e n# e$ r e	ZY nw xY w G d d          Z
 G d de
          Zd
dZ	g d	ZdS )a  
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.

DirDBMs 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!

Maintainer: Itamar Shtull-Trauring
    N)FilePathc                       e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd ZddZd Zd Zd Zd Zd Zd ZdS )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.
    c                    t           j                            |          | _        t	          |          | _        | j                                        s| j                                         dS t          j        | j        	                    d          j                  D ]}t          j
        |           t          j        | j        	                    d          j                  }|D ]U}|dd         }t           j                            |          rt          j
        |           @t          j        ||           VdS )zb
        @type name: str
        @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)selfnamefreplacementsolds        8lib/python3.11/site-packages/twisted/persisted/dirdbm.py__init__zDirDBM.__init__,   s   
 W__T**
"4..$$&& 	&O++----- Yt44W==BCC  	!9T_%:%:7%C%C%HIIL! & &f7>>#&& &IaLLLLIa%%%%& &    c                 z    t          j        |                              dd                              dd          S )z?
        Encode a key so it can be used as a filename.
           
   _   /   -)base64encodebytesreplacer   ks     r   _encodezDirDBM._encodeH   s5    
 !!$$,,UD99AA$MMMr   c                 z    t          j        |                    dd                              dd                    S )z3
        Decode a filename to get the key.
        r   r   r    r   )r!   decodebytesr#   r$   s     r   _decodezDirDBM._decodeO   s3     !!))D%"8"8"@"@t"L"LMMMr   c                     t          |j        d          5 }|                                }ddd           n# 1 swxY w Y   |S )z
        Read in the contents of a file.

        Override in subclasses to e.g. provide transparently encrypted dirdbm.
        rbN)_openr	   read)r   r	   r   ss       r   	_readFilezDirDBM._readFileU   sz     49d## 	qA	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   7;;c                     t          |j        d          5 }|                    |           |                                 ddd           dS # 1 swxY w Y   dS )zw
        Write data to a file.

        Override in subclasses to e.g. provide transparently encrypted dirdbm.
        wbN)r,   r	   writeflush)r   r	   datar   s       r   
_writeFilezDirDBM._writeFile_   s     49d## 	qGGDMMMGGIII	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   *AAAc                 N    t          | j                                                  S )zF
        @return: The number of key/value pairs in this Shelf
        )lenr   listdirr   s    r   __len__zDirDBM.__len__i   s      4?**,,---r   c                 l   t          |          t          k    st          d          t          |          t          k    st          d          |                     |          }| j                            |          }|                                r|                    d          }n|                    d          }	 |                     ||           |                                r|	                                 |
                    |           dS # t          $ r |	                                  w xY w)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)typebytes	TypeErrorr&   r   r   r   siblingExtensionr5   r   moveToBaseException)r   r%   vr   news        r   __setitem__zDirDBM.__setitem__o   s    Aww%6777Aww%8999LLOO o##A&&::<< 	/&&v..CC&&v..C	OOC###
 zz|| 

JJsOOOOO  	 	 	JJLLL	s   >D  D3c                    t          |          t          k    st          d          | j                            |                     |                    }	 |                     |          S # t          $ r t          |          w xY w)a  
        C{dirdbm[k]}
        Get the contents of a file in this directory as a string.

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

        @return: The value associated with C{k}
        @raise KeyError: Raised when there is no such key
        r<   )	r=   r>   r?   r   r   r&   r/   OSErrorKeyError)r   r%   r	   s      r   __getitem__zDirDBM.__getitem__   s     Aww%6777$$T\\!__55	>>$''' 	 	 	1++	s   A+ +Bc                 8   t          |          t          k    st          d          |                     |          }	 | j                            |                                           dS # t          $ r# t          | 	                    |                    w xY 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>   r?   r&   r   r   r   rG   rH   r)   r$   s     r   __delitem__zDirDBM.__delitem__   s     Aww%6777LLOO	,O!!!$$++----- 	, 	, 	,4<<??+++	,s   ,A, ,-Bc                     t          t          | j        | j                                                                                            S )z9
        @return: a L{list} of filenames (keys).
        )listmapr)   r   asBytesModer8   r9   s    r   keyszDirDBM.keys   s8     Cdo&A&A&C&C&K&K&M&MNNOOOr   c                 r    g }|                                  }|D ]}|                    | |                    |S )z?
        @return: a L{list} of file-contents (values).
        rP   append)r   valsrP   keys       r   valueszDirDBM.values   sC     yy{{ 	# 	#CKKS	""""r   c                 v    g }|                                  }|D ]}|                    || |         f            |S )zL
        @return: a L{list} of 2-tuples containing key/value pairs.
        rR   )r   itemsrP   rU   s       r   rX   zDirDBM.items   sH     yy{{ 	+ 	+CLL#tCy)****r   c                     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<   )r=   r>   r?   r&   r   r   isfiler   rU   s     r   has_keyzDirDBM.has_key   sW     CyyE!!6777ll3$$S))00222r   c                 (    || 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   rU   values      r   
setdefaultzDirDBM.setdefault   s%     d??DILCyr   Nc                     || v r| |         S |S )z
        @type key: bytes
        @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^   )r   rU   defaults      r   getz
DirDBM.get   s     $;;9Nr   c                 ,    |                      |          S )z)
        @see: L{DirDBM.has_key}
        )r\   r[   s     r   __contains__zDirDBM.__contains__   s     ||C   r   c                 D    |                                 D ]
\  }}|| |<   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}.

        @type dict: mapping
        @param dict: A mapping of key/value pairs to add to this dirdbm.
        N)rX   )r   dictrU   vals       r   updatezDirDBM.update   s4     

 	 	HCDII	 	r   c                     t          |          }|| j        k    sJ |                     |j                  }|                                 |                                 D ]}| |         ||<   |S )a<  
        Copy the contents of this dirdbm to the dirdbm at C{path}.

        @type path: L{str}
        @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	   clearrP   )r   r	   dr%   s       r   copyTozDirDBM.copyTo
  sm     ~~t&&&&NN49%%				 	 	A7AaDDr   c                 :    |                                  D ]}| |= dS )z<
        Delete all key/value pairs in this dirdbm.
        N)rP   r$   s     r   rl   zDirDBM.clear  s.      	 	AQ	 	r   c                     dS )zL
        Close this dbm: no-op, for dbm-style interface compliance.
        Nr^   r9   s    r   closezDirDBM.close%  s      r   c                    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<   )	r=   r>   r?   r   r   r&   rZ   getModificationTimerH   )r   rU   r	   s      r   rs   zDirDBM.getModificationTime*  ss     CyyE!!6777$$T\\#%6%677;;== 	 ++---3--r   )N)__name__
__module____qualname____doc__r   r&   r)   r/   r5   r:   rE   rI   rK   rP   rV   rX   r\   r`   rc   re   ri   rn   rl   rq   rs   r^   r   r   r   r   $   sh        & & &8N N NN N N    . . .     D  &, , ,$P P P    3 3 3     ! ! !	 	 	  (    
         r   r   c                       e Zd ZdZd Z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                 f    t          j        |          }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   rE   )r   r%   rC   s      r   rE   zShelf.__setitem__B  s/     LOO4A&&&&&r   c                 \    t          j        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
        )r{   loadsr   rI   r$   s     r   rI   zShelf.__getitem__O  s$     |F..tQ77888r   N)rt   ru   rv   rw   rE   rI   r^   r   r   ry   ry   :  s<         ' ' '9 9 9 9 9r   ry   c                      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modes      r   openr   ]  s     $<<r   )r   r   ry   )NN)rw   r!   r   r   r{   twisted.python.filepathr   r,   	NameErrorr   r   ry   __all__r^   r   r   <module>r      s        				  , , , , , ,	EE   EEES  S  S  S  S  S  S  S l 9  9  9  9  9F  9  9  9F	 	 	 	 &
%
%s    ''