U
    [ei                     @   s\   d Z ddlZddlmZ zddlmZ W n  ek
rH   ddlmZ Y nX G dd dZdS )z<For internal use only. It provides a slice-like file reader.    N)Union)Lockc                   @   s   e Zd ZdZeddddZeeef dddZ	e
eed	d
dZedddZddddZeedr~eee
dddZneee
dddZdS )
FileBufferzA slice-able file readerN)databasereturnc                 C   s6   t |d| _t| j j| _ttds2t | _	d S )Nrbpread)
open_handleosfstatfilenost_size_sizehasattrr   _lock)selfr    r   P/var/www/html/services/stratfitenv/lib/python3.8/site-packages/maxminddb/file.py__init__   s    
zFileBuffer.__init__)keyc                 C   sF   t |tr | |j|j |jS t |tr:| d|d S tdd S )N   r   zInvalid argument type.)
isinstanceslice_readstopstartint	TypeError)r   r   r   r   r   __getitem__   s
    

zFileBuffer.__getitem__)needler   r   c                 C   s0   |  | j| d ||}|dkr(|S || S )zReverse find needle from startr   )r   r   rfind)r   r    r   posr   r   r   r"      s    zFileBuffer.rfind)r   c                 C   s   | j S )zSize of file)r   r   r   r   r   size%   s    zFileBuffer.sizec                 C   s   | j   dS )z
Close fileN)r
   closer$   r   r   r   r&   )   s    zFileBuffer.closer   )
buffersizeoffsetr   c                 C   s   t | j ||S )zread that uses pread)r   r   r
   r   r   r'   r(   r   r   r   r   /   s    zFileBuffer._readc              
   C   s6   | j & | j| | j|W  5 Q R  S Q R X dS )a  read with a lock

            This lock is necessary as after a fork, the different processes
            will share the same file table entry, even if we dup the fd, and
            as such the same offsets. There does not appear to be a way to
            duplicate the file table entry and we cannot re-open based on the
            original path as that file may have replaced with another or
            unlinked.
            N)r   r
   seekreadr)   r   r   r   r   6   s    
)__name__
__module____qualname____doc__strr   r   r   r   r   bytesr"   r%   r&   r   r   r   r   r   r   r   r      s   
r   )	r/   r   typingr   multiprocessingr   ImportError	threadingr   r   r   r   r   <module>   s   