
NWc           @@  s[  d  Z  d d l m Z d d l Z d d l Z d d l Z y d d l m Z Wn! e	 k
 rq d d l
 m Z n Xd d l Z d d l Z d d l m Z d d l m Z d d l m Z m Z m Z m Z m Z m Z d	 d
 d d d d d g Z e j d  Z e j d e j  Z d	 e f d     YZ d e f d     YZ d e f d     YZ  d   Z! d
 e" f d     YZ# e$ d  Z% e$ d  Z& d e' f d     YZ( d e" f d     YZ) e* d  Z+ d   Z, d   Z- d e# f d      YZ. d!   Z/ d" e" f d#     YZ0 d$ e" f d%     YZ1 d& e" f d'     YZ2 d( e" f d)     YZ3 e3   Z4 [3 e$ e* d e$ d*  Z5 e j d+  Z6 d, d- d. d/ d0 d1 g Z7 e j d2  Z8 e j d3  Z9 d4   Z: d5   Z; e$ d e$ d6  Z< f  d7  Z= d8   Z> d9   Z? d:   Z@ d;   ZA d<   ZB d=   ZC d>   ZD d?   ZE d@ ZF e$ dA  ZG eH dB k rWeG   n  d S(C   s  
A small templating language

This implements a small templating language.  This language implements
if/elif/else, for/continue/break, expressions, and blocks of Python
code.  The syntax is::

  {{any expression (function calls etc)}}
  {{any expression | filter}}
  {{for x in y}}...{{endfor}}
  {{if x}}x{{elif y}}y{{else}}z{{endif}}
  {{py:x=1}}
  {{py:
  def foo(bar):
      return 'baz'
  }}
  {{default var = default_value}}
  {{# comment}}

You use this with the ``Template`` class or the ``sub`` shortcut.
The ``Template`` class takes the template string and the name of
the template (for errors) and a default namespace.  Then (like
``string.Template``) you can call the ``tmpl.substitute(**kw)``
method to make a substitution (or ``tmpl.substitute(a_dict)``).

``sub(content, **kw)`` substitutes the template immediately.  You
can use ``__name='tmpl.html'`` to set the name of the template.

If there are syntax errors ``TemplateError`` will be raised.
i    (   t   absolute_importN(   t   quote(   t   StringIOi   (   t   looper(   t   bytest   unicode_t   basestring_t   nextt
   is_unicodet   coerce_textt   TemplateErrort   Templatet   subt   HTMLTemplatet   sub_htmlt   htmlt   bunchs   \s+in\s+s   ^[a-z_][a-z0-9_]*$c           B@  s#   e  Z d  Z d d  Z d   Z RS(   s.   Exception raised while parsing a template
    c         C@  s&   t  j |  |  | |  _ | |  _ d  S(   N(   t	   Exceptiont   __init__t   positiont   name(   t   selft   messageR   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR   ;   s    	c         C@  s`   d j  |  j  } |  j r? d | |  j d |  j d f } n  |  j r\ | d |  j 7} n  | S(   Nt    s   %s at line %s column %si    i   s    in %s(   t   joint   argsR   R   (   R   t   msg(    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyt   __str__@   s    	!	N(   t   __name__t
   __module__t   __doc__t   NoneR   R   (    (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR
   7   s   t   _TemplateContinuec           B@  s   e  Z RS(    (   R   R   (    (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR    J   s   t   _TemplateBreakc           B@  s   e  Z RS(    (   R   R   (    (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR!   N   s   c         C@  sF   t  j j t  j j | j  |   } | j j | d | j d | j S(   Nt	   namespacet   get_template(	   t   ost   pathR   t   dirnameR   t	   __class__t   from_filenameR"   R#   (   R   t   from_templateR%   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyt   get_file_templateR   s    $	c           B@  s   e  Z i d  d 6d d 6e d 6Z d Z d Z d d d d d d d d  Z d d d e d  Z	 e
 e	  Z	 d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s   {{t   start_bracess   }}t
   end_bracesR   t   utf8i    c	         C@  s  | |  _  | d  k r2 |  j d |  j d f } n7 |  j j j   |  _ | d |  j d <| d |  j d <| |  _ t |  |  _ | d  k rO| d  k	 rOy t j	 |  }	 Wn t
 k
 r qOX|	 j }
 |	 j } d |
 k r|
 d } | j d  s| j d  r5| d  } q5n d	 |
 k r/|
 d	 } n d
 } | rO| d | 7} qOn  | |  _ t | d | d | d |  j |  _ | d  k ri  } n  | |  _ | |  _ | d  k	 r| |  _ n  d  S(   NR+   R,   i    i   t   __file__s   .pycs   .pyoiR   s   <string>s   :%sR   t   line_offsett
   delimeters(   t   contentR   t   default_namespaceR'   t   copyR0   R   t   _unicodet   syst	   _getframet
   ValueErrort	   f_globalst   f_linenot   endswithR   t   parset   _parsedR"   R#   t   default_inherit(   R   R1   R   R"   t
   stacklevelR#   R=   R/   R0   t   callert   globalst   lineno(    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR   d   sB    	
			
	$			c         C@  sb   t  | d  } | j   } | j   | r= | j |  } n  |  d | d | d | d | d |  S(   Nt   rbR1   R   R"   R=   R#   (   t   opent   readt   closet   decode(   t   clst   filenameR"   t   encodingR=   R#   t   ft   c(    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR(      s    
c         C@  s*   d |  j  j t t |    d |  j f S(   Ns   <%s %s name=%r>i   (   R'   R   t   hext   idR   (   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyt   __repr__   s    	c         O@  s   | rv | r t  d   n  t |  d k r< t  d   n  t | d d  si t  d | d f   n  | d } n  | } |  j | d <|  j r | j |  j  n  |  j |  \ } } } | s |  j } n  | r |  j | | | |  } n  | S(   Ns3   You can only give positional *or* keyword argumentsi   s)   You can only give one positional argumenti    t   itemssq   If you pass in a single argument, you must pass in a dictionary-like object (with a .items() method); you gave %rt   __template_name__(	   t	   TypeErrort   lent   hasattrR   R"   t   updatet
   _interpretR=   t   _interpret_inherit(   R   R   t   kwt   nst   resultt   defst   inherit(    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyt
   substitute   s,    	c         C@  sk   t  } g  } i  } |  j |  j | d | d | d | k rO | j d  } n d  } d j |  | | f S(   Nt   outRZ   t   __inherit__t    (   t   Truet   _interpret_codesR<   t   popR   R   (   R   RX   t   __traceback_hide__t   partsRZ   R[   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyRU      s    c   
      C@  s   t  } |  j s- t d d d  d |  j  n  |  j | |   } t |  j  } x* | j   D] \ } }	 t | | |	  q[ W| | _ | j	   } | | d <| j
 |  S(   Ns:   You cannot use inheritance without passing in get_templateR   R   R   (   R`   R#   R
   R   R   t   TemplateObjectRO   t   setattrt   bodyR3   R\   (
   R   Rg   RZ   t   inherit_templateRX   Rc   t   templt   self_R   t   value(    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyRV      s    		
c         C@  sP   t  } xC | D]; } t | t  r2 | j |  q |  j | | | |  q Wd  S(   N(   R`   t
   isinstanceR   t   appendt   _interpret_code(   R   t   codesRX   R]   RZ   Rc   t   item(    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyRa      s
    c      
   C@  s  t  } | d | d } } | d k rA |  j | d | |  nV| d k rY t    n>| d k rq t    n&| d k r | d | d | d	 } }	 }
 |  j |	 | |  }	 |  j | |	 |
 | | |  n| d
 k r | d } |  j | | | |  n| d k r| d j d  } |  j | d | |  } x3 | d D]' } |  j | | |  } | |  } qCW| j |  j	 | |   n| d k r| d | d } }	 | | k r|  j |	 | |  } | | | <qn | d k r| d }	 |  j |	 | |  } | | d <n | d k rq| d } | d } | d	 } t
 |  | | d | d | d | | | <| | <n& | d k rd  Sd st d |   d  S(   Ni    i   t   pyi   t   continuet   breakt   fori   i   t   condt   exprt   |t   defaultR[   R^   t   defRg   RX   t   post   comments   Unknown code: %r(   R`   t   _execR    R!   t   _evalt   _interpret_fort   _interpret_ift   splitRm   t   _reprt   TemplateDeft   AssertionError(   R   t   codeRX   R]   RZ   Rc   R   Rz   t   varsRv   R1   Rd   t   baset   partt   funct   varRY   Rk   t	   signature(    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyRn      sP     




c         C@  s   t  } x | D] } t |  d k r6 | | | d <ng t |  t |  k rs t d t |  t |  f   n  x' t | |  D] \ }	 }
 |
 | |	 <q Wy |  j | | | |  Wq t k
 r q q t k
 r Pq Xq Wd  S(   Ni   i    s&   Need %i items to unpack (got %i items)(   R`   RR   R7   t   zipRa   R    R!   (   R   R   Rv   R1   RX   R]   RZ   Rc   Rp   R   Rk   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR~     s     c   
      C@  s   t  } x | D] } t | t  s) t  | d | d } } | d k rS t  }	 n |  j | d | |  }	 |	 r |  j | d | | |  Pq q Wd  S(   Ni    i   t   elsei   i   (   R`   Rl   R   R   R}   Ra   (
   R   Rd   RX   R]   RZ   Rc   R   R   Rz   RY   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR     s    	c         C@  s   t  } yG y t | |  j |  } Wn# t k
 rG } t d |   n X| SWn\ t k
 r } t | d d   r | j d } n t |  } |  j	 | |  f | _   n Xd  S(   Ns    invalid syntax in expression: %sR   i    (
   R`   t   evalR2   t   SyntaxErrorR   t   getattrR   R   R	   t   _add_line_info(   R   R   RX   Rz   Rc   Rk   t   et   arg0(    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR}   (  s    c         B@  sw   e  } y | |  j | UWnY e k
 rr } | j rT |  j | j d |  f | _ n |  j d  |  f | _   n Xd  S(   Ni    (   R`   R2   R   R   R   R   (   R   R   RX   Rz   Rc   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR|   9  s    	"c         C@  s  t  } y | d  k r d S|  j rU y t |  } Wq t k
 rQ t |  } q XnH t | t  ss t |  } n  t	 |  r |  j
 r | j |  j
  } n  Wn5 t k
 r } |  j | j d |  f | _   n X|  j rht | t  rh|  j
 s
t d |   n  y | j |  j
  } Wqt k
 rd} t | j | j | j | j | j d |   qXnG |  j rt	 |  r|  j
 st d |   n  | j |  j
  } n  | Sd  S(   NR_   i    sH   Cannot decode bytes value %r into unicode (no default_encoding provided)s    in string %rsH   Cannot encode unicode value %r into bytes (no default_encoding provided)(   R`   R   R4   R   t   UnicodeDecodeErrorR   Rl   R   R	   R   t   default_encodingt   encodeR   R   R   RF   RI   t   objectt   startt   endt   reasont   UnicodeEncodeError(   R   Rk   Rz   Rc   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR   D  sL    				c         C@  s<   d | | d | d f } |  j  r8 | d |  j  7} n  | S(   Ns   %s at line %s column %si    i   s    in file %s(   R   (   R   R   Rz   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR   n  s
    	N(   R   R   R   R2   R   R   R=   R   R*   R(   t   classmethodRN   R\   RU   RV   Ra   Rn   R~   R   R}   R|   R   R   (    (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR   Y   s0   
		+							+					*c         K@  s4   | j  d  } t |  d | d | } | j |  S(   Nt   __nameR   R0   (   t   getR   R\   (   R1   R0   RW   R   t   tmpl(    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR   v  s    c         C@  s   t  |  d | } | j |  S(   NR   (   R   R\   (   R1   R   RH   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyt   paste_script_template_renderer|  s    c           B@  s5   e  Z d    Z d   Z d   Z d   Z d   Z RS(   c         K@  s1   x* | j    D] \ } } t |  | |  q Wd  S(   N(   RO   Rf   (   R   RW   R   Rk   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR     s    c         C@  s   | |  | <d  S(   N(    (   R   R   Rk   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyt   __setattr__  s    c         C@  s0   y |  | SWn t  k
 r+ t |   n Xd  S(   N(   t   KeyErrort   AttributeError(   R   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyt   __getattr__  s    c         C@  sX   d |  k rD y t  j |  |  SWqT t k
 r@ t  j |  d  SXn t  j |  |  Sd  S(   NRx   (   t   dictt   __getitem__R   (   R   t   key(    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR     s    c         C@  sL   d |  j  j d j g  t |  j    D] \ } } d | | f ^ q%  f S(   Ns   <%s %s>R   s   %s=%r(   R'   R   R   t   sortedRO   (   R   t   kt   v(    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyRN     s    	(   R   R   R   R   R   R   RN   (    (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR     s
   					c           B@  s,   e  Z d    Z d   Z d   Z d   Z RS(   c         C@  s   | |  _  d  S(   N(   Rk   (   R   Rk   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR     s    c         C@  s   |  j  S(   N(   Rk   (   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR     s    c         C@  s   |  j  S(   N(   Rk   (   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyt   __html__  s    c         C@  s   d |  j  j |  j f S(   Ns   <%s %r>(   R'   R   Rk   (   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyRN     s    (   R   R   R   R   R   RN   (    (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR     s   			c         C@  s   | r  t  |  d  r  |  j   S|  d  k r0 d St |  t  sN t |   }  n  t j d k r t |  t  r t	 j
 |  j d  d  }  |  j d  }  n t	 j
 |  d  }  t j d k  r t |   r |  j d d  }  q n  |  S(   NR   R_   t   3t   latin1i   t   asciit   xmlcharrefreplace(   RS   R   R   Rl   R   R	   R5   t   versionR   t   cgit   escapeRF   R   R   (   Rk   t   force(    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyt
   html_quote  s    
c         C@  s4   t  |   }  t |   r* |  j d  }  n  t |   S(   NR-   (   R	   R   R   t	   url_quote(   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyt   url  s    c          K@  s   g  } xq t  |  j    D]] \ } } | d  k r7 q n  | j d  rS | d  } n  | j d t |  t |  f  q Wt d j |   S(   Nt   _is   %s="%s"R   (   R   RO   R   R:   Rm   R   R   R   (   RW   Rd   R   Rk   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyt   attr  s    'c        
   B@  sH   e  Z e j j   Z e j e d  e d e d e	 d e
   d   Z RS(   R   R   R   R   c         C@  sW   t  | d  r$ | j   } t } n t } t j |  | |  } | rO t |  S| Sd  S(   NR   (   RS   R   t   FalseR`   R   R   R   (   R   Rk   Rz   R   t   plain(    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR     s    	
(   R   R   R   R2   R3   RT   R   R   R   R   R   R   (    (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR     s   
c         K@  s.   | j  d  } t |  d | } | j |  S(   NR   R   (   R   R   R\   (   R1   RW   R   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR     s    R   c           B@  sD   e  Z d d   Z d   Z d   Z d   Z d d  Z d   Z RS(   c         C@  sC   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   N(   t	   _templatet
   _func_namet   _func_signaturet   _bodyt   _nst   _post   _bound_self(   R   t   templatet	   func_namet   func_signatureRg   RX   Rz   t
   bound_self(    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR     s    						c         C@  s#   d |  j  |  j |  j j |  j f S(   Ns"   <tempita function %s(%s) at %s:%s>(   R   R   R   R   R   (   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyRN     s    c         C@  s   |    S(   N(    (   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR     s    c         O@  s   |  j  | |  } |  j j   } | j |  |  j d  k	 rM |  j | d <n  g  } i  } |  j j |  j | | |  d j	 |  S(   NR   R_   (
   t   _parse_signatureR   R3   RT   R   R   R   Ra   R   R   (   R   R   RW   t   valuesRX   R]   t   subdefs(    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyt   __call__  s    c      	   C@  sD   | d  k r |  S|  j |  j |  j |  j |  j |  j |  j d | S(   NR   (   R   R'   R   R   R   R   R   R   (   R   t   objt   type(    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyt   __get__  s
    c         C@  s  i  } |  j  \ } } } } i  } xc | j   D]U \ }	 }
 | r` |	 | k r` t d |	   n  |	 | k ry |
 | | <q. |
 | |	 <q. Wt |  } t |  } x | rLx' | r | d | k r | j d  q W| r | j d  }	 | j d  | |	 <q | rt |  | | <Pq t d d j g  | D] } t |  ^ q*   q WxK | j   D]= \ }	 } |	 | k rZ|  j j	 | |  j
 |  j  | |	 <qZqZWx- | D]% }	 |	 | k rt d |	   qqW| r| | | <n  | S(   Ns   Unexpected argument %si    s   Extra position arguments: %ss   , s   Missing argument: %s(   R   RO   RQ   t   listRb   t   tupleR   t   reprR   R}   R   R   (   R   R   RW   R   t   sig_argst   var_argst   var_kwt   defaultst   extra_kwR   Rk   R   t
   value_expr(    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR     sF    	0	 N(	   R   R   R   R   RN   R   R   R   R   (    (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR     s   				Re   c           B@  s   e  Z d    Z d   Z RS(   c         C@  s   | |  _  t |   |  _ d  S(   N(   t   _TemplateObject__namet   TemplateObjectGetterR   (   R   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR   C  s    	c         C@  s   d |  j  j |  j f S(   Ns   <%s %s>(   R'   R   R   (   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyRN   G  s    (   R   R   R   RN   (    (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyRe   A  s   	R   c           B@  s#   e  Z d    Z d   Z d   Z RS(   c         C@  s   | |  _  d  S(   N(   t#   _TemplateObjectGetter__template_obj(   R   t   template_obj(    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR   M  s    c         C@  s   t  |  j | t  S(   N(   R   R   t   Empty(   R   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR   P  s    c         C@  s   d |  j  j |  j f S(   Ns   <%s around %r>(   R'   R   R   (   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyRN   S  s    (   R   R   R   R   RN   (    (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR   K  s   		t   _Emptyc           B@  sV   e  Z d    Z d   Z d   Z d   Z d   Z d   Z e j	 d k  rT e Z
 n  RS(   c         O@  s   |  S(   N(    (   R   R   RW   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR   X  s    c         C@  s   d S(   NR_   (    (   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR   [  s    c         C@  s   d S(   NR   (    (   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyRN   ^  s    c         C@  s   d S(   Nu    (    (   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyt   __unicode__a  s    c         C@  s
   t  d  S(   N(    (   t   iter(   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyt   __iter__d  s    c         C@  s   t  S(   N(   R   (   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyt   __bool__g  s    R   (   R   R   R   R   RN   R   R   R   R5   R   t   __nonzero__(    (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR   W  s   						c         C@  s  | d k r) t j d t j d f } n  t } g  } d } | d d f } t j d t j | d  t j | d  f  }	 x.|	 j |   D]}
 |
 j d  } t	 |  |
 j
   | |  } | | d k r | r t d | d d | d |  n: | | d k r1| r1t d	 | d d | d |  n  | | d k rs|  | |
 j   !} | rj| j |  n  t } n& | j |  | |
 j   !| f  t } |
 j
   } | } q W| rt d
 | d d | d |  n  |  | } | r| j |  n  | rt |  } n  | S(   sY  
    Lex a string into chunks:

        >>> lex('hey')
        ['hey']
        >>> lex('hey {{you}}')
        ['hey ', ('you', (1, 7))]
        >>> lex('hey {{')
        Traceback (most recent call last):
            ...
        TemplateError: No }} to finish last expression at line 1 column 7
        >>> lex('hey }}')
        Traceback (most recent call last):
            ...
        TemplateError: }} outside expression at line 1 column 7
        >>> lex('hey {{ {{')
        Traceback (most recent call last):
            ...
        TemplateError: {{ inside expression at line 1 column 10

    R+   R,   i    i   s   %s|%ss   %s inside expressionR   R   s   %s outside expressions   No %s to finish last expressionN(   R   R   R2   R   t   ret   compileR   t   finditert   groupt   find_positionR   R
   R   Rm   R`   t   trim_lex(   t   sR   t   trim_whitespaceR/   R0   t   in_exprt   chunkst   lastt   last_post   token_ret   matchRv   Rz   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyt   lexu  sL    
	 

s.   ^(?:if |elif |for |def |inherit |default |py:)R   t   endift   endfort   enddefRr   Rs   s   \n\r?[\t ]*$s	   ^[\t ]*\nc   	      C@  sd  d } xWt |   D]I\ } } t | t  r4 q n  | d } t j |  r` | t k r` q n  | so d } n |  | d } | d t |   k r d } n |  | d } t | t  s t | t  r q n  | p t j |  } | d k r| j	   rt
 } n  | d k	 r:| d | k r:| j	   r:d } n  | r | syt j |  sy| t |   d k r | j	   r | r| d k r| j	   s| d k rd |  | d <qt j |  } | | j   d  } | |  | d <n  | r\| } | t |   d k r)| j	   r)d |  | d <qYt j |  } | | j   } | |  | d <q\q q W|  S(   sa  
    Takes a lexed set of tokens, and removes whitespace when there is
    a directive on a line by itself:

       >>> tokens = lex('{{if x}}\nx\n{{endif}}\ny', trim_whitespace=False)
       >>> tokens
       [('if x', (1, 3)), '\nx\n', ('endif', (3, 3)), '\ny']
       >>> trim_lex(tokens)
       [('if x', (1, 3)), 'x\n', ('endif', (3, 3)), 'y']
    i    R_   i   i   R   N(   R   t	   enumerateRl   R   t   statement_ret   searcht   single_statementsRR   t   trail_whitespace_ret   stripR`   t   lead_whitespace_reR   R   (	   t   tokenst	   last_trimt   it   currentRp   t   prevt
   next_chunkt   prev_okt   m(    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR     sN    
			)	##c         C@  sa   |  j  d | |  } | d k r= | |  j d | |  } n | d | | } | d | | f S(   s/   Given a string and index, return (line, column)s   
i    i   (   t   countt   rfind(   t   stringt   indext
   last_indexR   t   linest   column(    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR     s
    c         C@  s   | d k r) t j d t j d f } n  t |  d | d | d | } g  } x, | r{ t | |  \ } } | j |  qP W| S(   s  
    Parses a string into a kind of AST

        >>> parse('{{x}}')
        [('expr', (1, 3), 'x')]
        >>> parse('foo')
        ['foo']
        >>> parse('{{if x}}test{{endif}}')
        [('cond', (1, 3), ('if', (1, 3), 'x', ['test']))]
        >>> parse('series->{{for x in y}}x={{x}}{{endfor}}')
        ['series->', ('for', (1, 11), ('x',), 'y', ['x=', ('expr', (1, 27), 'x')])]
        >>> parse('{{for x, y in z:}}{{continue}}{{endfor}}')
        [('for', (1, 3), ('x', 'y'), 'z', [('continue', (1, 21))])]
        >>> parse('{{py:x=1}}')
        [('py', (1, 3), 'x=1')]
        >>> parse('{{if x}}a{{elif y}}b{{else}}c{{endif}}')
        [('cond', (1, 3), ('if', (1, 3), 'x', ['a']), ('elif', (1, 12), 'y', ['b']), ('else', (1, 23), None, ['c']))]

    Some exceptions::

        >>> parse('{{continue}}')
        Traceback (most recent call last):
            ...
        TemplateError: continue outside of for loop at line 1 column 3
        >>> parse('{{if x}}foo')
        Traceback (most recent call last):
            ...
        TemplateError: No {{endif}} at line 1 column 3
        >>> parse('{{else}}')
        Traceback (most recent call last):
            ...
        TemplateError: else outside of an if block at line 1 column 3
        >>> parse('{{if x}}{{for x in y}}{{endif}}{{endfor}}')
        Traceback (most recent call last):
            ...
        TemplateError: Unexpected endif at line 1 column 25
        >>> parse('{{if}}{{endif}}')
        Traceback (most recent call last):
            ...
        TemplateError: if with no expression at line 1 column 3
        >>> parse('{{for x y}}{{endfor}}')
        Traceback (most recent call last):
            ...
        TemplateError: Bad for (no "in") in 'x y' at line 1 column 3
        >>> parse('{{py:x=1\ny=2}}')
        Traceback (most recent call last):
            ...
        TemplateError: Multi-line py blocks must start with a newline at line 1 column 3
    R+   R,   R   R/   R0   N(   R   R   R2   R   t
   parse_exprRm   (   R   R   R/   R0   R   RY   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR;     s    2
	c         C@  s  t  |  d t  r% |  d |  d f S|  d \ } } | j   } | j d  r| d j d  } | j d  s | j d  r | j d  } d | k r | j d d  } | j d d	  } n  | d 7} n' d | k r t d
 d | d |  n  d | | f |  d f S| d$ k rUd | k rAt d d | d |  n  | | f |  d f S| j d  rtt |  | |  S| j d  s| d k rt d | j   d d | d |  n | d% k rt d | d | d |  n | d& k rt d | d | d |  n | j d  r-t	 |  | |  S| j d  rLt
 |  | |  S| j d  rkt |  | |  S| j d   rt |  | |  S| j d!  rd" | |  d d f |  d f Sd# | |  d d f |  d f S('   Ni    i   s   py:i   s    	s   
s   s   
R_   s.   Multi-line py blocks must start with a newlineR   R   Rq   Rr   Rs   Rt   s   continue outside of for loops   if s   elif R   s   %s outside of an if blockt   ift   elifs   %s with no expressionR   R   R   s   Unexpected %ss   for s   default s   inherit s   def t   #R{   Rv   (   s   continues   break(   s   ifs   elifs   for(   R   R   R   (   Rl   R   R   t
   startswitht   lstript   replaceR
   t
   parse_condR   t	   parse_fort   parse_defaultt   parse_inheritt	   parse_def(   R   R   t   contextRv   Rz   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR  ;  sb    

c         C@  s   |  d d } g  } | d	 } x |  sB t  d d | d |  n  t |  d t  r |  d d d k r d | f t |  |  d f St |  | |  \ } }  | j |  q! Wd  S(
   Ni    i   R	  s   Missing {{endif}}R   R   R   Ru   (   s   if(   R
   Rl   R   t   parse_one_condRm   (   R   R   R  R   t   piecesR   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR  p  s    
c         C@  s  |  d |  d \ } } }  g  } | j  d  r= | d  } n  | j d  rk d | | d j   | f } nk | j d  r d	 | | d
 j   | f } n= | d k r d | d  | f } n d s t d | | f   x |  s t d d | d |  n  t |  d t  rV|  d d d k sL|  d d j d  sL|  d d d k rV| |  f St |  | |  \ } }  | j	 |  q Wd  S(   Ni    i   t   :is   if R	  i   s   elif R
  i   R   s   Unexpected token %r at %ss   No {{endif}}R   R   R   (
   R:   R  R  R   R   R
   Rl   R   R  Rm   (   R   R   R  t   firstRz   R1   R   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR    s.    
c         C@  s  |  d \ } } |  d }  d | } g  } | j  d  s? t  | j d  r[ | d  } n  | d j   } t j |  } | s t d | d	 | d
 |  n  | | j    } d | k r t d | d	 | d
 |  n  t g  | | j    j	 d  D] } | j   r | j   ^ q  } | | j
   }	 x |  sRt d d	 | d
 |  n  t |  d t  r|  d d d k rd | | |	 | f |  d f St |  | |  \ }
 }  | j |
  q1Wd  S(   Ni    i   Rt   s   for R  ii   s   Bad for (no "in") in %rR   R   t   (s=   You cannot have () in the variable section of a for loop (%r)t   ,s   No {{endfor}}R   (   s   for(   R  R   R:   R   t   in_reR   R
   R   R   R   R   Rl   R  Rm   (   R   R   R  R  Rz   R1   R   R   R   Rv   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR    s@    


 !c         C@  s  |  d \ } } | j  d  s% t  | j d  d  d } | j d d  } t |  d k r~ t d | d | d |  n  | d j   } d | k r t d	 d | d |  n  t j |  s t d
 | d | d |  n  | d j   } d | | | f |  d f S(   Ni    s   default i   t   =s:   Expression must be {{default var=value}}; no = found in %rR   R   R  s'   {{default x, y = ...}} is not supporteds-   Not a valid variable name for {{default}}: %rRx   (	   R  R   R   R   RR   R
   R   t   var_reR   (   R   R   R  R  Rz   Rd   R   Rv   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR    s&    
c         C@  sR   |  d \ } } | j  d  s% t  | j d  d  d } d | | f |  d f S(   Ni    s   inherit i   R[   (   R  R   R   R   (   R   R   R  R  Rz   Rv   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR    s    c   
      C@  s  |  d \ } } |  d }  | j  d  s/ t  | j d  d  d } | j d  ra | d  } n  d | k r | } d d  d  i  f } nb | j d  s t d | d	 | d
 |  n4 | d  } | j d d  \ } } t | | |  } | d } g  } x |  st d d	 | d
 |  n  t |  d t  rb|  d d d k rbd | | | | f |  d f St	 |  | |  \ }	 }  | j
 |	  q Wd  S(   Ni    i   s   def R  iR  t   )s*   Function definition doesn't end with ): %sR   R   Ry   s   Missing {{enddef}}R   (    (   s   def(   R  R   R   R   R:   R
   t   parse_signatureRl   R   R  Rm   (
   R   R   R  R  R   R   t   sigt   sig_textR1   R   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR    s6    


c         @  s  t  j t |   j    g  } d  } d  } i  } t   f d  } x1d  } |   \ }	 }
 |	 t  j k rm Pn  |	 t  j k r |
 d k s |
 d k r |
 } |   \ }	 }
 n  |	 t  j k r t	 d |  d | d |  n  |
 } |   \ }	 }
 |	 t  j k s|	 t  j k ri|
 d k ri| d k r.| } n" | d k rC| } n | j
 |  |	 t  j k rE PqE qE n  | d  k	 rt	 d |  d | d |  n  |	 t  j k rE |
 d k rE d  } d  } d	 } d  } } g  } x| t  \ }	 }
 } } | d  k r| } n  | } |	 t  j k r;| r;t	 d |  d | d |  n  | r|	 t  j k sl|	 t  j k r|
 d k rt |  | |  } | | | <| j
 |  Pn  | j
 |	 |
 f  | r|	 t  j k r|
 | k r| d
 7} q| r|	 t  j k r|
 | k r| d
 8} | skd  } } qkq| r|	 t  j k r|
 d k r|
 } d
 } i d d 6d d 6d d 6| } qqWqE qE W| | | | f S(   Nc         @  sy   y+ t     \ } } \ } } \ } } } Wn t k
 rH t j d f SX|  rk | | | | f | | f f S| | f Sd  S(   NR_   (   R   t   StopIterationt   tokenizet	   ENDMARKER(   Rz   t   tok_typet
   tok_stringt   srowt   scolt   erowt   ecolt   line(   R   (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyt	   get_token  s    +t   *s   **s   Invalid signature: (%s)R   R   R  R  i    i   R  t   [t   {R  t   ]t   }(   R  R.  R/  (   R#  t   generate_tokensR   t   readlineR   R   R$  t   OPt   NAMER
   Rm   R`   t   isolate_expression(   R!  R   Rz   R   t   var_argR   R   R,  t   var_arg_typeR%  R&  t   var_namet	   nest_typet   unnest_typet
   nest_countt	   start_post   end_posRd   R   R   t   default_expr(    (   R   s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR    s~    	'*		
	*
!!
"-c   	      C@  s   | \ } } | d 8} | \ } } | d 8} |  j  t  } | | k rV | | | | !S| | | g } | j | | d | ! | t |  k  r | j | | |   n  d j |  S(   Ni   R_   (   t
   splitlinesR`   t   extendRR   Rm   R   (	   R  R=  R>  R'  R(  R)  R*  R  Rd   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyR6  F  s    

sn   %prog [OPTIONS] TEMPLATE arg=value

Use py:arg=value to set a Python value; otherwise all values are
strings.
c      	   C@  s  d d  l  } d d  l } d d  l } d d  l } |  d  k rL | j d }  n  | j d  } | j d t |  d t	  } | j
 d d d d	 d
 d d d | j
 d d d d d d d | j
 d d d d d d d | j |   \ } }  t |   d k  rd GH| j d  n  |  d } |  d }  i  }	 | j rK|	 j | j  n  x} |  D]u }
 d |
 k r}d |
 GH| j d  n  |
 j d d  \ } }
 | j d  r| d  } t |
  }
 n  |
 |	 | <qRW| d k r| j j   } d } n% t | d  } | j   } | j   | j r&t } n t } | | d | } | j |	  } | j rt | j d   } | j |  | j   n | j j |  d  S(!   Ni    i   t   PasteR   t   usages   -os   --outputt   destt   outputt   metavart   FILENAMEt   helps(   File to write output to (default stdout)s   --htmlt   use_htmlt   actiont
   store_trues9   Use HTML style filling (including automatic HTML quoting)s   --envt   use_envs-   Put the environment in as top-level variabless!   You must give a template filenamei   R  s   Bad argument: %rs   py:i   t   -s   <stdin>RB   R   t   wb(   R5   t   optparset   pkg_resourcesR$   R   t   argvt   get_distributiont   OptionParserR	   t   _fill_command_usaget
   add_optiont
   parse_argsRR   t   exitRL  RT   t   environR   R  R   t   stdinRD   RC   RE   RI  R   R   R\   RE  t   writet   stdout(   R   R5   RO  RP  R$   t   distt   parsert   optionst   template_nameR   Rk   R   t   template_contentRJ   t   TemplateClassR   RY   (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyt   fill_command]  sv    			

		
	
			t   __main__(I   R   t
   __future__R    R   R5   R   t   urllibR   R   t   ImportErrort   urllib.parseR$   R#  t   ioR   t   _looperR   t   compat3R   R   R   R   R   R	   t   __all__R   R  t   IR  R   R
   R    R!   R*   R   R   R   R   R   R   R   R   R`   R   R   R   R   R   R   Re   R   R   R   R   R   R   R   R   R   R   R;   R  R  R  R  R  R  R  R  R6  RT  Rb  R   (    (    (    s:   /bar/jli/Chip-seq/script/cython/Cython/Tempita/_tempita.pyt   <module>   sv   .	 "			M
	?	:	
=5			#				L	@