
    h1)                        S r SSKJr  SSKrSSKrSSKrSSKrSSKJrJ	r	J
r
JrJr  SSKJr  \" SSS9r " S S	5      r " S
 S\5      rSS jr/ SQrg)aw  
A L{DirDBM} is a L{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.

L{DirDBM}s 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!
    )annotationsN)AnyStrIterableMappingTypeVaroverload)FilePath_T)boundc                     \ rS rSrSrSS jrSS jrSS jrSS jrSS jr	S S jr
S!S	 jrSS
 jrS"S jrS#S jrS#S jrS$S jrS%S jrS&S jr\S'S j5       r\S(S j5       rS)S*S jjrS%S jrS+S jrS,S jrS-S jrS-S jrS.S jrSrg)/DirDBM    z
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                   [         R                  R                  U5      U l        [	        U5      U l        U R
                  R                  5       (       d  U R
                  R                  5         g[        R                  " U R
                  R                  S5      R                  5       H  n[         R                  " U5        M     [        R                  " U R
                  R                  S5      R                  5      nU HZ  nUSS n[         R                  R                  U5      (       a  [         R                  " U5        MD  [         R                  " X$5        M\     g)z:
@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        _/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/persisted/dirdbm.py__init__DirDBM.__init__(   s     WW__T*
"4.$$&&OO++- YYt44W=BBC		! D99T__%:%:7%C%H%HIL!f77>>#&&IIaLIIa% "    c                n    [         R                  " U5      R                  SS5      R                  SS5      $ )z/
Encode a key so it can be used as a filename.
   
   _   /   -)base64encodebytesreplacer   ks     r"   _encodeDirDBM._encodeC   s/    
 !!!$,,UD9AA$MMr%   c                n    [         R                  " UR                  SS5      R                  SS5      5      $ )z#
Decode a filename to get the key.
r(   r'   r*   r)   )r+   decodebytesr-   r.   s     r"   _decodeDirDBM._decodeJ   s-     !!!))D%"8"@"@t"LMMr%   c                |    UR                  5        nUR                  5       nSSS5        U$ ! , (       d  f       W$ = f)zi
Read in the contents of a file.

Override in subclasses to e.g. provide transparently encrypted dirdbm.
N)openread)r   r   r   ss       r"   	_readFileDirDBM._readFileP   s1     YY[AA  [s   ,
;c                    UR                  S5       nUR                  U5        UR                  5         SSS5        g! , (       d  f       g= f)z_
Write data to a file.

Override in subclasses to e.g. provide transparently encrypted dirdbm.
wN)r7   writeflush)r   r   datar   s       r"   
_writeFileDirDBM._writeFileZ   s/     YYs^qGGDMGGI ^^s	   "=
Ac                H    [        U R                  R                  5       5      $ )z6
@return: The number of key/value pairs in this Shelf
)lenr   listdirr   s    r"   __len__DirDBM.__len__d   s     4??**,--r%   c                   [        U5      [        :X  d  [        S5      e[        U5      [        :X  d  [        S5      eU R                  U5      nU R                  R                  U5      nUR                  5       (       a  UR                  S5      nOUR                  S5      n U R                  XB5        UR                  5       (       a  UR                  5         UR                  U5        g! [         a    UR                  5         e f = f)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	TypeErrorr0   r   r   r   siblingExtensionrA   r   moveToBaseException)r   r/   vr!   news        r"   __setitem__DirDBM.__setitem__j   s     Aw%677Aw%899LLO oo##A&::<<&&v.C&&v.C	OOC#
 zz||

JJsO  	JJL	s   "C* *Dc                    [        U5      [        :X  d  [        S5      eU R                  R	                  U R                  U5      5      n U R                  U5      $ ! [         a    [        U5      ef = f)z
C{dirdbm[k]} Get the contents of a file in this directory as a string.

@param k: key to lookup

@return: The value associated with C{k}

@raise KeyError: Raised when there is no such key
rJ   )	rK   rL   rM   r   r   r0   r:   OSErrorKeyError)r   r/   r   s      r"   __getitem__DirDBM.__getitem__   se     Aw%677$$T\\!_5	>>$'' 	1+	s   
A A1c                   [        U5      [        :X  d  [        S5      eU R                  U5      n U R                  R                  U5      R                  5         g! [         a    [        U R                  U5      5      ef = f)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
rJ   N)
rK   rL   rM   r0   r   r   r   rV   rW   r4   r.   s     r"   __delitem__DirDBM.__delitem__   sk     Aw%677LLO	,OO!!!$++- 	,4<<?++	,s   )A %B c                    [        [        U R                  U R                  R	                  5       R                  5       5      5      $ )z)
@return: a L{list} of filenames (keys).
)listmapr4   r   asBytesModerE   rF   s    r"   keysDirDBM.keys   s0     Cdoo&A&A&C&K&K&MNOOr%   c                b    / nU R                  5       nU H  nUR                  X   5        M     U$ )z/
@return: a L{list} of file-contents (values).
ra   append)r   valsra   keys       r"   valuesDirDBM.values   s1     yy{CKK	" r%   c                f    / nU R                  5       nU H  nUR                  X0U   45        M     U$ )z<
@return: a L{list} of 2-tuples containing key/value pairs.
rd   )r   itemsra   rg   s       r"   rk   DirDBM.items   s6     yy{CLL#Cy)* r%   c                    [        U5      [        :X  d  [        S5      eU R                  U5      nU R                  R                  U5      R                  5       $ )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.
rJ   )rK   rL   rM   r0   r   r   isfiler   rg   s     r"   has_keyDirDBM.has_key   sH     CyE!677ll3$$S)0022r%   c                     X;  a  X U'   U$ X   $ )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   rg   values      r"   
setdefaultDirDBM.setdefault   s     ?ILyr%   c                    g Nrs   ro   s     r"   get
DirDBM.get       r%   c                    g rx   rs   r   rg   defaults      r"   ry   rz      r{   r%   Nc                    X;   a  X   $ U$ )z
@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)}
rs   r}   s      r"   ry   rz      s     ;9Nr%   c                $    U R                  U5      $ )z
@see: L{DirDBM.has_key}
)rp   ro   s     r"   __contains__DirDBM.__contains__   s     ||C  r%   c                >    UR                  5        H	  u  p#X0U'   M     g)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}.

