B
     \$                 @   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 )a  
This module implements code highlighting of numba function annotations.

Example:

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

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

    )warnzFThe pretty_annotate functionality is experimental and might change APIc             C   sn   y(ddl m} ddlm} ddlm} W n tk
rD   tdY nX | }|d|dd}|| ||}| S )Nr   )	highlight)PythonLexer)HtmlFormatterz%please install the 'pygments' packageT)Z	noclassesstyleZnowrap)pygmentsr   pygments.lexersr   pygments.formattersr   ImportError
splitlines)coder   r   r   r   pylexhfres r   4lib/python3.7/site-packages/numba/pretty_annotate.pyhllines   s    r   c             C   sj   y(ddl m} ddlm} ddlm} W n tk
rD   tdY nX | }||d}|| ||}| S )Nr   )r   )r   )TerminalFormatterz%please install the 'pygments' package)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   s6   yddl m}  W n tk
r,   tdY nX | dS )Nr   )Templatez#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
   Zansi_template)r   r   r   r   get_ansi_template9   s    r   c              C   s6   yddl m}  W n tk
r,   tdY nX | dS )Nr   )r   z#please install the 'jinja2' packagea  
    <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 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><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=" 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_templateT   s     	r   c             C   s>   | d }d}x,| d D ] \}}|d||   | d }qW |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.
    Zpython_indent python_lines 
r   )Z
annotationZ
ident_dictsnlr   r   r   reform_code   s
    r    c               @   s(   e Zd ZdZ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_annotate import Annotate
    ... @numba.jit
    ... def test(q):
    ...     res = 0
    ...     for i in range(q):
    ...         res += i
    ...     return res
    ...     
    ... test(10)
    ... Annotate(test)

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

    c       
      K   s   | dd}|jstdx|jD ]}||}q"W || _xN| D ]B\}}tt||}tt||}	dd t	|d ||	D |d< qDW d S )Nr   defaultz5function need to be jitted for at least one signaturec             S   s"   g | ]\\}}}}||||fqS r   r   ).0abcdr   r   r   
<listcomp>  s    z%Annotate.__init__.<locals>.<listcomp>r   Zpygments_lines)
getZ
signatures
ValueErrorZget_annotation_infoannitemsr   r    r   zip)
selfZfunctionkwargsr   Zsigr+   kvr   restr   r   r   __init__  s    zAnnotate.__init__c             C   s   t  j| jdS )N)	func_data)r   renderr+   )r.   r   r   r   _repr_html_  s    zAnnotate._repr_html_c             C   s   t  j| jdS )N)r4   )r   r5   r+   )r.   r   r   r   __repr__  s    zAnnotate.__repr__N)__name__
__module____qualname____doc__r3   r6   r7   r   r   r   r   r!      s   r!   N)
r;   warningsr   FutureWarningr   r   r   r   r    r!   r   r   r   r   <module>   s    