ó
Nä²Wc           @@  s`  d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l m Z d  d l m	 Z	 d  d l
 m Z d  d l m Z y d  d l m Z Wn! e k
 rÃ d  d l m Z n Xd d	 l m Z d d
 l m Z d d l m Z d e f d „  ƒ  YZ e j d j d ƒ ƒ j Z e j d j d ƒ e j ƒ j Z d e f d „  ƒ  YZ d S(   i    (   t   absolute_importN(   t   datetime(   t   partial(   t   defaultdict(   t   escape(   t   StringIOi   (   t   Version(   t   CCodeWriteri   (   t   Utilst   AnnotationCCodeWriterc           B@  s¶   e  Z d d e d  „ Z d „  Z d „  Z e d „ Z d „  Z d „  Z	 d j
 ƒ  Z e j d ƒ Z d d „ Z d d	 „ Z d
 „  Z d d d d „ Z d „  Z d „  Z d d „ Z RS(   c         C@  sÅ   t  j |  | | d | ƒ| d  k r… t ƒ  |  _ d  |  _ t t t t ƒ ƒ |  _	 t t t t
 ƒ ƒ |  _ t t t t ƒ ƒ |  _ n< | j |  _ | j	 |  _	 | j |  _ | j |  _ | j |  _ d  S(   Nt   copy_formatting(   R   t   __init__t   NoneR   t   annotation_buffert   last_annotated_posR   R   t   listt   annotationst   strt   codet   sett   scopes(   t   selft   create_fromt   bufferR
   (    (    s;   /bar/jli/Chip-seq/script/cython/Cython/Compiler/Annotate.pyR      s    	c         C@  s   t  | | | ƒ S(   N(   R	   (   R   R   R   R
   (    (    s;   /bar/jli/Chip-seq/script/cython/Cython/Compiler/Annotate.pyt
   create_new-   s    c         C@  s$   t  j |  | ƒ |  j j | ƒ d  S(   N(   R   t   writeR   (   R   t   s(    (    s;   /bar/jli/Chip-seq/script/cython/Cython/Compiler/Annotate.pyR   0   s    c         C@  sÃ   | d  k	 rc t j |  | | ƒ |  j rc |  j j rc |  j | d j | d j |  j j ƒ qc n  |  j rª |  j \ } } } |  j	 | j } | | c |  j
 j ƒ  7<n  t ƒ  |  _
 | |  _ d  S(   Ni    i   (   R   R   t   mark_post	   funcstatet   scopeR   t   filenamet   addR   R   R   t   getvalueR   (   R   t   post   tracet   source_desct   linet   _t   pos_code(    (    s;   /bar/jli/Chip-seq/script/cython/Cython/Compiler/Annotate.pyR   4   s    /	c         C@  s1   |  j  | d j | d j | d | f ƒ d  S(   Ni    i   i   (   R   R   t   append(   R   R!   t   item(    (    s;   /bar/jli/Chip-seq/script/cython/Cython/Compiler/Annotate.pyt   annotateA   s    c         C@  s¤   |  j  g } xG t d ƒ D]9 } d t d d | d ƒ } | j d | | f ƒ q Wy d d l m } Wn t k
 r} n X| j | ƒ  j d ƒ ƒ d	 j | ƒ S(
   s2   css template will later allow to choose a colormapiÿ   u   FFFF%02xi   g      $@s)   .cython.score-%d {background-color: #%s;}i    (   t   HtmlFormatters   .cythons   
(	   t   _css_templatet   ranget   intR'   t   pygments.formattersR*   t   ImportErrort   get_style_defst   join(   R   t   csst   it   colorR*   (    (    s;   /bar/jli/Chip-seq/script/cython/Cython/Compiler/Annotate.pyt   _cssD   s    sÈ   
    function toggleDiv(id) {
        theDiv = id.nextElementSibling
        if (theDiv.style.display != 'block') theDiv.style.display = 'block';
        else theDiv.style.display = 'none';
    }
    s•  
        body.cython { font-family: courier; font-size: 12; }

        .cython.tag  {  }
        .cython.line { margin: 0em }
        .cython.code { font-size: 9; color: #444444; display: none; margin: 0px 0px 0px 8px; border-left: 8px none; }

        .cython.line .run { background-color: #B0FFB0; }
        .cython.line .mis { background-color: #FFB0B0; }
        .cython.code.run  { border-left: 8px solid #B0FFB0; }
        .cython.code.mis  { border-left: 8px solid #FFB0B0; }

        .cython.code .py_c_api  { color: red; }
        .cython.code .py_macro_api  { color: #FF7000; }
        .cython.code .pyx_c_api  { color: #FF3000; }
        .cython.code .pyx_macro_api  { color: #FF7000; }
        .cython.code .refnanny  { color: #FFA000; }
        .cython.code .trace  { color: #FFA000; }
        .cython.code .error_goto  { color: #FFA000; }

        .cython.code .coerce  { color: #008000; border: 1px dotted #008000 }
        .cython.code .py_attr { color: #FF0000; font-weight: bold; }
        .cython.code .c_attr  { color: #0000FF; }
        .cython.code .py_call { color: #FF0000; font-weight: bold; }
        .cython.code .c_call  { color: #0000FF; }
    c   
      C@  sµ   t  j | ƒ  } | j ƒ  } Wd  QX|  j j | i  ƒ } t  j t j j | ƒ ƒ } t j j	 | ƒ d d } t
 j | d d d ƒ) }	 |	 j |  j | | | | | ƒ ƒ Wd  QXd  S(   Ni    s   .htmlt   wt   encodings   UTF-8(   R   t   open_source_filet   readR   t   gett   decode_filenamet   ost   patht   basenamet   splitextt   codecst   openR   t   _save_annotation(
   R   t   source_filenamet   target_filenamet   coverage_xmlt   fR   t   generated_codet   c_filet   html_filenamet
   out_buffer(    (    s;   /bar/jli/Chip-seq/script/cython/Cython/Compiler/Annotate.pyt   save_annotationu   s    c         C@  s²   d } | r4 d j  d t j t | ƒ d ƒ ƒ } n  t j d ƒ j  d |  j ƒ  d |  j d t j	 d	 | r| t
 j j | ƒ n d d
 | ƒ g } | r® | j d | | f ƒ n  | S(   Nt    u$    with coverage data from {timestamp}t	   timestampiè  uU              <!DOCTYPE html>
            <!-- Generated by Cython {watermark} -->
            <html>
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <title>Cython: {filename}</title>
                <style type="text/css">
                {css}
                </style>
                <script>
                {js}
                </script>
            </head>
            <body class="cython">
            <p><span style="border-bottom: solid 1px grey;">Generated by Cython {watermark}</span>{more_info}</p>
            <p>
                <span style="background-color: #FFFF00">Yellow lines</span> hint at Python interaction.<br />
                Click on a line that starts with a "<code>+</code>" to see the C code that Cython generated for it.
            </p>
            R2   t   jst	   watermarkR   t	   more_infou'   <p>Raw output: <a href="%s">%s</a></p>
(   t   formatR   t   fromtimestampR-   t   textwrapt   dedentR5   t   _jsR   RO   R<   R=   R>   R'   (   R   RH   RC   t   coverage_timestampt   coverage_infot   outlist(    (    s;   /bar/jli/Chip-seq/script/cython/Cython/Compiler/Annotate.pyt   _save_annotation_header   s    	*c         C@  s   d S(   Nu   </body></html>
(   u   </body></html>
(    (   R   (    (    s;   /bar/jli/Chip-seq/script/cython/Cython/Compiler/Annotate.pyt   _save_annotation_footer¢   s    c         C@  sÓ   | d k	 r? | r? | j d d ƒ j ƒ  } |  j | | ƒ } n
 d } } t |  j | ƒ } t |  j | ƒ }	 g  }
 |
 j |  j | | | ƒ ƒ |
 j |  j	 | | | |	 | ƒ ƒ |
 j |  j
 ƒ  ƒ d j |
 ƒ S(   s#  
        lines : original cython source code split by lines
        generated_code : generated c code keyed by line number in original file
        target filename : name of the file in which to store the generated html
        c_file : filename in which the c_code has been written
        RM   RL   N(   R   R:   t   stript   _get_line_coveraget   dictR   R   t   extendRY   t   _save_annotation_bodyRZ   R1   (   R   R   RG   RH   RC   RE   RV   t   covered_linest   annotation_itemsR   RX   (    (    s;   /bar/jli/Chip-seq/script/cython/Cython/Compiler/Annotate.pyRB   ¥   s    
"c         C@  sÃ   d  } x | j d ƒ D]| } | j d ƒ s1 q n  | j d ƒ | k sg t j j | j d ƒ ƒ | k rq | } Pq | j | j d ƒ ƒ r | } q q W| d  k r¦ d  St d „  | j d ƒ Dƒ ƒ S(   Ns   .//classR   c         s@  s9   |  ]/ } t  | j d  ƒ ƒ t  | j d ƒ ƒ f Vq d S(   t   numbert   hitsN(   R-   R:   (   t   .0R$   (    (    s;   /bar/jli/Chip-seq/script/cython/Cython/Compiler/Annotate.pys	   <genexpr>È   s   s
   lines/line(   R   t   iterfindR:   R<   R=   t   abspatht   endswithR]   (   R   RE   RC   t   coverage_datat   entry(    (    s;   /bar/jli/Chip-seq/script/cython/Cython/Compiler/Annotate.pyR\   º   s    !c         C@  s}   y4 d d l  m } d d l m } d d l m } Wn t k
 rN t | ƒ SX| | | d t d t ƒ | d t	 ƒ ƒ } | S(   Ni    (   t	   highlight(   t   CythonLexer(   R*   t   stripnlt   stripallt   nowrap(
   t   pygmentsRj   t   pygments.lexersRk   R.   R*   R/   t   html_escapet   Falset   True(   R   R   Rj   Rk   R*   t	   html_code(    (    s;   /bar/jli/Chip-seq/script/cython/Cython/Compiler/Annotate.pyt   _htmlify_codeÌ   s    c         @  s<  d g } d } t  d „  d j ƒ  Dƒ ƒ j } |  j d  ƒ ‡  f d †  }	 |  j | ƒ j ƒ  }
 t t t |
 ƒ ƒ ƒ } | s† d  } n  x¢t	 |
 d ƒ D]‘\ } } y | | } Wn t
 k
 rÉ d } n> Xt | | ƒ } | j | ƒ rû | t | ƒ } n  t | ƒ } | ƒ  ‰  t |	 | ƒ } d ˆ  d	 d
 ˆ  d ˆ  d ˆ  d } | r^d } d } n d } d } d } | d  k	 r¶| | k r¶| | } | d  k	 r¶| rªd n d } q¶n  | j d j d | d | d | d | d | d | j ƒ  d | ƒ ƒ | r– | j d j d | d | d | ƒ ƒ q– q– W| j d ƒ | S(   Nu   <div class="cython">u
   /* â€¦ */
c         s@  s   |  ] } | d  f Vq d S(   i    N(    (   Rd   t   name(    (    s;   /bar/jli/Chip-seq/script/cython/Cython/Compiler/Annotate.pys	   <genexpr>Þ   s    sG   refnanny trace py_macro_api py_c_api pyx_macro_api pyx_c_api error_gotoc         @  s0   |  j  } ˆ  | c d 7<d | |  j | ƒ f S(   Ni   u   <span class='%s'>%s</span>(   t	   lastgroupt   group(   t   matcht
   group_name(   t   calls(    s;   /bar/jli/Chip-seq/script/cython/Cython/Compiler/Annotate.pyR)   ä   s    	i   RL   i   t   py_c_apii   t	   pyx_c_apit   py_macro_apit   pyx_macro_apis    onclick='toggleDiv(this)'t   +s   &#xA0;t   runt   misuƒ   <pre class="cython line score-{score}"{onclick}>{expandsymbol}<span class="{covered}">{line:0{lineno_width}d}</span>: {code}</pre>
t   scoret   expandsymbolt   coveredt   lineno_widthR$   R   t   onclicku=   <pre class='cython code score-{score} {covered}'>{code}</pre>u   </div>(   R]   t   splitt   copyR   R   Ru   t
   splitlinest   lenR   t	   enumeratet   KeyErrort   _replace_pos_commentt
   startswithRq   t   _parse_codeR'   RQ   t   rstrip(   R   t   cython_codeRG   Ra   R   R`   RX   t   pos_comment_markert   new_calls_mapR)   t   linesR†   t   kR$   t   c_codeRƒ   R‡   R„   R…   Rc   (    (   R{   s;   /bar/jli/Chip-seq/script/cython/Cython/Compiler/Annotate.pyR_   Ú   s^    		
	*	
	
N(   t   __name__t
   __module__R   Rs   R   R   R   R   R)   R5   R[   RU   RS   RT   R+   RK   RY   RZ   RB   R\   Ru   R_   (    (    (    s;   /bar/jli/Chip-seq/script/cython/Cython/Compiler/Annotate.pyR	      s    					
#			s  (?P<refnanny>__Pyx_X?(?:GOT|GIVE)REF|__Pyx_RefNanny[A-Za-z]+)|(?P<trace>__Pyx_Trace[A-Za-z]+)|(?:(?P<pyx_macro_api>__Pyx_[A-Z][A-Z_]+)|(?P<pyx_c_api>(?:__Pyx_[A-Z][a-z_][A-Za-z_]*)|__pyx_convert_[A-Za-z_]*)|(?P<py_macro_api>Py[A-Z][a-z]+_[A-Z][A-Z_]+)|(?P<py_c_api>Py[A-Z][a-z]+_[A-Z][a-z][A-Za-z_]*))(?=\()|(?P<error_goto>(?:(?<=;) *if .* +)?\{__pyx_filename = .*goto __pyx_L\w+;\})t   asciis)   ^\s*/\*(?:(?:[^*]|\*[^/])*\n)+\s*\*/\s*\nt   AnnotationItemc           B@  s)   e  Z d  d d „ Z d „  Z d „  Z RS(   RL   i    c         C@  s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   t   stylet   textt   tagt   size(   R   Rœ   R   Rž   RŸ   (    (    s;   /bar/jli/Chip-seq/script/cython/Cython/Compiler/Annotate.pyR   6  s    			c         C@  s   d |  j  |  j |  j f S(   Nu)   <span class='cython tag %s' title='%s'>%s(   Rœ   R   Rž   (   R   (    (    s;   /bar/jli/Chip-seq/script/cython/Cython/Compiler/Annotate.pyt   start<  s    c         C@  s   |  j  d f S(   Nu   </span>(   RŸ   (   R   (    (    s;   /bar/jli/Chip-seq/script/cython/Cython/Compiler/Annotate.pyt   end?  s    (   R˜   R™   R   R    R¡   (    (    (    s;   /bar/jli/Chip-seq/script/cython/Cython/Compiler/Annotate.pyR›   4  s   	(    t
   __future__R    R<   t   os.patht   reR@   RS   R   t	   functoolsR   t   collectionsR   t   xml.sax.saxutilsR   Rq   R   R/   t   ioRL   R   t   CodeR   R   R	   t   compilet   decodet   subR   t   MRŽ   t   objectR›   (    (    (    s;   /bar/jli/Chip-seq/script/cython/Cython/Compiler/Annotate.pyt   <module>   s2   ÿ 		