ó
\K]c           @   sg   d  Z  d d l m Z e d e  d   Z d   Z d   Z d   Z d   Z d	 d d
     YZ	 d S(   sI   
This module implements code highlighting of numba function annotations.
i˙˙˙˙(   t   warnsF   The pretty_annotate functionality is experimental and might change APIc         C   s   y4 d d l  m } d d l m } d d l m } Wn t k
 rS t d   n X|   } | d t d | d t  } | |  | |  } | j   S(	   Ni˙˙˙˙(   t	   highlight(   t   PythonLexer(   t   HtmlFormatters%   please install the 'pygments' packaget	   noclassest   stylet   nowrap(	   t   pygmentsR   t   pygments.lexersR   t   pygments.formattersR   t   ImportErrort   Truet
   splitlines(   t   codeR   R   R   R   t   pylext   hft   res(    (    s4   lib/python2.7/site-packages/numba/pretty_annotate.pyt   hllines
   s    	c         C   s   y4 d d l  m } d d l m } d d l m } Wn t k
 rS t d   n X|   } | d |  } | |  | |  } | j   S(   Ni˙˙˙˙(   R   (   R   (   t   TerminalFormatters%   please install the 'pygments' packageR   (   R   R   R   R   R	   R   R
   R   (   R   R   R   R   R   R   R   R   (    (    s4   lib/python2.7/site-packages/numba/pretty_annotate.pyt   htlines   s    	c          C   s>   y d d l  m }  Wn t k
 r3 t d   n X|  d  S(   Ni˙˙˙˙(   t   Templates#   please install the 'jinja2' packagesá  
    {%- 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 -%}
    (   t   jinja2R   R
   t   ansi_template(   R   (    (    s4   lib/python2.7/site-packages/numba/pretty_annotate.pyt   get_ansi_template'   s    c          C   s>   y d d l  m }  Wn t k
 r3 t d   n X|  d  S(   Ni˙˙˙˙(   R   s#   please install the 'jinja2' packagesđ  
    <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   (    (    s4   lib/python2.7/site-packages/numba/pretty_annotate.pyt   get_html_templateB   s    c         C   sI   |  d } d } x2 |  d D]& \ } } | d | | | d } q W| S(   sĘ   
    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.
    t   python_indentt    t   python_linest    s   
(    (   t
   annotationt
   ident_dictt   st   nt   l(    (    s4   lib/python2.7/site-packages/numba/pretty_annotate.pyt   reform_codeŇ   s
    
t   Annotatec           B   s,   e  Z d  Z d d  Z d   Z d   Z RS(   s  
    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)
    c         K   sŘ   | j  d d  } | j s* t d   n  | j d |  } | |  _ x | j   D]~ \ } } t t |  |  } t t |  |  }	 g  t	 | d | |	  D]' \ \ }
 } } } |
 | | | f ^ q | d <qR Wd  S(   NR   t   defaults5   function need to be jitted for at least one signaturet	   signatureR   t   pygments_lines(
   t   gett
   signaturest
   ValueErrort   get_annotation_infot   annt   itemsR   R"   R   t   zip(   t   selft   functionR%   t   kwargsR   R+   t   kt   vR   t   restt   at   bt   ct   d(    (    s4   lib/python2.7/site-packages/numba/pretty_annotate.pyt   __init__	  s    		c         C   s   t    j d |  j  S(   Nt	   func_data(   R   t   renderR+   (   R.   (    (    s4   lib/python2.7/site-packages/numba/pretty_annotate.pyt   _repr_html_  s    c         C   s   t    j d |  j  S(   NR9   (   R   R:   R+   (   R.   (    (    s4   lib/python2.7/site-packages/numba/pretty_annotate.pyt   __repr__  s    N(   t   __name__t
   __module__t   __doc__t   NoneR8   R;   R<   (    (    (    s4   lib/python2.7/site-packages/numba/pretty_annotate.pyR#   ŕ   s   (	N(    (
   R?   t   warningsR    t   FutureWarningR   R   R   R   R"   R#   (    (    (    s4   lib/python2.7/site-packages/numba/pretty_annotate.pyt   <module>   s   					