o
    8ήcD%                     @   sT   d Z ddlmZ ede dd Zdd Zdd	 Zd
d Zdd ZG dd dZ	dS )zI
This module implements code highlighting of numba function annotations.
    )warnzFThe pretty_annotate functionality is experimental and might change APIc                 C   sj   zddl m} ddlm} ddlm} W n ty   tdw | }	 |d|dd}|| ||}| S )Nr   	highlightPythonLexer)HtmlFormatter%please install the 'pygments' packageT)	noclassesstylenowrap)pygmentsr   pygments.lexersr   pygments.formattersr   ImportError
splitlines)coder
   r   r   r   pylexhfres r   M/tmp/pip-target-vg8gfxp4/lib/python/numba/core/annotations/pretty_annotate.pyhllines
   s   r   c                 C   sf   zddl m} ddlm} ddlm} W n ty   tdw | }	 ||d}|| ||}| S )Nr   r   r   )TerminalFormatterr   )r
   )r   r   r   r   r   r   r   r   )r   r
   r   r   r   r   r   r   r   r   r   htlines   s   
r   c                  C   .   zddl m}  W | dS  ty   tdw )Nr   Template#please install the 'jinja2' packagea  
    {%- for func_key in func_data.keys() -%}
        Function name: [34m{{func_data[func_key]['funcname']}}[39;49;00m
        {%- if func_data[func_key]['filename'] -%}
        {{'
'}}In file: [34m{{func_data[func_key]['filename'] -}}[39;49;00m
        {%- endif -%}
        {{'
'}}With signature: [34m{{func_key[1]}}[39;49;00m
        {{- "
" -}}
        {%- for num, line, hl, hc in func_data[func_key]['pygments_lines'] -%}
                {{-'
'}}{{ num}}: {{hc-}}
                {%- if func_data[func_key]['ir_lines'][num] -%}
                    {%- for ir_line, ir_line_type in func_data[func_key]['ir_lines'][num] %}
                        {{-'
'}}--{{- ' '*func_data[func_key]['python_indent'][num]}}
                        {{- ' '*(func_data[func_key]['ir_indent'][num][loop.index0]+4)
                        }}{{ir_line }}[41m{{ir_line_type-}}[39;49;00m
                    {%- endfor -%}
                {%- endif -%}
            {%- endfor -%}
    {%- endfor -%}
    )jinja2r   r   ansi_templater   r   r   r   get_ansi_template'      r    c                  C   r   )Nr   r   r   a4  
    <html>
    <head>
        <style>

            .annotation_table {
                color: #000000;
                font-family: monospace;
                margin: 5px;
                width: 100%;
            }

            /* override JupyterLab style */
            .annotation_table td {
                text-align: left;
                background-color: transparent; 
                padding: 1px;
            }

            .annotation_table tbody tr:nth-child(even) {
                background: white;
            }

            .annotation_table code
            {
                background-color: transparent; 
                white-space: normal;
            }

            /* End override JupyterLab style */

            tr:hover {
                background-color: rgba(92, 200, 249, 0.25);
            }

            td.object_tag summary ,
            td.lifted_tag summary{
                font-weight: bold;
                display: list-item;
            }

            span.lifted_tag {
                color: #00cc33;
            }

            span.object_tag {
                color: #cc3300;
            }


            td.lifted_tag {
                background-color: #cdf7d8;
            }

            td.object_tag {
                background-color: #fef5c8;
            }

            code.ir_code {
                color: grey;
                font-style: italic;
            }

            .metadata {
                border-bottom: medium solid black;
                display: inline-block;
                padding: 5px;
                width: 100%;
            }

            .annotations {
                padding: 5px;
            }

            .hidden {
                display: none;
            }

            .buttons {
                padding: 10px;
                cursor: pointer;
            }
        </style>
    </head>

    <body>
        {% for func_key in func_data.keys() %}
            <div class="metadata">
            Function name: {{func_data[func_key]['funcname']}}<br />
            {% if func_data[func_key]['filename'] %}
                in file: {{func_data[func_key]['filename']|escape}}<br />
            {% endif %}
            with signature: {{func_key[1]|e}}
            </div>
            <div class="annotations">
            <table class="annotation_table tex2jax_ignore">
                {%- for num, line, hl, hc in func_data[func_key]['pygments_lines'] -%}
                    {%- if func_data[func_key]['ir_lines'][num] %}
                        <tr><td style="text-align:left;" class="{{func_data[func_key]['python_tags'][num]}}">
                            <details>
                                <summary>
                                    <code>
                                    {{num}}:
                                    {{'&nbsp;'*func_data[func_key]['python_indent'][num]}}{{hl}}
                                    </code>
                                </summary>
                                <table class="annotation_table">
                                    <tbody>
                                        {%- for ir_line, ir_line_type in func_data[func_key]['ir_lines'][num] %}
                                            <tr class="ir_code">
                                                <td style="text-align: left;"><code>
                                                &nbsp;
                                                {{- '&nbsp;'*func_data[func_key]['python_indent'][num]}}
                                                {{ '&nbsp;'*func_data[func_key]['ir_indent'][num][loop.index0]}}{{ir_line|e -}}
                                                <span class="object_tag">{{ir_line_type}}</span>
                                                </code>
                                                </td>
                                            </tr>
                                        {%- endfor -%}
                                    </tbody>
                                </table>
                                </details>
                        </td></tr>
                    {% else -%}
                        <tr><td style="text-align:left; padding-left: 22px;" class="{{func_data[func_key]['python_tags'][num]}}">
                            <code>
                                {{num}}:
                                {{'&nbsp;'*func_data[func_key]['python_indent'][num]}}{{hl}}
                            </code>
                        </td></tr>
                    {%- endif -%}
                {%- endfor -%}
            </table>
            </div>
        {% endfor %}
    </body>
    </html>
    )r   r   r   r   r   r   r   get_html_templateB   r!   r"   c                 C   s:   | d }d}| d D ]\}}|d||   | d }q
|S )z
    Extract the code from the Numba annotation datastructure. 

    Pygments can only highlight full multi-line strings, the Numba
    annotation is list of single lines, with indentation removed.
    python_indent python_lines 
r   )
annotation
ident_dictsnlr   r   r   reform_code   s
   r-   c                   @   s*   e Zd ZdZd	ddZdd Zdd ZdS )
Annotatea  
    Construct syntax highlighted annotation for a given jitted function:

    Example:

    >>> import numba
    >>> from numba.pretty_annotate import Annotate
    >>> @numba.jit
    ... def test(q):
    ...     res = 0
    ...     for i in range(q):
    ...         res += i
    ...     return res
    ...
    >>> test(10)
    45
    >>> Annotate(test)

    The last line will return an HTML and/or ANSI representation that will be
    displayed accordingly in Jupyter/IPython.

    Function annotations persist across compilation for newly encountered
    type signatures and as a result annotations are shown for all signatures
    by default.

    Annotations for a specific signature can be shown by using the
    ``signature`` parameter.

    >>> @numba.jit
    ... def add(x, y):
    ...     return x + y
    ...
    >>> add(1, 2)
    3
    >>> add(1.3, 5.7)
    7.0
    >>> add.signatures
    [(int64, int64), (float64, float64)]
    >>> Annotate(add, signature=add.signatures[1])  # annotation for (float64, float64)
    Nc           
      K   s|   | dd}|jstd|j|d}|| _| D ]!\}}tt||}tt||}	dd t	|d ||	D |d< qd S )	Nr
   defaultz5function need to be jitted for at least one signature)	signaturec                 S   s"   g | ]\\}}}}||||fqS r   r   ).0abcdr   r   r   
<listcomp>  s   " z%Annotate.__init__.<locals>.<listcomp>r%   pygments_lines)
get
signatures
ValueErrorget_annotation_infoannitemsr   r-   r   zip)
selffunctionr0   kwargsr
   r<   kvr   restr   r   r   __init__
  s    zAnnotate.__init__c                 C      t  j| jdS N)	func_data)r"   renderr<   r?   r   r   r   _repr_html_     zAnnotate._repr_html_c                 C   rF   rG   )r    rI   r<   rJ   r   r   r   __repr__  rL   zAnnotate.__repr__)N)__name__
__module____qualname____doc__rE   rK   rM   r   r   r   r   r.      s
    
(r.   N)
rQ   warningsr   FutureWarningr   r   r    r"   r-   r.   r   r   r   r   <module>   s     