ó
ė'Gc           @   s  d  d l  m Z m Z m Z d  d l m Z d  d l m Z d  d l m	 Z	 d  d l
 m
 Z
 d  d l m Z d  d l Z d d  Z d	 e e f d
     YZ i e d  e d  6e d  e d  6e d  e d  6e d  e d  6e d  e d  6e d  e d  6e d  e d  6e d  e d  6e d  e d  6e d  e d  6e d  e d   6e d!  e d"  6e d#  e d$  6e d%  e d&  6e d'  e d(  6e d)  e d*  6e d+  e d,  6e d-  e d.  6e d/  e d0  6e d1  e d2  6e d3  e d4  6e d5  e d6  6e d7  e d8  6e d9  e d:  6e d;  e d<  6e d=  e d>  6e d?  e d@  6Z e dA k rd  d l Z d  d l Z e
   Z x+ e j dB D] Z e e j e  e  qßWe j e j  n  d S(C   iĸĸĸĸ(   t   ContentHandlert   make_parsert   SAXParseException(   t   InputSource(   t
   SGMLParser(   t   StringIO(   t   ConfigParser(   t
   entitydefsNc         C   sĪ   t  |  d  r |  j   }  n  | s0 t   } n  t |  } yC t   } | j t |    t   } | j |  | j	 |  Wn t
 k
 r | j |   n X| S(   Nt   read(   t   hasattrR   R   t
   OpmlParserR   t   setByteStreamR   R   t   setContentHandlert   parseR   t   feed(   t   opmlt   configt
   opmlParsert   sourcet   parser(    (    s<   /home/sa3ruby/intertwingly.net/code/venus-bzr/planet/opml.pyt   opml2config
   s    		R
   c           B   s;   e  Z e j d   Z d   Z d   Z d   Z d   Z RS(   s	   &(#?\w+);c         C   s'   t  j |   t j |   | |  _ d  S(   N(   R    t   __init__R   R   (   t   selfR   (    (    s<   /home/sa3ruby/intertwingly.net/code/venus-bzr/planet/opml.pyR   %   s    c      
   C   sŋ  | d k r d  S| j  d  r{ | d d k r^ | j  d  r^ t | j    } d | d <n  | d j   d k r{ d  Sn  | j  d  s | d j   r xa | j   D]L } | j   d k rĐ | | j   rĐ t | j    } | | | d <PqĐ qĐ Wd  Sn  | j  d	  s!| d	 j   ri| j  d
  sB| d
 j   rFd  St | j    } | d
 | d	 <n  | d } |  j j |  sŧ|  j j |  |  j j	 | d |  j
 | d	   n  d  S(   Nt   outlinet   typet   linkt   urlt   rsst   atomt   xmlUrlt   xmlurlt   textt   titlet   name(   s   rsss   atom(   t   has_keyt   dictt   itemst   lowert   stript   keysR   t   has_sectiont   add_sectiont   sett   unescape(   R   R!   t   attrst	   attributeR   (    (    s<   /home/sa3ruby/intertwingly.net/code/venus-bzr/planet/opml.pyt   startElement*   s0       !"!! 
c         C   s!  |  j  j |  } xö t d t |  d  D]Ü } | | t j   k r+ t | | } |  j  j |  } | r | j d  | | <n t t	 |   | | <| | j
 d  r| | j
 d  rã t t | | d d   | | <qt t | | d   | | <qq+ q+ Wd j |  j d  S(   Ni   i   t   #s   #xi   u    s   utf-8(   t   entitiest   splitt   ranget   lenR   R'   t   matcht   groupt   unichrt   ordt
   startswitht   intt   joint   encode(   R   R   t   parsedt   it	   codepointR4   (    (    s<   /home/sa3ruby/intertwingly.net/code/venus-bzr/planet/opml.pyR+   T   s    $(c         C   sŦ   t  |  } x | D] } y | | j d  | | <Wq | | j d  } d j g  | D]" } | t k rv t | py | ^ qZ  } | | | <q Xq W|  j | |  d  S(   Ns   utf-8s
   iso-8859-1u    (   R#   t   decodeR:   t   cp1252R.   (   R   R!   R,   R-   t   workt   c(    (    s<   /home/sa3ruby/intertwingly.net/code/venus-bzr/planet/opml.pyt   unknown_starttagk   s    8(	   t   __name__t
   __module__t   ret   compileR0   R   R.   R+   RC   (    (    (    s<   /home/sa3ruby/intertwingly.net/code/venus-bzr/planet/opml.pyR
   "   s
   		*	iŽ   i   i   i   i  i   i   i   i&   i   i    i   i!   i   iÆ  i   i0   i   i`  i   i9   i   iR  i   i}  i   i   i   i   i   i   i   i   i   i"   i   i   i   i   i   iÜ  i   i"!  i   ia  i   i:   i   iS  i   i~  i   ix  i   t   __main__i   (   t   xml.saxR    R   R   t   xml.sax.xmlreaderR   t   sgmllibR   t	   cStringIOR   R   t   htmlentitydefsR   RF   t   NoneR   R
   R6   R@   RD   t   syst   urllibR   t   argvR   t   urlopent   writet   stdout(    (    (    s<   /home/sa3ruby/intertwingly.net/code/venus-bzr/planet/opml.pyt   <module>   sT   U	