
    Vh1)                        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      Z G d d	      Z G d
 de      ZddZg dZy)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                      e Zd ZdZddZddZddZddZddZddZ	ddZ
dd	Zd d
Zd!dZd!dZd"dZd#dZd$dZed%d       Zed&d       Zd'd(dZd#dZd)dZd*dZd+dZd+dZd,dZ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.
    c                   t         j                  j                  |      | _        t	        |      | _        | j
                  j                         s| j
                  j                          yt        j                  | j
                  j                  d      j                        D ]  }t        j                  |        t        j                  | j
                  j                  d      j                        }|D ]R  }|dd }t         j                  j                  |      rt        j                  |       =t        j                  ||       T y)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)selfnamefreplacementsolds        H/home/dcms/DCMS/lib/python3.12/site-packages/twisted/persisted/dirdbm.py__init__zDirDBM.__init__(   s     WW__T*
"4.$$&OO++- YYt44W=BBC 		!99T__%:%:7%C%H%HIL! &f77>>#&IIaLIIa%&    c                l    t        j                  |      j                  dd      j                  dd      S )z?
        Encode a key so it can be used as a filename.
           
   _   /   -)base64encodebytesreplacer   ks     r!   _encodezDirDBM._encodeC   s/    
 !!!$,,UD9AA$MMr#   c                l    t        j                  |j                  dd      j                  dd            S )z3
        Decode a filename to get the key.
        r&   r%   r(   r'   )r)   decodebytesr+   r,   s     r!   _decodezDirDBM._decodeJ   s-     !!!))D%"8"@"@t"LMMr#   c                r    |j                         5 }|j                         }ddd       |S # 1 sw Y   S xY w)z
        Read in the contents of a file.

        Override in subclasses to e.g. provide transparently encrypted dirdbm.
        N)openread)r   r   r   ss       r!   	_readFilezDirDBM._readFileP   s4     YY[ 	AA		s   ,6c                    |j                  d      5 }|j                  |       |j                          ddd       y# 1 sw Y   yxY w)zw
        Write data to a file.

        Override in subclasses to e.g. provide transparently encrypted dirdbm.
        wN)r3   writeflush)r   r   datar   s       r!   
_writeFilezDirDBM._writeFileZ   s:     YYs^ 	qGGDMGGI	 	 	s	   "=Ac                H    t        | j                  j                               S )zF
        @return: The number of key/value pairs in this Shelf
        )lenr   listdirr   s    r!   __len__zDirDBM.__len__d   s     4??**,--r#   c                   t        |      t        k(  st        d      t        |      t        k(  st        d      | j                  |      }| j                  j                  |      }|j                         r|j                  d      }n|j                  d      }	 | j                  ||       |j                         r|j                          |j                  |       y# t        $ r |j                           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   siblingExtensionr<   r   moveToBaseException)r   r-   vr    news        r!   __setitem__zDirDBM.__setitem__j   s     Aw%677Aw%899LLO oo##A&::<&&v.C&&v.C	OOC#
 zz|

JJsO  	JJL	s   C C:c                    t        |      t        k(  st        d      | j                  j	                  | j                  |            }	 | j                  |      S # t        $ r t        |      w xY 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
        rC   )	rD   rE   rF   r   r   r.   r6   OSErrorKeyError)r   r-   r   s      r!   __getitem__zDirDBM.__getitem__   se     Aw%677$$T\\!_5	>>$'' 	1+	s   	A A/c                   t        |      t        k(  st        d      | j                  |      }	 | j                  j                  |      j                          y# t        $ r t        | j                  |            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
        rC   N)
