U
    ZÊ¹j¦X  ã                   @   sv   d 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	„ Zd
d„ Zdd„ ZdZddd„ZdS )z7Tools for setting up printing in interactive sessions. é    )Úversion_tuple)ÚBytesIO)Úlatex)Úpreview)Údebug)Ú	Printablec                    s*   ddl }ddl‰ ‡ ‡‡fdd„}||_dS )z.Setup printing in Python interactive session. r   Nc                    s(   | dk	r$dˆ _ tˆ| fˆŽƒ | ˆ _ dS )z’Python's pretty-printer display hook.

           This function was adapted from:

            https://www.python.org/dev/peps/pep-0217/

        N)Ú_Úprint)Úarg©ÚbuiltinsÚsettingsÚstringify_func© úY/var/www/html/bwave/python/venv/lib/python3.8/site-packages/sympy/interactive/printing.pyÚ_displayhook   s    z+_init_python_printing.<locals>._displayhook)Úsysr   Údisplayhook)r   r   r   r   r   r   r   Ú_init_python_printing   s    r   c                    s|  zddl m‰ W n tk
r$   Y nX ˆ
dkrb| j ¡ }|dkrFd‰
n|dkrTd‰
nd‰
td	ˆ
ƒ |d
krtdˆ
 ‰nd‰d}d}tdˆ ƒ}d|||ˆ
|f }| ¡ ‰dˆ }dd |¡g‰tdˆƒ tdˆƒ |	pÖt	‰‡ ‡‡fdd„}‡‡‡‡	fdd„‰‡‡‡‡	fdd„‰‡
‡‡fdd„‰d‰‡‡‡fdd „‰‡‡‡‡‡‡fd!d"„}‡‡‡‡‡fd#d$„}‡‡‡‡fd%d&„}‡‡‡‡fd'd(„}‡fd)d*„}ddl
‰ tˆ jƒtd+ƒk�rltttttttg}| jjd, }|tg D ]}| ||¡ �qÎ| jjd- }|d.k�r td/ƒ |D ]}| ||¡ �q|t_n4td0ƒ |D ]}||jk�r,|j |¡ �q,tjt_| jjd1 }|d2k�r’td3ƒ |D ]}| ||¡ �qv|t_nf|d4k�rÄtd5ƒ |D ]}| ||¡ �q¨|t_n4td6ƒ |D ]}||jk�rÐ|j |¡ �qÐtjt_| jjd7 }|d8k�r6td9ƒ |D ]}| ||¡ �q|t_n4td:ƒ |D ]}||jk�rB|j |¡ �qBtjt_n|  d;|¡ dS )<z/Setup printing in IPython interactive session. r   )Úlatex_to_pngNZlightbgZBlackÚlinuxZWhiteZGrayz*init_printing: Automatic foreground color:Úsvgz
\special{color %s}Ú Ztightz0cm,0cmé–   z-T %s -D %d -bg %s -fg %s -O %sg«ªªªªª @z
--no-fontsz
--scale={}zinit_printing: DVIOPTIONS:zinit_printing: DVIOPTIONS_SVG:c                    s0   ˆ| ƒr|  ˆ| ƒ¡ n|  ˆ jj | ¡¡ dS )z*caller for pretty, for use in IPython 0.11N)ÚtextÚlibÚpretty)r
   ÚpÚcycle)ÚIPythonÚ
