B
    v9a/                 @   sR   d dl Zd dlZd dlZd dlZddgZdZ	e
ZG dd dejjZd
d	dZdS )    Nerroropeni   c               @   s   e Zd ZeZeZd+ddZdd Zdd Zdd	 ZeZ	d
d Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZeZd d! Zd"d# ZeZd$d% Zd&d' Zd(d) Zd*S ),	_Databasecc             C   sL   || _ |dk| _|d | _|d | _|d | _d | _| | | | d S )Nrz.dirz.datz.bak)_mode	_readonly_dirfile_datfile_bakfile_index_create_update)selfZfilebasenamemodeflag r   /usr/lib/python3.7/dumb.py__init__0   s    




z_Database.__init__c             C   s   |dkrFx<| j | j| jfD ](}yt| W q tk
r@   Y qX qW ytj| j ddd}W n^ tk
r   |dkrdd l}|j	dt
dd	 tj| j d
dd}| | j  W d Q R X Y n
X |  d S )Nnr   zLatin-1)encoding)r   r   r   zIThe database file is missing, the semantics of the 'c' flag will be used.   )
stacklevelw)r
   r   r	   _osremoveOSError_ior   warningswarnDeprecationWarning_chmodclose)r   r   filenamefr   r   r   r   r   I   s     

z_Database._createc          	   C   s   i | _ ytj| jddd}W n> tk
rZ   | j | _|dkrVdd l}|jdt	dd Y nPX d	| _|> x6|D ].}|
 }t|\}}|d}|| j |< qnW W d Q R X d S )
Nr   zLatin-1)r   )r   r   r   zFThe index file is missing, the semantics of the 'c' flag will be used.r   )r   F)r   r   r   r	   r   r   	_modifiedr   r   r    rstrip_astZliteral_evalencode)r   r   r$   r   linekeypos_and_siz_pairr   r   r   r   _   s     


z_Database._updatec          	   C   s   | j d ks| jsd S y| j| j W n tk
r:   Y nX y| j| j| j W n tk
rf   Y nX | jj	| jdddF}| 
| j x2| j  D ]$\}}d|d|f }|| qW W d Q R X d S )Nr   zLatin-1)r   z%r, %r
)r   r%   r   unlinkr   r   renamer	   r   r   r!   itemsdecodewrite)r   r$   r*   r+   entryr   r   r   _commitv   s    z_Database._commitc             C   s   | j d krtdd S )Nz"DBM object has already been closed)r   r   )r   r   r   r   _verify_open   s    
z_Database._verify_openc          	   C   s\   t |tr|d}|   | j| \}}t| jd}|| |	|}W d Q R X |S )Nzutf-8rb)

isinstancestrr(   r3   r   r   r   r
   seekread)r   r*   possizr$   Zdatr   r   r   __getitem__   s    


z_Database.__getitem__c          	   C   sr   t | jdR}|dd t| }|t d t t }|d||   |}|| W d Q R X |t|fS )Nzrb+r             )	r   r   r
   r7   inttell
_BLOCKSIZEr0   len)r   valr$   r9   Znposr   r   r   _addval   s    z_Database._addvalc          	   C   s:   t | jd}|| || W d Q R X |t|fS )Nzrb+)r   r   r
   r7   r0   rB   )r   r9   rC   r$   r   r   r   _setval   s    
z_Database._setvalc          	   C   sP   || j |< tj| jddd*}| | j |d|d|f  W d Q R X d S )NazLatin-1)r   z%r, %r
)r   r   r   r	   r!   r0   r/   )r   r*   r+   r$   r   r   r   _addkey   s    
z_Database._addkeyc             C   s  | j rdd l}|jdtdd t|tr4|d}nt|ttfsJt	dt|tr`|d}nt|ttfsvt	d| 
  d| _|| jkr| || | n^| j| \}}|t d	 t }t|t d	 t }||kr| ||| j|< n| || j|< d S )
Nr   z'The database is opened for reading onlyr<   )r   zutf-8zkeys must be bytes or stringszvalues must be bytes or stringsTr=   )r   r   r   r    r5   r6   r(   bytes	bytearray	TypeErrorr3   r%   r   rG   rD   rA   rB   rE   )r   r*   rC   r   r9   r:   Z	oldblocksZ	newblocksr   r   r   __setitem__   s,    



z_Database.__setitem__c             C   sT   | j rdd l}|jdtdd t|tr2|d}|   d| _| j	|= | 
  d S )Nr   z'The database is opened for reading onlyr<   )r   zutf-8T)r   r   r   r    r5   r6   r(   r3   r%   r   r2   )r   r*   r   r   r   r   __delitem__   s    


z_Database.__delitem__c             C   s.   y
t | jS  tk
r(   tdd Y nX d S )Nz"DBM object has already been closed)listr   rJ   r   )r   r   r   r   keys   s    
z_Database.keysc                s         fdd j D S )Nc                s   g | ]}| | fqS r   r   ).0r*   )r   r   r   
<listcomp>   s    z#_Database.items.<locals>.<listcomp>)r3   r   rN   )r   r   )r   r   r.      s    z_Database.itemsc             C   sP   t |tr|d}y
|| jkS  tk
rJ   | jd krDtdd n Y nX d S )Nzutf-8z"DBM object has already been closed)r5   r6   r(   r   rJ   r   )r   r*   r   r   r   __contains__  s    



z_Database.__contains__c             C   s.   y
t | jS  tk
r(   tdd Y nX d S )Nz"DBM object has already been closed)iterr   rJ   r   )r   r   r   r   iterkeys  s    
z_Database.iterkeysc             C   s.   y
t | jS  tk
r(   tdd Y nX d S )Nz"DBM object has already been closed)rB   r   rJ   r   )r   r   r   r   __len__  s    
z_Database.__len__c          	   C   s,   z|    W d d  | _ | _ | _| _X d S )N)r2   r   r
   r	   r   )r   r   r   r   r"     s    z_Database.closec             C   s    t | jdr| j|| j d S )Nchmod)hasattrr   rU   r   )r   filer   r   r   r!   "  s    z_Database._chmodc             C   s   | S )Nr   )r   r   r   r   	__enter__&  s    z_Database.__enter__c             G   s   |    d S )N)r"   )r   argsr   r   r   __exit__)  s    z_Database.__exit__N)r   )__name__
__module____qualname__r   r   r   r   r   r2   syncr3   r;   rD   rE   rG   rK   rL   rN   r.   rQ   rS   __iter__rT   r"   __del__r!   rX   rZ   r   r   r   r   r   #   s2   

	'r   r     c             C   sf   yt d}t | W n tk
r,   Y nX || @ }|dkrXdd l}|jdtdd t| ||dS )Nr   )r   r   r   r   z)Flag must be one of 'r', 'w', 'c', or 'n'r<   )r   )r   )r   umaskAttributeErrorr   r   r    r   )rW   r   r   Zumr   r   r   r   r   -  s    


)r   ra   )Zastr'   ior   osr   Zcollections.abccollections__all__rA   r   r   abcMutableMappingr   r   r   r   r   r   <module>   s     