o
    JPZ                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ G dd deZG d	d
 d
eZdS )    N)AbstractReader)MibInfo)decode)debug)errorc                   @   s<   e Zd ZdZdd Zdd ZdddZd	d
 ZdddZdS )FileLikez4Stripped down, binary file mock to work with ZipFilec                 C   s@   || _ || _|d d | _t|| _g | _d| _d| _d| _d S )Nr   F)namebufnulllenbuflistposclosedZ	softspace)selfr	   r    r   8/usr/lib/python3/dist-packages/pysmi/reader/zipreader.py__init__   s   

zFileLike.__init__c                 C   s"   | j sd| _ | j| _d| _d S d S )NTr   )r   r
   r	   r   r   r   r   r   close   s
   
zFileLike.closer   c                 C   sZ   | j r|  j| j| j 7  _g | _ |dkr|| j7 }n	|dkr%|| j7 }td|| _d S )N      r   )r   r	   r
   joinr   r   max)r   r   moder   r   r   seek%   s   
zFileLike.seekc                 C   s   | j S N)r   r   r   r   r   tell2   s   zFileLike.tellc                 C   s`   | j r|  j| j| j 7  _g | _ |dk r| j}n	t| j| | j}| j| j| }|| _|S )Nr   )r   r	   r
   r   r   minr   )r   nZnewposrr   r   r   read5   s   zFileLike.readN)r   )r   )	__name__
__module____qualname____doc__r   r   r   r   r!   r   r   r   r   r      s    

r   c                   @   s@   e Zd ZdZdZdddZdd Zdd	 Zd
d ZdddZ	dS )	ZipReaderzFetch ASN.1 MIB text by name from a ZIP archive.

    *ZipReader* class instance tries to locate ASN.1 MIB files
    by name, fetch and return their contents to caller.
    FTc              	   C   s   || _ i | _d| _z| jt|dd| _W dS  tyH   tjtj@ o0td| j t	
 d f  |sEtd| j t	
 d f | _Y dS Y dS w )zCreate an instance of *ZipReader* serving a ZIP archive.

           Args:
               path (str): path to ZIP archive containing MIB files

           Keyword Args:
               ignoreErrors (bool): ignore ZIP archive access errors
        Nrb)fileObjzZIP file %s open failure: %sr   zfile %s access error: %s)_name_members_pendingError_readZipDirectoryopen	Exceptionr   logger
flagReadersysexc_infor   Z
PySmiError)r   pathZignoreErrorsr   r   r   r   N   s   	$zZipReader.__init__c                 C   s   t |}t|trd }i }| D ]e}tj|j}|sq|j	ds*|j	dr_|
|j}| t||j}| D ] \}}	||v rM|d7 }||v sE||jd gg||< || |	 q=qttj|jd d   }
||j|
gg||< q|S )Nz.zipz.ZIP+   )zipfileZipFile
isinstancer   infolistosr3   basenamefilenameendswithr!   r,   itemsextendtimemktimedatetimeZ	date_timeZ	timetuple)r   r(   archivemembersmemberr<   ZinnerZipBlobZinnerMembersZinnerFilenamerefmtimer   r   r   r,   e   s.   


	zZipReader._readZipDirectoryc              
   C   s   |D ]>\}}}|st || jd}t|}z||}W q ty@   tjtj@ o;td|j	t
 d f  Y  dS  Y  dS w ||fS )N)r   z$ZIP read component %s read error: %sr   ) r   )r   r)   r6   r7   r!   r.   r   r/   r0   r   r1   r2   )r   refsr(   r<   rG   ZdataObjrC   r   r   r   _readZipFile   s   
(zZipReader._readZipFilec                 C   s   d| j j| jf S )Nz%s{"%s"})	__class__r"   r)   r   r   r   r   __str__   s   zZipReader.__str__Nc                 C   s0  t jt j@ ot d|| jf  | jr| j| js"tjd| | d| |D ]g\}}t jt j@ o7t d|  z| j| }W n	 t	yH   Y q'w | 
|\}}|sSq't jt j@ okt d|tdt|| j|f  t|| jkr|td| j|f td| j|f |||d	t|f  S tjd| | d)
Nzlooking for MIB %s at %szsource MIB %s not found)readerztrying MIB %sz(source MIB %s, mtime %s, read from %s/%sz%a, %d %b %Y %H:%M:%S GMTzMIB %s/%s too largezzip://%s/%s)r3   filer   rG   )r   r/   r0   r)   r+   r*   r   ZPySmiReaderFileNotFoundErrorZgetMibVariantsKeyErrorrJ   r@   strftimegmtimer   Z
maxMibSizeIOErrorr   r   )r   ZmibnameZzipBlobZmibaliasZmibfilerI   ZmibDatarG   r   r   r   getData   s:   zZipReader.getData)Tr   )
r"   r#   r$   r%   ZuseIndexFiler   r,   rJ   rL   rS   r   r   r   r   r&   F   s    
%r&   )r:   r1   r@   rB   r6   Zpysmi.reader.baser   Zpysmi.mibinfor   Zpysmi.compatr   Zpysmir   r   objectr   r&   r   r   r   r   <module>   s   3