o
    ooia4                     @   sd  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 g dZdejdeje defddZdeje fddZdeje fddZdejdefddZddddejejef dedeje deje dejf
ddZdejejef dejfddZej				d.dddd d!ededejejef deje deje d"ejeedf d#edejejedf d$ed%edefd&d'Zej						(d/dddd d!edeje dejejedf deje deje d"ejeedf d#edejejedf d$ed%edefd)d'Zej						(d/dddd d!edeje dejejedf deje deje d"ejeedf d#edejejedf d$ed%edefd*d'Zejd+d,						(d0dd(d(d d!edeje dejejedf deje deje d"ejeedf d#edejejedf d$ed%edefd-d'ZdS )1z.Render DOT source files with Graphviz ``dot``.    N   )DEFAULT_SOURCE_EXTENSION)_tools)
exceptions)
parameters   )dot_command)execute)
get_formatget_filepathrenderoutfileformatreturnc             
   C   s   zt | }W n7 ty=   |du r'd| jdt| dt d}t|tj	d| jdd| dtj
d	 | Y S w |dusDJ |dur^| |kr^tj	d
|d|tjd	 |S |S )a
  Return format inferred from outfile suffix and/or given ``format``.

    Args:
        outfile: Path for the rendered output file.
        format: Output format for rendering (``'pdf'``, ``'png'``, ...).

    Returns:
        The given ``format`` falling back to the inferred format.

    Warns:
        graphviz.UnknownSuffixWarning: If the suffix of ``outfile``
            is empty/unknown.
        graphviz.FormatSuffixMismatchWarning: If the suffix of ``outfile``
            does not match the given ``format``.
    N*cannot infer rendering format from suffix  of outfile: z/ (provide format or outfile with a suffix from )zunknown outfile suffix z (expected: .)categoryzexpected format z) from outfile differs from given format: )infer_format
ValueErrorsuffixosfspathget_supported_suffixesr   RequiredArgumentErrorwarningswarnUnknownSuffixWarninglowerFormatSuffixMismatchWarning)r   r   inferred_formatmsg r#   N/home/ubuntu/.local/lib/python3.10/site-packages/graphviz/backend/rendering.pyr
      s8   
r
   c                   C   s   dd t  D S )zReturn a sorted list of supported outfile suffixes for exception/warning messages.

    >>> get_supported_suffixes()  # doctest: +ELLIPSIS
    ['.bmp', ...]
    c                 S   s   g | ]}d | qS )r   r#   ).0r   r#   r#   r$   
<listcomp>C   s    z*get_supported_suffixes.<locals>.<listcomp>)get_supported_formatsr#   r#   r#   r$   r   =   s   r   c                   C   s
   t tjS )zReturn a sorted list of supported formats for exception/warning messages.

    >>> get_supported_formats()  # doctest: +ELLIPSIS
    ['bmp', ...]
    )sortedr   FORMATSr#   r#   r#   r$   r'   F   s   
r'   c                 C   s   | j stdt| d| j d\}}}|r|r#J | j d| }zt| W |S  tyL   td| j dt| d|dt d		w )
a  Return format inferred from outfile suffix.

    Args:
        outfile: Path for the rendered output file.

    Returns:
        The inferred format.

    Raises:
        ValueError: If the suffix of ``outfile`` is empty/unknown.

    >>> infer_format(pathlib.Path('spam.pdf'))  # doctest: +NO_EXE
    'pdf'

    >>> infer_format(pathlib.Path('spam.gv.svg'))
    'svg'

    >>> infer_format(pathlib.Path('spam.PNG'))
    'png'

    >>> infer_format(pathlib.Path('spam'))
    Traceback (most recent call last):
        ...
    ValueError: cannot infer rendering format from outfile: 'spam' (missing suffix)

    >>> infer_format(pathlib.Path('spam.wav'))  # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
    Traceback (most recent call last):
        ...
    ValueError: cannot infer rendering format from suffix '.wav' of outfile: 'spam.wav'
    (unknown format: 'wav', provide outfile with a suffix from ['.bmp', ...])
    z,cannot infer rendering format from outfile: z (missing suffix)r   z.startswith('.')r   r   z (unknown format: z%, provide outfile with a suffix from r   )	r   r   r   r   	partitionr   r   verify_formatr   )r   startsepformat_r#   r#   r$   r   O   s*    

r   )renderer	formatterfilepathr/   r0   c                C   sh   t | } tj|dd tj|dd tj|dd |||f}ddd |D }| | j d| S )zReturn ``filepath`` + ``[[.formatter].renderer].format``.

    See also:
        https://www.graphviz.org/doc/info/command.html#-O
    T)requiredFr   c                 s   s    | ]	}|d ur|V  qd S )Nr#   )r%   ar#   r#   r$   	<genexpr>   s    zget_outfile.<locals>.<genexpr>)	r   promote_pathliker   r+   verify_rendererverify_formatterjoinwith_suffixr   )r1   r   r/   r0   suffix_argsr   r#   r#   r$   get_outfile   s   
	
