<s> from HTMLParser import HTMLParser <EOL> from collections import deque <EOL> version = '<STR_LIT>' <EOL> DATA = <NUM_LIT:1> <EOL> META = <NUM_LIT:2> <EOL> COMMENT = <NUM_LIT:3> <EOL> PI = <NUM_LIT:4> <EOL> CODE = <NUM_LIT:5> <EOL> AMP = <NUM_LIT:6> <EOL> class Attribute ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __getitem__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return self . get ( key , '<STR_LIT>' ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> data = '<STR_LIT>' <EOL> for key , value in self . items ( ) : <EOL> pair = '<STR_LIT>' % ( key , value ) <EOL> data = data + pair <EOL> return data <EOL> class Root ( list ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name = None , attr = { } ) : <EOL> """<STR_LIT:U+0020>""" <EOL> self . name = name <EOL> self . attr = Attribute ( attr ) <EOL> list . __init__ ( self ) <EOL> __repr__ = object . __repr__ <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> html = '<STR_LIT>' <EOL> for ind in self : <EOL> html = '<STR_LIT>' % ( html , ind ) <EOL> return html <EOL> def sail ( self ) : <EOL> """<STR_LIT>""" <EOL> for indi in self [ : ] : <EOL> for indj in indi . sail ( ) : <EOL> yield ( indj ) <EOL> yield ( indi ) <EOL> def index ( self , item ) : <EOL> """<STR_LIT>""" <EOL> count = <NUM_LIT:0> <EOL> for ind in self : <EOL> if ind is item : return count <EOL> count = count + <NUM_LIT:1> <EOL> raise ValueError <EOL> def remove ( self , item ) : <EOL> """<STR_LIT>""" <EOL> index = self . index ( item ) <EOL> del self [ index ] <EOL> def find ( self , name , * args ) : <EOL> """<STR_LIT>""" <EOL> for ind in self . sail ( ) : <EOL> if ind . name == name : <EOL> for key , value in args : <EOL> if ind . attr [ key ] != value : <EOL> break <EOL> else : <EOL> yield ( ind ) <EOL> def find_with_root ( self , name , * args ) : <EOL> """<STR_LIT>""" <EOL> for root , ind in self . sail_with_root ( ) : <EOL> if ind . name == name : <EOL> for key , value in args : <EOL> if ind . attr [ key ] != value : <EOL> break <EOL> else : <EOL> yield ( root , ind ) <EOL> def byid ( self , id ) : <EOL> """<STR_LIT>""" <EOL> return self . take ( '<STR_LIT:id>' , id ) <EOL> def take ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> seq = self . match ( * args ) <EOL> try : <EOL> item = seq . next ( ) <EOL> except StopIteration : <EOL> return None <EOL> else : <EOL> return item <EOL> def take_with_root ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> seq = self . match_with_root ( * args ) <EOL> try : <EOL> item = seq . next ( ) <EOL> except StopIteration : <EOL> return None <EOL> else : <EOL> return item <EOL> pass <EOL> def match ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> for ind in self . sail ( ) : <EOL> for key , value in args : <EOL> if ind . attr [ key ] != value : <EOL> break <EOL> else : <EOL> yield ( ind ) <EOL> def match_with_root ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> for root , ind in self . sail_with_root ( ) : <EOL> for key , value in args : <EOL> if ind . attr [ key ] != value : <EOL> break <EOL> else : <EOL> yield ( root , ind ) <EOL> def join ( self , delim , * args ) : <EOL> """<STR_LIT>""" <EOL> data = '<STR_LIT>' <EOL> for ind in self . sail ( ) : <EOL> if ind . name in args : <EOL> data = '<STR_LIT>' % ( data , delim , ind ) <EOL> return data <EOL> def fst ( self , name , * args ) : <EOL> """<STR_LIT>""" <EOL> seq = self . find ( name , * args ) <EOL> try : <EOL> item = seq . next ( ) <EOL> except StopIteration : <EOL> return None <EOL> else : <EOL> return item <EOL> def fst_with_root ( self , name , * args ) : <EOL> """<STR_LIT>""" <EOL> seq = self . find_with_root ( name , * args ) <EOL> try : <EOL> item = seq . next ( ) <EOL> except StopIteration : <EOL> return None <EOL> else : <EOL> return item <EOL> def text ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . join ( '<STR_LIT>' , DATA ) <EOL> def write ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> fd = open ( filename , '<STR_LIT:w>' ) <EOL> fd . write ( self ) <EOL> fd . close ( ) <EOL> def sail_with_root ( self ) : <EOL> """<STR_LIT>""" <EOL> for indi in self [ : ] : <EOL> for indj in indi . sail_with_root ( ) : <EOL> yield ( indj ) <EOL> yield ( ( self , indi ) ) <EOL> def walk ( self ) : <EOL> """<STR_LIT>""" <EOL> for ind in self . sail ( ) : <EOL> yield ( ind , ind . name , ind . attr ) <EOL> def walk_with_root ( self ) : <EOL> """<STR_LIT>""" <EOL> for root , ind in self . sail_with_root ( ) : <EOL> yield ( ( root , root . name , root . attr ) , <EOL> ( ind , ind . name , ind . attr ) ) <EOL> def insert_after ( self , y , k ) : <EOL> """<STR_LIT>""" <EOL> ind = self . index ( y ) <EOL> self . insert ( ind + <NUM_LIT:1> , k ) <EOL> def insert_before ( self , y , k ) : <EOL> """<STR_LIT>""" <EOL> ind = self . index ( y ) <EOL> self . insert ( ind , k ) <EOL> class Tag ( Root ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , attr = { } ) : <EOL> """<STR_LIT>""" <EOL> Root . __init__ ( self , name , attr ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> html = '<STR_LIT>' % ( self . name , self . attr ) <EOL> for ind in self : <EOL> html = '<STR_LIT>' % ( html , ind ) <EOL> html = html + '<STR_LIT>' % self . name <EOL> return html <EOL> class Data ( Root ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data ) : <EOL> """<STR_LIT>""" <EOL> Root . __init__ ( self , DATA ) <EOL> self . data = data <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . data <EOL> def text ( self ) : <EOL> return self . data <EOL> class XTag ( Root ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , attr = { } ) : <EOL> """<STR_LIT>""" <EOL> Root . __init__ ( self , name , attr ) <EOL> def __str__ ( self ) : <EOL> html = '<STR_LIT>' % ( self . name , self . attr ) <EOL> return html <EOL> class Meta ( Root ) : <EOL> """<STR_LIT:U+0020>""" <EOL> def __init__ ( self , data ) : <EOL> Root . __init__ ( self , META ) <EOL> self . data = data <EOL> def __str__ ( self ) : <EOL> html = '<STR_LIT>' % self . data <EOL> return html <EOL> class Code ( Root ) : <EOL> """<STR_LIT:U+0020>""" <EOL> def __init__ ( self , data ) : <EOL> Root . __init__ ( self , CODE ) <EOL> self . data = data <EOL> def __str__ ( self ) : <EOL> html = '<STR_LIT>' % self . data <EOL> return html <EOL> class Amp ( Root ) : <EOL> """<STR_LIT:U+0020>""" <EOL> def __init__ ( self , data ) : <EOL> Root . __init__ ( self , AMP ) <EOL> self . data = data <EOL> def __str__ ( self ) : <EOL> html = '<STR_LIT>' % self . data <EOL> return html <EOL> class Pi ( Root ) : <EOL> """<STR_LIT:U+0020>""" <EOL> def __init__ ( self , data ) : <EOL> Root . __init__ ( self , PI ) <EOL> self . data = data <EOL> def __str__ ( self ) : <EOL> html = '<STR_LIT>' % self . data <EOL> return html <EOL> class Comment ( Root ) : <EOL> """<STR_LIT:U+0020>""" <EOL> def __init__ ( self , data ) : <EOL> Root . __init__ ( self , COMMENT ) <EOL> self . data = data <EOL> def __str__ ( self ) : <EOL> html = '<STR_LIT>' % self . data <EOL> return html <EOL> class Tree ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . outmost = Root ( '<STR_LIT>' ) <EOL> self . stack = deque ( ) <EOL> self . stack . append ( self . outmost ) <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> self . outmost = Root ( '<STR_LIT>' ) <EOL> self . stack . clear ( ) <EOL> self . stack . append ( self . outmost ) <EOL> def last ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . stack [ - <NUM_LIT:1> ] <EOL> def nest ( self , name , attr ) : <EOL> """<STR_LIT>""" <EOL> item = Tag ( name , attr ) <EOL> pointer = self . stack . pop ( ) <EOL> pointer . append ( item ) <EOL> self . stack . append ( pointer ) <EOL> self . stack . append ( item ) <EOL> def dnest ( self , data ) : <EOL> """<STR_LIT>""" <EOL> top = self . last ( ) <EOL> item = Data ( data ) <EOL> top . append ( item ) <EOL> def xnest ( self , name , attr ) : <EOL> """<STR_LIT>""" <EOL> top = self . last ( ) <EOL> item = XTag ( name , attr ) <EOL> top . append ( item ) <EOL> def ynest ( self , data ) : <EOL> """<STR_LIT:U+0020>""" <EOL> top = self . last ( ) <EOL> item = Meta ( data ) <EOL> top . append ( item ) <EOL> def mnest ( self , data ) : <EOL> """<STR_LIT:U+0020>""" <EOL> top = self . last ( ) <EOL> item = Comment ( data ) <EOL> top . append ( item ) <EOL> def cnest ( self , data ) : <EOL> """<STR_LIT:U+0020>""" <EOL> top = self . last ( ) <EOL> item = Code ( data ) <EOL> top . append ( item ) <EOL> def rnest ( self , data ) : <EOL> """<STR_LIT:U+0020>""" <EOL> top = self . last ( ) <EOL> item = Amp ( data ) <EOL> top . append ( item ) <EOL> def inest ( self , data ) : <EOL> """<STR_LIT:U+0020>""" <EOL> top = self . last ( ) <EOL> item = Pi ( data ) <EOL> top . append ( item ) <EOL> def enclose ( self , name ) : <EOL> """<STR_LIT>""" <EOL> count = <NUM_LIT:0> <EOL> for ind in reversed ( self . stack ) : <EOL> count = count + <NUM_LIT:1> <EOL> if ind . name == name : <EOL> break <EOL> else : <EOL> count = <NUM_LIT:0> <EOL> for i in xrange ( <NUM_LIT:0> , count ) : <EOL> self . stack . pop ( ) <EOL> class Html ( HTMLParser ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> HTMLParser . __init__ ( self ) <EOL> self . struct = Tree ( ) <EOL> def fromfile ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> fd = open ( fname , '<STR_LIT:r>' ) <EOL> data = fd . read ( ) <EOL> fd . close ( ) <EOL> return self . feed ( data ) <EOL> def feed ( self , data ) : <EOL> """<STR_LIT:U+0020>""" <EOL> self . struct . clear ( ) <EOL> HTMLParser . feed ( self , data ) <EOL> return self . struct . outmost <EOL> def handle_starttag ( self , name , attr ) : <EOL> """<STR_LIT>""" <EOL> self . struct . nest ( name , attr ) <EOL> pass <EOL> def handle_startendtag ( self , name , attr ) : <EOL> """<STR_LIT>""" <EOL> self . struct . xnest ( name , attr ) <EOL> def handle_endtag ( self , name ) : <EOL> """<STR_LIT>""" <EOL> self . struct . enclose ( name ) <EOL> pass <EOL> def handle_data ( self , data ) : <EOL> """<STR_LIT>""" <EOL> self . struct . dnest ( data ) <EOL> def handle_decl ( self , decl ) : <EOL> """<STR_LIT:U+0020>""" <EOL> self . struct . ynest ( decl ) <EOL> def unknown_decl ( self , decl ) : <EOL> """<STR_LIT:U+0020>""" <EOL> self . struct . ynest ( decl ) <EOL> def handle_charref ( self , data ) : <EOL> """<STR_LIT:U+0020>""" <EOL> self . struct . cnest ( data ) <EOL> def handle_entityref ( self , data ) : <EOL> """<STR_LIT:U+0020>""" <EOL> self . struct . rnest ( data ) <EOL> def handle_pi ( self , data ) : <EOL> """<STR_LIT:U+0020>""" <EOL> self . struct . inest ( data ) <EOL> def handle_comment ( self , data ) : <EOL> """<STR_LIT:U+0020>""" <EOL> self . struct . mnest ( data ) </s>
