
    Ϫfv'                         d Z ddlZddlZddlZddlZddlmZ 	 e  G d d      Z
 G d de
      Zd
dZ	g d	Zy# e$ r e	ZY 'w xY w)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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)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        :/usr/lib/python3/dist-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._encodeH   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._decodeO   s-     !!!))D%"8"@"@t"LMMr   c                 ~    t        |j                  d      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.
        rbN)_openr	   read)r   r	   r   ss       r   	_readFilezDirDBM._readFileU   s9     499d# 	qA		s   2<c                     t        |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.
        wbN)r,   r	   writeflush)r   r	   datar   s       r   
_writeFilezDirDBM._writeFile_   s=     499d# 	qGGDMGGI	 	 	s   "A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__i   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   siblingExtensionr5   r   moveToBaseException)r   r%   vr   news        r   __setitem__zDirDBM.__setitem__o   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)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__   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
        r<   N)
r=   r>   r?   r&   r   r   r   rG   rH   r)   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mapr)   r   asBytesModer8   r9   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).
        rP   append)r   valsrP   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.
        rR   )r   itemsrP   rU   s       r   rX   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.
        r<   )r=   r>   r?   r&   r   r   isfiler   rU   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   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                 $    | j                  |      S )z)
        @see: L{DirDBM.has_key}
        )r\   r[   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}.

        @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   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}.

        @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
  sa     ~t&&&NN499%		 	A7AaD	r   c                 4    | j                         D ]  }| |=  y)z<
        Delete all key/value pairs in this dirdbm.
        N)rP   r$   s     r   rl   zDirDBM.clear  s       	AQ	r   c                      y)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                  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
        r<   )	r=   r>   r?   r   r   r&   rZ   getModificationTimerH   )r   rU   r	   s      r   rs   zDirDBM.getModificationTime*  s[     CyE!677$$T\\#%6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   $   st    &8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   rE   )r   r%   rC   s      r   rE   zShelf.__setitem__B  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   rI   r$   s     r   rI   zShelf.__getitem__O  s      ||F..tQ788r   N)rt   ru   rv   rw   rE   rI   r^   r   r   ry   ry   :  s    '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`       	  ,	
S  S l 9F  9F	 &S
  Es   : AA