<s> import xml . sax <EOL> from boto import utils <EOL> class XmlHandler ( xml . sax . ContentHandler ) : <EOL> def __init__ ( self , root_node , connection ) : <EOL> self . connection = connection <EOL> self . nodes = [ ( '<STR_LIT:root>' , root_node ) ] <EOL> self . current_text = '<STR_LIT>' <EOL> def startElement ( self , name , attrs ) : <EOL> self . current_text = '<STR_LIT>' <EOL> t = self . nodes [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] . startElement ( name , attrs , self . connection ) <EOL> if t is not None : <EOL> if isinstance ( t , tuple ) : <EOL> self . nodes . append ( t ) <EOL> else : <EOL> self . nodes . append ( ( name , t ) ) <EOL> def endElement ( self , name ) : <EOL> self . nodes [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] . endElement ( name , self . current_text , self . connection ) <EOL> if self . nodes [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] == name : <EOL> self . nodes . pop ( ) <EOL> self . current_text = '<STR_LIT>' <EOL> def characters ( self , content ) : <EOL> self . current_text += content <EOL> def parse ( self , s ) : <EOL> if not isinstance ( s , bytes ) : <EOL> s = s . encode ( '<STR_LIT:utf-8>' ) <EOL> xml . sax . parseString ( s , self ) <EOL> class Element ( dict ) : <EOL> def __init__ ( self , connection = None , element_name = None , <EOL> stack = None , parent = None , list_marker = ( '<STR_LIT>' , ) , <EOL> item_marker = ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> pythonize_name = False ) : <EOL> dict . __init__ ( self ) <EOL> self . connection = connection <EOL> self . element_name = element_name <EOL> self . list_marker = utils . mklist ( list_marker ) <EOL> self . item_marker = utils . mklist ( item_marker ) <EOL> if stack is None : <EOL> self . stack = [ ] <EOL> else : <EOL> self . stack = stack <EOL> self . pythonize_name = pythonize_name <EOL> self . parent = parent <EOL> def __getattr__ ( self , key ) : <EOL> if key in self : <EOL> return self [ key ] <EOL> for k in self : <EOL> e = self [ k ] <EOL> if isinstance ( e , Element ) : <EOL> try : <EOL> return getattr ( e , key ) <EOL> except AttributeError : <EOL> pass <EOL> raise AttributeError <EOL> def get_name ( self , name ) : <EOL> if self . pythonize_name : <EOL> name = utils . pythonize_name ( name ) <EOL> return name <EOL> def startElement ( self , name , attrs , connection ) : <EOL> self . stack . append ( name ) <EOL> for lm in self . list_marker : <EOL> if name . endswith ( lm ) : <EOL> l = ListElement ( self . connection , name , self . list_marker , <EOL> self . item_marker , self . pythonize_name ) <EOL> self [ self . get_name ( name ) ] = l <EOL> return l <EOL> if len ( self . stack ) > <NUM_LIT:0> : <EOL> element_name = self . stack [ - <NUM_LIT:1> ] <EOL> e = Element ( self . connection , element_name , self . stack , self , <EOL> self . list_marker , self . item_marker , <EOL> self . pythonize_name ) <EOL> self [ self . get_name ( element_name ) ] = e <EOL> return ( element_name , e ) <EOL> else : <EOL> return None <EOL> def endElement ( self , name , value , connection ) : <EOL> if len ( self . stack ) > <NUM_LIT:0> : <EOL> self . stack . pop ( ) <EOL> value = value . strip ( ) <EOL> if value : <EOL> if isinstance ( self . parent , Element ) : <EOL> self . parent [ self . get_name ( name ) ] = value <EOL> elif isinstance ( self . parent , ListElement ) : <EOL> self . parent . append ( value ) <EOL> class ListElement ( list ) : <EOL> def __init__ ( self , connection = None , element_name = None , <EOL> list_marker = [ '<STR_LIT>' ] , item_marker = ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> pythonize_name = False ) : <EOL> list . __init__ ( self ) <EOL> self . connection = connection <EOL> self . element_name = element_name <EOL> self . list_marker = list_marker <EOL> self . item_marker = item_marker <EOL> self . pythonize_name = pythonize_name <EOL> def get_name ( self , name ) : <EOL> if self . pythonize_name : <EOL> name = utils . pythonize_name ( name ) <EOL> return name <EOL> def startElement ( self , name , attrs , connection ) : <EOL> for lm in self . list_marker : <EOL> if name . endswith ( lm ) : <EOL> l = ListElement ( self . connection , name , <EOL> self . list_marker , self . item_marker , <EOL> self . pythonize_name ) <EOL> setattr ( self , self . get_name ( name ) , l ) <EOL> return l <EOL> if name in self . item_marker : <EOL> e = Element ( self . connection , name , parent = self , <EOL> list_marker = self . list_marker , <EOL> item_marker = self . item_marker , <EOL> pythonize_name = self . pythonize_name ) <EOL> self . append ( e ) <EOL> return e <EOL> else : <EOL> return None <EOL> def endElement ( self , name , value , connection ) : <EOL> if name == self . element_name : <EOL> if len ( self ) > <NUM_LIT:0> : <EOL> empty = [ ] <EOL> for e in self : <EOL> if isinstance ( e , Element ) : <EOL> if len ( e ) == <NUM_LIT:0> : <EOL> empty . append ( e ) <EOL> for e in empty : <EOL> self . remove ( e ) <EOL> else : <EOL> setattr ( self , self . get_name ( name ) , value ) </s>