_can_printr   r   r   Ú_print_plainO   s    z,_init_ipython_printing.<locals>._print_plainc              
      s`   t ƒ }zt| ddˆ|ˆˆ ˆd� W n4 tk
rV } ztddt|ƒƒ ‚ W 5 d }~X Y nX | ¡ S )NÚpngr   ©ÚoutputZviewerÚeulerZoutputbufferÚextra_preambleÚ
dvioptionsÚfontsizezpng printing:ú"_preview_wrapper exception raised:)r   r   Ú	Exceptionr   ÚreprÚgetvalue©ÚoZ
exprbufferÚe)r'   r%   r&   r(   r   r   Ú_preview_wrapperV   s    
  þ
ÿz0_init_ipython_printing.<locals>._preview_wrapperc              
      sf   t ƒ }zt| ddˆ|ˆˆ ˆd� W n4 tk
rV } ztddt|ƒƒ ‚ W 5 d }~X Y nX | ¡  d¡S )Nr   r   r#   zsvg printing:r)   zutf-8)r   r   r*   r   r+   r,   Údecoder-   )Údvioptions_svgr%   r&   r(   r   r   Ú_svg_wrapperc   s    
  þ
ÿz,_init_ipython_printing.<locals>._svg_wrapperc              
      sr   z6zˆ| ˆ ˆd�W W S  t k
r2   ˆ| ƒ Y W S X W n6 tk
rl } ztdt|ƒƒ W Y ¢d S d }~X Y nX d S )N)ÚcolorÚscalezmatplotlib exception caught:)Ú	TypeErrorÚ
ValueErrorr   r+   )r.   r/   )Ú	forecolorr   r5   r   r   Ú_matplotlib_wrapperp   s    z3_init_ipython_printing.<locals>._matplotlib_wrapper)Z_latexZ	_sympystrZ_prettyZ
_sympyreprc                    s   zât tttf}tˆ |ƒrftˆ ƒjdd„ |D ƒksHtˆ ƒjdd„ |D ƒkrNW dS t‡fdd„ˆ D ƒƒW S tˆ t	ƒrŠt‡‡ fdd„ˆ D ƒƒW S tˆ t
ƒršW dS tˆ tƒrªW dS t‡ fdd„ˆD ƒƒrÆW dS tˆ ttfƒrÞˆrÞW dS W dS  tk
� rú   Y dS X d	S )
zÃReturn True if type o can be printed with one of the SymPy printers.

        If o is a container type, this is True if and only if every element of
        o can be printed in this way.
        c                 s   s   | ]}|j V  qd S ©N)Ú__str__©Ú.0Úir   r   r   Ú	<genexpr>‘   s     z=_init_ipython_printing.<locals>._can_print.<locals>.<genexpr>c                 s   s   | ]}|j V  qd S r:   )Ú__repr__r<   r   r   r   r?   ’   s     Fc                 3   s   | ]}ˆ |ƒV  qd S r:   r   r<   )r    r   r   r?   ”   s     c                 3   s"   | ]}ˆ |ƒoˆ ˆ| ƒV  qd S r:   r   r<   )r    r.   r   r   r?   –   s     Tc                 3   s   | ]}t ˆ |ƒV  qd S r:   )Úhasattr)r=   Úhook©r.   r   r   r?   œ   s     N)ÚlistÚtupleÚsetÚ	frozensetÚ
isinstanceÚtyper;   r@   ÚallÚdictÚboolr   ÚanyÚfloatÚintÚRuntimeError)r.   Zbuiltin_types)r    Úprint_builtinÚprinting_hooksrC   r   r    ‚   s*    
ÿ


z*_init_ipython_printing.<locals>._can_printc              
      s–   ˆ | ƒr’ˆ| fdˆiˆ—Ž}ˆdkr,d| }z
ˆ|ƒW S  t k
r� } z<tdt|ƒdƒ ˆdkrtˆ| fddiˆ—Ž}ˆ|ƒ W Y ¢S d}~X Y nX dS )zˆ
        A function that returns a png rendered by an external latex
        distribution, falling back to matplotlib rendering
        ÚmodeÚplainú$\displaystyle %s$úpreview failed with:z# Falling back to matplotlib backendÚinlineN©rP   r   r+   ©r.   Úsr/   )r    r9   r0   r   Ú
latex_moder   r   r   Ú_print_latex_png¨   s    

ÿz0_init_ipython_printing.<locals>._print_latex_pngc              
      sn   ˆ | ƒrjˆ| fdˆiˆ—Ž}ˆdkr,d| }z
ˆ|ƒW S  t k
rh } ztdt|ƒdƒ W 5 d}~X Y nX dS )zz
        A function that returns a svg rendered by an external latex
        distribution, no fallback available.
        rS   rT   rU   rV   z No fallback available.NrX   rY   )r    r3   r   r[   r   r   r   Ú_print_latex_svgº   s    

ÿz0_init_ipython_printing.<locals>._print_latex_svgc                    s(   ˆ | ƒr$ˆ| fddiˆ—Ž}ˆ|ƒS dS )zD
        A function that returns a png rendered by mathtext
        rS   rW   Nr   ©r.   rZ   )r    r9   r   r   r   r   Ú_print_latex_matplotlibÉ   s    z7_init_ipython_printing.<locals>._print_latex_matplotlibc                    s4   ˆ | ƒr0ˆ| fdˆiˆ—Ž}ˆdkr,d| S |S dS )zW
        A function to generate the latex representation of SymPy expressions.
        rS   rT   rU   Nr   r^   )r    r   r[   r   r   r   Ú_print_latex_textÑ   s
    z1_init_ipython_printing.<locals>._print_latex_textc                    s8   | j jr(ˆ |ƒ}d|krtƒ  t|ƒ ntt|ƒƒ dS )zžIPython's pretty-printer display hook, for use in IPython 0.10

           This function was adapted from:

            ipython/IPython/hooks.py:155

        Ú
N)ÚrcÚpprintr	   r+   )Úselfr
   Úout)r   r   r   Ú_result_displayÛ   s    
z/_init_ipython_printing.<locals>._result_displayz0.11z
text/plainzimage/svg+xml)r   z"init_printing: using svg formatterz*init_printing: not using any svg formatterz	image/png)Tr"   z"init_printing: using png formatterZ
matplotlibz)init_printing: using matplotlib formatterz*init_printing: not using any png formatterz
text/latex)TZmathjaxz&init_printing: using mathjax formatterz-init_printing: not using text/latex formatterZresult_display)ZIPython.lib.latextoolsr   ÚImportErrorÚcolorsÚlowerr   ÚroundÚsplitÚformatÚdefault_latexr   r   Ú__version__rN   rE   rD   rF   rG   rK   rO   Údisplay_formatterÚ
formattersr   Zfor_typeZ
_repr_svg_Ztype_printersÚpopZ_repr_disabledZ
_repr_png_Z_repr_latex_Zset_hook)Úipr   Ú	use_latexr%   r8   Ú	backcolorr(   r[   rQ   Úlatex_printerr5   r   r4   Z	imagesizeÚoffsetÚ
resolutionZdviZ	svg_scaler!   r\   r]   r_   r`   rf   Zprintable_typesZplaintext_formatterÚclsZsvg_formatterZpng_formatterZlatex_formatterr   )r   r    r9   r0   r3   r'   r2   r%   r&   r(   r8   r   r[   r   rQ   rR   r5   r   r   r   Ú_init_ipython_printing!   s®    


    ÿ

