ó
ži‚Nc           @   sa   d  Z  d d l Z d d l Z d d l m Z d d l m Z d e f d „  ƒ  YZ d „  Z d S(   s<   utilities for generating and formatting literal Python code.iÿÿÿÿN(   t   StringIO(   t
   exceptionst   PythonPrinterc           B   sn   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z	 d	 „  Z
 d
 „  Z d „  Z RS(   c         C   sD   d |  _  g  |  _ d |  _ | |  _ g  |  _ t |  _ |  j ƒ  d  S(   Ni    s       (   t   indentt   indent_detailt   indentstringt   streamt   line_buffert   Falset   in_indent_linest   _reset_multi_line_flags(   t   selfR   (    (    s.   /usr/lib/python2.7/dist-packages/mako/pygen.pyt   __init__   s    						c         C   s   |  j  j | ƒ d  S(   N(   R   t   write(   R   t   text(    (    s.   /usr/lib/python2.7/dist-packages/mako/pygen.pyR   &   s    c         C   s:   t  |  _ x* t j d | ƒ D] } |  j j | ƒ q Wd S(   s¶   print a line or lines of python which already contain indentation.
 
        The indentation of the total block of lines will be adjusted to that of
        the current indent level.s   \r?\nN(   R   R	   t   ret   splitR   t   append(   R   t   blockt   l(    (    s.   /usr/lib/python2.7/dist-packages/mako/pygen.pyt   write_indented_block)   s    	c         G   s"   x | D] } |  j  | ƒ q Wd S(   s"   print a series of lines of python.N(   t	   writeline(   R   t   linest   line(    (    s.   /usr/lib/python2.7/dist-packages/mako/pygen.pyt
   writelines2   s    c      
   C   sÑ  |  j  s |  j ƒ  t |  _  n  t } | d k sU t j d | ƒ sU t j d | ƒ r^ t } n t } | oƒ t | ƒ oƒ | d d k } | r| r| sª |  j | ƒ r|  j	 d k r|  j	 d 8_	 t |  j
 ƒ d k rï t j d ƒ ‚ n  |  j
 j ƒ  qn  | d k rd S|  j j |  j | ƒ d ƒ t j d	 | ƒ rÍt j d
 | ƒ } | rŠ| j d ƒ } |  j	 d 7_	 |  j
 j | ƒ qÍd } t j d | ƒ } | rÍ|  j	 d 7_	 |  j
 j | ƒ qÍn  d S(   s¿   print a line of python, indenting it according to the current
        indent level.
 
        this also adjusts the indentation counter according to the
        content of the line.

        s   ^\s*#s   ^\s*$i    t   #i   s   Too many whitespace closuresNs   
s   :[ \t]*(?:#.*)?$s   ^\s*(if|try|elif|while|for)s(   ^\s*(def|class|else|elif|except|finally)(   R	   t   _flush_adjusted_linest   TrueR   t   NoneR   t   matcht   lent   _is_unindentorR   R   R   t   SyntaxExceptiont   popR   R   t   _indent_linet   searcht   groupR   (   R   R   t   decreased_indentt   hastextt
   is_commentR   t   indentort   m2(    (    s.   /usr/lib/python2.7/dist-packages/mako/pygen.pyR   7   sB    		
	"c         C   s   |  j  ƒ  d S(   s1   close this printer, flushing any remaining lines.N(   R   (   R   (    (    s.   /usr/lib/python2.7/dist-packages/mako/pygen.pyt   close   s    c         C   sV   t  |  j ƒ d k r t S|  j d } | d k r6 t St j d | ƒ } | sR t St S(   ss   return true if the given line is an 'unindentor', 
        relative to the last 'indent' event received.
 
        i    iÿÿÿÿs"   ^\s*(else|elif|except|finally).*\:N(   R   R   R   R   R   R   R   (   R   R   R(   R   (    (    s.   /usr/lib/python2.7/dist-packages/mako/pygen.pyR   ƒ   s    t    c         C   s!   t  j d | |  j |  j | ƒ S(   s²   indent the given line according to the current indent level.
 
        stripspace is a string of space that will be truncated from the
        start of the line before indenting.s   ^%s(   R   t   subR   R   (   R   R   t
   stripspace(    (    s.   /usr/lib/python2.7/dist-packages/mako/pygen.pyR"   ®   s    c         C   s   t  t  |  _ |  _ d S(   s^   reset the flags which would indicate we are in a backslashed
        or triple-quoted section.N(   R   t   backslashedt   triplequoted(   R   (    (    s.   /usr/lib/python2.7/dist-packages/mako/pygen.pyR
   ·   s    c         C   s   |  j  p |  j } t j d | ƒ r0 t |  _  n	 t |  _  t t j d | ƒ ƒ } | d k sm | d d k r} |  j |  _ n  | S(   sc   return true if the given line is part of a multi-line block,
        via backslash or triple-quote.s   \\$s   \"\"\"|\'\'\'i   i   i    (   R.   R/   R   R#   R   R   R   t   findall(   R   R   t   current_statet   triples(    (    s.   /usr/lib/python2.7/dist-packages/mako/pygen.pyt   _in_multi_line½   s    
	c         C   sÉ   d  } |  j ƒ  xŸ |  j D]” } |  j | ƒ rF |  j j | d ƒ q | j ƒ  } | d  k rŽ t j d | ƒ rŽ t j	 d | ƒ j
 d ƒ } n  |  j j |  j | | ƒ d ƒ q Wg  |  _ |  j ƒ  d  S(   Ns   
s   ^[ \t]*[^# \t]s	   ^([ \t]*)i   (   R   R
   R   R3   R   R   t
   expandtabsR   R#   R   R$   R"   (   R   R-   t   entry(    (    s.   /usr/lib/python2.7/dist-packages/mako/pygen.pyR   Ô   s    
$	(   t   __name__t
   __module__R   R   R   R   R   R*   R   R"   R
   R3   R   (    (    (    s.   /usr/lib/python2.7/dist-packages/mako/pygen.pyR      s   						H		+			c            sé   t  t  g ‰  d
 \ ‰ ‰ ‡  ‡ ‡ f d †  } d d „ } g  } d } x” t j d |  ƒ D]€ } | | ƒ rz | j | ƒ qX | j ƒ  } | d k rÂ t j d | ƒ rÂ t j d | ƒ j d ƒ } n  | j | | | ƒ ƒ qX Wd	 j	 | ƒ S(   s<   remove the left-whitespace margin of a block of Python code.i    i   c            s  ˆ  ˆ p ˆ  ˆ } t  j d |  ƒ r3 t ˆ  ˆ <n
 t ˆ  ˆ <d „  } xÌ |  rˆ  ˆ r© | d ˆ  ˆ |  ƒ \ } }  | r‰ t ˆ  ˆ <q| d ˆ  ˆ |  ƒ \ } }  qI | d |  ƒ \ } }  | rÈ | S| d |  ƒ \ } }  | rü | j d ƒ ˆ  ˆ <qI n  | d |  ƒ \ } }  qI W| S(	   Ns   \\$c         S   sC   t  j |  | ƒ } | r5 | | t | j d ƒ ƒ f Sd  | f Sd  S(   Ni    (   R   R   R   R$   R   (   t   regt   tt   m(    (    s.   /usr/lib/python2.7/dist-packages/mako/pygen.pyR   ó   s    s   %ss   .*?(?=%s|$)R   s   \"\"\"|\'\'\'i    s   .*?(?=\"\"\"|\'\'\'|#|$)(   R   R#   R   R   R$   (   R   t   start_stateR   R:   (   t   stateR.   R/   (    s.   /usr/lib/python2.7/dist-packages/mako/pygen.pyt   in_multi_lineë   s(    
		
 R+   c         S   s   t  j d | d |  ƒ S(   Ns   ^%sR+   (   R   R,   (   R   R-   (    (    s.   /usr/lib/python2.7/dist-packages/mako/pygen.pyR"     s    s   \r?\ns   ^[ \t]*[^# \t]s	   ^([ \t]*)s   
(   i    i   N(
   R   R   R   R   R   R4   R#   R   R$   t   join(   R   R=   R"   R   R-   R   (    (   R<   R.   R/   s.   /usr/lib/python2.7/dist-packages/mako/pygen.pyt   adjust_whitespaceå   s    $(	   t   __doc__R   t   stringR    t   makoR   t   objectR   R?   (    (    (    s.   /usr/lib/python2.7/dist-packages/mako/pygen.pyt   <module>   s
   Ø