U
    d/e                     @   s   d 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 dd	lmZ dd
lmZ dgZeeZG dd deje	je
jejejZdS )zDSave DOT code objects, render with Graphviz dot, and open in viewer.    N   )DEFAULT_ENCODING)_tools)saving)jupyter_integration)piping)	rendering)unflatteningSourcec                       s  e Zd ZdZeejdddddeddfej	e
jef ej	e
jedf eje eje eje eje eje d dddZejddddddefddddeej	e
jedf ej	e
jedf eje eje eje eje eje eje
j dd	
 fd
dZ fddZeje dddZeedddZejdddddej	e
jedf ej	e
jedf eje ed fddZ  ZS )r
   a{  Verbatim DOT source code string to be rendered by Graphviz.

    Args:
        source: The verbatim DOT source code string.
        filename: Filename for saving the source (defaults to ``'Source.gv'``).
        directory: (Sub)directory for source saving and rendering.
        format: Rendering output format (``'pdf'``, ``'png'``, ...).
        engine: Layout engine used (``'dot'``, ``'neato'``, ...).
        encoding: Encoding for saving the source.

    Note:
        All parameters except ``source`` are optional. All of them
        can be changed under their corresponding attribute name
        after instance creation.
       )Zsupported_numberN)filename	directoryformatengineencodingrenderer	formatterreturnc                 C   s   t |}|jrtj||nt|}|dkr8t }t	
d|| t||d}	|	 }
W 5 Q R X | |
||||||||d	S )a  Return an instance with the source string read from the given file.

        Args:
            filename: Filename for loading/saving the source.
            directory: (Sub)directory for source loading/saving and rendering.
            format: Rendering output format (``'pdf'``, ``'png'``, ...).
            engine: Layout command used (``'dot'``, ``'neato'``, ...).
            encoding: Encoding for loading/saving the source.
        Nzread %r with encoding %r)r   )r   r   r   r   r   r   r   loaded_from_path)r   Zpromote_pathlike_directorypartsospathjoinfspathlocalegetpreferredencodinglogdebugopenread)clsr   r   r   r   r   r   r   filepathfdsource r$   4/tmp/pip-unpacked-wheel-n8ok7rre/graphviz/sources.py	from_file)   s&    
    zSource.from_file)r   r   r   )
r#   r   r   r   r   r   r   r   r   r   c          
   	      s*   t  j|||||||d |	| _|| _d S )N)r   r   r   r   r   r   r   )super__init___loaded_from_path_source)
selfr#   r   r   r   r   r   r   r   r   	__class__r$   r%   r(   L   s    

  zSource.__init__c                    s   t  jf | j| jd|S )z3Return the kwargs to create a copy of the instance.)r#   r   )r'   _copy_kwargsr*   r)   )r+   kwargsr,   r$   r%   r.   ]   s
    zSource._copy_kwargs)r   c                 c   sR   | j jdd}|dd E dH  |dd D ] }|ds>dnd}|| V  q,dS )zwYield the DOT source code read from file line by line.

        Yields: Line ending with a newline (``'\n'``).
        T)keependsN
 )r*   
splitlinesendswith)r+   lineslinesuffixr$   r$   r%   __iter__c   s
    zSource.__iter__c                 C   s   | j }|ds|d7 }|S )znThe DOT source code as string.

        Normalizes so that the string always ends in a final newline.
        r2   )r*   r5   )r+   r#   r$   r$   r%   r#   n   s    
zSource.source)skip_existing)r   r   r:   r   c                   sD   |dko| j otj| j | j}|r2td| j t j|||dS )a  Save the DOT source to file. Ensure the file ends with a newline.

        Args:
            filename: Filename for saving the source (defaults to ``name`` + ``'.gv'``)
            directory: (Sub)directory for source saving and rendering.
            skip_existing: Skip write if file exists (default: ``None``).
                By default skips if instance was loaded from the target path:
                ``.from_file(self.filepath)``.

        Returns:
            The (possibly relative) path of the saved source file.
        Nz;.save(skip_existing=None) skip writing Source.from_file(%r))r   r   r:   )	r)   r   r   samefiler!   r   r   r'   save)r+   r   r   r:   skipr,   r$   r%   r<   y   s    
zSource.save)NN)__name__
__module____qualname____doc__classmethodr   Zdeprecate_positional_argsr   typingUnionr   PathLikestrOptionalr&   r(   r.   Iteratorr9   propertyr#   boolr<   __classcell__r$   r$   r,   r%   r
      sj   
 !

 

   )rA   r   loggingr   rC   r   r   r3   r   r   r   r   r   r	   __all__	getLoggerr>   r   ZRenderZSaveZJupyterIntegrationZPipeZ	Unflattenr
   r$   r$   r$   r%   <module>   s$   
 