U W['@sdZddlZddlZddlZz ddlZWnek rDddlZYnXddlmZze Wne k rre Z YnXGdddZ Gddde Z d dd Z d ddgZdS) 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.|jnrt|j djD]}t |qBt|j dj}|D]4}|dd}tj |rt |qjt ||qjdS)zb @type name: str @param name: Base path to use for the directory storage. z*.newz*.rplN) ospathabspathZdnamer _dnamePathisdirZcreateDirectoryglobchildremoveexistsrename)selfnamefZ replacementsoldr:/usr/lib/python3/dist-packages/twisted/persisted/dirdbm.py__init__2s       zDirDBM.__init__cCst|ddddS)z? Encode a key so it can be used as a filename.  _/-)base64Z encodestringreplacerkrrr_encodeOszDirDBM._encodecCst|ddddS)z3 Decode a filename to get the key. rrrr)rZ decodestringrrrrr_decodeWszDirDBM._decodec Cs$t|jd}|}W5QRX|S)z Read in the contents of a file. Override in subclasses to e.g. provide transparently encrypted dirdbm. rb)_openrread)rrrsrrr _readFile^szDirDBM._readFilec Cs.t|jd}|||W5QRXdS)zw Write data to a file. Override in subclasses to e.g. provide transparently encrypted dirdbm. wbN)r!rwriteflush)rrdatarrrr _writeFileis zDirDBM._writeFilecCst|jS)zF @return: The number of key/value pairs in this Shelf )lenrlistdirrrrr__len__tszDirDBM.__len__cCst|tkstdt|tks(td||}|j|}|rR|d}n |d}z|||Wn| YnX|r| | |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 TypeErrorrrr r ZsiblingExtensionr)r ZmoveTo)rrvrnewrrr __setitem__{s"      zDirDBM.__setitem__cCsTt|tkstd|j||}z ||WStk rNt|YnXdS)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.N) r/r0r1rr rr$EnvironmentErrorKeyError)rrrrrr __getitem__s  zDirDBM.__getitem__cCsZt|tkstd||}z|j|Wn"tk rTt| |YnXdS)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/r0r1rrr r r5r6rrrrr __delitem__s  zDirDBM.__delitem__cCstt|j|jS)z9 @return: a L{list} of filenames (keys). )listmaprrZ asBytesModer+r,rrrkeyssz DirDBM.keyscCs(g}|}|D]}|||q|S)z? @return: a L{list} of file-contents (values). r;append)rZvalsr;keyrrrvaluess z DirDBM.valuescCs,g}|}|D]}||||fq|S)zL @return: a L{list} of 2-tuples containing key/value pairs. r<)ritemsr;r>rrrr@s z DirDBM.itemscCs.t|tkstd||}|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/r0r1rrr isfilerr>rrrhas_keys  zDirDBM.has_keycCs||kr|||<|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)rr>valuerrr setdefaultszDirDBM.setdefaultNcCs||kr||S|SdS)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)} Nr)rr>defaultrrrgets z DirDBM.getcCs ||S)z) @see: L{DirDBM.has_key} )rCrBrrr __contains__ szDirDBM.__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)r@)rdictr>valrrrupdatesz DirDBM.updatecCsHt|}||jkst||j}||D]}||||<q2|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. )rrAssertionError __class__rclearr;)rrdrrrrcopyTo s   z DirDBM.copyTocCs|D] }||=qdS)z< Delete all key/value pairs in this dirdbm. N)r;rrrrrN5s z DirDBM.clearcCsdS)zL Close this dbm: no-op, for dbm-style interface compliance. Nrr,rrrclose=sz DirDBM.closecCsBt|tkstd|j||}|r6|St|dS)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.N) r/r0r1rr rrAgetModificationTimer6)rr>rrrrrRCs  zDirDBM.getModificationTime)N)__name__ __module__ __qualname____doc__rrrr$r)r-r4r7r8r;r?r@rCrErGrHrKrPrNrQrRrrrrr*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)pickledumpsrr4)rrr2rrrr4\s 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 )rXloadsrr7rrrrr7js zShelf.__getitem__N)rSrTrUrVr4r7rrrrrWTsrWcCst|S)z This is for 'anydbm' compatibility. @param file: The parameter to pass to the DirDBM constructor. @param flag: ignored @param mode: ignored )r)fileflagmoderrropenys r^)NN)rVrrr ZcPicklerX ImportErrorZtwisted.python.filepathrr! NameErrorr^rrW__all__rrrrs$   ,%