????
Your IP : 3.143.203.155
3
\�.�@sTdZddlZddlZddlZddlZddgZdZ e
ZGdd�dej�Z
dd
d�ZdS)a�A dumb and slow but simple dbm clone.
For database spam, spam.dir contains the index (a text file),
spam.bak *may* contain a backup of the index (also a text file),
while spam.dat contains the data (a binary file).
XXX TO DO:
- seems to contain a bug when updating...
- reclaim free space (currently, space once occupied by deleted or expanded
items is never reused)
- support concurrent access (currently, if two processes take turns making
updates, they can mess up the index)
- support efficient access to large databases (currently, the whole index
is read when the database is opened, and some updates rewrite the whole index)
- support opening for read-only (flag = 'm')
�N�error�openic@s�eZdZeZeZd+dd�Zdd�Zdd�Zdd �ZeZ d
d�Z
dd
�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�ZeZd d!�Zd"d#�ZeZd$d%�Zd&d'�Zd(d)�Zd*S),� _Database�ccCsJ||_|dk|_|d|_|d|_|d|_d|_|j|�|j�dS)N�rz.dirz.datz.bak)�_mode� _readonly�_dirfile�_datfile�_bakfile�_index�_create�_update)�selfZfilebasename�mode�flag�r� /usr/lib64/python3.6/dbm/dumb.py�__init__0s
z_Database.__init__cCs�|dkrFx<|j|j|jfD](}ytj|�Wqtk
r@YqXqWytj|jddd�}Wn^tk
r�|dkr�ddl}|j dt
dd �tj|jd
dd��}|j|j�WdQRXYn
X|j�dS)N�nrzLatin-1)�encodingrrzIThe database file is missing, the semantics of the 'c' flag will be used.�)�
stacklevel�w)rr)
r
rr �_os�remove�OSError�_ior�warnings�warn�DeprecationWarning�_chmod�close)rr�filename�frrrrr
Is
z_Database._createcCs�i|_ytj|jddd�}Wntk
r:|j|_YnPXd|_|�>x6|D].}|j�}tj |�\}}|j
d�}||j|<qNWWdQRXdS)NrzLatin-1)rF)rrrr rr� _modified�rstrip�_astZliteral_eval�encode)rr$�line�key�pos_and_siz_pairrrrr_s
z_Database._updatecCs�|jdks|jrdSy|jj|j�Wntk
r<YnXy|jj|j|j�Wntk
rhYnX|jj |jddd��F}|j
|j�x2|jj�D]$\}}d|jd�|f}|j
|�q�WWdQRXdS)NrzLatin-1)rz%r, %r
)rr%r�unlinkrr�renamer rrr!�items�decode�write)rr$r*r+�entryrrr�_commitqsz_Database._commitcCs|jdkrtd��dS)Nz"DBM object has already been closed)rr)rrrr�_verify_open�s
z_Database._verify_openc Cs\t|t�r|jd�}|j�|j|\}}tj|jd��}|j|�|j |�}WdQRX|S)Nzutf-8�rb)
�
isinstance�strr(r3rrrr
�seek�read)rr*�pos�sizr$Zdatrrr�__getitem__�s
z_Database.__getitem__cCsrtj|jd��R}|jdd�t|j��}|tdtt}|jd||�|}|j|�WdQRX|t|�fS)Nzrb+r���) rrr
r7�int�tell�
_BLOCKSIZEr0�len)r�valr$r9Znposrrr�_addval�sz_Database._addvalc
Cs:tj|jd��}|j|�|j|�WdQRX|t|�fS)Nzrb+)rrr
r7r0rB)rr9rCr$rrr�_setval�s
z_Database._setvalcCsP||j|<tj|jddd��*}|j|j�|jd|jd�|f�WdQRXdS)N�azLatin-1)rz%r, %r
)rrrr r!r0r/)rr*r+r$rrr�_addkey�s
z_Database._addkeycCs|jrddl}|jdtdd�t|t�r4|jd�}nt|ttf�sJt d��t|t�r`|jd�}nt|ttf�svt d��|j
�d|_||jkr�|j
||j|��n^|j|\}}|td t}t|�td t}||kr�|j||�|j|<n|j|�|j|<dS)
Nrz'The database is opened for reading onlyr<)rzutf-8zkeys must be bytes or stringszvalues must be bytes or stringsTr=)rrrr r5r6r(�bytes� bytearray� TypeErrorr3r%rrGrDrArBrE)rr*rCrr9r:Z oldblocksZ newblocksrrr�__setitem__�s,
z_Database.__setitem__cCsT|jrddl}|jdtdd�t|t�r2|jd�}|j�d|_|j |=|j
�dS)Nrz'The database is opened for reading onlyr<)rzutf-8T)rrrr r5r6r(r3r%rr2)rr*rrrr�__delitem__�s
z_Database.__delitem__cCs.y
t|j�Stk
r(td�d�YnXdS)Nz"DBM object has already been closed)�listrrJr)rrrr�keys�s
z_Database.keyscs �j��fdd��jj�D�S)Ncsg|]}|�|f�qSrr)�.0r*)rrr�
<listcomp>�sz#_Database.items.<locals>.<listcomp>)r3rrN)rr)rrr.�sz_Database.itemscCsPt|t�r|jd�}y
||jkStk
rJ|jdkrDtd�d�n�YnXdS)Nzutf-8z"DBM object has already been closed)r5r6r(rrJr)rr*rrr�__contains__�s
z_Database.__contains__cCs.y
t|j�Stk
r(td�d�YnXdS)Nz"DBM object has already been closed)�iterrrJr)rrrr�iterkeyss
z_Database.iterkeyscCs.y
t|j�Stk
r(td�d�YnXdS)Nz"DBM object has already been closed)rBrrJr)rrrr�__len__s
z_Database.__len__cCs,z|j�Wdd|_|_|_|_XdS)N)r2rr
r r)rrrrr"sz_Database.closecCs t|jd�r|jj||j�dS)N�chmod)�hasattrrrUr)r�filerrrr!sz_Database._chmodcCs|S)Nr)rrrr� __enter__!sz_Database.__enter__cGs|j�dS)N)r")r�argsrrr�__exit__$sz_Database.__exit__N)r)�__name__�
__module__�__qualname__rrrr
rr2�syncr3r;rDrErGrKrLrNr.rQrS�__iter__rTr"�__del__r!rXrZrrrrr#s2
'rr�c
Csfytjd�}tj|�Wntk
r,YnX||@}|dkrXddl}|jdtdd �t|||d
�S)aEOpen the database file, filename, and return corresponding object.
The flag argument, used to control how the database is opened in the
other DBM implementations, supports only the semantics of 'c' and 'n'
values. Other values will default to the semantics of 'c' value:
the database will always opened for update and will be created if it
does not exist.
The optional mode argument is the UNIX mode of the file, used only when
the database has to be created. It defaults to octal code 0o666 (and
will be modified by the prevailing umask).
rrrrrNz)Flag must be one of 'r', 'w', 'c', or 'n'r<)r)r)rrrr)r�umask�AttributeErrorrrr r)rWrrZumrrrrr(s
)rra)�__doc__Zastr'�ior�osr�collections�__all__rArr�MutableMappingrrrrrr�<module>s