ó
[³XMc           @   sÓ   d  Z  d d l Z d d l Z d d l Z d d l Z y d d l Z Wn e k
 re d d l Z n Xy e Wn e	 k
 r‡ e
 Z n Xd f  d „  ƒ  YZ d e f d „  ƒ  YZ e e d „ Z
 d d d g Z d S(	   s„  
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
iÿÿÿÿNt   DirDBMc           B   sÎ   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 d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s­   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         C   sè   t  j j | ƒ |  _ t  j j |  j ƒ s= t  j |  j ƒ n§ x6 t j t  j j |  j d ƒ ƒ D] } t  j | ƒ q_ Wt j t  j j |  j d ƒ ƒ } xJ | D]B } | d  } t  j j	 | ƒ rÐ t  j | ƒ qž t  j
 | | ƒ qž Wd S(   sb   
        @type name: str
        @param name: Base path to use for the directory storage.
        s   *.news   *.rpliüÿÿÿN(   t   ost   patht   abspatht   dnamet   isdirt   mkdirt   globt   joint   removet   existst   rename(   t   selft   namet   ft   replacementst   old(    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyt   __init__/   s    
(!
c         C   s%   t  j | ƒ j d d ƒ j d d ƒ S(   s6   Encode a key so it can be used as a filename.
        s   
t   _t   /t   -(   t   base64t   encodestringt   replace(   R   t   k(    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyt   _encodeJ   s    c         C   s%   t  j | j d d ƒ j d d ƒ ƒ S(   s*   Decode a filename to get the key.
        R   s   
R   R   (   R   t   decodestringR   (   R   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyt   _decodeP   s    c         C   s)   t  | d ƒ } | j ƒ  } | j ƒ  | S(   s€   Read in the contents of a file.
        
        Override in subclasses to e.g. provide transparently encrypted dirdbm.
        t   rb(   t   _opent   readt   close(   R   R   R   t   s(    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyt	   _readFileU   s    
c         C   s4   t  | d ƒ } | j | ƒ | j ƒ  | j ƒ  d S(   sv   Write data to a file.
        
        Override in subclasses to e.g. provide transparently encrypted dirdbm.
        t   wbN(   R   t   writet   flushR   (   R   R   t   dataR   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyt
   _writeFile_   s    
c         C   s   t  t j |  j ƒ ƒ S(   sF   
        @return: The number of key/value pairs in this Shelf
        (   t   lenR   t   listdirR   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyt   __len__i   s    c         C   sð   t  | ƒ t j k s t d ‚ t  | ƒ t j k s< t d ‚ |  j | ƒ } t j j |  j | ƒ } t j j	 | ƒ r‚ | d } n
 | d } y |  j
 | | ƒ Wn t j | ƒ ‚  n3 Xt j j	 | ƒ rÜ t j | ƒ n  t j | | ƒ d S(   sØ   
        C{dirdbm[k] = v}
        Create or modify a textfile in this directory

        @type k: str
        @param k: key to set
        
        @type v: str
        @param v: value to associate with C{k}
        s   DirDBM key must be a strings   DirDBM value must be a strings   .rpls   .newN(   t   typet   typest
   StringTypet   AssertionErrorR   R   R   R   R   R
   R&   R	   R   (   R   R   t   vR   t   new(    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyt   __setitem__o   s    
 c         C   sg   t  | ƒ t j k s t d ‚ t j j |  j |  j | ƒ ƒ } y |  j	 | ƒ SWn t
 | ‚ n Xd S(   s  
        C{dirdbm[k]}
        Get the contents of a file in this directory as a string.
        
        @type k: str
        @param k: key to lookup
        
        @return: The value associated with C{k}
        @raise KeyError: Raised when there is no such key
        s   DirDBM key must be a stringN(   R*   R+   R,   R-   R   R   R   R   R   R!   t   KeyError(   R   R   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyt   __getitem__Ž   s    !c         C   sƒ   t  | ƒ t j k s t d ‚ |  j | ƒ } y# t j t j j |  j	 | ƒ ƒ Wn, t
 t f k
 r~ t |  j | ƒ ƒ ‚ n Xd S(   sÎ   
        C{del dirdbm[foo]}
        Delete a file in this directory.
        
        @type k: str
        @param k: key to delete
        
        @raise KeyError: Raised when there is no such key
        s   DirDBM key must be a stringN(   R*   R+   R,   R-   R   R   R	   R   R   R   t   OSErrort   IOErrorR1   R   (   R   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyt   __delitem__    s    
 # c         C   s   t  |  j t j |  j ƒ ƒ S(   s9   
        @return: a C{list} of filenames (keys).
        (   t   mapR   R   R(   R   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyt   keys¯   s    c         C   s8   g  } |  j  ƒ  } x | D] } | j |  | ƒ q W| S(   s?   
        @return: a C{list} of file-contents (values).
        (   R7   t   append(   R   t   valsR7   t   key(    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyt   valuesµ   s
    c         C   s>   g  } |  j  ƒ  } x% | D] } | j | |  | f ƒ q W| S(   sL   
        @return: a C{list} of 2-tuples containing key/value pairs.
        (   R7   R8   (   R   t   itemsR7   R:   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyR<   ¿   s
    c         C   sO   t  | ƒ t j k s t d ‚ |  j | ƒ } t j j t j j |  j	 | ƒ ƒ S(   s³   
        @type key: str
        @param key: The key to test
        
        @return: A true value if this dirdbm has the specified key, a faluse
        value otherwise.
        s   DirDBM key must be a string(
   R*   R+   R,   R-   R   R   R   t   isfileR   R   (   R   R:   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyt   has_keyÉ   s    c         C   s%   |  j  | ƒ s | |  | <| S|  | S(   s¼   
        @type key: str
        @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   R:   t   value(    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyt
   setdefaultÕ   s    
c         C   s   |  j  | ƒ r |  | S| Sd S(   s  
        @type key: str
        @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
        C{self.has_key(key)}
        N(   R>   (   R   R:   t   default(    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyt   getâ   s    
c         C   sO   t  | ƒ t j k s t d ‚ |  j | ƒ } t j j t j j |  j	 | ƒ ƒ S(   s¿   
        C{key in dirdbm}

        @type key: str
        @param key: The key to test
                
        @return: A true value if C{self.has_key(key)}, a false value otherwise.
        s   DirDBM key must be a string(
   R*   R+   R,   R-   R   R   R   R=   R   R   (   R   R:   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyt   __contains__ñ   s    	c         C   s+   x$ | j  ƒ  D] \ } } | |  | <q Wd S(   sþ   
        Add all the key/value pairs in C{dict} to this dirdbm.  Any conflicting
        keys will be overwritten with the values from C{dict}.

        @type dict: mapping
        @param dict: A mapping of key/value pairs to add to this dirdbm.
        N(   R<   (   R   t   dictR:   t   val(    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyt   updateþ   s    c         C   si   t  j j | ƒ } | |  j k s' t ‚ |  j | ƒ } | j ƒ  x" |  j ƒ  D] } |  | | | <qM W| S(   sL  
        Copy the contents of this dirdbm to the dirdbm at C{path}.
        
        @type path: C{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   R   R   R-   t	   __class__t   clearR7   (   R   R   t   dR   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyt   copyTo	  s    
c         C   s"   x |  j  ƒ  D] } |  | =q Wd S(   s<   
        Delete all key/value pairs in this dirdbm.
        N(   R7   (   R   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyRH     s    c         C   s   d S(   sL   
        Close this dbm: no-op, for dbm-style interface compliance.
        N(    (   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyR   $  s    c         C   sn   t  | ƒ t j k s t d ‚ t j j |  j |  j | ƒ ƒ } t j j	 | ƒ ra t j j
 | ƒ St | ‚ d S(   sÉ   
        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
        s   DirDBM key must be a stringN(   R*   R+   R,   R-   R   R   R   R   R   R=   t   getmtimeR1   (   R   R:   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyt   getModificationTime)  s
    !N(   t   __name__t
   __module__t   __doc__R   R   R   R!   R&   R)   R0   R2   R5   R7   R;   R<   R>   R@   t   NoneRB   RC   RF   RJ   RH   R   RL   (    (    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyR    (   s,   				
	
						
	
							t   Shelfc           B   s    e  Z d  Z d „  Z d „  Z RS(   sÂ   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  j | ƒ } t j |  | | ƒ d S(   sÉ   
        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(   t   picklet   dumpsR    R0   (   R   R   R.   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyR0   ?  s    
c         C   s   t  j t j |  | ƒ ƒ S(   s'  
        C{dirdbm[foo]}
        Get and unpickle the contents of a file in this directory.
        
        @type k: str
        @param k: The key to lookup
        
        @return: The value associated with the given key
        @raise KeyError: Raised if the given key does not exist
        (   RR   t   loadsR    R2   (   R   R   (    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyR2   L  s    (   RM   RN   RO   R0   R2   (    (    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyRQ   8  s   	c         C   s
   t  |  ƒ S(   s§   
    This is for 'anydbm' compatibility.
    
    @param file: The parameter to pass to the DirDBM constructor.

    @param flag: ignored
    @param mode: ignored
    (   R    (   t   filet   flagt   mode(    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyt   openZ  s    	RX   (   RO   R   R+   R   R   t   cPickleRR   t   ImportErrorR   t	   NameErrorRX   R    RQ   RP   t   __all__(    (    (    s<   /usr/lib/python2.7/dist-packages/twisted/persisted/dirdbm.pyt   <module>   s"   
ÿ "