ó
œ]Nc           @   s"  d  Z  d d l m Z d d l Z y$ d d l m Z d d l m Z Wn1 e k
 ry d d l	 m Z d d l
 m Z n Xy e Wn! e k
 r¥ d d l m Z n Xd Z e d Z e d	 Z d
 e j f d „  ƒ  YZ d d „ Z d d d „ Z d „  Z d d d „ Z d d d d „ Z d S(   s  
Limited XInclude support for the ElementTree package.

While lxml.etree has full support for XInclude (see
`etree.ElementTree.xinclude()`), this module provides a simpler, pure
Python, ElementTree compatible implementation that supports a simple
form of custom URL resolvers.
iÿÿÿÿ(   t   etreeN(   t   urljoin(   t   urlopen(   t   Sets!   {http://www.w3.org/2001/XInclude}t   includet   fallbackt   FatalIncludeErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s7   /usr/lib/python2.7/dist-packages/lxml/ElementInclude.pyR   M   s   c         C   sk   t  |  d ƒ } | d k r3 t j | ƒ j ƒ  } n* | j ƒ  } | sN d } n  | j | ƒ } | j ƒ  | S(   Nt   rbt   xmls   utf-8(   t   openR    t   parset   getroott   readt   decodet   close(   t   hrefR   t   encodingt   filet   data(    (    s7   /usr/lib/python2.7/dist-packages/lxml/ElementInclude.pyt   default_loader]   s    	
c         C   s‰   | d k r' t  j |  | ƒ j ƒ  } n^ d |  k rB t |  ƒ } n t |  d ƒ } | j ƒ  } | j ƒ  | sv d } n  | j | ƒ } | S(   NR
   s   ://R	   s   utf-8(   R    R   R   R   R   R   R   R   (   R   R   R   t   parserR   t   f(    (    s7   /usr/lib/python2.7/dist-packages/lxml/ElementInclude.pyt   _lxml_default_loaderm   s    
	c            s   d  d  ‡  f d † } | S(   Nc            s   ˆ  |  | | ƒ S(   N(    (   R   R   R   R   (   t   loader(    s7   /usr/lib/python2.7/dist-packages/lxml/ElementInclude.pyt   load€   s    (   t   None(   R   R   (    (   R   s7   /usr/lib/python2.7/dist-packages/lxml/ElementInclude.pyt   _wrap_et_loader   s    c         C   s’   | d  k r] t |  d ƒ r0 |  } |  j ƒ  }  n |  j ƒ  } t | d ƒ r{ | j j } q{ n t |  d ƒ r{ |  j ƒ  }  n  t |  | d | ƒd  S(   NR   t   docinfot   base_url(   R   t   hasattrR   t   getroottreeR   t   URLt   _include(   t   elemR   R   t   tree(    (    s7   /usr/lib/python2.7/dist-packages/lxml/ElementInclude.pyR   ‘   s    c         C   s»  | d  k	 r t | ƒ } n t } | d  k r9 t ƒ  } n  |  j ƒ  j } t |  j d ƒ ƒ } xW| D]O} | j t	 k rQt
 | | j d ƒ ƒ } | j d d ƒ }	 | j ƒ  }
 |	 d k r{| | k rÚ t d | ƒ ‚ n  | j | ƒ | | |	 d | ƒ} | d  k r!t d | |	 f ƒ ‚ n  t | | | ƒ } | j rX| j pHd | j | _ n  |
 d  k rh| S|
 j | | ƒ q³|	 d	 k r>| | |	 d
 | j d
 ƒ ƒ} | d  k rÊt d | |	 f ƒ ‚ n  | j ƒ  } | d  k	 rû| j pîd | | _ n3 |
 d  k r| S|
 j pd | | j p'd |
 _ |
 j | ƒ q³t d |	 ƒ ‚ qd | j t k r | j ƒ  }
 |
 d  k	 r³|
 j t	 k r³t d | j ƒ ‚ q³qd t d | j ƒ ‚ qd W|  S(   Ns"   {http://www.w3.org/2001/XInclude}*R   R   R
   s    recursive include of %r detectedR   s   cannot load %r as %rt    t   textR   s)   unknown parse type in xi:include tag (%r)s0   xi:fallback tag must be child of xi:include (%r)s0   Invalid element found in XInclude namespace (%r)(   R   R   R   t   setR    R   t   listt   itert   tagt   XINCLUDE_INCLUDER   t   gett	   getparentR   t   addR"   t   tailt   replacet   getpreviousR&   t   removet   XINCLUDE_FALLBACK(   R#   R   t   _parent_hrefsR   t   load_includeR   t   include_elementst   eR   R   t   parentt   nodeR&   t   predecessor(    (    s7   /usr/lib/python2.7/dist-packages/lxml/ElementInclude.pyR"   ž   sd    	#(   t   __doc__t   lxmlR    t   copyt   urlparseR   t   urllib2R   t   ImportErrort   urllib.parset   urllib.requestR'   t	   NameErrort   setsR   t   XINCLUDER+   R3   t   LxmlSyntaxErrorR   R   R   R   R   R   R"   (    (    (    s7   /usr/lib/python2.7/dist-packages/lxml/ElementInclude.pyt   <module>3   s*   

	