rD   rE   rF   r.   r   r   r   rN   rO   r1   r,   s     r!   __delitem__zDirDBM.__delitem__   sk     Aw%677LLO	,OO!!!$++- 	,4<<?++	,s   )A $A>c                    t        t        | j                  | j                  j	                         j                                     S )z9
        @return: a L{list} of filenames (keys).
        )listmapr1   r   asBytesModer?   r@   s    r!   keyszDirDBM.keys   s0     Cdoo&A&A&C&K&K&MNOOr#   c                `    g }| j                         }|D ]  }|j                  | |           |S )z?
        @return: a L{list} of file-contents (values).
        rW   append)r   valsrW   keys       r!   valueszDirDBM.values   s8     yy{ 	#CKKS	"	#r#   c                d    g }| j                         }|D ]  }|j                  || |   f        |S )zL
        @return: a L{list} of 2-tuples containing key/value pairs.
        rY   )r   itemsrW   r\   s       r!   r_   zDirDBM.items   s=     yy{ 	+CLL#tCy)*	+r#   c                    t        |      t        k(  st        d      | j                  |      }| j                  j                  |      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.
        rC   )rD   rE   rF   r.   r   r   isfiler   r\   s     r!   has_keyzDirDBM.has_key   sH     CyE!677ll3$$S)00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   r\   values      r!   
setdefaultzDirDBM.setdefault   s#     d?DILCyr#   c                     y Nre   rb   s     r!   getz
DirDBM.get       r#   c                     y ri   re   r   r\   defaults      r!   rj   z
DirDBM.get   rk   r#   Nc                    || 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)}
        re   rm   s      r!   rj   z
DirDBM.get   s     $;9Nr#   c                $    | j                  |      S )z)
        @see: L{DirDBM.has_key}
        )rc   rb   s     r!   __contains__zDirDBM.__contains__   s     ||C  r#   c                >    |j                         D ]
  \  }}|| |<    y)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)r_   )r   otherr\   vals       r!   updatezDirDBM.update   s&      	HCDI	r#   c                    t        |      }|| j                  k7  sJ | j                  |j                        }|j	                          | j                         D ]
  }| |   ||<    |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   clearrW   )r   r   fpathdr-   s        r!   copyTozDirDBM.copyTo
  sa     '''NN5::&		 	A7AaD	r#   c                4    | j                         D ]  }| |=  y)z<
        Delete all key/value pairs in this dirdbm.
        N)rW   r,   s     r!   rx   zDirDBM.clear  s       	AQ	r#   c                     y)zL
        Close this dbm: no-op, for dbm-style interface compliance.
        Nre   r@   s    r!   closezDirDBM.close$  s    r#   c                    t        |      t        k(  st        d      | j                  j	                  | j                  |            }|j                         r|j                         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
        rC   )	rD   rE   rF   r   r   r.   ra   getModificationTimerO   )r   r\   r   s      r!   r   zDirDBM.getModificationTime)  s[     CyE!677$$T\\#%67;;=++--3-r#   )r   rE   returnNone)r-   rE   r   rE   )r   FilePath[AnyStr]r   rE   )r   r   r;   rE   r   r   )r   int)r-   rE   rJ   rE   r   r   )r-   rE   r   r   )r   zIterable[bytes])r   zIterable[tuple[bytes, bytes]])r\   rE   r   bool)r\   rE   rf   rE   r   rE   )r\   rE   r   rE   )r\   rE   rn   r
   r   z
bytes | _Tri   )r\   rE   rn   z	_T | Noner   zbytes | _T | None)rs   zMapping[bytes, bytes]r   r   )r   rE   r   r   )r   r   )r\   rE   r   float)__name__
__module____qualname____doc__r"   r.   r1   r6   r<   rA   rL   rP   rR   rW   r]   r_   rc   rg   r   rj   rq   ru   r{   rx   r~   r   re   r#   r!   r   r       s    &6NN. D$,$P3    !&
 r#   r   c                      e Zd ZdZd Zd Zy)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                \    t        j                  |      }t        j                  | ||       y)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   rL   )r   r-   rJ   s      r!   rL   zShelf.__setitem__A  s#     LLO4A&r#   c                T    t        j                  t        j                  | |            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   rP   r,   s     r!   rP   zShelf.__getitem__N  s      ||F..tQ788r#   N)r   r   r   r   rL   rP   re   r#   r!   r   r   9  s    '9r#   r   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!   r3   r3   \  s     $<r#   )r3   r   r   )NN)r   
__future__r   r)   r   r   r   typingr   r   r   r   r   twisted.python.filepathr	   r
   r   r   r3   __all__re   r#   r!   <module>r      sV    #   	  ? ? ,TV  V r 9F  9F	 &r#   