<s> from lxml import etree <EOL> from html5lib . treebuilders . etree import tag_regexp <EOL> from gettext import gettext <EOL> _ = gettext <EOL> import _base <EOL> from html5lib . constants import voidElements <EOL> from html5lib import ihatexml <EOL> class Root ( object ) : <EOL> def __init__ ( self , et ) : <EOL> self . elementtree = et <EOL> self . children = [ ] <EOL> if et . docinfo . internalDTD : <EOL> self . children . append ( Doctype ( self , et . docinfo . root_name , <EOL> et . docinfo . public_id , <EOL> et . docinfo . system_url ) ) <EOL> root = et . getroot ( ) <EOL> node = root <EOL> while node . getprevious ( ) is not None : <EOL> node = node . getprevious ( ) <EOL> while node is not None : <EOL> self . children . append ( node ) <EOL> node = node . getnext ( ) <EOL> self . text = None <EOL> self . tail = None <EOL> def __getitem__ ( self , key ) : <EOL> return self . children [ key ] <EOL> def getnext ( self ) : <EOL> return None <EOL> def __len__ ( self ) : <EOL> return <NUM_LIT:1> <EOL> class Doctype ( object ) : <EOL> def __init__ ( self , root_node , name , public_id , system_id ) : <EOL> self . root_node = root_node <EOL> self . name = name <EOL> self . public_id = public_id <EOL> self . system_id = system_id <EOL> self . text = None <EOL> self . tail = None <EOL> def getnext ( self ) : <EOL> return self . root_node . children [ <NUM_LIT:1> ] <EOL> class FragmentRoot ( Root ) : <EOL> def __init__ ( self , children ) : <EOL> self . children = [ FragmentWrapper ( self , child ) for child in children ] <EOL> self . text = self . tail = None <EOL> def getnext ( self ) : <EOL> return None <EOL> class FragmentWrapper ( object ) : <EOL> def __init__ ( self , fragment_root , obj ) : <EOL> self . root_node = fragment_root <EOL> self . obj = obj <EOL> if hasattr ( self . obj , '<STR_LIT:text>' ) : <EOL> self . text = self . obj . text <EOL> else : <EOL> self . text = None <EOL> if hasattr ( self . obj , '<STR_LIT>' ) : <EOL> self . tail = self . obj . tail <EOL> else : <EOL> self . tail = None <EOL> self . isstring = isinstance ( obj , basestring ) <EOL> def __getattr__ ( self , name ) : <EOL> return getattr ( self . obj , name ) <EOL> def getnext ( self ) : <EOL> siblings = self . root_node . children <EOL> idx = siblings . index ( self ) <EOL> if idx < len ( siblings ) - <NUM_LIT:1> : <EOL> return siblings [ idx + <NUM_LIT:1> ] <EOL> else : <EOL> return None <EOL> def __getitem__ ( self , key ) : <EOL> return self . obj [ key ] <EOL> def __nonzero__ ( self ) : <EOL> return bool ( self . obj ) <EOL> def getparent ( self ) : <EOL> return None <EOL> def __str__ ( self ) : <EOL> return str ( self . obj ) <EOL> def __unicode__ ( self ) : <EOL> return unicode ( self . obj ) <EOL> def __len__ ( self ) : <EOL> return len ( self . obj ) <EOL> class TreeWalker ( _base . NonRecursiveTreeWalker ) : <EOL> def __init__ ( self , tree ) : <EOL> if hasattr ( tree , "<STR_LIT>" ) : <EOL> tree = Root ( tree ) <EOL> elif isinstance ( tree , list ) : <EOL> tree = FragmentRoot ( tree ) <EOL> _base . NonRecursiveTreeWalker . __init__ ( self , tree ) <EOL> self . filter = ihatexml . InfosetFilter ( ) <EOL> def getNodeDetails ( self , node ) : <EOL> if isinstance ( node , tuple ) : <EOL> node , key = node <EOL> assert key in ( "<STR_LIT:text>" , "<STR_LIT>" ) , _ ( "<STR_LIT>" ) % key <EOL> return _base . TEXT , getattr ( node , key ) <EOL> elif isinstance ( node , Root ) : <EOL> return ( _base . DOCUMENT , ) <EOL> elif isinstance ( node , Doctype ) : <EOL> return _base . DOCTYPE , node . name , node . public_id , node . system_id <EOL> elif isinstance ( node , FragmentWrapper ) and node . isstring : <EOL> return _base . TEXT , node <EOL> elif node . tag == etree . Comment : <EOL> return _base . COMMENT , node . text <EOL> elif node . tag == etree . Entity : <EOL> return _base . ENTITY , node . text [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> else : <EOL> match = tag_regexp . match ( node . tag ) <EOL> if match : <EOL> namespace , tag = match . groups ( ) <EOL> else : <EOL> namespace = None <EOL> tag = node . tag <EOL> attrs = { } <EOL> for name , value in node . attrib . items ( ) : <EOL> match = tag_regexp . match ( name ) <EOL> if match : <EOL> attrs [ ( match . group ( <NUM_LIT:1> ) , match . group ( <NUM_LIT:2> ) ) ] = value <EOL> else : <EOL> attrs [ ( None , name ) ] = value <EOL> return ( _base . ELEMENT , namespace , self . filter . fromXmlName ( tag ) , <EOL> attrs , len ( node ) > <NUM_LIT:0> or node . text ) <EOL> def getFirstChild ( self , node ) : <EOL> assert not isinstance ( node , tuple ) , _ ( "<STR_LIT>" ) <EOL> assert len ( node ) or node . text , "<STR_LIT>" <EOL> if node . text : <EOL> return ( node , "<STR_LIT:text>" ) <EOL> else : <EOL> return node [ <NUM_LIT:0> ] <EOL> def getNextSibling ( self , node ) : <EOL> if isinstance ( node , tuple ) : <EOL> node , key = node <EOL> assert key in ( "<STR_LIT:text>" , "<STR_LIT>" ) , _ ( "<STR_LIT>" ) % key <EOL> if key == "<STR_LIT:text>" : <EOL> if len ( node ) : <EOL> return node [ <NUM_LIT:0> ] <EOL> else : <EOL> return None <EOL> else : <EOL> return node . getnext ( ) <EOL> return node . tail and ( node , "<STR_LIT>" ) or node . getnext ( ) <EOL> def getParentNode ( self , node ) : <EOL> if isinstance ( node , tuple ) : <EOL> node , key = node <EOL> assert key in ( "<STR_LIT:text>" , "<STR_LIT>" ) , _ ( "<STR_LIT>" ) % key <EOL> if key == "<STR_LIT:text>" : <EOL> return node <EOL> return node . getparent ( ) </s>
