o
    JPZ%
                     @   s`   d dl Z d dlZd dlZd dlmZ d dlmZmZ d dlm	Z	 d dlm
Z
 G dd deZdS )    N)AbstractWriter)encodedecode)debug)errorc                   @   s8   e Zd ZdZdZdd Zdd Zddd	ZdddZdS )
FileWriterzStores transformed MIB modules in files at specified location.

       User is expected to pass *FileReader* class instance to
       *MibCompiler* on instantiation. The rest is internal to *MibCompiler*.
     c                 C   s   t tj|| _dS )zCreates an instance of *FileReader* class.

           Args:
               path: writable directory to store created files
        N)r   ospathnormpath_path)selfr
    r   8/usr/lib/python3/dist-packages/pysmi/writer/localfile.py__init__   s   zFileWriter.__init__c                 C   s   d| j j| jf S )Nz%s{"%s"})	__class____name__r   )r   r   r   r   __str__    s   zFileWriter.__str__Fc              
   C   sd   t j| jt|| j }d }zt|}| }|  |W S  t	t
tfy1   |r.|  Y dS w )Nr   )r	   r
   joinr   r   suffixopenreadcloseOSErrorIOErrorUnicodeEncodeError)r   mibnamedryRunfilenamefdatar   r   r   getData#   s   zFileWriter.getDatar   c           	      C   sx  |rt jt j@ ot d d S  d S tj| js9zt| j W n ty8   t	j
d| jt d f | dw |rKdddd |D  d | }tj| jt|| j }d }ztj| jd	\}}t|t| t| t|| W n/ tttfy   t }|rzt| W n	 ty   Y nw t	j
d
||d f || dw t jt j@ ot d||f  d S  d S )Nzdry run modez-failure creating destination directory %s: %s   )writerz#
r   c                 S   s   g | ]}d | qS )z# %s
r   ).0xr   r   r   
<listcomp>A   s    z&FileWriter.putData.<locals>.<listcomp>)dirzfailure writing file %s: %s)filer#   z%s stored in %s)r   loggerZ
flagWriterr	   r
   existsr   makedirsr   r   ZPySmiWriterErrorsysexc_infor   r   r   tempfileZmkstempwriter   r   renamer   r   unlink)	r   r   r    Zcommentsr   r   Ztfilefdexcr   r   r   putData3   sB    
(zFileWriter.putDataN)F)r   F)	r   
__module____qualname____doc__r   r   r   r!   r4   r   r   r   r   r      s    
r   )r	   r,   r.   Zpysmi.writer.baser   Zpysmi.compatr   r   Zpysmir   r   r   r   r   r   r   <module>   s   