Ë
    ojv'  ã                   óŽ   — 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ã   € ô
 —W‘W—_‘_ TÓ*ˆŒ
Ü" 4›.ˆŒØ�‰×$Ñ$Ô&Ø�O‰O×+Ñ+Õ-ô —Y‘Y˜tŸ™×4Ñ4°WÓ=×BÑBÓCò �Ü—	‘	˜!•ðäŸ9™9 T§_¡_×%:Ñ%:¸7Ó%C×%HÑ%HÓIˆLØ!ò &�Ø˜˜�f�Ü—7‘7—>‘> #Ô&Ü—I‘I˜a•Lä—I‘I˜a Õ%ñ&ó    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/   € ô
 ×!Ñ! !Ó$×,Ñ,¨U°DÓ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   € ô �4—9‘9˜dÓ#ð 	 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=   € ô �4—9‘9˜dÓ#ð 	 qØ�G‰G�DŒMØ�G‰GŒI÷	÷ 	ñ 	ú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Ï   € ô �A‹wœ%ÒÜÐ6Ó7Ð7Ü�A‹wœ%ÒÜÐ8Ó9Ð9Ø�L‰L˜‹Oˆð �o‰o×#Ñ# AÓ&ˆØ�:‰:Œ<Ø×&Ñ& vÓ.‰Cà×&Ñ& vÓ.ˆCð	Ø�O‰O˜C Ô#ð
 �z‰zŒ|Ø—
‘
”Ø�J‰J�s�Oøô ò 	Ø�J‰JŒLØð	ú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   € ô �A‹wœ%ÒÜÐ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   € ô �A‹wœ%ÒÜÐ6Ó7Ð7Ø�L‰L˜‹Oˆð	,Ø�O‰O×!Ñ! !Ó$×+Ñ+Õ-øÜò 	,Ü˜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˜Ÿ™ d§o¡o×&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   € ð ˆØ�y‰y‹{ˆØò 	#ˆCØ�K‰K˜˜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=   € ð ˆØ�y‰y‹{ˆØò 	+ˆCØ�L‰L˜#˜t C™yÐ)Õ*ð	+àˆ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   € ô �C‹yœEÒ!ÜÐ6Ó7Ð7Ø�l‰l˜3ÓˆØ�‰×$Ñ$ 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Ø�C‰yÐ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&   € ð Ÿ
™
›ò 	‰HˆC�Øˆ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—‘Ò&Ð&Ð&à�N‰N˜4Ÿ9™9Ó%ˆØ	�‰Œ	Ø—‘“ò 	ˆAØ˜‘7ˆAˆaŠDð	àˆ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[   € ô �C‹yœEÒ!ÜÐ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#   € ô �L‰L˜‹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×.Ñ.¨t°QÓ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 9ˆFô  9óF	ò &�øðS
 ò Ø‚Eðús   š: ºAÁA