ó
C÷;]c           @   s  d  Z  d d l m Z y$ d d l m Z d d l m Z Wn1 e k
 rm d d l m Z d d l	 m Z n Xd Z
 e
 d Z e
 d Z e
 d Z d	 Z d
 e j f d „  ƒ  YZ d e f d „  ƒ  YZ d d „ Z d d d „ Z d „  Z d d e d „ Z d d e 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   etree(   t   urljoin(   t   urlopens!   {http://www.w3.org/2001/XInclude}t   includet   fallbackt   *i   t   FatalIncludeErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s2   lib/python2.7/site-packages/lxml/ElementInclude.pyR   K   s   t   LimitedRecursiveIncludeErrorc           B   s   e  Z RS(    (   R   R   (    (    (    s2   lib/python2.7/site-packages/lxml/ElementInclude.pyR	   O   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(    (    s2   lib/python2.7/site-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(    (    s2   lib/python2.7/site-packages/lxml/ElementInclude.pyt   _lxml_default_loaderq   s    
	c            s   d  d  ‡  f d † } | S(   Nc            s   ˆ  |  | | ƒ S(   N(    (   R   R   R   R   (   t   loader(    s2   lib/python2.7/site-packages/lxml/ElementInclude.pyt   load…   s    (   t   None(   R   R   (    (   R   s2   lib/python2.7/site-packages/lxml/ElementInclude.pyt   _wrap_et_loader„   s    c         C   sÆ   | d  k r d } n | d k  r4 t d | ƒ ‚ n  | d  k r‘ t |  d ƒ rd |  } |  j ƒ  }  n |  j ƒ  } t | d ƒ r¯ | j j } q¯ n t |  d ƒ r¯ |  j ƒ  }  n  t |  | | | ƒ d  S(   Niÿÿÿÿi    s;   expected non-negative depth or None for 'max_depth', got %rR   t   docinfo(   R   t
   ValueErrort   hasattrR   t   getroottreeR   t   URLt   _include(   t   elemR   t   base_urlt	   max_deptht   tree(    (    s2   lib/python2.7/site-packages/lxml/ElementInclude.pyR   œ   s    	c         C   sä  | d  k	 r t | ƒ } n t } | d  k r9 t ƒ  } n  |  j ƒ  j } t |  j t ƒ ƒ } x€| D]x} | j	 t
 k rzt | | j d ƒ ƒ }	 | j d d ƒ }
 | j ƒ  } |
 d k r¤|	 | k rÚ t d |	 ƒ ‚ n  | d k rù t d |	 ƒ ‚ n  | j |	 ƒ | |	 |
 d | ƒ} | d  k r@t d |	 |
 f ƒ ‚ n  t | | |	 | d	 | ƒ } | j r| j pqd
 | j | _ n  | d  k r‘| S| j | | ƒ qÜ|
 d k rg| |	 |
 d | j d ƒ ƒ} | d  k rót d |	 |
 f ƒ ‚ n  | j ƒ  } | d  k	 r$| j pd
 | | _ n3 | d  k r4| S| j p@d
 | | j pP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(   NR   R   R   s    recursive include of %r detectedi    s5   maximum xinclude depth reached when including file %sR   s   cannot load %r as %ri   t    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   XINCLUDE_ITER_TAGt   tagt   XINCLUDE_INCLUDER   t   gett	   getparentR   R	   t   addR#   t   tailt   replacet   getpreviousR)   t   removet   XINCLUDE_FALLBACK(   R$   R   R%   R&   t   _parent_hrefst   load_includeR   t   include_elementst   eR   R   t   parentt   nodeR)   t   predecessor(    (    s2   lib/python2.7/site-packages/lxml/ElementInclude.pyR#   °   sj    	#N(   t   __doc__t   lxmlR    t   urlparseR   t   urllib2R   t   ImportErrort   urllib.parset   urllib.requestt   XINCLUDER/   R7   R-   t   DEFAULT_MAX_INCLUSION_DEPTHt   LxmlSyntaxErrorR   R	   R   R   R   R   R   R#   (    (    (    s2   lib/python2.7/site-packages/lxml/ElementInclude.pyt   <module>3   s*   


	