o
    JPZ                     @   sh   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dS )	    N)AbstractReader)MibInfo)decode)debug)errorc                       sX   e Zd ZdZdZdZdddZdd Zddd	Ze	d
d Z
 fddZdd Z  ZS )
FileReaderzFetch ASN.1 MIB text by name from local file.

    *FileReader* class instance tries to locate ASN.1 MIB files
    by name, fetch and return their contents to caller.
    Tz.indexc                 C   s*   t j|| _|| _|| _d| _d| _dS )a,  Create an instance of *FileReader* serving a directory.

           Args:
               path (str): directory to search MIB files

           Keyword Args:
               recursive (bool): whether to include subdirectories
               ignoreErrors (bool): ignore filesystem access errors
        FN)ospathnormpath_path
_recursive_ignoreErrors_indexLoaded	_mibIndex)selfr	   	recursiveignoreErrors r   8/usr/lib/python3/dist-packages/pysmi/reader/localfile.py__init__   s
   

zFileReader.__init__c                 C   s   d| j j| jf S )Nz%s{"%s"})	__class____name__r   )r   r   r   r   __str__*   s   zFileReader.__str__c              	   C   s   |s|gS |g}zt |}W n ty)   |r| Y S td|t d f w |D ]}t jt	|t	|}t j
|rH|| || q,|S )Nzdirectory %s access error: %s   )r   listdirOSErrorr   
PySmiErrorsysexc_infor	   joinr   isdirextend
getSubdirs)r   r	   r   r   dirsZsubdirsdr   r   r   r"   -   s    zFileReader.getSubdirsc                 C   s   i }t j| r>z+t| }tdd | D }|  tjtj	@ o0td| t
|f  W |S  W |S  ty=   Y |S w |S )Nc                 S   s   g | ]
}|  d d qS )N   )split).0xr   r   r   
<listcomp>K   s    z(FileReader.loadIndex.<locals>.<listcomp>z-loaded MIB index map from %s file, %s entries)r   r	   existsopendict	readlinescloser   logger
flagReaderlenIOError)	indexFileZmibIndexfr   r   r   	loadIndexD   s&   zFileReader.loadIndexc                    s   | j r>| js| tj| j| j| _d| _|| jv r>t	j
t	j@ o5t	
d|| j| f  || j| fgS  || j| fgS tt| |S )NTzfound %s in MIB index: %s)useIndexFiler   r5   r   r	   r   r   r3   r   r   r/   r0   superr   getMibVariants)r   mibnamer   r   r   r8   V   s   
zFileReader.getMibVariantsc           	      C   s  t jt j@ ot d| jrdpd|f  | | j| j| jD ]}| |D ]\}}tj	
t|t|}t jt j@ o@t d|  tj	|rtj	|rzMt|d }t jt j@ ojt d|tdt|f  t|dd	}|| j}|  t|| jkrtd
| td| |||dt|fW     S  ttfy   t jt j@ ot d|t d f  | jstd|t d f Y nw tjd| | dq%qtjd| | d)Nz%slooking for MIB %szrecursively  ztrying MIB %s   z+source MIB %s mtime is %s, fetching data...z%a, %d %b %Y %H:%M:%S GMTrb)modezMIB %s too largez	file://%s)r	   filenamemtimezsource file %s open failure: %sr   zfile %s access error: %sz"source MIB %s is older than needed)readerzsource MIB %s not found) r   r/   r0   r   r"   r   r   r8   r   r	   r   r   r*   isfilestattimestrftimegmtimer+   readZ
maxMibSizer.   r1   r2   r   r   r   r   r   r   ZPySmiReaderFileNotModifiedErrorZPySmiReaderFileNotFoundError)	r   r9   r	   ZmibaliasZmibfiler4   rA   fpZmibDatar   r   r   getDatae   sJ   &zFileReader.getData)TT)r   
__module____qualname____doc__r6   r3   r   r   r"   staticmethodr5   r8   rJ   __classcell__r   r   r:   r   r      s    


r   )r   r   rE   Zpysmi.reader.baser   Zpysmi.mibinfor   Zpysmi.compatr   Zpysmir   r   r   r   r   r   r   <module>   s   