mò
YñEc           @   sú  d  Z  d k Z d Z d Z d Z d Z d Z d Z d e e e f Z d	 Z	 d
 e
 f d „  ƒ  YZ d „  Z d e f d „  ƒ  YZ d „  Z d „  Z d e f d „  ƒ  YZ d e e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e i f d „  ƒ  YZ d e f d  „  ƒ  YZ d! f  d" „  ƒ  YZ e d# j o d k Z e e i ƒ d$ j o! d% e i d& f GHe i  ƒ  n e! e i d' ƒ i" ƒ  Z# e d( ƒ Z$ e i% e$ ƒ Z& e& i' e# ƒ e$ i( GHn d S()   sº   A very compact, XML based templating system.

    It has only 5 different directives, and doesn't allow any logic inside the
    templates, but instead expects the application to provide a dict with all
    the data (as strings or XML nodes) completely prepared. This makes it quite
    restrictive, and harder to seperately work on design and logic (the
    template has to very strictly adhere to the format of the dict), but makes
    it an environment extremely suitable for developers (since all development
    is done from code rather than from the template, and they don't have to
    learn a new, quirky templating language).

    Quick example:

    >>> from templess.templess import template
    >>> t = template('''
    ...   <foo xmlns:t="http://johnnydebris.net/xmlns/templess">
    ...     <bar t:content="bar" />
    ...   </foo>
    ... ''')
    >>> t.unicode({'bar': 'bar content'})
    <foo>
      <bar>bar content</bar>
    </foo>
 
