;ς
ΞΡAHc           @   s  d  Z  d Z d Z d k Z d k Z d k l Z d k l Z d e	 f d     YZ
 d   Z y e i i p d	 Z Wn e j
 o d	 Z n Xd
 f  d     YZ d e f d     YZ d   Z d   Z e d  Z e d  Z d f  d     YZ e i d  Z e 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)

$Id: Itpl.py 2918 2007-12-31 14:34:47Z vivainio $
s   Ka-Ping Yee <ping@lfw.org>s   MITN(   s	   tokenprog(   s
   StringTypes	   ItplErrorc           B   s   t  Z d   Z d   Z RS(   Nc         C   s   | |  _  | |  _ d  S(   N(   s   texts   selfs   pos(   s   selfs   texts   pos(    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys   __init__V   s    	c         C   s   d t  |  i  |  i f Sd  S(   Ns&   unfinished expression in %s at char %d(   s   reprs   selfs   texts   pos(   s   self(    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys   __str__Y   s    (   s   __name__s
   __module__s   __init__s   __str__(    (    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys	   ItplErrorU   s   	c         C   sF   t  i |  |  } | t j o t |  |   n | | i   f Sd  S(   N(   s	   tokenprogs   matchs   texts   poss   Nones	   ItplErrors   end(   s   texts   poss   match(    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys   matchorfail]   s    s   asciis   Itplc           B   s8   t  Z d  Z e d 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.s   backslashreplacec         C   s^  t  | t  o t d  n | |  _ | |  _ | |  _ d }	 g  } d } xΥn oΝt
 i | d |  } | d j  o Pn | | d } | d j oΠ | i d | | | !f  | d d f \ } } xw | oo t | |  \ } } | i d \ } } | | | !}
 |
 d j o | d } qΚ |
 d	 j o | d } qΚ qΚ W| i d | | d | d !f  qN | |	 j ox| i d | | | !f  t | | d  \ } } x | t |  j  o| | d
 j o) | d t |  j  o | | d |	 j o t | | d  \ } } qͺ| | d j o | d d f \ } } x | ow t | |  \ } } | i d \ } } | | | !}
 |
 d d j o | d } qA|
 d d j o | d } qAqAWqͺPqͺW| i d | | d | !f  qN | i d | | | d !f  | d | d j } qU W| t |  j  o | i d | | f  n | |  _ d S(   s  The single mandatory 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.

        Optional arguments:

        - codec('utf_8'): a string containing the name of a valid Python
        codec.

        - encoding_errors('backslashreplace'): a string with a valid error handling
        policy.  See the codecs module documentation for details.

        These are used to encode the format string if a call to str() fails on
        the expanded result.s   needs string initializers?   abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_i    i   s   $s   {i   i   s   }s   .s   ([s   )]N(   s
   isinstances   formats
   basestrings	   TypeErrors   selfs   codecs   encoding_errorss	   namecharss   chunkss   poss   strings   finds   dollars   nextchars   appends   levels   matchorfails   matchs   regss   tstarts   tends   tokens   len(   s   selfs   formats   codecs   encoding_errorss   tends   dollars   levels   poss   tstarts	   namecharss   tokens   nextchars   chunkss   match(    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys   __init__s   sj     			     & =   " c         C   s   d t  |  i  Sd  S(   Ns
   <Itpl %s >(   s   reprs   selfs   format(   s   self(    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys   __repr__Ε   s    c   	      C   sΧ   g  } | i } xx |  i D]m \ } } | oP t | | |  } y | t |   Wq t j
 o | t |   q Xq | |  q Wd i |  } y t |  SWn) t j
 o | i |  i |  i  Sn Xd S(   s  Evaluate to a string in the given globals/locals.

        The final output is built by calling str(), but if this fails, the
        result is encoded with the instance's codec and error handling policy,
        via a call to out.encode(self.codec,self.encoding_errors)s    N(   s   results   appends   apps   selfs   chunkss   lives   chunks   evals   globs   locs   vals   strs   UnicodeEncodeErrors   unicodes   joins   outs   UnicodeErrors   encodes   codecs   encoding_errors(	   s   selfs   globs   locs   vals   chunks   lives   results   outs   app(    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys   _strΘ   s"     	
 c         C   s`   t  i d  } x" | i d t j o | i } q W| i | i f \ } } |  i
 | |  Sd S(   s<   Evaluate and substitute the appropriate parts of the string.i   s   __name__N(   s   syss	   _getframes   frames	   f_globalss   __name__s   f_backs   f_localss   locs   globs   selfs   _str(   s   selfs   locs   globs   frame(    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys   __str__ί   s       (   s   __name__s
   __module__s   __doc__s   itpl_encodings   __init__s   __repr__s   _strs   __str__(    (    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys   Itplk   s
    R		s   ItplNSc           B   s2   t  Z d  Z e d d d  Z d   Z d   Z RS(   st  Class representing a string with interpolation abilities.

    This inherits from Itpl, but at creation time a namespace is provided
    where the evaluation will occur.  The interpolation becomes a bit more
    efficient, as no traceback needs to be extracte.  It also allows the
    caller to supply a different namespace for the interpolation to occur than
    its own.s   utf_8s   backslashreplacec         C   sC   | t j o
 | } n | |  _ | |  _  t i |  | | |  d S(   s  ItplNS(format,globals[,locals]) -> interpolating string instance.

        This constructor, besides a format string, takes a globals dictionary
        and optionally a locals (which defaults to globals if not provided).

        For further details, see the Itpl constructor.N(	   s   localss   Nones   globalss   selfs   Itpls   __init__s   formats   codecs   encoding_errors(   s   selfs   formats   globalss   localss   codecs   encoding_errors(    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys   __init__σ   s     
		c         C   s   |  i |  i |  i  Sd S(   s<   Evaluate and substitute the appropriate parts of the string.N(   s   selfs   _strs   globalss   locals(   s   self(    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys   __str__  s     c         C   s   d t  |  i  Sd  S(   Ns   <ItplNS %s >(   s   reprs   selfs   format(   s   self(    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys   __repr__  s    (   s   __name__s
   __module__s   __doc__s   Nones   __init__s   __str__s   __repr__(    (    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys   ItplNSκ   s    	c         C   s   t  t |    Sd  S(   N(   s   strs   Itpls   text(   s   text(    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys   itpl
  s    c         C   s   t  |   GHd  S(   N(   s   itpls   text(   s   text(    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys   printpl  s    c         C   s   t  t |  | |   Sd  S(   N(   s   strs   ItplNSs   texts   globalss   locals(   s   texts   globalss   locals(    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys   itplns  s    c         C   s   t  |  | |  GHd  S(   N(   s   itplnss   texts   globalss   locals(   s   texts   globalss   locals(    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys	   printplns  s    s   ItplFilec           B   s2   t  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(   s   files   self(   s   selfs   file(    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys   __init__  s    c         C   s   d t  |  i  d Sd  S(   Ns   <interpolated s   >(   s   reprs   selfs   file(   s   self(    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys   __repr__  s    c         C   s   t  |  i |  Sd  S(   N(   s   getattrs   selfs   files   attr(   s   selfs   attr(    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys   __getattr__  s    c         C   s    |  i i t t |    d  S(   N(   s   selfs   files   writes   strs   Itpls   text(   s   selfs   text(    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys   write  s    (   s   __name__s
   __module__s   __doc__s   __init__s   __repr__s   __getattr__s   write(    (    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys   ItplFile  s
    			c         C   s   t  |   Sd 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.N(   s   ItplFiles   file(   s   file(    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys   filter  s     c         C   s   |  o |  i p
 t i i Sd 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()'.N(   s   ifiles   files   syss   stdout(   s   ifile(    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys   unfilter  s     (   s   __doc__s
   __author__s   __license__s   strings   syss   tokenizes	   tokenprogs   typess
   StringTypes
   ValueErrors	   ItplErrors   matchorfails   stdins   encodings   itpl_encodings   AttributeErrors   Itpls   ItplNSs   itpls   printpls   Nones   itplnss	   printplnss   ItplFiles   stdouts   filters   unfilter(   s
   StringTypes   strings   __license__s   itpls   ItplNSs   filters   unfilters	   ItplErrors
   __author__s   syss   itplnss   itpl_encodings	   printplnss	   tokenprogs   ItplFiles   matchorfails   printpls   Itpl(    (    s<   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/Itpl.pys   ?0   s*   			 		