<s> import xml . sax <EOL> import cgi <EOL> from boto . compat import six , StringIO <EOL> class ResponseGroup ( xml . sax . ContentHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , connection = None , nodename = None ) : <EOL> """<STR_LIT>""" <EOL> self . _connection = connection <EOL> self . _nodename = nodename <EOL> self . _nodepath = [ ] <EOL> self . _curobj = None <EOL> self . _xml = StringIO ( ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . __dict__ ) <EOL> def get ( self , name ) : <EOL> return self . __dict__ . get ( name ) <EOL> def set ( self , name , value ) : <EOL> self . __dict__ [ name ] = value <EOL> def to_xml ( self ) : <EOL> return "<STR_LIT>" % ( self . _nodename , self . _xml . getvalue ( ) , self . _nodename ) <EOL> def startElement ( self , name , attrs , connection ) : <EOL> self . _xml . write ( "<STR_LIT>" % name ) <EOL> self . _nodepath . append ( name ) <EOL> if len ( self . _nodepath ) == <NUM_LIT:1> : <EOL> obj = ResponseGroup ( self . _connection ) <EOL> self . set ( name , obj ) <EOL> self . _curobj = obj <EOL> elif self . _curobj : <EOL> self . _curobj . startElement ( name , attrs , connection ) <EOL> return None <EOL> def endElement ( self , name , value , connection ) : <EOL> self . _xml . write ( "<STR_LIT>" % ( cgi . escape ( value ) . replace ( "<STR_LIT>" , "<STR_LIT>" ) , name ) ) <EOL> if len ( self . _nodepath ) == <NUM_LIT:0> : <EOL> return <EOL> obj = None <EOL> curval = self . get ( name ) <EOL> if len ( self . _nodepath ) == <NUM_LIT:1> : <EOL> if value or not curval : <EOL> self . set ( name , value ) <EOL> if self . _curobj : <EOL> self . _curobj = None <EOL> elif self . _curobj : <EOL> self . _curobj . endElement ( name , value , connection ) <EOL> self . _nodepath . pop ( ) <EOL> return None <EOL> class Item ( ResponseGroup ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , connection = None ) : <EOL> """<STR_LIT>""" <EOL> ResponseGroup . __init__ ( self , connection , "<STR_LIT>" ) <EOL> class ItemSet ( ResponseGroup ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , connection , action , params , page = <NUM_LIT:0> ) : <EOL> ResponseGroup . __init__ ( self , connection , "<STR_LIT>" ) <EOL> self . objs = [ ] <EOL> self . iter = None <EOL> self . page = page <EOL> self . action = action <EOL> self . params = params <EOL> self . curItem = None <EOL> self . total_results = <NUM_LIT:0> <EOL> self . total_pages = <NUM_LIT:0> <EOL> self . is_valid = False <EOL> self . errors = [ ] <EOL> def startElement ( self , name , attrs , connection ) : <EOL> if name == "<STR_LIT>" : <EOL> self . curItem = Item ( self . _connection ) <EOL> elif self . curItem is not None : <EOL> self . curItem . startElement ( name , attrs , connection ) <EOL> return None <EOL> def endElement ( self , name , value , connection ) : <EOL> if name == '<STR_LIT>' : <EOL> self . total_results = value <EOL> elif name == '<STR_LIT>' : <EOL> self . total_pages = value <EOL> elif name == '<STR_LIT>' : <EOL> if value == '<STR_LIT:True>' : <EOL> self . is_valid = True <EOL> elif name == '<STR_LIT>' : <EOL> self . errors . append ( { '<STR_LIT>' : value , '<STR_LIT>' : None } ) <EOL> elif name == '<STR_LIT>' : <EOL> self . errors [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] = value <EOL> elif name == '<STR_LIT>' : <EOL> self . objs . append ( self . curItem ) <EOL> self . _xml . write ( self . curItem . to_xml ( ) ) <EOL> self . curItem = None <EOL> elif self . curItem is not None : <EOL> self . curItem . endElement ( name , value , connection ) <EOL> return None <EOL> def __next__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . iter is None : <EOL> self . iter = iter ( self . objs ) <EOL> try : <EOL> return next ( self . iter ) <EOL> except StopIteration : <EOL> self . iter = None <EOL> self . objs = [ ] <EOL> if int ( self . page ) < int ( self . total_pages ) : <EOL> self . page += <NUM_LIT:1> <EOL> self . _connection . get_response ( self . action , self . params , self . page , self ) <EOL> return next ( self ) <EOL> else : <EOL> raise <EOL> next = __next__ <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def to_xml ( self ) : <EOL> """<STR_LIT>""" <EOL> for item in self : <EOL> pass <EOL> return ResponseGroup . to_xml ( self ) </s>