Nt   templesss   0.3 unreleaseds'   Guido Wesdorp <johnny@johnnydebris.net>s5   $Date: 2007-03-07 14:25:10 +0100 (Wed, 07 Mar 2007) $s   $Author: johnny $s   $Revision: 101 $s   %s v%s, (c) %s 2005s&   http://johnnydebris.net/xmlns/templesst	   xmlstringc           B   s   t  Z d  Z RS(   s~   wrapper around unicode to mark a string as XML

        when Templess encounters this object, it will not escape entities
    (   t   __name__t
   __module__t   __doc__(    (    (    t3   /data/htdocs/projects/kupusite/templess/templess.pyR   .   s   c         c   s   x |  D] } t | ƒ Vq Wd  S(   N(   t   iterablet   it   objectcontext(   R   R   (    (    R   t   iterwrapper4   s     R   c           B   sS   t  Z d  Z d „  Z g  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   sO  wrapper around objects to allow Templess to traverse attributes

        this is similar to the nastyness Zope exposes to its users in TAL,
        when Templess retrieves a key from the context, if the context is
        an objectcontext wrapping an object, instead of just doing a 
        __getitem__ the following will be done:

        - first a __getitem__ is tried, then a __getattr_, if the value is
          still not found a KeyError is raised

        - before the value is returned, a check is done whether it's callable,
          and if so it's called (without arguments)
    c         C   s   | |  _ d  S(   N(   t   ot   selft   object(   R   R
   (    (    R   t   __init__H   s    c         C   s´   y |  i | } Wny t t f j
 og y |  i i | } Wq t j
 o= y |  i i i | } Wqˆ t j
 o t | ‚ qˆ Xq Xn Xt | ƒ o | ƒ  } n |  i	 | ƒ S(   N(
   R   R   t   namet   rett	   TypeErrort   KeyErrort   __dict__t	   __class__t   callablet   wrap(   R   R   R   (    (    R   t   __getitem__L   s    c         C   s   t  |  i d |  i i ƒ ƒ  S(   Nt   __str__(   t   getattrR   R   t   __repr__(   R   (    (    R   R   [   s    c         C   s   d |  i i i f S(   Ns   <objectcontext for "%s">(   R   R   R   R   (   R   (    (    R   R   ^   s    c         C   s   |  i t |  i | ƒ ƒ } d  S(   N(   R   R   R   R   R   R   (   R   R   R   (    (    R   t   __getattr__a   s    c         C   s%   t  | ƒ o t | ƒ Sn t | ƒ S(   N(   t   is_iterable_not_stringt   objR	   R   (   R   R   (    (    R   R   e   s    c         C   s   t  |  i | ƒ S(   N(   t   hasattrR   R   R   (   R   R   (    (    R   t   __hasattr__j   s    (   R   R   R   R   t
   __marker__R   R   R   R   R   R   (    (    (    R   R   8   s    						c         C   sc   t  |  ƒ t t t t g j p t |  t ƒ o t Sn y t	 |  ƒ Wn t
 j
 o n Xt St S(   s3   determine if something is iterable but not a stringN(   t   typet   vt   strt   unicodeR   R   t
   isinstancet   nodet   Falset   iterR   t   True(   R!   (    (    R   R   n   s     /c         C   s@   |  i d d ƒ i d d ƒ i d d ƒ i d d ƒ i d	 d
 ƒ S(   s<   entitize a string so it can be used in XML content and attrst   &s   &amp;t   "s   &quot;t   <s   &lt;t   >s   &gt;t   's   &apos;N(   t   st   replace(   R.   (    (    R   t   entitize|   s     R%   c           B   s   t  Z d  Z d „  Z RS(   st   node base
    
        very specific to Templess, the nodes contain as little functionality
        as possible
    c         C   s   g  S(   N(    (   R   R   (    (    R   t   find‰   s    (   R   R   R   R1   (    (    (    R   R%   ‚   s    t   elnodec           B   sq   t  Z d  Z d d „ Z d „  Z e Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z RS(   s   XML elements   UTF-8c         C   sL   | |  _  t | ƒ |  _ | |  _ | |  _ | d  j	 o | i |  ƒ n d  S(   N(   R   R   t   dictt   attrst   parentt   charsett   Nonet   append(   R   R   R4   R5   R6   (    (    R   R      s    			c         C   s„   |  i |  i ƒ g } t |  ƒ p | i d ƒ nE | i d ƒ x! |  D] } | i | i ƒ  ƒ qG W| i |  i ƒ  ƒ d i	 | ƒ S(   s?   return a string (XML) representation of ourselves (unrendered!)u    />u   >t    N(
   R   t   _start_node_startR4   R   t   lenR8   t   childR#   t	   _end_nodet   join(   R   R   R<   (    (    R   R#   —   s      c         c   s|   t  |  ƒ } x! |  i |  i | ƒ D]
 } | Vq# Wx) |  D]! } x | i ƒ  D]
 } | VqK Wq8 Wt  |  ƒ o |  i ƒ  Vn d S(   s6   returns self as a generator (yielding unicode strings)N(	   R;   R   t   issinglet   _start_nodeR4   t   chunkR<   t   generateR=   (   R   R?   RA   R<   (    (    R   RB   ¥   s        c         C   s   d |  i i |  i f S(   Ns	   <%s "%s">(   R   R   R   R   (   R   (    (    R   R   °   s    c         c   sK   |  i | j o |  Vn x, |  D]$ } x | i | ƒ D]
 } | Vq5 Wq Wd  S(   N(   R   R   R<   R1   t   found(   R   R   RC   R<   (    (    R   R1   ³   s      c         C   s†   t  | t ƒ o | Sne t  | t ƒ o t | ƒ SnG t  | t ƒ o t | |  i ƒ } n! t  | t ƒ p t | ƒ } n t | ƒ S(   N(	   R$   R.   R%   R   R#   R"   R   R6   R0   (   R   R.   (    (    R   t   _strconvertº   s    c         c   s$   |  i | ƒ V| o d Vn d Vd  S(   Nu    />u   >(   R   R:   R4   t   single(   R   R4   RE   (    (    R   R@   Å   s    c         C   s   d |  i |  i | ƒ S(   Nu   <(   R   R   t   _serialize_attrsR4   (   R   R4   (    (    R   R:   Ì   s    c         C   s   d |  i d S(   Nu   </R,   (   R   R   (   R   (    (    R   R=   Ï   s    c         C   sn   t  | ƒ p d Sn t | i ƒ  ƒ } d d i g  } | D]( \ } } | | d |  i	 | ƒ d q; ~ ƒ S(   s!   return the attributes as a stringR9   t    s   ="R*   N(
   R;   R4   t   sortedt   itemsR>   t   _[1]t   kR!   R   RD   (   R   R4   RI   RJ   R!   RK   (    (    R   RF   Ò   s
     (   R   R   R   R   R#   t   __unicode__RB   R   R1   RD   R@   R:   R=   RF   (    (    (    R   R2   Œ   s    								t   templessnodec           B   sJ   t  Z d  Z d d „ Z e d „ Z d „  Z d „  Z d „  Z d „  Z	 RS(   s×   templess element node

        has a special method 'convert' that makes it convert itself to a
        normal elnode by processing all templess directives (returns a new
        node, doesn't process in-place)
    s   UTF-8c         C   sU   | |  _  t | ƒ |  _ | |  _ | |  _ | |  _ | d  j	 o | i |  ƒ n d  S(   N(	   R   R   R3   R4   R5   t   t_prefixR6   R7   R8   (   R   R   R4   R5   RN   R6   (    (    R   R   â   s    				c         C   së  |  i i ƒ  }	 |  i |	 | ƒ } | p d Sn |	 i d |  i f d ƒ |  i	 |	 ƒ } |  i |	 ƒ } |  i |	 | ƒ }	 d } | o | | } n | o | | } n | d j oU | } | p t |  i |	 | |  i ƒ } n x |  D] }
 |
 i | | ƒ qê W| SnÜt | ƒ o:g  } x)| D]!} t | t ƒ p t | t ƒ o^ | } | p t |  i |	 | |  i ƒ } n x |  D] }
 |
 i | | ƒ qW| i | ƒ q&| } | p t |  i |	 | |  i ƒ } n t | t ƒ o8 | i o | i i | ƒ n | | _ | i | ƒ n t  |  i! | ƒ | |  i ƒ | i | ƒ q&W| Sn• | } | p t |  i |	 | |  i ƒ } n t | t ƒ o8 | i o | i i | ƒ n | | _ | i | ƒ n t  |  i! | ƒ | |  i ƒ | Sd S(   sû  process the templess directives
        
            returns a copy of self, or a list of copies of self (in case of
            t:content), can return unexpected results in case of t:replace,
            use only on root nodes of documents in production situation
            (iow: if you want to convert a tree, always make sure there's no
            t:repeat on the node you call convert() on)

            context is a context dict (see docs), parent is an internal helper
            argument
        R9   s   xmlns:%sN("   R   R4   t   copyt   _handle_condt   contextt   contt   popRN   R7   t   _get_replacet
   replacekeyt   _get_contentt
   contentkeyt   _process_attrt   cvalueR5   t   retnodeR2   R   R6   R<   t   convertR   R   t   ccontextR$   R3   R   t	   newparentR8   R%   t   removet   textnodeRD   (   R   RQ   R5   RR   RU   R\   R   R]   RY   R4   R<   RZ   RW   (    (    R   R[   ë   st         
	

	c         C   sƒ   |  i d j o t Sn |  i d } | i | t ƒ } | o | | Sn |  i d } | i | t ƒ } | o | | Sn t S(   s­   handle the 't:cond' and 't:not' directives
        
            returns False if there's a t:cond or t:not that doesn't allow 
            rendering, True otherwise
        s   :conds   :notN(   R   RN   R7   R(   t   attrkeyR4   RS   R&   t   condRQ   t   cnot(   R   R4   RQ   R`   Ra   Rb   (    (    R   RP   ?  s     c         C   sÙ   |  i d  j o | Sn |  i d } | i | t ƒ } | o— g  } | i	 d ƒ D] } | | i ƒ  qR ~ } xd | D]X }	 |	 i	 d ƒ \ }
 } | | } | o! | d j o | i |
 d  ƒ qu | | |
 <qu Wn | S(   Ns   :attrt   ;RG   R9   (   R   RN   R7   R4   R`   RS   R&   t   strvalueRJ   t   splitR!   t   stript   pairst   pairRK   RQ   t   value(   R   R4   RQ   R`   Rg   Ri   RJ   Rd   R!   Rh   RK   (    (    R   RX   Q  s    0 
c         C   s;   |  i d  j o t Sn |  i d } | i | t ƒ } | S(   Ns   :replace(   R   RN   R7   R&   R`   R4   RS   R/   (   R   R4   R`   R/   (    (    R   RT   a  s
    c         C   s;   |  i d  j o t Sn |  i d } | i | t ƒ } | S(   Ns   :content(   R   RN   R7   R&   R`   R4   RS   t   content(   R   R4   R`   Rj   (    (    R   RV   h  s
    (
   R   R   R   R   R7   R[   RP   RX   RT   RV   (    (    (    R   RM   Ú   s    	T			R_   c           B   sD   t  Z d  Z d d „ Z d „  Z e Z d „  Z d „  Z d „  Z RS(   s   text elements   UTF-8c         C   s`   t  | t ƒ p t | | ƒ } n | |  _ | |  _ | |  _ | d  j	 o | i |  ƒ n d  S(   N(   R$   t   textR#   R6   R   R5   R7   R8   (   R   Rk   R5   R6   (    (    R   R   r  s    			c         C   s   |  i S(   N(   R   Rk   (   R   (    (    R   R#   {  s    c         c   s   |  i ƒ  Vd  S(   N(   R   R#   (   R   (    (    R   RB   €  s    c         C   s   |  i |  i | ƒ S(   N(   R   R   Rk   R5   (   R   RQ   R5   (    (    R   R[   ƒ  s    c         C   sK   |  i d } t | ƒ t |  i ƒ j  o | d 7} n d |  i i | f S(   Ni
   s   ...s	   <%s "%s">(   R   Rk   t   reprtextR;   R   R   (   R   Rl   (    (    R   R   †  s    (	   R   R   R   R   R#   RL   RB   R[   R   (    (    (    R   R_   o  s    				t	   cdatanodec           B   s   t  Z d  Z d „  Z e Z RS(   s
   cdata nodec         C   s   d |  i f S(   Ns   <![CDATA[%s]]>(   R   Rk   (   R   (    (    R   R#     s    (   R   R   R   R#   RL   (    (    (    R   Rm   Œ  s    	t   commentnodec           B   s   t  Z d  Z d „  Z e Z RS(   s   comment nodec         C   s   d |  i f S(   Ns	   <!--%s-->(   R   Rk   (   R   (    (    R   R#   —  s    (   R   R   R   R#   RL   (    (    (    R   Rn   ”  s    	t   treebuilderhandlerc           B   sV   t  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   s-   nanosax handler to convert XML to a node treec         C   s   | |  _  d  S(   N(   R6   R   (   R   R6   (    (    R   R   Ÿ  s    c         C   s   d  |  _ d  |  _ d  |  _ d  S(   N(   R7   R   t   currentt   roott   templess_prefix(   R   (    (    R   t   startdoc¢  s    		c         C   s   d  S(   N(    (   R   (    (    R   t   enddoc§  s    c         C   s¶   xk | i ƒ  D]] \ } } | i d ƒ oA | t j o4 d | j o d |  _ qj | i d ƒ d |  _ q q Wt	 | | |  i |  i |  i ƒ |  _ |  i d  j o |  i |  _ n d  S(   Nt   xmlnst   :R9   i   (   R4   RI   t   keyRi   t
   startswitht   XMLNSR   Rr   Re   RM   R   Rp   R6   Rq   R7   (   R   R   R4   Ri   Rw   (    (    R   t   startelª  s     	c         C   s   |  i i |  _ d  S(   N(   R   Rp   R5   (   R   R   (    (    R   t   endel·  s    c         C   s   t  | |  i |  i ƒ d  S(   N(   R_   Rk   R   Rp   R6   (   R   Rk   (    (    R   Rk   º  s    c         C   s   t  | |  i |  i ƒ d  S(   N(   Rn   Rk   R   Rp   R6   (   R   Rk   (    (    R   t   comment½  s    c         C   s   t  | |  i |  i ƒ d  S(   N(   Rm   Rk   R   Rp   R6   (   R   Rk   (    (    R   t   cdataÀ  s    (   R   R   R   R   Rs   Rt   Rz   R{   Rk   R|   R}   (    (    (    R   Ro     s    							t   templatec           B   sA   t  Z d  Z d d „ Z d d „ Z d „  Z d „  Z d „  Z RS(   s­   a Templess template

        call 'unicode()' to get a unicode string, 'generate()' to get a 
        generator that yields bits of string, and 'render()' to get a node
    s   UTF-8c         C   s¤   | |  _  t | d ƒ o | i ƒ  } n | d j o t | | ƒ } n t | t ƒ o | i d ƒ } n t | ƒ } t
 i | ƒ } | i | ƒ | i |  _ d  S(   Nt   reads   UTF-8(   R6   R   R   t   dataR   R#   R$   t   encodeRo   t   handlert   nanosaxt   nsparsert   parsert   parseRq   t   tree(   R   R€   R6   R…   R‚   (    (    R   R   É  s    	c         C   s   |  i | ƒ i | ƒ S(   s4   returns a complete string with the rendered templateN(   R   R#   RQ   R   R6   (   R   RQ   R6   (    (    R   t   render_to_stringÖ  s     c         C   s   |  i i | ƒ } | i ƒ  S(   s+   returns a unicode rendering of the templateN(   R   R‡   R[   RQ   R%   R#   (   R   RQ   R%   (    (    R   R#   Û  s     c         C   s   |  i i | ƒ } | i ƒ  S(   s;   returns a generator that generates bits of string on demandN(   R   R‡   R[   RQ   R%   RB   (   R   RQ   R%   (    (    R   RB   à  s     c         C   s   |  i i | ƒ } | S(   s   render the tree to a 'normal' node
        
            processes all templess directives and returns the resulting tree
        N(   R   R‡   R[   RQ   R%   (   R   RQ   R%   (    (    R   t   renderå  s     (   R   R   R   R   Rˆ   R#   RB   R‰   (    (    (    R   R~   Ã  s    		t   cgitemplatec           B   s;   t  Z d  Z d d h  d „ Z d „  Z d „  Z d „  Z RS(   s0   simple cgi engine for serving templess templatess   text/html; charset=UTF-8s   templates/cgierror.htmlc         C   s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   R~   R   t   content_typet   error_templatet   headers(   R   R~   R‹   RŒ   R   (    (    R   R   ï  s    			c   	      C   sÌ   | i d ƒ p t | d <n ym t |  i ƒ } z t | ƒ } | i | ƒ } Wd | i
 ƒ  Xt | ƒ t j p t ‚ |  i |  i | ƒ Wn: d k } | i ƒ  \ } } } |  i | | | ƒ } n Xd S(   s·   render and output the document

            this also prints the Content-Type header, if you want it to print
            other headers use the headers arg of the constructor
        t   footerN(   RQ   t   has_keyt
   __footer__t   openR   R~   t   fpt   tRˆ   R   t   closeR    R"   t   AssertionErrort   print_outputR‹   t   syst   exc_infot   exct   et   tbt   handle_exception(	   R   RQ   R’   Rš   R›   R   R—   R“   R™   (    (    R   R‰   ö  s       	c         C   sÛ   d k  l } | | ƒ } t |  i ƒ } z‘ t | ƒ }
 h  d t
 | ƒ <d t
 | ƒ <d d i | ƒ <d g  }	 | D] } |	 h  d t
 | ƒ <qy ~	 <d t <} |
 i | ƒ } Wd	 | i ƒ  X|  i d
 | ƒ d	 S(   s;   internal method that generates and prints an exception page(   s	   format_tbt	   exceptiont   messaget	   tracebacks   
t   tblistt   lineRŽ   Ns   text/html; charset=UTF-8(   RŸ   t	   format_tbR›   R    R‘   R   RŒ   R’   R~   R“   R"   R™   Rš   R>   RJ   t   lR   RQ   Rˆ   R   R”   R–   (   R   R™   Rš   R›   R’   RQ   R¢   R£   R   RJ   R“   R    (    (    R   Rœ     s     r c         C   s6   d | GHx  |  i i ƒ  D] } d | GHq WH| GHd S(   s   actually print the outputs   Content-Type: %ss   %s: %sN(   R‹   R   R   RI   t   kvt   body(   R   R‹   R¥   R¤   (    (    R   R–     s     	 (   R   R   R   R   R‰   Rœ   R–   (    (    (    R   RŠ   í  s
    		t   __main__i   s   usage: %s <xmlfile>i    i   s   UTF-8()   R   Rƒ   t   __appname__t   __version__t
   __author__t   __last_modified_date__t   __last_author__t   __revision__R   Ry   R#   R   R	   R   R   R   R0   R%   t   listR2   RM   R_   Rm   Rn   t	   nshandlerRo   R~   RŠ   R   R—   R;   t   argvt   exitR‘   R   t   xmlR‚   R„   R…   R†   Rq   (   R   R_   R	   R…   R§   RŠ   R±   Ro   Rm   R¬   Rƒ   Rª   R‚   R~   R¨   R%   Ry   R   R©   R—   Rn   RM   R   R2   R   R«   R0   (    (    R   t   ?   sB   		6		
N•	&*:	
