o
    §ƒðj:T  ã                   @   s„   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. é    )ÚBytesIO)Úlatex)Úpreview)Údebug)Ú	Printable)Úimport_modulec                    s*   ddl }ddl‰ ‡ ‡‡fdd„}||_dS )z.Setup printing in Python interactive session. r   Nc                    s0   | durdˆ _ tˆ| fi ˆ¤Žƒ | ˆ _ dS 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© úœ/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/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                    sL  t ddd�‰ zddlm‰ W n	 ty   Y nw ˆ
du r6| j ¡ }|dkr(d‰
n	|d	kr/d
‰
nd‰
tdˆ
ƒ |dkr?dˆ
 ‰nd‰d}d}tdˆ ƒ}d|||ˆ
|f }| ¡ ‰dˆ }dd 	|¡g‰tdˆƒ tdˆƒ |	ppt
‰‡ ‡‡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+„}tttttttg}| jjd, }|tg D ]}| ||¡ qØ| jjd- }|d.v rþtd/ƒ |D ]}| ||¡ qñ|t_ntd0ƒ |D ]}||jv �r|j |¡ �qtjt_| jjd1 }|d2v �r7td3ƒ |D ]	}| ||¡ �q)|t_n3|d4k�rPtd5ƒ |D ]	}| ||¡ �qB|t_ntd6ƒ |D ]}||jv �rd|j |¡ �qVtjt_| jjd7 }|d8v �rŠtd9ƒ |D ]	}| ||¡ �q{|t_dS td:ƒ |D ]}||jv �rž|j |¡ �q�tjt_dS );z/Setup printing in IPython interactive session. ÚIPythonz1.0)Úmin_module_versionr   )Úlatex_to_pngNÚlightbgÚBlackÚlinuxÚWhiteÚGrayz*init_printing: Automatic foreground color:Úsvgz
\special{color %s}Ú Ú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                    s2   ˆ| ƒr|  ˆ| ƒ¡ dS |  ˆ jj | ¡¡ dS )z*caller for pretty, for use in IPython 0.11N)ÚtextÚlibÚpretty)r
   ÚpÚcycle)r   Ú
_can_printr   r   r   Ú_print_plainP   s   z,_init_ipython_printing.<locals>._print_plainc              
      sT   t ƒ }zt| ddˆ|ˆˆ ˆd� W | ¡ S  ty) } z	tddt|ƒƒ ‚ d }~ww )NÚpngr   ©ÚoutputÚviewerÚeulerÚoutputbufferÚextra_preambleÚ
dvioptionsÚfontsizezpng printing:ú"_preview_wrapper exception raised:)r   r   Ú	Exceptionr   ÚreprÚgetvalue©ÚoÚ
exprbufferÚe)r/   r,   r.   r0   r   r   Ú_preview_wrapperW   s   
þûÿ€üz0_init_ipython_printing.<locals>._preview_wrapperc              
      s\   t ƒ }zt| ddˆ|ˆˆ ˆd� W n ty& } z	tddt|ƒƒ ‚ d }~ww | ¡  d¡S )Nr   r   r)   zsvg printing:r1   zutf-8)r   r   r2   r   r3   r4   Údecoder5   )Údvioptions_svgr,   r.   r0   r   r   Ú_svg_wrapperd   s   

þÿ€üz,_init_ipython_printing.<locals>._svg_wrapperc              
      sf   zz	ˆ| ˆ ˆd�W W S  t y   ˆ| ƒ Y W S w  ty2 } ztdt|ƒƒ W Y d }~d S d }~ww )N)ÚcolorÚscalezmatplotlib exception caught:)Ú	TypeErrorÚ
ValueErrorr   r3   )r6   r8   )Ú	forecolorr   r>   r   r   Ú_matplotlib_wrapperq   s   ÿ€þz3_init_ipython_printing.<locals>._matplotlib_wrapper)Ú_latexÚ	_sympystrÚ_prettyÚ
_sympyreprc                    sø   zqt tttf}tˆ |ƒr3tˆ ƒjdd„ |D ƒvs$tˆ ƒjdd„ |D ƒvr'W dS t‡fdd„ˆ D ƒƒW S tˆ t	ƒrEt‡‡ fdd„ˆ D ƒƒW S tˆ t
ƒrMW dS tˆ tƒrUW dS t‡ fdd„ˆD ƒƒrcW dS tˆ ttfƒroˆroW dS W dS  ty{   Y dS w )	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   ó   � | ]}|j V  qd S ©N)Ú__str__©Ú.0Úir   r   r   Ú	<genexpr>’   ó   € z=_init_ipython_printing.<locals>._can_print.<locals>.<genexpr>c                 s   rG   rH   )Ú__repr__rJ   r   r   r   rM   “   rN   Fc                 3   s   � | ]}ˆ |ƒV  qd S rH   r   rJ   )r&   r   r   rM   •   s   € c                 3   s$   � | ]}ˆ |ƒoˆ ˆ| ƒV  qd S rH   r   rJ   )r&   r6   r   r   rM   —   s   €" Tc                 3   s   � | ]}t ˆ |ƒV  qd S rH   )Úhasattr)rK   Úhook©r6   r   r   rM   �   s   € )ÚlistÚtupleÚsetÚ	frozensetÚ
isinstanceÚtyperI   rO   ÚallÚdictÚboolr   ÚanyÚfloatÚintÚRuntimeError)r6   Úbuiltin_types)r&   Úprint_builtinÚprinting_hooksrR   r   r&   ƒ   s*   



