Ϫfv'dZddlZddlZddlZddlZddlmZ eGddZ Gdde Z d dZ gd Z y#e$re ZY'wxYw) 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)FilePathceZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZddZdZdZdZdZdZdZy)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. ctjj||_t ||_|j j s|j jytj|j jdjD]}tj|tj|j jdj}|D]R}|dd}tjj|rtj|=tj||Ty)zb @type name: str @param name: Base path to use for the directory storage. z*.newz*.rplN) ospathabspathdnamer _dnamePathisdircreateDirectoryglobchildremoveexistsrename)selfnamef replacementsolds :/usr/lib/python3/dist-packages/twisted/persisted/dirdbm.py__init__zDirDBM.__init__,s WW__T* "4.$$& OO + + -YYt44W=BBC  !  99T__%:%:7%C%H%HIL! &f77>>#&IIaLIIa%  &cltj|jddjddS)z? Encode a key so it can be used as a filename.  _/-)base64 encodebytesreplacerks r_encodezDirDBM._encodeHs/ !!!$,,UD9AA$MMrcltj|jddjddS)z3 Decode a filename to get the key. rrr r)r! decodebytesr#r$s r_decodezDirDBM._decodeOs-!!!))D%"8"@"@t"LMMrc~t|jd5}|j}ddd|S#1swYSxYw)z Read in the contents of a file. Override in subclasses to e.g. provide transparently encrypted dirdbm. rbN)_openr read)rr rss r _readFilezDirDBM._readFileUs9 499d # qA  s2<ct|jd5}|j||jdddy#1swYyxYw)zw Write data to a file. Override in subclasses to e.g. provide transparently encrypted dirdbm. wbN)r,r writeflush)rr datars r _writeFilezDirDBM._writeFile_s= 499d # q GGDM GGI   s "AA cHt|jjS)zF @return: The number of key/value pairs in this Shelf )lenr listdirrs r__len__zDirDBM.__len__is4??**,--rct|tk(s tdt|tk(s td|j|}|jj |}|j r|jd}n|jd} |j|||j r|j|j|y#t$r|jwxYw)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 rrsiblingExtensionr5rmoveTo BaseException)rr%vrnews r __setitem__zDirDBM.__setitem__osAw%67 7Aw%89 9 LLOoo##A& ::<&&v.C&&v.C  OOC # zz| JJsO   JJL  s CC:ct|tk(s td|jj |j |} |j |S#t$r t|wxYw)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 rr&r/OSErrorKeyError)rr%r s r __getitem__zDirDBM.__getitem__seAw%67 7$$T\\!_5 >>$' ' 1+  s AA/ct|tk(s td|j|} |jj |j y#t$rt|j|wxYw)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 rrrGrHr)r$s r __delitem__zDirDBM.__delitem__skAw%67 7 LLO , OO ! !! $ + + - ,4<<?+ + ,s )A$A>ctt|j|jj j S)z9 @return: a L{list} of filenames (keys). )listmapr)r asBytesModer8r9s rkeysz DirDBM.keyss0C doo&A&A&C&K&K&MNOOrc`g}|j}|D]}|j|||S)z? @return: a L{list} of file-contents (values). rPappend)rvalsrPkeys rvaluesz DirDBM.valuess8yy{ #C KKS " # rcdg}|j}|D]}|j|||f|S)zL @return: a L{list} of 2-tuples containing key/value pairs. rR)ritemsrPrUs rrXz DirDBM.itemss=yy{ +C LL#tCy) * + rct|tk(s td|j|}|jj |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 risfilerrUs rhas_keyzDirDBM.has_keysHCyE!67 7ll3$$S)0022rc"||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. )rrUvalues r setdefaultzDirDBM.setdefaults# d?DILCyrNc||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^)rrUdefaults rgetz DirDBM.gets $;9 Nrc$|j|S)z) @see: L{DirDBM.has_key} )r\r[s r __contains__zDirDBM.__contains__s||C  rc>|jD] \}}|||< 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)rdictrUvals rupdatez DirDBM.updates&  HCDI rct|}||jk7sJ|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. )rr __class__r clearrP)rr dr%s rcopyToz DirDBM.copyTo sa~t&&& NN499 %   A7AaD rc4|jD]}||=y)z< Delete all key/value pairs in this dirdbm. N)rPr$s rrlz DirDBM.clears  AQ rcy)zL Close this dbm: no-op, for dbm-style interface compliance. Nr^r9s rclosez DirDBM.close%srct|tk(s td|jj |j |}|j r|jSt|)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 rr&rZgetModificationTimerH)rrUr s rrszDirDBM.getModificationTime*s[CyE!67 7$$T\\#%67 ;;=++- -3- r)N)__name__ __module__ __qualname____doc__rr&r)r/r5r:rErIrKrPrVrXr\r`rcrerirnrlrqrsr^rrrr$st&8NN . D&,$P  3  ! (  rrceZdZdZdZdZy)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\tj|}tj|||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)pickledumpsrrE)rr%rCs rrEzShelf.__setitem__Bs# LLO4A&rcTtjtj||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{loadsrrIr$s rrIzShelf.__getitem__Os ||F..tQ788rN)rtrurvrwrErIr^rrryry:s ' 9rryct|S)z This is for 'anydbm' compatibility. @param file: The parameter to pass to the DirDBM constructor. @param flag: ignored @param mode: ignored )r)fileflagmodes ropenr]s $<r)rrry)NN) rwr!rrr{twisted.python.filepathrr, NameErrorrrry__all__r^rrrs`   , S S l 9F 9F  &S  Es:AA