o
    ooi]                     @   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U dZeejddddddeddfdej	e
jef dej	e
jedf deje d	eje d
eje deje deje dd fddZejdddddddefdddddedej	e
jedf dej	e
jedf deje d	eje d
eje deje deje deje
j ddf fddZeed< eje
j ed<  fddZdeje fddZedefddZejddd		d"dddej	e
jedf dej	e
jedf deje def 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.
    r   cls)supported_number
ignore_argNfilename	directoryformatengineencodingrenderer	formatterreturnc                 C   s   t |}|jrtj||nt|}|du rt }t	
d|| t||d}	|	 }
W d   n1 s8w   Y  | |
||||||||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   promote_pathlike_directorypartsospathjoinfspathlocalegetpreferredencodinglogdebugopenread)r   r   r   r   r   r   r   r   filepathfdsource r&   D/home/ubuntu/.local/lib/python3.10/site-packages/graphviz/sources.py	from_file)   s    

zSource.from_fileself)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)
r)   r%   r   r   r   r   r   r   r   r   	__class__r&   r'   r+   L   s   


zSource.__init__r-   r,   c                    s   t  jd| j| jd|S )z3Return the kwargs to create a copy of the instance.)r%   r   Nr&   )r*   _copy_kwargsr-   r,   )r)   kwargsr.   r&   r'   r0   a   s
   zSource._copy_kwargsc                 c   sT    | 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__g   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.
        r4   )r-   r7   )r)   r%   r&   r&   r'   r%   r   s   
zSource.source)skip_existingr<   c                   sD   |du o| j otj| j | j}|rt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>   }   s   
zSource.save)NN)__name__
__module____qualname____doc__classmethodr   deprecate_positional_argsr   typingUnionr   PathLikestrOptionalr(   r+   __annotations__r0   Iteratorr;   propertyr%   boolr>   __classcell__r&   r&   r.   r'   r
      s   
 !


)rC   r   loggingr   rF   r   r   r5   r   r   r   r   r   r	   __all__	getLoggerr@   r   RenderSaveJupyterIntegrationPipe	Unflattenr
   r&   r&   r&   r'   <module>   s$    