&





ry   c                 C   sr   ddl m} d|krdS zddlm} W n> tk
rf   zddlm} W n tk
r`   Y Y dS X Y nX t| |ƒS )z%Is a shell instance an IPython shell?r   )Úmodulesr   F)ÚInteractiveShell)r   rz   ZIPython.core.interactiveshellr{   rg   ZIPython.iplibrH   )Úshellrz   r{   r   r   r   Ú_is_ipython3  s    r}   FTNÚTransparentÚ10ptrT   ç      ð?c                    sæ  ddl }ddlm} | r4|dk	r&|}qNddlm} n|dk	rB|}nddlm} d}|dkr‚z
tƒ }W n tk
rx   Y n
X |dk	}|r’|s’t|ƒ}|�rF| �rFzDddl	}t
|jƒt
dƒkrÈddlm} nddlm} dd	lm} W n tk
rö   Y nPX t|||fƒ�sFd
d |j¡k�rFˆdk�r0tdƒ d‰|dk�rFtdƒ d}t�sf|�sf|jˆˆˆˆd� n.|‰ | �r†‡ ‡‡‡‡fdd„}n‡ ‡fdd„}|�rÖ| dd¡}|�r´tdƒ t|||||	|
|||||f|Ž nt|f|Ž dS )a,  
    Initializes pretty-printer depending on the environment.

    Parameters
    ==========

    pretty_print : bool, default=True
        If ``True``, use :func:`~.pretty_print` to stringify or the provided pretty
        printer; if ``False``, use :func:`~.sstrrepr` to stringify or the provided string
        printer.
    order : string or None, default='lex'
        There are a few different settings for this parameter:
        ``'lex'`` (default), which is lexographic order;
        ``'grlex'``, which is graded lexographic order;
        ``'grevlex'``, which is reversed graded lexographic order;
        ``'old'``, which is used for compatibility reasons and for long expressions;
        ``None``, which sets it to lex.
    use_unicode : bool or None, default=None
        If ``True``, use unicode characters;
        if ``False``, do not use unicode characters;
        if ``None``, make a guess based on the environment.
    use_latex : string, bool, or None, default=None
        If ``True``, use default LaTeX rendering in GUI interfaces (png and
        mathjax);
        if ``False``, do not use LaTeX rendering;
        if ``None``, make a guess based on the environment;
        if ``'png'``, enable LaTeX rendering with an external LaTeX compiler,
        falling back to matplotlib if external compilation fails;
        if ``'matplotlib'``, enable LaTeX rendering with matplotlib;
        if ``'mathjax'``, enable LaTeX text generation, for example MathJax
        rendering in IPython notebook or text rendering in LaTeX documents;
        if ``'svg'``, enable LaTeX rendering with an external latex compiler,
        no fallback
    wrap_line : bool
        If True, lines will wrap at the end; if False, they will not wrap
        but continue as one line. This is only relevant if ``pretty_print`` is
        True.
    num_columns : int or None, default=None
        If ``int``, number of columns before wrapping is set to num_columns; if
        ``None``, number of columns before wrapping is set to terminal width.
        This is only relevant if ``pretty_print`` is ``True``.
    no_global : bool, default=False
        If ``True``, the settings become system wide;
        if ``False``, use just for this console/session.
    ip : An interactive console
        This can either be an instance of IPython,
        or a class that derives from code.InteractiveConsole.
    euler : bool, optional, default=False
        Loads the euler package in the LaTeX preamble for handwritten style
        fonts (https://www.ctan.org/pkg/euler).
    forecolor : string or None, optional, default=None
        DVI setting for foreground color. ``None`` means that either ``'Black'``,
        ``'White'``, or ``'Gray'`` will be selected based on a guess of the IPython
        terminal color setting. See notes.
    backcolor : string, optional, default='Transparent'
        DVI setting for background color. See notes.
    fontsize : string or int, optional, default='10pt'
        A font size to pass to the LaTeX documentclass function in the
        preamble. Note that the options are limited by the documentclass.
        Consider using scale instead.
    latex_mode : string, optional, default='plain'
        The mode used in the LaTeX printer. Can be one of:
        ``{'inline'|'plain'|'equation'|'equation*'}``.
    print_builtin : boolean, optional, default=True
        If ``True`` then floats and integers will be printed. If ``False`` the
        printer will only print SymPy types.
    str_printer : function, optional, default=None
        A custom string printer function. This should mimic
        :func:`~.sstrrepr`.
    pretty_printer : function, optional, default=None
        A custom pretty printer. This should mimic :func:`~.pretty`.
    latex_printer : function, optional, default=None
        A custom LaTeX printer. This should mimic :func:`~.latex`.
    scale : float, optional, default=1.0
        Scale the LaTeX output when using the ``'png'`` or ``'svg'`` backends.
        Useful for high dpi screens.
    settings :
        Any additional settings for the ``latex`` and ``pretty`` commands can
        be used to fine-tune the output.

    Examples
    ========

    >>> from sympy.interactive import init_printing
    >>> from sympy import Symbol, sqrt
    >>> from sympy.abc import x, y
    >>> sqrt(5)
    sqrt(5)
    >>> init_printing(pretty_print=True) # doctest: +SKIP
    >>> sqrt(5) # doctest: +SKIP
      ___
    \/ 5
    >>> theta = Symbol('theta') # doctest: +SKIP
    >>> init_printing(use_unicode=True) # doctest: +SKIP
    >>> theta # doctest: +SKIP
    \u03b8
    >>> init_printing(use_unicode=False) # doctest: +SKIP
    >>> theta # doctest: +SKIP
    theta
    >>> init_printing(order='lex') # doctest: +SKIP
    >>> str(y + x + y**2 + x**2) # doctest: +SKIP
    x**2 + x + y**2 + y
    >>> init_printing(order='grlex') # doctest: +SKIP
    >>> str(y + x + y**2 + x**2) # doctest: +SKIP
    x**2 + x + y**2 + y
    >>> init_printing(order='grevlex') # doctest: +SKIP
    >>> str(y * x**2 + x * y**2) # doctest: +SKIP
    x**2*y + x*y**2
    >>> init_printing(order='old') # doctest: +SKIP
    >>> str(x**2 + y**2 + x + y) # doctest: +SKIP
    x**2 + x + y**2 + y
    >>> init_printing(num_columns=10) # doctest: +SKIP
    >>> x**2 + x + y**2 + y # doctest: +SKIP
    x + y +
    x**2 + y**2

    Notes
    =====

    The foreground and background colors can be selected when using ``'png'`` or
    ``'svg'`` LaTeX rendering. Note that before the ``init_printing`` command is
    executed, the LaTeX rendering is handled by the IPython console and not SymPy.

    The colors can be selected among the 68 standard colors known to ``dvips``,
    for a list see [1]_. In addition, the background color can be
    set to  ``'Transparent'`` (which is the default value).

    When using the ``'Auto'`` foreground color, the guess is based on the
    ``colors`` variable in the IPython console, see [2]_. Hence, if
    that variable is set correctly in your IPython console, there is a high
    chance that the output will be readable, although manual settings may be
    needed.


    References
    ==========

    .. [1] https://en.wikibooks.org/wiki/LaTeX/Colors#The_68_standard_colors_known_to_dvips

    .. [2] https://ipython.readthedocs.io/en/stable/config/details.html#terminal-colors

    See Also
    ========

    sympy.printing.latex
    sympy.printing.pretty

    r   N)ÚPrinter)r   )ÚsstrreprFz1.0)ÚTerminalInteractiveShell)ÚInteractiveConsolezipython-consoler   z*init_printing: Setting use_unicode to TrueTz(init_printing: Setting use_latex to True©ÚorderÚuse_unicodeÚ	wrap_lineÚnum_columnsc                    s   ˆ | fˆˆˆˆdœ|—ŽS )Nr…   r   ©Úexprr   ©Ú_stringify_funcr‰   r†   r‡   rˆ   r   r   Ú<lambda>  s    ýüzinit_printing.<locals>.<lambda>c                    s   ˆ | fdˆi|—ŽS )Nr†   r   rŠ   )r�   r†   r   r   rŽ   %  s   ÿÿÿrS   zMinit_printing: Mode is not able to be set due to internalsof IPython printing)r   Zsympy.printing.printerr�   Zsympy.printingr   r‚   Úget_ipythonÚ	NameErrorr}   r   r   rn   Z!IPython.terminal.interactiveshellrƒ   Z*IPython.frontend.terminal.interactiveshellÚcoder„   rg   rH   ÚjoinÚargvr   Ú	NO_GLOBALZset_global_settingsrq   ry   r   )Zpretty_printr†   r‡   rs   rˆ   r‰   Z	no_globalrr   r%   r8   rt   r(   r[   rQ   Zstr_printerZpretty_printerru   r5   r   r   r�   r   Z
in_ipythonr   rƒ   r„   Zmode_in_settingsr   rŒ   r   Úinit_printingH  s~     
ÿ

 ÿÿ
     þýr•   )TNNNNNFNFNr~   r   rT   TNNNr€   )Ú__doc__Zsympy.external.importtoolsr   Úior   Zsympy.printing.latexr   rm   Zsympy.printing.previewr   Zsympy.utilities.miscr   Zsympy.printing.defaultsr   r   ry   r}   r”   r•   r   r   r   r   Ú<module>   s:                              ú