ÿz*_init_ipython_printing.<locals>._can_printc              
      s”   ˆ | ƒrHˆ| fdˆiˆ¤Ž}ˆdkrd| }zˆ|ƒW S  t yG } z tdt|ƒdƒ ˆdkr9ˆ| fddiˆ¤Ž}ˆ|ƒW  Y d}~S d}~ww 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©r_   r   r3   ©r6   Úsr8   )r&   rB   r9   r   Ú
latex_moder   r   r   Ú_print_latex_png©   s    

ÿ€ûúz0_init_ipython_printing.<locals>._print_latex_pngc              
      sr   ˆ | ƒr7ˆ| fdˆiˆ¤Ž}ˆdkrd| }zˆ|ƒW S  t y6 } ztdt|ƒdƒ W Y d}~dS d}~ww dS )zz
        A function that returns a svg rendered by an external latex
        distribution, no fallback available.
        rc   rd   re   rf   z No fallback available.Nrh   ri   )r&   r<   r   rk   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
        rc   rg   Nr   ©r6   rj   )r&   rB   r   r   r   r   Ú_print_latex_matplotlibÊ   s   þz7_init_ipython_printing.<locals>._print_latex_matplotlibc                    s4   ˆ | ƒrˆ| fdˆiˆ¤Ž}ˆdkrd| S |S dS )zW
        A function to generate the latex representation of SymPy expressions.
        rc   rd   re   Nr   rn   )r&   r   rk   r   r   r   Ú_print_latex_textÒ   s   üz1_init_ipython_printing.<locals>._print_latex_textz
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 formatterÚ
matplotlibz)init_printing: using matplotlib formatterz*init_printing: not using any png formatterz
text/latex)TÚmathjaxz&init_printing: using mathjax formatterz-init_printing: not using text/latex formatter)r   ÚIPython.lib.latextoolsr   ÚImportErrorÚcolorsÚlowerr   ÚroundÚsplitÚformatÚdefault_latexr]   rT   rS   rU   rV   rZ   r^   Údisplay_formatterÚ
formattersr   Úfor_typeÚ
_repr_svg_Útype_printersÚpopÚ_repr_disabledÚ
_repr_png_Ú_repr_latex_)Úipr   Ú	use_latexr,   rA   Ú	backcolorr0   rk   ra   Úlatex_printerr>   r   r=   Ú	imagesizeÚoffsetÚ
resolutionÚdviÚ	svg_scaler'   rl   rm   ro   rp   Úprintable_typesÚplaintext_formatterÚclsÚsvg_formatterÚpng_formatterÚlatex_formatterr   )r   r&   rB   r9   r<   r/   r;   r,   r.   r0   rA   r   rk   r   ra   rb   r>   r   r   r   Ú_init_ipython_printing!   s¨   ÿ	



ÿ

&€

€

€r“   c                 C   sn   ddl m} d|vrdS zddlm} W n ty1   zddlm} W n ty.   Y Y dS w Y nw t| |ƒS )z%Is a shell instance an IPython shell?r   )Úmodulesr   F)ÚInteractiveShell)r   r”   ÚIPython.core.interactiveshellr•   rt   ÚIPython.iplibrW   )Úshellr”   r•   r   r   r   Ú_is_ipython  s   ýÿý
r™   FTNÚTransparentÚ10ptrd   ç      ð?c                    s®  ddl }ddlm} | r|dur|}nddlm} n|dur!|}nddlm} d}|du r@ztƒ }W n	 ty;   Y nw |du}|rH|sHt|ƒ}|r‡| r‡zddl	m
} ddlm} W n	 tyc   Y n$w t|||fƒs‡d	d
 |j¡vr‡ˆdu r}tdƒ d‰|du r‡tdƒ d}ts•|s•|jˆˆˆˆd� n|‰ | r¤‡ ‡‡‡‡fdd„}n‡ ‡fdd„}|rÍ| dd¡}|r¹tdƒ t|||||	|
|||||fi |¤Ž dS t|fi |¤Ž 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#   )ÚsstrreprF)Ú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    ÿÿÿrc   zMinit_printing: Mode is not able to be set due to internalsof IPython printing)r   Úsympy.printing.printerr�   Úsympy.printingr#   rž   Úget_ipythonÚ	NameErrorr™   Ú!IPython.terminal.interactiveshellrŸ   Úcoder    rt   rW   ÚjoinÚargvr   Ú	NO_GLOBALÚset_global_settingsr€   r“   r   )Úpretty_printr¢   r£   r…   r¤   r¥   Ú	no_globalr„   r,   rA   r†   r0   rk   ra   Ústr_printerÚpretty_printerr‡   r>   r   r   r�   r   Ú
in_ipythonrŸ   r    Úmode_in_settingsr   r¨   r   Úinit_printing1  sn    
ÿÿÿÿ
þ
ýr»   )TNNNNNFNFNrš   r›   rd   TNNNrœ   )Ú__doc__Úior   Úsympy.printing.latexr   rz   Úsympy.printing.previewr   Úsympy.utilities.miscr   Úsympy.printing.defaultsr   Úsympy.externalr   r   r“   r™   r³   r»   r   r   r   r   Ú<module>   s(     |ú