<s> import copy <EOL> from . import ElementTree <EOL> XINCLUDE = "<STR_LIT>" <EOL> XINCLUDE_INCLUDE = XINCLUDE + "<STR_LIT>" <EOL> XINCLUDE_FALLBACK = XINCLUDE + "<STR_LIT>" <EOL> class FatalIncludeError ( SyntaxError ) : <EOL> pass <EOL> def default_loader ( href , parse , encoding = None ) : <EOL> file = open ( href ) <EOL> if parse == "<STR_LIT>" : <EOL> data = ElementTree . parse ( file ) . getroot ( ) <EOL> else : <EOL> data = file . read ( ) <EOL> if encoding : <EOL> data = data . decode ( encoding ) <EOL> file . close ( ) <EOL> return data <EOL> def include ( elem , loader = None ) : <EOL> if loader is None : <EOL> loader = default_loader <EOL> i = <NUM_LIT:0> <EOL> while i < len ( elem ) : <EOL> e = elem [ i ] <EOL> if e . tag == XINCLUDE_INCLUDE : <EOL> href = e . get ( "<STR_LIT>" ) <EOL> parse = e . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if parse == "<STR_LIT>" : <EOL> node = loader ( href , parse ) <EOL> if node is None : <EOL> raise FatalIncludeError ( <EOL> "<STR_LIT>" % ( href , parse ) <EOL> ) <EOL> node = copy . copy ( node ) <EOL> if e . tail : <EOL> node . tail = ( node . tail or "<STR_LIT>" ) + e . tail <EOL> elem [ i ] = node <EOL> elif parse == "<STR_LIT:text>" : <EOL> text = loader ( href , parse , e . get ( "<STR_LIT>" ) ) <EOL> if text is None : <EOL> raise FatalIncludeError ( <EOL> "<STR_LIT>" % ( href , parse ) <EOL> ) <EOL> if i : <EOL> node = elem [ i - <NUM_LIT:1> ] <EOL> node . tail = ( node . tail or "<STR_LIT>" ) + text + ( e . tail or "<STR_LIT>" ) <EOL> else : <EOL> elem . text = ( elem . text or "<STR_LIT>" ) + text + ( e . tail or "<STR_LIT>" ) <EOL> del elem [ i ] <EOL> continue <EOL> else : <EOL> raise FatalIncludeError ( <EOL> "<STR_LIT>" % parse <EOL> ) <EOL> elif e . tag == XINCLUDE_FALLBACK : <EOL> raise FatalIncludeError ( <EOL> "<STR_LIT>" % e . tag <EOL> ) <EOL> else : <EOL> include ( e , loader ) <EOL> i = i + <NUM_LIT:1> </s>
