³ò
æßCc           @   s´   d  Z  d d k Z d d k Z d d k l Z d d k l Z d e f d „  ƒ  YZ d „  Z	 d d d	 „  ƒ  YZ
 d
 „  Z d „  Z d d d „  ƒ  YZ e i d „ Z d d „ Z d S(   sË  String interpolation for Python (by Ka-Ping Yee, 14 Feb 2000).

This module lets you quickly and conveniently interpolate values into
strings (in the flavour of Perl or Tcl, but with less extraneous
punctuation).  You get a bit more power than in the other languages,
because this module allows subscripting, slicing, function calls,
attribute lookup, or arbitrary expressions.  Variables and expressions
are evaluated in the namespace of the caller.

The itpl() function returns the result of interpolating a string, and
printpl() prints out an interpolated string.  Here are some examples:

    from Itpl import printpl
    printpl("Here is a $string.")
    printpl("Here is a $module.member.")
    printpl("Here is an $object.member.")
    printpl("Here is a $functioncall(with, arguments).")
    printpl("Here is an ${arbitrary + expression}.")
    printpl("Here is an $array[3] member.")
    printpl("Here is a $dictionary['member'].")

The filter() function filters a file object so that output through it
is interpolated.  This lets you produce the illusion that Python knows
how to do interpolation:

    import Itpl
    sys.stdout = Itpl.filter()
    f = "fancy"
    print "Isn't this $f?"
    print "Standard output has been replaced with a $sys.stdout object."
    sys.stdout = Itpl.unfilter()
    print "Okay, back $to $normal."

Under the hood, the Itpl class represents a string that knows how to
interpolate values.  An instance of the class parses the string once
upon initialization; the evaluation and substitution can then be done
each time the instance is evaluated with str(instance).  For example:

    from Itpl import Itpl
    s = Itpl("Here is $foo.")
    foo = 5
    print str(s)
    foo = "bar"
    print str(s)
iÿÿÿÿN(   t
   StringType(   t	   tokenprogt	   ItplErrorc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   | |  _  | |  _ d  S(   N(   t   textt   pos(   t   selfR   R   (    (    s   Itpl.pyt   __init__4   s    	c         C   s   d t  |  i ƒ |  i f S(   Ns&   unfinished expression in %s at char %d(   t   reprR   R   (   R   (    (    s   Itpl.pyt   __str__7   s    (   t   __name__t
   __module__R   R   (    (    (    s   Itpl.pyR   3   s   	c         C   sB   t  i |  | ƒ } | d  j o t |  | ƒ ‚ n | | i ƒ  f S(   N(   R   t   matcht   NoneR   t   end(   R   R   R   (    (    s   Itpl.pyt   matchorfail;   s    t   Itplc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s*  Class representing a string with interpolation abilities.
    
    Upon creation, an instance works out what parts of the format
    string are literal and what parts need to be evaluated.  The
    evaluation and substitution happens in the namespace of the
    caller when str(instance) is called.c         C   s;  t  | ƒ t j o t d ‚ n | |  _ d } g  } d } xÂt i | d | ƒ } | d j  o Pn | | d } | d j oË | i d | | | !f ƒ | d d } } xw | oo t | | ƒ \ } } | i d \ }	 }
 | |	 |
 !} | d j o | d } q® | d	 j o | d } q® q® W| i d | | d | d !f ƒ q> | | j os| i d | | | !f ƒ t | | d ƒ \ } } x| t	 | ƒ j  o| | d
 j oI | d t	 | ƒ j  o2 | | d | j o t | | d ƒ \ } } qŽ| | d j o— | d d } } x„ | ow t | | ƒ \ } } | i d \ }	 }
 | |	 |
 !} | d d j o | d } q | d d j o | d } q q WqŽPqŽW| i d | | d | !f ƒ q> | i d | | | d !f ƒ | d | d j } q> | t	 | ƒ j  o | i d | | f ƒ n | |  _
 d S(   s   The single argument to this constructor is a format string.

        The format string is parsed according to the following rules:

        1.  A dollar sign and a name, possibly followed by any of: 
              - an open-paren, and anything up to the matching paren 
              - an open-bracket, and anything up to the matching bracket 
              - a period and a name 
            any number of times, is evaluated as a Python expression.

        2.  A dollar sign immediately followed by an open-brace, and
            anything up to the matching close-brace, is evaluated as
            a Python expression.

        3.  Outside of the expressions described in the above two rules,
            two dollar signs in a row give you one literal dollar sign.s   needs string initializert?   abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_i    t   $i   t   {i   i   t   }t   .s   ([s   )]N(   t   typeR    t	   TypeErrort   formatt   stringt   findt   appendR   t   regst   lent   chunks(   R   R   t	   namecharsR   R   t   dollart   nextchart   levelR   t   tstartt   tendt   token(    (    s   Itpl.pyR   I   sR    	
 &,
(".c         C   s   d t  |  i ƒ d S(   Ns   <Itpl t   >(   R   R   (   R   (    (    s   Itpl.pyt   __repr__   s    c      
   C   sÁ   y d d Wn t  i i } n Xx" | i d t j o | i } q% W| i | i } } g  } xN |  i D]C \ } } | o# | i t	 t
 | | | ƒ ƒ ƒ qj | i | ƒ qj Wt i | d ƒ S(   s<   Evaluate and substitute the appropriate parts of the string.i   i    R	   t    (   t   syst   exc_tracebackt   tb_framet	   f_globalsR	   t   f_backt   f_localsR   R   t   strt   evalR   t   join(   R   t   framet   loct   globt   resultt   livet   chunk(    (    s   Itpl.pyR      s    %
 *(   R	   R
   t   __doc__R   R&   R   (    (    (    s   Itpl.pyR   A   s   	D	c         C   s   t  t |  ƒ ƒ S(   N(   R.   R   (   R   (    (    s   Itpl.pyt   itplŸ   s    c         C   s   t  |  ƒ GHd  S(   N(   R8   (   R   (    (    s   Itpl.pyt   printpl    s    t   ItplFilec           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s@   A file object that filters each write() through an interpolator.c         C   s   | |  _  d  S(   N(   t   file(   R   R;   (    (    s   Itpl.pyR   ¤   s    c         C   s   d t  |  i ƒ d S(   Ns   <interpolated R%   (   R   R;   (   R   (    (    s   Itpl.pyR&   ¥   s    c         C   s   t  |  i | ƒ S(   N(   t   getattrR;   (   R   t   attr(    (    s   Itpl.pyt   __getattr__¦   s    c         C   s    |  i  i t t | ƒ ƒ ƒ d  S(   N(   R;   t   writeR.   R   (   R   R   (    (    s   Itpl.pyR?   §   s    (   R	   R
   R7   R   R&   R>   R?   (    (    (    s   Itpl.pyR:   ¢   s
   			c         C   s
   t  |  ƒ S(   sí   Return an ItplFile that filters writes to the given file object.
    
    'file = filter(file)' replaces 'file' with a filtered object that
    has a write() method.  When called with no argument, this creates
    a filter to sys.stdout.(   R:   (   R;   (    (    s   Itpl.pyt   filter©   s    c         C   s   |  o
 |  i  p
 t i i  S(   sÙ   Return the original file that corresponds to the given ItplFile.
    
    'file = unfilter(file)' undoes the effect of 'file = filter(file)'.
    'sys.stdout = unfilter()' undoes the effect of 'sys.stdout = filter()'.(   R;   R(   t   stdout(   t   ifile(    (    s   Itpl.pyt   unfilter±   s    (    (    (   R7   R(   R   t   typesR    t   tokenizeR   t
   ValueErrorR   R   R   R8   R9   R:   RA   R@   R   RC   (    (    (    s   Itpl.pys   <module>-   s   	^		