o bi'@sdZddlZddlZddlZddlZddlmZzeWn ey'e ZYnwGdddZ Gddde Z d dd Z gd Z dS) 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@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZddZddZd-dd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,ZdS).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. cCstj||_t||_|js|jdSt|j djD]}t |q"t|j dj}|D]}|dd}tj |rJt |q6t ||q6dS)zb @type name: str @param name: Base path to use for the directory storage. z*.newz*.rplN) ospathabspathdnamer _dnamePathisdircreateDirectoryglobchildremoveexistsrename)selfnamef replacementsoldr:/usr/lib/python3/dist-packages/twisted/persisted/dirdbm.py__init__,s      zDirDBM.__init__cCst|ddddS)z? Encode a key so it can be used as a filename.  _/-)base64 encodebytesreplacerkrrr_encodeHszDirDBM._encodecCst|ddddS)z3 Decode a filename to get the key. rrrr)r decodebytesrr rrr_decodeOzDirDBM._decodecCs:t|jd }|}Wd|S1swY|S)z Read in the contents of a file. Override in subclasses to e.g. provide transparently encrypted dirdbm. rbN)_openrread)rrrsrrr _readFileUs   zDirDBM._readFilecCsDt|jd}|||WddS1swYdS)zw Write data to a file. Override in subclasses to e.g. provide transparently encrypted dirdbm. wbN)r'rwriteflush)rrdatarrrr _writeFile_s  "zDirDBM._writeFilecCst|jS)zF @return: The number of key/value pairs in this Shelf )lenr listdirrrrr__len__iszDirDBM.__len__cCst|tks tdt|tkstd||}|j|}|r)|d}n|d}z|||Wn t yB| w|rK| | |dS)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 byteszDirDBM value must be bytesz.rplz.newN) typebytes TypeErrorr"r r rsiblingExtensionr/ BaseExceptionrmoveTo)rr!vrnewrrr __setitem__os$       zDirDBM.__setitem__cCsJt|tks td|j||}z||WSty$t|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 r4) r5r6r7r r r"r*OSErrorKeyError)rr!rrrr __getitem__s   zDirDBM.__getitem__cCsRt|tks td||}z |j|WdSty(t| |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 r4N) r5r6r7r"r r rr>r?r$r rrr __delitem__s   zDirDBM.__delitem__cCstt|j|jS)z9 @return: a L{list} of filenames (keys). )listmapr$r asBytesModer1r2rrrkeysr%z DirDBM.keyscCs(g}|}|D] }|||q|S)z? @return: a L{list} of file-contents (values). rEappend)rvalsrEkeyrrrvaluess z DirDBM.valuescCs,g}|}|D] }||||fq|S)zL @return: a L{list} of 2-tuples containing key/value pairs. rF)ritemsrErIrrrrKs z DirDBM.itemscCs.t|tks td||}|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. r4)r5r6r7r"r r isfilerrIrrrhas_keys  zDirDBM.has_keycCs||vr |||<|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)rrIvaluerrr setdefaultszDirDBM.setdefaultNcCs||vr||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)rrIdefaultrrrgets z DirDBM.getcCs ||S)z) @see: L{DirDBM.has_key} )rNrMrrr __contains__s zDirDBM.__contains__cCs|D]\}}|||<qdS)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)rK)rdictrIvalrrrupdates z DirDBM.updatecCsHt|}||jks J||j}||D]}||||<q|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. )rr __class__rclearrE)rrdr!rrrcopyTo s   z DirDBM.copyTocCs|D]}||=qdS)z< Delete all key/value pairs in this dirdbm. N)rEr rrrrXs z DirDBM.clearcCsdS)zL Close this dbm: no-op, for dbm-style interface compliance. Nrr2rrrclose%sz DirDBM.closecCs>t|tks td|j||}|r|St|)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 r4) r5r6r7r r r"rLgetModificationTimer?)rrIrrrrr\*s zDirDBM.getModificationTime)N)__name__ __module__ __qualname____doc__rr"r$r*r/r3r=r@rArErJrKrNrPrRrSrVrZrXr[r\rrrrr$s.  "     rc@s eZdZdZddZddZdS)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. cCst|}t|||dS)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)pickledumpsrr=)rr!r;rrrr=Bs zShelf.__setitem__cCstt||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 )rbloadsrr@r rrrr@Os zShelf.__getitem__N)r]r^r_r`r=r@rrrrra:s racCst|S)z This is for 'anydbm' compatibility. @param file: The parameter to pass to the DirDBM constructor. @param flag: ignored @param mode: ignored )r)fileflagmoderrropen]s rh)rhrra)NN) r`rr rrbtwisted.python.filepathrr' NameErrorrhrra__all__rrrrs"   #