mò
ƒYñEc           @   sý   d  Z  d k Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e 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	 i d Z e e ƒ i ƒ  Z e ƒ  Z e e ƒ Z e i e ƒ n d S(   sÉ    NanoSax is a very simple event-based XML processing library, much like SAX. 

    It's written to accompany the Templess library, but can easily be used
    stand-alone for quick and easy XML parsing
Nt   XMLErrorc           B   s   t  Z d „  Z d „  Z RS(   Nc         C   s   | |  _  | |  _ d  S(   N(   t   linenot   selft   message(   R   R   R   (    (    t4   /data/htdocs/projects/mamerunner/templess/nanosax.pyt   __init__   s    	c         C   s   d |  i |  i f S(   Ns   parse error in line %s: %s(   R   R   R   (   R   (    (    R   t   __str__   s    (   t   __name__t
   __module__R   R   (    (    (    R   R       s   	t   nsparserc           B   sø   t  Z d  Z d Z d Z d Z d Z d Z e i	 d e i
 ƒ Z e i	 d ƒ Z e i	 d e i
 ƒ Z e i	 d	 e i ƒ Z e i	 d
 ƒ Z e i	 d e i ƒ Z e i	 d e i ƒ Z e i	 d ƒ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sj   very simple parser for XML

        emits events like SAX, except the API is a lot ('even' ;) simpler
    i   i   i   i   i   s   ^[\w\:\-]+$sN   ^([\w\:\-]+)(\s+(([\w\:\-]+)=(("(?=([^"]*)")[^"]*")|('(?=([^']*)')[^']*'))))*$s5   ([\w\:\-]+)((\="(?=([^">]*)"))|(\=\'(?=([^\'>]*)\')))s
   <\?xml.*?>s   encoding="([^"]+)"s   <\?.*?>s    <!DOCTYPE\s+[\w\:\-]+\s+\[.*?\]>s%   <!DOCTYPE\s+[\w\:\-]+\s+SYSTEM\s+.*?>c         C   s   | |  _  d  S(   N(   t   handlerR   (   R   R
   (    (    R   R   -   s    c         C   s  |  i | ƒ } |  i i ƒ  xà |  i | ƒ D]Ï \ } } } | |  i	 j o |  i i
 | ƒ q, | |  i j o  |  i i |  i | | ƒ Œ  q, | |  i j o |  i i | ƒ q, | |  i j o |  i i | ƒ q, | |  i j o |  i i | ƒ q, q, W|  i i ƒ  d S(   sÈ   parse the xml using self.handler
        
            xml is supposed to be either a unicode or ascii string, or a 
            string with the character set as defined in the xml declaration
        N(   R   t   _handle_pist   xmlR
   t   startdoct   _parse_into_chunkst   typeR   t   chunkt	   TYPE_TEXTt   textt
   TYPE_STARTt   startelt   _parse_startt   TYPE_ENDt   endelt   TYPE_COMMENTt   commentt
   TYPE_CDATAt   cdatat   enddoc(   R   R   R   R   R   (    (    R   t   parse0   s        c         C   sø   |  i i | ƒ } d } | oQ | i d ƒ } | i | d ƒ } |  i	 i | ƒ } | o | i d ƒ } qp n x^ |  i |  i |  i f D]D } x; | i | ƒ } | p Pn | i | i d ƒ d ƒ } q’ Wq‰ Wt | t ƒ o t | | ƒ } n | S(   s¯  handle processing instructions
        
            takes care of handling (if appropriate) the XML declaration, and
            of discarding any processing instructions and document type 
            declarations etc. the lib can't deal with

            returns unicode, if the input string is not already unicode the
            charset mentioned in the XML declaration will be used for
            conversion (if any)
        s   UTF-8i    t    i   N(   R   t   _reg_xml_declt   searchR   t   matcht   charsett   groupt   declt   replacet   _reg_encodingt   encmatcht
   _reg_dtd_1t
   _reg_dtd_2t   _reg_pit   regt
   isinstancet   strt   unicode(   R   R   R$   R"   R'   R+   R!   (    (    R   R   E   s(    
    $c   
      c   s¾  | i ƒ  } d } d } g  } |  i | i d ƒ | d ƒ xi| oad } | i d ƒ oj | i d ƒ } |  i | d j | d ƒ | d	 | !}	 | | d
 7} |  i | |	 f V| |	 i d ƒ 7} nÔ| i d ƒ oj | i d ƒ } |  i | d j | d ƒ | d | !}	 | | d
 7} |  i | |	 f V| |	 i d ƒ 7} nZ| i d ƒ o× | i d ƒ } |  i | d j | d ƒ | d | !}	 |	 i ƒ  } |  i |  i i | ƒ | d | f ƒ | i ƒ  } |  i | i ƒ  | i ƒ  j | d | | f ƒ | | d 7} |  i | | f V| |	 i d ƒ 7} ns| i d ƒ o| i d ƒ } |  i | d j | d ƒ | d | !}	 t } |	 d d j o |	 d  }	 t } n |	 i ƒ  d } |  i |  i i | ƒ | d | f ƒ | | d 7} | p! | i | ƒ |  i | |	 f Vn! |  i | |	 f V|  i | | f V| |	 i d ƒ 7} n` | i d ƒ } |  i | d j | d ƒ | |  }	 | | 7} |  i | |	 f V| |	 i d ƒ 7} | | } q= W|  i | | d ƒ d  S(   Ni    i   t   <s   text before document starts	   <![CDATA[s   ]]>iÿÿÿÿs   CDATA section not closedi	   i   s   
s   <!--s   -->s   comment not closedi   s   </t   >s   end tag not closedi   s$   illegal element name '%s' in end tags.   closing tag '%s' doesn't match openingtag '%s's   start tag not closedt   /s!   illegal element name '%s' for tags   text after document ends   document not closed(   R   t   stript   offsett   currlinet	   namestackR   t   _testt
   startswitht   findt   endpost   dataR   t   countR   t   namet	   _reg_nameR!   t   popt	   startnameR   t   Falset   issinglet   Truet   splitt   appendR   R   (
   R   R   RA   R<   R5   R?   R9   R4   R3   R:   (    (    R   R   b   sz     



c         C   sÚ   |  i i | i ƒ  ƒ } |  i | | d | f ƒ | i d ƒ } | i d ƒ t	 | ƒ i ƒ  } h  } xg |  i i | ƒ } | p Pn | i | i d ƒ | i d ƒ d ƒ } | i d ƒ | | i d ƒ <qi W| | f S(   Ns   illegal start tag content '%s'i   i    i   R   (   R   t
   _reg_startR!   R:   R2   R6   R   R#   R<   t   lent   attrst	   _reg_attrR    R%   (   R   R   R:   R<   R!   RG   (    (    R   R   ¥   s      (!c         C   s   | p t | | ƒ ‚ n d S(   s@   raises an exception with message as text when assertion is falseN(   t	   assertionR    R   R   (   R   RI   R   R   (    (    R   R6   µ   s     (   R   R   t   __doc__R   R   R   R   R   t   ret   compilet   UR=   RE   RH   t   SR   R&   R*   R(   R)   R   R   R   R   R   R6   (    (    (    R   R	      s(    				C	t	   nshandlerc           B   sM   t  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   s¢   handler for nsparser
    
        this provides the interface to implement, and can serve as a base
        class when you don't want to implement everything
    c         C   s   d  S(   N(    (   R   (    (    R   R   À   s    c         C   s   d  S(   N(    (   R   (    (    R   R   Ã   s    c         C   s   d  S(   N(    (   R   R<   RG   (    (    R   R   Æ   s    c         C   s   d  S(   N(    (   R   R<   (    (    R   R   É   s    c         C   s   d  S(   N(    (   R   R   (    (    R   R   Ì   s    c         C   s   d  S(   N(    (   R   R   (    (    R   R   Ï   s    c         C   s   d  S(   N(    (   R   R   (    (    R   R   Ò   s    (
   R   R   RJ   R   R   R   R   R   R   R   (    (    (    R   RO   º   s    						t   echohandlerc           B   sG   t  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   Nc         C   s   g  |  _ d  S(   N(   R   t   buffer(   R   (    (    R   R   Ö   s    c         C   s   d i  |  i ƒ |  _ d  S(   NR   (   t   joinR   RQ   R   (   R   (    (    R   R   Ù   s    c         C   sr   |  i d | g 7_ t | ƒ o< |  i i d ƒ |  i d i d „  | i ƒ  Dƒ ƒ 7_ n |  i i d ƒ d  S(   NR/   t    c         c   s(   x! |  ] \ } } d  | | f Vq Wd S(   s   %s="%s"N(   t   [outmost-iterable]t   kt   v(   RT   RU   RV   (    (    R   t   <generator expression>à   s    R0   (   R   RQ   R<   RF   RG   RD   RR   t	   iteritems(   R   R<   RG   (    (    R   R   Ü   s
    ,c         C   sQ   |  i d d j o! |  i i ƒ  |  i i d ƒ n |  i d | d g 7_ d  S(   NiÿÿÿÿR0   s   />s   </(   R   RQ   R>   RD   R<   (   R   R<   (    (    R   R   ä   s    c         C   s   |  i i | ƒ d  S(   N(   R   RQ   RD   R   (   R   R   (    (    R   R   ì   s    c         C   s   |  i d | d g 7_ d  S(   Ns   <!--s   -->(   R   RQ   R   (   R   R   (    (    R   R   ï   s    c         C   s   |  i d | d g 7_ d  S(   Ns	   <![CDATA[s   ]]>(   R   RQ   R   (   R   R   (    (    R   R   ò   s    (	   R   R   R   R   R   R   R   R   R   (    (    (    R   RP   Õ   s   						t   __main__i   s   usage: %s <xmlfile>i    i   (   RJ   RK   t	   ExceptionR    t   objectR	   RO   RP   R   t   sysRF   t   argvt   exitt   fnamet   opent   readR   t   ht   pR   (
   R   Rc   R_   RO   R    Rb   R\   RK   R	   RP   (    (    R   t   ?
   s    	¤  		