@param dict: A mapping of key/value pairs to add to this dirdbm.
N)rk   )r   otherrg   vals       r"   updateDirDBM.update   s     HCI &r%   c                    [        U5      nX R                  :w  d   eU R                  UR                  5      nUR	                  5         U R                  5        H	  nX   X4'   M     U$ )z
Copy the contents of this dirdbm to the dirdbm at C{path}.

@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   clearra   )r   r   fpathdr/   s        r"   copyToDirDBM.copyTo
  sV     '''NN5::&		A7AD r%   c                6    U R                  5        H  nX	 M     g)z,
Delete all key/value pairs in this dirdbm.
N)ra   r.   s     r"   r   DirDBM.clear  s     A r%   c                    g)z<
Close this dbm: no-op, for dbm-style interface compliance.
Nrs   rF   s    r"   closeDirDBM.close$  s    r%   c                    [        U5      [        :X  d  [        S5      eU R                  R	                  U R                  U5      5      nUR                  5       (       a  UR                  5       $ [        U5      e)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
rJ   )	rK   rL   rM   r   r   r0   rn   getModificationTimerW   )r   rg   r   s      r"   r   DirDBM.getModificationTime)  s]     CyE!677$$T\\#%67;;==++--3-r%   )r   r   )r   rL   returnNone)r/   rL   r   rL   )r   FilePath[AnyStr]r   rL   )r   r   r@   rL   r   r   )r   int)r/   rL   rQ   rL   r   r   )r/   rL   r   r   )r   zIterable[bytes])r   zIterable[tuple[bytes, bytes]])rg   rL   r   bool)rg   rL   rt   rL   r   rL   )rg   rL   r   rL   )rg   rL   r~   r
   r   z
bytes | _Trx   )rg   rL   r~   z	_T | Noner   zbytes | _T | None)r   zMapping[bytes, bytes]r   r   )r   rL   r   r   )r   r   )rg   rL   r   float)__name__
__module____qualname____firstlineno____doc__r#   r0   r4   r:   rA   rG   rS   rX   r[   ra   rh   rk   rp   ru   r   ry   r   r   r   r   r   r   __static_attributes__rs   r%   r"   r   r       s    &6NN. D$,$P3    !&
 r%   r   c                  $    \ rS rSrSrS rS rSrg)Shelfi9  z
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                \    [         R                  " U5      n[        R                  XU5        g)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   rS   )r   r/   rQ   s      r"   rS   Shelf.__setitem__A  s!     LLO4A&r%   c                T    [         R                  " [        R                  X5      5      $ )z
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   rX   r.   s     r"   rX   Shelf.__getitem__N  s     ||F..t788r%   rs   N)r   r   r   r   r   rS   rX   r   rs   r%   r"   r   r   9  s    '9r%   r   c                    [        U 5      $ )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"   r7   r7   \  s     $<r%   )r7   r   r   )NN)r   
__future__r   r+   r   r   r   typingr   r   r   r   r   twisted.python.filepathr	   r
   r   r   r7   __all__rs   r%   r"   <module>r      sV    #   	  ? ? ,TV  V r 9F  9F	 &r%   