U
    d/e                      @   sv   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gZ	e
eZG dd dejejejZdS )	zHSave DOT code objects, render with Graphviz ``dot``, and open in viewer.    N   )_tools)backend)savingRenderc                   @   s  e Zd ZdZejddddddddejej	e
df ejej	e
df eeeje
 eje
 eje
 ejeedf eeejej	e
df eje
 eee
ddd	Zejej	e
f e
edd
ddZejdddejej	e
df ejej	e
df eeee
dddZdS )r   z4Write source lines to file and render with Graphviz.   )Zsupported_numberNF)outfileengineraise_if_result_existsoverwrite_source)filename	directoryviewcleanupformatrenderer	formatterneato_no_opquiet
quiet_viewr   r	   r
   r   returnc                C   s   t |}|dk	r6| j||d}|dkr6t| j|}| j||||||	|||dd
\}}|dk	rr|dkrr| |}| j||dd}|	| | j
||}|rtd| t| |
s|r| j|| j|
d |S )aR  Save the source to file and render with the Graphviz engine.

        Args:
            filename: Filename for saving the source
                (defaults to ``name`` + ``'.gv'``).s
            directory: (Sub)directory for source saving and rendering.
            view (bool): Open the rendered result
                with the default application.
            cleanup (bool): Delete the source file
                after successful rendering.
            format: The output format used for rendering
                (``'pdf'``, ``'png'``, etc.).
            renderer: The output renderer used for rendering
                (``'cairo'``, ``'gd'``, ...).
            formatter: The output formatter used for rendering
                (``'cairo'``, ``'gd'``, ...).
            neato_no_op: Neato layout engine no-op flag.
            quiet (bool): Suppress ``stderr`` output
                from the layout subprocess.
            quiet_view (bool): Suppress ``stderr`` output
                from the viewer process
                (implies ``view=True``, ineffective on Windows platform).
            outfile: Path for the rendered output file.
            engine: Layout engine for rendering
                (``'dot'``, ``'neato'``, ...).
            raise_if_result_exits: Raise :exc:`graphviz.FileExistsError`
                if the result file exists.
            overwrite_source: Allow ``dot`` to write to the file it reads from.
                Incompatible with ``raise_if_result_exists``.

        Returns:
            The (possibly relative) path of the rendered file.

        Raises:
            ValueError: If ``engine``, ``format``, ``renderer``, or ``formatter``
                are unknown.
            graphviz.RequiredArgumentError: If ``formatter`` is given
                but ``renderer`` is None.
            ValueError: If ``outfile`` is the same file as the source file
                unless ``overwite_source=True``.
            graphviz.ExecutableNotFound: If the Graphviz ``dot`` executable
                is not found.
            graphviz.CalledProcessError: If the returncode (exit status)
                of the rendering ``dot`` subprocess is non-zero.
            RuntimeError: If viewer opening is requested but not supported.

        Example:
            >>> doctest_mark_exe()
            >>> import graphviz
            >>> dot = graphviz.Graph(name='spam', directory='doctest-output')
            >>> dot.render(format='png').replace('\\', '/')
            'doctest-output/spam.gv.png'
            >>> dot.render(outfile='spam.svg').replace('\\', '/')
            'doctest-output/spam.svg'

        Note:
            The layout command is started from the directory of ``filepath``,
            so that references to external files
            (e.g. ``[image=images/camelot.png]``)
            can be given as paths relative to the DOT source file.
        N)r   T)
r	   r   r   r   r   r   r   r
   r   verify)r   Zskip_existingz	delete %r)r   r   )r   Zpromote_pathlikeZ_get_formatpathlibPathr   Z_get_render_parametersZ_get_filepathsaveappendZ_renderlogdebugosremove_view_format)selfr   r   r   r   r   r   r   r   r   r   r   r	   r
   r   argskwargsfilepathrendered r'   6/tmp/pip-unpacked-wheel-n8ok7rre/graphviz/rendering.pyrender   s6    M




zRender.render)r%   r   r   r   c                C   st   d| dt jj dt jj g}|D ]}t| |d}|dk	r& qdq&t| jd|dt jjd|||d dS )z9Start the right viewer based on file format and platform.Z_view__Nz$ has no built-in viewer support for z on z	 platform)r   )r   ZviewingZPLATFORMgetattrRuntimeError	__class__)r"   r%   r   r   ZmethodnamesnameZview_methodr'   r'   r(   r       s     zRender._view)r   r   r   r   r   r   c                 C   s   | j ||d|||dS )a  Save the source to file, open the rendered result in a viewer.

        Convenience short-cut for running ``.render(view=True)``.

        Args:
            filename: Filename for saving the source
                (defaults to ``name`` + ``'.gv'``).
            directory: (Sub)directory for source saving and rendering.
            cleanup (bool): Delete the source file after successful rendering.
            quiet (bool): Suppress ``stderr`` output from the layout subprocess.
            quiet_view (bool): Suppress ``stderr`` output
                from the viewer process (ineffective on Windows).

        Returns:
            The (possibly relative) path of the rendered file.

        Raises:
            graphviz.ExecutableNotFound: If the Graphviz executable
                is not found.
            graphviz.CalledProcessError: If the exit status is non-zero.
            RuntimeError: If opening the viewer is not supported.

        Short-cut method for calling :meth:`.render` with ``view=True``.

        Note:
            There is no option to wait for the application to close,
            and no way to retrieve the application's exit status.
        T)r   r   r   r   r   r   )r)   )r"   r   r   r   r   r   r'   r'   r(   r      s
    #
  zRender.view)
NNFFNNNNFF)NNFFF)__name__
__module____qualname____doc__r   Zdeprecate_positional_argstypingUnionr   PathLikestrboolOptionalintr)   r    r   r'   r'   r'   r(   r      sj   
           o  
      )r2   loggingr   r   r3    r   r   r   __all__	getLoggerr/   r   ZSaver   ZViewr'   r'   r'   r(   <module>   s   
