ó
}c‹Ac           @   sû   d  Z  y d d l Z Wn d d l Z n Xd d l Z e j Z d Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ e ƒ  Z	 d d d	 „  ƒ  YZ
 d
 e
 f d „  ƒ  YZ d e
 f d „  ƒ  YZ d e
 f d „  ƒ  YZ d d d „  ƒ  YZ d d d „  ƒ  YZ d S(   s§   simpleTALES Implementation

		Copyright (c) 2004 Colin Stewart (http://www.owlfish.com/)
		All rights reserved.
		
		Redistribution and use in source and binary forms, with or without
		modification, are permitted provided that the following conditions
		are met:
		1. Redistributions of source code must retain the above copyright
		   notice, this list of conditions and the following disclaimer.
		2. Redistributions in binary form must reproduce the above copyright
		   notice, this list of conditions and the following disclaimer in the
		   documentation and/or other materials provided with the distribution.
		3. The name of the author may not be used to endorse or promote products
		   derived from this software without specific prior written permission.
		
		THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
		IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
		OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
		IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
		INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
		NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
		DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
		THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
		(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
		THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
		
		If you make any bug fixes or feature enhancements please let me know!
		
		The classes in this module implement the TALES specification, used
		by the simpleTAL module.
		
		Module Dependencies: logging
iÿÿÿÿNs    This represents a Default value.t   PathNotFoundExceptionc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s   lib/simpletal/simpleTALES.pyR    /   s   t   ContextContentExceptionc           B   s   e  Z d  Z RS(   s”    This is raised when invalid content has been placed into the Context object.
		For example using non-ascii characters instead of Unicode strings.
	(   R   R   t   __doc__(    (    (    s   lib/simpletal/simpleTALES.pyR   2   s   t   ContextVariablec           B   s2   e  Z d d  „ Z d d „ Z d „  Z d „  Z RS(   c         C   s   | |  _  d  S(   N(   t   ourValue(   t   selft   value(    (    s   lib/simpletal/simpleTALES.pyt   __init__;   s    c         C   s&   t  |  j ƒ r t |  j d ƒ S|  j S(   N(    (   t   callableR   t   apply(   R   t   currentPath(    (    s   lib/simpletal/simpleTALES.pyR   >   s    c         C   s   |  j  S(   N(   R   (   R   (    (    s   lib/simpletal/simpleTALES.pyt   rawValueC   s    c         C   s   t  |  j ƒ S(   N(   t   reprR   (   R   (    (    s   lib/simpletal/simpleTALES.pyt   __str__F   s    N(   R   R   t   NoneR	   R   R   R   (    (    (    s   lib/simpletal/simpleTALES.pyR   :   s   	t   RepeatVariablec           B   s˜   e  Z d  Z d „  Z d d „ 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 d „  Z RS(   s    To be writtenc         C   s/   t  j |  d ƒ | |  _ d |  _ d  |  _ d  S(   Ni   i    (   R   R	   t   sequencet   positionR   t   map(   R   R   (    (    s   lib/simpletal/simpleTALES.pyR	   K   s    		c         C   s#   |  j  d  k r |  j ƒ  n  |  j  S(   N(   R   R   t	   createMap(   R   R   (    (    s   lib/simpletal/simpleTALES.pyR   Q   s    c         C   s
   |  j  ƒ  S(   N(   R   (   R   (    (    s   lib/simpletal/simpleTALES.pyR   V   s    c         C   s   |  j  d 7_  d  S(   Ni   (   R   (   R   (    (    s   lib/simpletal/simpleTALES.pyt	   incrementY   s    c         C   sÃ   i  |  _  |  j |  j  d <|  j |  j  d <|  j |  j  d <|  j |  j  d <|  j |  j  d <|  j |  j  d <t |  j ƒ |  j  d <|  j	 |  j  d <|  j
 |  j  d	 <|  j |  j  d
 <|  j |  j  d <d  S(   Nt   indext   numbert   event   oddt   startt   endt   lengtht   lettert   Lettert   romant   Roman(   R   t   getIndext	   getNumbert   getEvent   getOddt   getStartt   getEndt   lenR   t   getLowerLettert   getUpperLettert   getLowerRomant   getUpperRoman(   R   (    (    s   lib/simpletal/simpleTALES.pyR   \   s    	c         C   s   |  j  S(   N(   R   (   R   (    (    s   lib/simpletal/simpleTALES.pyR"   l   s    c         C   s   |  j  d S(   Ni   (   R   (   R   (    (    s   lib/simpletal/simpleTALES.pyR#   o   s    c         C   s   |  j  d d k r d Sd S(   Ni   i    i   (   R   (   R   (    (    s   lib/simpletal/simpleTALES.pyR$   r   s    c         C   s   |  j  d d k r d Sd S(   Ni   i    i   (   R   (   R   (    (    s   lib/simpletal/simpleTALES.pyR%   w   s    c         C   s   |  j  d k r d Sd S(   Ni    i   (   R   (   R   (    (    s   lib/simpletal/simpleTALES.pyR&   |   s    c         C   s$   |  j  t |  j ƒ d k r  d Sd S(   Ni   i    (   R   R(   R   (   R   (    (    s   lib/simpletal/simpleTALES.pyR'      s    c         C   se   d } |  j  } | d k r d Sx? | d k r` t | d ƒ \ } } t t d ƒ | ƒ | } q" W| S(   Nt    i    t   ai   (   R   t   divmodt   chrt   ord(   R   t   resultt   nextColt   thisCol(    (    s   lib/simpletal/simpleTALES.pyR)   †   s    	c         C   s   |  j  ƒ  j ƒ  S(   N(   R)   t   upper(   R   (    (    s   lib/simpletal/simpleTALES.pyR*      s    c         C   s•   d d d  d! d" d# d$ d% d& d' d( d) d* f } |  j  d k r@ d S|  j  d } d } x; | D]3 \ } } x$ | | k rŒ | | 7} | | 8} qi WqZ W| S(+   Nt   miè  t   cmi„  t   diô  t   cdi  t   cid   t   xciZ   t   li2   t   xli(   t   xi
   t   ixi	   t   vi   t   ivi   t   ii   iŸ  t    R-   (   R6   iè  (   R7   i„  (   R8   iô  (   R9   i  (   R:   id   (   R;   iZ   (   R<   i2   (   R=   i(   (   R>   i
   (   s   ixi	   (   R@   i   (   RA   i   (   RB   i   (   R   (   R   t   romanNumeralListt   numR2   R    t   integer(    (    s   lib/simpletal/simpleTALES.pyR+   “   s,    	
c         C   s   |  j  ƒ  j ƒ  S(   N(   R+   R5   (   R   (    (    s   lib/simpletal/simpleTALES.pyR,   ­   s    N(   R   R   R   R	   R   R   R   R   R   R"   R#   R$   R%   R&   R'   R)   R*   R+   R,   (    (    (    s   lib/simpletal/simpleTALES.pyR   I   s    											
		t   PathFunctionVariablec           B   s   e  Z d  „  Z d d „ Z RS(   c         C   s    t  j |  d | ƒ| |  _ d  S(   NR   (   R   R	   t   func(   R   RH   (    (    s   lib/simpletal/simpleTALES.pyR	   ±   s    c         C   sM   | d  k	 rI | \ } } t t |  j d j | | ƒ f ƒ ƒ } | ‚ n  d  S(   Nt   /(   R   R   R   RH   t   join(   R   R   R   t   pathsR2   (    (    s   lib/simpletal/simpleTALES.pyR   µ   s    (N(   R   R   R	   R   R   (    (    (    s   lib/simpletal/simpleTALES.pyRG   °   s   	t   CachedFuncResultc           B   s   e  Z d d  „ Z d „  Z RS(   c         C   s.   y |  j  SWn t j |  ƒ |  _  n X|  j  S(   N(   t   cachedValueR   R   (   R   R   (    (    s   lib/simpletal/simpleTALES.pyR   ½   s
    c         C   s   y
 |  `  Wn n Xd  S(   N(   RM   (   R   (    (    s   lib/simpletal/simpleTALES.pyt
   clearCacheÄ   s    
N(   R   R   R   R   RN   (    (    (    s   lib/simpletal/simpleTALES.pyRL   ¼   s   t   PythonPathFunctionsc           B   s>   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s   | |  _  d  S(   N(   t   context(   R   RP   (    (    s   lib/simpletal/simpleTALES.pyR	   Ë   s    c         C   s   |  j  j | ƒ S(   N(   RP   t   evaluatePath(   R   t   expr(    (    s   lib/simpletal/simpleTALES.pyt   pathÎ   s    c         C   s   |  j  j | ƒ S(   N(   RP   t   evaluateString(   R   RR   (    (    s   lib/simpletal/simpleTALES.pyt   stringÑ   s    c         C   s   |  j  j | ƒ S(   N(   RP   t   evaluateExists(   R   RR   (    (    s   lib/simpletal/simpleTALES.pyt   existsÔ   s    c         C   s   |  j  j | ƒ S(   N(   RP   t   evaluateNoCall(   R   RR   (    (    s   lib/simpletal/simpleTALES.pyt   nocall×   s    c         G   sˆ   t  | ƒ d r' | d  } | d } n | } d  } d } xH | t  | ƒ k  rƒ | | } | d 7} | | } | d 7} | r< | Sq< W| S(   Ni   iÿÿÿÿi    i   (   R(   R   (   R   t	   argumentst   pairst   defaultValueR   t   testR   (    (    s   lib/simpletal/simpleTALES.pyR]   Ú   s    




(   R   R   R	   RS   RU   RW   RY   R]   (    (    (    s   lib/simpletal/simpleTALES.pyRO   Ê   s   					t   Contextc           B   s­   e  Z d d  d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d d „ Z
 d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d „  Z d „  Z RS(   i    c         C   sq   | |  _  i  |  _ i  |  _ g  |  _ g  |  _ |  j | ƒ t j d ƒ |  _ d |  _	 d |  _
 t |  ƒ |  _ d  S(   Ns   simpleTALES.Contexti   i    (   t   allowPythonPatht   globalst   localst
   localStackt   repeatStackt   populateDefaultVariablest   loggingt	   getLoggert   logt   truet   falseRO   t   pythonPathFuncs(   R   t   optionsR_   (    (    s   lib/simpletal/simpleTALES.pyR	   ð   s    							c         C   sc   |  j  j |  j ƒ |  j j ƒ  |  _ | |  j | <|  j d |  j ƒ |  j ƒ  |  j | | ƒ d  S(   Nt   repeat(   Rc   t   appendt	   repeatMapt   copyt	   addGlobalt
   pushLocalst   setLocal(   R   t   namet   vart   initialValue(    (    s   lib/simpletal/simpleTALES.pyt	   addRepeatü   s    
c         C   s)   |  j  j ƒ  |  _ |  j d |  j ƒ d  S(   NRl   (   Rc   t   popRn   Rp   (   R   Rs   (    (    s   lib/simpletal/simpleTALES.pyt   removeRepeat  s    c         C   s   | |  j  | <d  S(   N(   R`   (   R   Rs   R   (    (    s   lib/simpletal/simpleTALES.pyRp     s    c         C   s)   |  j  j |  j ƒ |  j j ƒ  |  _ d  S(   N(   Rb   Rm   Ra   Ro   (   R   (    (    s   lib/simpletal/simpleTALES.pyRq     s    c         C   s   | |  j  | <d  S(   N(   Ra   (   R   Rs   R   (    (    s   lib/simpletal/simpleTALES.pyRr     s    c         C   s   |  j  j ƒ  |  _ d  S(   N(   Rb   Rw   Ra   (   R   (    (    s   lib/simpletal/simpleTALES.pyt	   popLocals  s    c         C   sS  | d  k	 r" | |  j d <d } n d } | j ƒ  } yõ | j d ƒ r] |  j | d j ƒ  ƒ S| j d ƒ rƒ |  j | d j ƒ  ƒ S| j d ƒ r© |  j | d j ƒ  ƒ S| j d	 ƒ rÏ |  j | d
 j ƒ  ƒ S| j d ƒ rõ |  j	 | d j ƒ  ƒ S| j d ƒ r|  j
 | d j ƒ  ƒ S|  j | ƒ SWn# t k
 rN} | rEd  S| ‚ n Xd  S(   Nt   attrsi   i    s   path:i   s   exists:i   s   nocall:s   not:i   s   string:s   python:(   R   R`   t   stript
   startswithRQ   t   lstripRV   RX   t   evaluateNotRT   t   evaluatePythonR    (   R   RR   t   originalAttst   suppressExceptiont   e(    (    s   lib/simpletal/simpleTALES.pyt   evaluate  s.    	c         C   s‚  |  j  s$ |  j j d | ƒ |  j Si  } xE |  j j ƒ  D]4 \ } } t | t ƒ rd | j ƒ  } n  | | | <q: W|  j	 j
 | d <|  j	 j | d <|  j	 j | d <|  j	 j | d <|  j	 j | d <i  } xE |  j j ƒ  D]4 \ } } t | t ƒ r| j ƒ  } n  | | | <qØ Wy3 t | | | ƒ } t | t ƒ r>| j ƒ  S| SWn8 t k
 r}} |  j j d t | ƒ ƒ d t | ƒ SXd  S(	   NsH   Parameter allowPythonPath is false.  NOT Evaluating python expression %sRS   RU   RW   RY   R]   s6   Exception occurred evaluating python path, exception: s   Exception: %s(   R_   Rg   t   warnRi   R`   t   itemst
   isinstanceR   R   Rj   RS   RU   RW   RY   R]   Ra   t   evalR   t	   Exceptiont   str(   R   RR   R`   Rs   R   Ra   R2   R‚   (    (    s   lib/simpletal/simpleTALES.pyR   >  s4    	  
c         C   s}   | j  d ƒ } t | ƒ d k rh x; | D]3 } y |  j | j ƒ  ƒ SWq( t k
 rZ } q( Xq( Wt ‚ n |  j | d ƒ Sd  S(   Nt   |i   i    (   t   splitR(   Rƒ   R{   R    t   PATHNOTFOUNDEXCEPTIONt   traversePath(   R   RR   t   allPathsRS   R‚   (    (    s   lib/simpletal/simpleTALES.pyRQ   ]  s    	c         C   s¡   | j  d ƒ } y$ |  j | d d d ƒ} |  j SWn t k
 rH } n XxN | d D]B } y& |  j | j ƒ  ƒ } | r |  j SWqT t k
 r• } qT XqT W|  j S(   NRŠ   i    t   canCalli   (   R‹   R   Rh   R    Rƒ   R{   Ri   (   R   RR   RŽ   R2   R‚   RS   t
   pathResult(    (    s   lib/simpletal/simpleTALES.pyRV   o  s    c         C   sŒ   | j  d ƒ } y |  j | d d d ƒSWn t k
 r? } n Xx? | d D]3 } y |  j | j ƒ  ƒ SWqK t k
 r} } qK XqK Wt ‚ d  S(   NRŠ   i    R   i   (   R‹   R   R    Rƒ   R{   RŒ   (   R   RR   RŽ   R‚   RS   (    (    s   lib/simpletal/simpleTALES.pyRX   ‡  s    c         C   s›   y |  j  | ƒ } Wn t k
 r, } |  j SX| d  k r@ |  j S| t k rS |  j Sy* t | ƒ } | d k ru |  j S|  j SWn n X| s” |  j S|  j S(   Ni    (   Rƒ   R    Rh   R   t   DEFAULTVALUERi   R(   (   R   RR   R   R‚   t	   resultLen(    (    s   lib/simpletal/simpleTALES.pyR~   š  s$    c   	      C   sv  d } d } xct  d t | ƒ ƒ D]L} | d k rA | d 8} q" | | d k r`yæ| | d d k r{ | d 7} d } n»| | d d k r\| j d | d ƒ } | d k r6| | d | !} |  j | ƒ } | d  k	 rLt | ƒ t d ƒ k r| | 7} qLt | ƒ t d ƒ k r0| t | d	 ƒ 7} qL| t t | ƒ d	 ƒ 7} n  | | } q6nÚ | j d
 | d ƒ } | d k rt | ƒ } n  | | d | !} |  j | ƒ } | d  k	 r(t | ƒ t d ƒ k rÞ| | 7} q(t | ƒ t d ƒ k r| t | d	 ƒ 7} q(| t t | ƒ d	 ƒ 7} n  | | d } Wqnt	 k
 r\} |  j
 j d ƒ qnXq" | | | 7} q" W| S(   NR-   i    i   t   $t   {t   }i   u    t   asciiRC   iÿÿÿÿs   Trailing $ detected(   t   xrangeR(   t   findRƒ   R   t   typet   unicodeR‰   R   t
   IndexErrorRg   R„   (	   R   RR   R2   t	   skipCountR   t   endPosRS   R   R‚   (    (    s   lib/simpletal/simpleTALES.pyRT   ·  sN    
	i   c   
      C   s,  | j  d ƒ s | j  d ƒ rY | j d ƒ s< | j d ƒ rL | d d !} q‡ | d } n. | j d ƒ sw | j d ƒ r‡ | d d !} n  | j d ƒ } | d } | j  d ƒ rx| d } |  j j | ƒ r|  j | } t | t ƒ rö | j ƒ  } qut | ƒ rut	 | d ƒ } quqx|  j
 j | ƒ rx|  j
 | } t | t ƒ rT| j ƒ  } qut | ƒ rut	 | d	 ƒ } quqxn  |  j j | ƒ rš|  j | } n( |  j
 j | ƒ r¼|  j
 | } n t ‚ d } xÇ| d D]»} | j  d ƒ r±| d } |  j j | ƒ rP|  j | } t | t ƒ r/| j ƒ  } q®t | ƒ r®t	 | d
 ƒ } q®q±|  j
 j | ƒ r±|  j
 | } t | t ƒ r| j ƒ  } q®t | ƒ r®t	 | d ƒ } q®q±n  yO t | t ƒ rÛ| j | | f ƒ } n$ t | ƒ rùt	 | d ƒ } n | } Wn t k
 r} | j ƒ  SXt | | ƒ r>t | | ƒ } nF y6 y | | } Wn! t k
 rr| t | ƒ } n XWn t ‚ n X| d } qÓW| ryO t | t ƒ rÂ| j | | f ƒ }	 n$ t | ƒ ràt	 | d ƒ }	 n | }	 Wq(t k
 r} | j ƒ  SXn! t | t ƒ r"| j }	 n | }	 |	 S(   Nt   "t   'i   iÿÿÿÿi    RI   t   ?(    (    (    (    (    (    (   R|   t   endswithR‹   Ra   t   has_keyR†   R   R   R
   R   R`   RŒ   t   hasattrt   getattrt	   TypeErrort   intt	   realValue(
   R   RR   R   t   pathListRS   t   valR   t   tempR‚   R2   (    (    s   lib/simpletal/simpleTALES.pyR   ô  s”    

    
      

  
 c         C   s"   d t  |  j ƒ d t  |  j ƒ S(   Ns	   Globals: s   Locals: (   R‰   R`   Ra   (   R   (    (    s   lib/simpletal/simpleTALES.pyR   D  s    c         C   sƒ   i  } i  |  _  d  | d <t | d <| | d <|  j  | d <d  | d <x( | j ƒ  D] } |  j | | | ƒ qQ W|  j d | ƒ d  S(   Nt   nothingt   defaultRk   Rl   Rz   t   CONTEXTS(   Rn   R   R‘   t   keysRp   (   R   Rk   t   varsRs   (    (    s   lib/simpletal/simpleTALES.pyRd   G  s    	



N(   R   R   R   R	   Rv   Rx   Rp   Rq   Rr   Ry   Rƒ   R   RQ   RV   RX   R~   RT   R   R   Rd   (    (    (    s   lib/simpletal/simpleTALES.pyR^   ï   s"   						!						=P	(    (    (    (   R   Re   t   DummyLoggert	   simpletalt   __version__R‘   Rˆ   R    R   RŒ   R   R   RG   RL   RO   R^   (    (    (    s   lib/simpletal/simpleTALES.pyt   <module>"   s    		g%