<s> """<STR_LIT>""" <EOL> from lxml import etree <EOL> import copy <EOL> try : <EOL> from urlparse import urljoin <EOL> from urllib2 import urlopen <EOL> except ImportError : <EOL> from urllib . parse import urljoin <EOL> from urllib . request import urlopen <EOL> try : <EOL> set <EOL> except NameError : <EOL> from sets import Set as set <EOL> XINCLUDE = "<STR_LIT>" <EOL> XINCLUDE_INCLUDE = XINCLUDE + "<STR_LIT>" <EOL> XINCLUDE_FALLBACK = XINCLUDE + "<STR_LIT>" <EOL> class FatalIncludeError ( etree . LxmlSyntaxError ) : <EOL> pass <EOL> def default_loader ( href , parse , encoding = None ) : <EOL> file = open ( href , '<STR_LIT:rb>' ) <EOL> if parse == "<STR_LIT>" : <EOL> data = etree . parse ( file ) . getroot ( ) <EOL> else : <EOL> data = file . read ( ) <EOL> if not encoding : <EOL> encoding = '<STR_LIT:utf-8>' <EOL> data = data . decode ( encoding ) <EOL> file . close ( ) <EOL> return data <EOL> def _lxml_default_loader ( href , parse , encoding = None , parser = None ) : <EOL> if parse == "<STR_LIT>" : <EOL> data = etree . parse ( href , parser ) . getroot ( ) <EOL> else : <EOL> if "<STR_LIT>" in href : <EOL> f = urlopen ( href ) <EOL> else : <EOL> f = open ( href , '<STR_LIT:rb>' ) <EOL> data = f . read ( ) <EOL> f . close ( ) <EOL> if not encoding : <EOL> encoding = '<STR_LIT:utf-8>' <EOL> data = data . decode ( encoding ) <EOL> return data <EOL> def _wrap_et_loader ( loader ) : <EOL> def load ( href , parse , encoding = None , parser = None ) : <EOL> return loader ( href , parse , encoding ) <EOL> return load <EOL> def include ( elem , loader = None , base_url = None ) : <EOL> if base_url is None : <EOL> if hasattr ( elem , '<STR_LIT>' ) : <EOL> tree = elem <EOL> elem = elem . getroot ( ) <EOL> else : <EOL> tree = elem . getroottree ( ) <EOL> if hasattr ( tree , '<STR_LIT>' ) : <EOL> base_url = tree . docinfo . URL <EOL> elif hasattr ( elem , '<STR_LIT>' ) : <EOL> elem = elem . getroot ( ) <EOL> _include ( elem , loader , base_url = base_url ) <EOL> def _include ( elem , loader = None , _parent_hrefs = None , base_url = None ) : <EOL> if loader is not None : <EOL> load_include = _wrap_et_loader ( loader ) <EOL> else : <EOL> load_include = _lxml_default_loader <EOL> if _parent_hrefs is None : <EOL> _parent_hrefs = set ( ) <EOL> parser = elem . getroottree ( ) . parser <EOL> include_elements = list ( <EOL> elem . iter ( '<STR_LIT>' ) ) <EOL> for e in include_elements : <EOL> if e . tag == XINCLUDE_INCLUDE : <EOL> href = urljoin ( base_url , e . get ( "<STR_LIT>" ) ) <EOL> parse = e . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> parent = e . getparent ( ) <EOL> if parse == "<STR_LIT>" : <EOL> if href in _parent_hrefs : <EOL> raise FatalIncludeError ( <EOL> "<STR_LIT>" % href <EOL> ) <EOL> _parent_hrefs . add ( href ) <EOL> node = load_include ( href , parse , parser = parser ) <EOL> if node is None : <EOL> raise FatalIncludeError ( <EOL> "<STR_LIT>" % ( href , parse ) <EOL> ) <EOL> node = _include ( node , loader , _parent_hrefs ) <EOL> if e . tail : <EOL> node . tail = ( node . tail or "<STR_LIT>" ) + e . tail <EOL> if parent is None : <EOL> return node <EOL> parent . replace ( e , node ) <EOL> elif parse == "<STR_LIT:text>" : <EOL> text = load_include ( href , parse , encoding = e . get ( "<STR_LIT>" ) ) <EOL> if text is None : <EOL> raise FatalIncludeError ( <EOL> "<STR_LIT>" % ( href , parse ) <EOL> ) <EOL> predecessor = e . getprevious ( ) <EOL> if predecessor is not None : <EOL> predecessor . tail = ( predecessor . tail or "<STR_LIT>" ) + text <EOL> elif parent is None : <EOL> return text <EOL> else : <EOL> parent . text = ( parent . text or "<STR_LIT>" ) + text + ( e . tail or "<STR_LIT>" ) <EOL> parent . remove ( e ) <EOL> else : <EOL> raise FatalIncludeError ( <EOL> "<STR_LIT>" % parse <EOL> ) <EOL> elif e . tag == XINCLUDE_FALLBACK : <EOL> parent = e . getparent ( ) <EOL> if parent is not None and parent . tag != XINCLUDE_INCLUDE : <EOL> raise FatalIncludeError ( <EOL> "<STR_LIT>" % e . tag <EOL> ) <EOL> else : <EOL> raise FatalIncludeError ( <EOL> "<STR_LIT>" % e . tag <EOL> ) <EOL> return elem </s>