r;   c                 C   s   t | } | dt S )z&Return ``outfile.with_suffix('.gv')``.r   )r   r5   r9   r   )r   r#   r#   r$   r      s   
r   .)r   raise_if_result_existsoverwrite_filepathengineneato_no_opquietr<   r=   c          
      C      dS )zBRequire ``format`` and ``filepath`` with default ``outfile=None``.Nr#   
r>   r   r1   r/   r0   r?   r@   r   r<   r=   r#   r#   r$   r          r   Fc          
      C   rA   )z<Optional ``format`` and ``filepath`` with given ``outfile``.Nr#   rB   r#   r#   r$   r      rC   c          
      C   rA   )zGRequired/optional ``format`` and ``filepath`` depending on ``outfile``.Nr#   rB   r#   r#   r$   r      rC      )supported_numberc                C   st  |r|	rt dttj||f\}}|durUt||d}|du r$t|}|	sA|j|jkrA| | krAt d|jd|jd|j|jkrK| n|j}
d|
|jg}n'|du rbt	
d|d	|du rot	
d
|d	t||||d}d|jg}tj| ||||d}|rtj|rt	dt|||7 }|dusJ dtj||jjr|jnd|dd t|S )aY  Render file with ``engine`` into ``format`` and return result filename.

    Args:
        engine: Layout engine for rendering (``'dot'``, ``'neato'``, ...).
        format: Output format for rendering (``'pdf'``, ``'png'``, ...).
            Can be omitted if an ``outfile`` with a known ``format`` is given,
            i.e. if ``outfile`` ends  with a known ``.{format}`` suffix.
        filepath: Path to the DOT source file to render.
            Can be omitted if ``outfile`` is given,
            in which case it defaults to ``outfile.with_suffix('.gv')``.
        renderer: Output renderer (``'cairo'``, ``'gd'``, ...).
        formatter: Output formatter (``'cairo'``, ``'gd'``, ...).
        neato_no_op: Neato layout engine no-op flag.
        quiet: Suppress ``stderr`` output from the layout subprocess.
        outfile: Path for the rendered output file.
        raise_if_result_exists: Raise :exc:`graphviz.FileExistsError`
            if the result file exists.
        overwrite_filepath: 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 ``format`` or ``filepath`` are None
            unless ``outfile`` is given.
        graphviz.RequiredArgumentError: If ``formatter`` is given
            but ``renderer`` is None.
        ValueError: If ``outfile`` and ``filename`` are the same file
            unless ``overwite_filepath=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.
        graphviz.FileExistsError: If ``raise_if_exists``
            and the result file exists.

    Warns:
        graphviz.UnknownSuffixWarning: If the suffix of ``outfile``
            is empty or unknown.
        graphviz.FormatSuffixMismatchWarning: If the suffix of ``outfile``
            does not match the given ``format``.

    Example:
        >>> doctest_mark_exe()
        >>> import pathlib
        >>> import graphviz
        >>> assert pathlib.Path('doctest-output/spam.gv').write_text('graph { spam }') == 14
        >>> graphviz.render('dot', 'png', 'doctest-output/spam.gv').replace('\\', '/')
        'doctest-output/spam.gv.png'
        >>> graphviz.render('dot', filepath='doctest-output/spam.gv',
        ...                 outfile='doctest-output/spam.png').replace('\\', '/')
        'doctest-output/spam.png'
        >>> graphviz.render('dot', outfile='doctest-output/spam.pdf').replace('\\', '/')
        'doctest-output/spam.pdf'

    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.

    See also:
        Upstream docs: https://www.graphviz.org/doc/info/command.html
    zAoverwrite_filepath cannot be combined with raise_if_result_existsN)r   zoutfile z# must be different from input file z+ (pass overwrite_filepath=True to override)z-oz1filepath: (required if outfile is not given, got r   z/format: (required if outfile is not given, got )r   r/   r0   z-O)r/   r0   r?   zoutput file exists: zwork around pytype false alarmT)cwdr@   capture_output)r   mapr   r5   r
   r   nameresolveparentr   r   r;   r   commandr   pathexistsFileExistsErrorr   r	   	run_checkparts)r>   r   r1   r/   r0   r?   r@   r   r<   r=   outfile_argargscmdr#   r#   r$   r      sZ   N




)....).....F)NNNNNF)__doc__r   pathlibtypingr   	_defaultsr    r   r   r   r   r	   __all__PathOptionalstrr
   Listr   r'   r   UnionPathLiker;   r   overloadboolintr   deprecate_positional_argsr#   r#   r#   r$   <module>   s4   *		6
 
		
		
		

		