<s> __version__ = ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> import sys <EOL> import re <EOL> from exceptions import StopIteration <EOL> import json <EOL> from pprint import pprint <EOL> from . hydrate import EntityHydrator <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> errout = sys . stderr . write <EOL> class RallyResponseError ( Exception ) : pass <EOL> class ErrorResponse ( object ) : <EOL> SECURITY_ERROR = '<STR_LIT>' <EOL> INVALID_CREDENTIALS = "<STR_LIT>" <EOL> def __init__ ( self , status_code , problem ) : <EOL> self . status_code = status_code <EOL> self . headers = { '<STR_LIT>' : True } <EOL> self . content = { '<STR_LIT>' : { '<STR_LIT>' : [ problem ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> } <EOL> } <EOL> prob_str = str ( problem ) <EOL> if ErrorResponse . SECURITY_ERROR in prob_str : <EOL> self . content [ '<STR_LIT>' ] [ '<STR_LIT>' ] = [ ErrorResponse . SECURITY_ERROR ] <EOL> if ErrorResponse . INVALID_CREDENTIALS in prob_str : <EOL> self . content [ '<STR_LIT>' ] [ '<STR_LIT>' ] = [ ErrorResponse . INVALID_CREDENTIALS ] <EOL> class RallyRESTResponse ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , session , context , request , response , hydration , limit ) : <EOL> """<STR_LIT>""" <EOL> self . session = session <EOL> self . context = context <EOL> self . resource = request <EOL> self . data = None <EOL> request_path_elements = request . split ( '<STR_LIT:?>' ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:/>' ) <EOL> self . target = request_path_elements [ - <NUM_LIT:1> ] <EOL> if re . match ( '<STR_LIT>' , self . target ) : <EOL> self . target = request_path_elements [ - <NUM_LIT:2> ] <EOL> if self . target . lower ( ) == '<STR_LIT>' : <EOL> self . target = '<STR_LIT>' <EOL> self . errors = [ ] <EOL> self . warnings = [ ] <EOL> self . _item_type = self . target <EOL> self . status_code = response . status_code <EOL> self . headers = response . headers <EOL> if isinstance ( response , ErrorResponse ) : <EOL> if '<STR_LIT>' in response . content : <EOL> if '<STR_LIT>' in response . content [ '<STR_LIT>' ] : <EOL> self . errors = response . content [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> return <EOL> self . _stdFormat = True <EOL> self . content = json . loads ( response . content ) <EOL> self . request_type , self . data = self . _determineRequestResponseType ( request ) <EOL> if self . request_type == '<STR_LIT>' : <EOL> target = self . target <EOL> if target . endswith ( '<STR_LIT>' ) : <EOL> target = target [ : - <NUM_LIT:2> ] <EOL> if target not in self . content . keys ( ) : <EOL> ckls = [ k . lower ( ) for k in self . content . keys ( ) ] <EOL> if target . lower ( ) not in ckls : <EOL> forensic_info = "<STR_LIT>" % ( response . status_code , response . content ) <EOL> problem = '<STR_LIT>' % target <EOL> raise RallyResponseError ( '<STR_LIT>' % ( problem , forensic_info ) ) <EOL> else : <EOL> matching_item_ix = ckls . index ( target . lower ( ) ) <EOL> target = self . content . keys ( ) [ matching_item_ix ] <EOL> self . target = target <EOL> self . _stdFormat = False <EOL> self . _item_type = target <EOL> self . data = { u'<STR_LIT>' : { u'<STR_LIT>' : { target : self . content [ target ] } } } <EOL> self . data [ u'<STR_LIT>' ] = self . content [ target ] [ '<STR_LIT>' ] <EOL> self . data [ u'<STR_LIT>' ] = self . content [ target ] [ '<STR_LIT>' ] <EOL> del self . content [ target ] [ '<STR_LIT>' ] <EOL> del self . content [ target ] [ '<STR_LIT>' ] <EOL> self . data [ u'<STR_LIT>' ] = <NUM_LIT:1> <EOL> self . data [ u'<STR_LIT>' ] = <NUM_LIT:1> <EOL> qr = self . data <EOL> self . errors = qr [ u'<STR_LIT>' ] <EOL> self . warnings = qr [ u'<STR_LIT>' ] <EOL> self . startIndex = int ( qr [ u'<STR_LIT>' ] ) if u'<STR_LIT>' in qr else <NUM_LIT:0> <EOL> self . pageSize = int ( qr [ u'<STR_LIT>' ] ) if u'<STR_LIT>' in qr else <NUM_LIT:0> <EOL> self . resultCount = int ( qr [ u'<STR_LIT>' ] ) if u'<STR_LIT>' in qr else <NUM_LIT:0> <EOL> self . _limit = limit if limit > <NUM_LIT:0> else self . resultCount <EOL> self . _page = [ ] <EOL> if u'<STR_LIT>' in qr : <EOL> self . _page = qr [ u'<STR_LIT>' ] <EOL> else : <EOL> if u'<STR_LIT>' in qr and u'<STR_LIT>' in qr [ u'<STR_LIT>' ] : <EOL> self . _page = qr [ u'<STR_LIT>' ] [ u'<STR_LIT>' ] <EOL> if qr . get ( '<STR_LIT>' , None ) : <EOL> self . _page = qr [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> self . _servable = <NUM_LIT:0> <EOL> if self . resultCount > <NUM_LIT:0> : <EOL> self . _servable = self . resultCount <EOL> if self . startIndex > <NUM_LIT:1> : <EOL> self . _servable = self . resultCount - self . startIndex <EOL> self . _servable = min ( self . _servable , self . _limit ) <EOL> self . _served = <NUM_LIT:0> <EOL> self . _curIndex = <NUM_LIT:0> <EOL> self . hydrator = EntityHydrator ( context , hydration = hydration ) <EOL> if self . errors : <EOL> self . status_code = <NUM_LIT> if self . status_code == <NUM_LIT:200> else self . status_code <EOL> def _determineRequestResponseType ( self , request ) : <EOL> if u'<STR_LIT>' in self . content : <EOL> return '<STR_LIT>' , self . content [ u'<STR_LIT>' ] <EOL> if u'<STR_LIT>' in self . content : <EOL> return '<STR_LIT>' , self . content [ u'<STR_LIT>' ] <EOL> if u'<STR_LIT>' in self . content : <EOL> return '<STR_LIT>' , self . content [ u'<STR_LIT>' ] <EOL> if u'<STR_LIT>' in self . content : <EOL> return '<STR_LIT>' , self . content [ u'<STR_LIT>' ] <EOL> if u'<STR_LIT>' in self . content : <EOL> return '<STR_LIT>' , self . content [ u'<STR_LIT>' ] <EOL> if '<STR_LIT>' in self . content and self . content [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return '<STR_LIT>' , self . content <EOL> else : <EOL> return '<STR_LIT>' , self . content <EOL> def _item ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . status_code == <NUM_LIT:200> : <EOL> return self . _page [ <NUM_LIT:0> ] <EOL> else : <EOL> return None <EOL> def __nonzero__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if <NUM_LIT:200> <= self . status_code < <NUM_LIT> : <EOL> return True <EOL> else : <EOL> return False <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( self . _served >= self . _servable ) or ( self . _limit and self . _served >= self . _limit ) : <EOL> raise StopIteration <EOL> if self . _stdFormat : <EOL> if self . _curIndex == self . pageSize : <EOL> self . _page [ : ] = self . __retrieveNextPage ( ) <EOL> self . _curIndex = <NUM_LIT:0> <EOL> if not self . _page : <EOL> raise StopIteration <EOL> try : <EOL> item = self . _page [ self . _curIndex ] <EOL> except IndexError : <EOL> item = None <EOL> if self . target in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> try : <EOL> self . page [ : ] = self . __retrieveNextPage ( ) <EOL> self . _curIndex = <NUM_LIT:0> <EOL> item = self . page [ self . _curIndex ] <EOL> except : <EOL> exception_type , value , traceback = sys . exc_info ( ) <EOL> exc_name = re . search ( "<STR_LIT>" , str ( exception_type ) ) . group ( <NUM_LIT:1> ) <EOL> problem = '<STR_LIT>' % ( exc_name , value , self . target ) <EOL> errout ( "<STR_LIT>" % problem ) <EOL> if item is None : <EOL> verbiage = "<STR_LIT>" + "<STR_LIT>" <EOL> problem = verbiage % ( self . _curIndex + <NUM_LIT:1> , self . _served , self . resultCount ) <EOL> errout ( "<STR_LIT>" % problem ) <EOL> raise StopIteration <EOL> else : <EOL> item = self . _page [ self . _item_type ] <EOL> item_type_key = u'<STR_LIT>' <EOL> if item_type_key not in item : <EOL> item [ item_type_key ] = unicode ( self . _item_type ) <EOL> del item [ u'<STR_LIT>' ] <EOL> del item [ u'<STR_LIT>' ] <EOL> entityInstance = self . hydrator . hydrateInstance ( item ) <EOL> self . _curIndex += <NUM_LIT:1> <EOL> self . _served += <NUM_LIT:1> <EOL> return entityInstance <EOL> def __retrieveNextPage ( self ) : <EOL> """<STR_LIT>""" <EOL> self . startIndex += self . pageSize <EOL> nextPageUrl = re . sub ( '<STR_LIT>' , '<STR_LIT>' % self . startIndex , self . resource ) <EOL> if not nextPageUrl . startswith ( '<STR_LIT:http>' ) : <EOL> nextPageUrl = '<STR_LIT>' % ( self . context . serviceURL ( ) , nextPageUrl ) <EOL> try : <EOL> response = self . session . get ( nextPageUrl ) <EOL> except Exception as ex : <EOL> exception_type , value , traceback = sys . exc_info ( ) <EOL> sys . stderr . write ( '<STR_LIT>' % ( exception_type , value ) ) <EOL> sys . stderr . write ( traceback ) <EOL> sys . exit ( <NUM_LIT:9> ) <EOL> return [ ] <EOL> content = json . loads ( response . content ) <EOL> return content [ u'<STR_LIT>' ] [ u'<STR_LIT>' ] <EOL> def __repr__ ( self ) : <EOL> if self . status_code == <NUM_LIT:200> and self . _page : <EOL> try : <EOL> entity_type = self . _page [ <NUM_LIT:0> ] [ u'<STR_LIT>' ] <EOL> return "<STR_LIT>" % ( entity_type , self . resultCount , self . startIndex , self . pageSize , self . _curIndex ) <EOL> except : <EOL> return "<STR_LIT>" % ( self . status_code , <EOL> self . errors , <EOL> self . warnings , <EOL> self . _page ) <EOL> else : <EOL> if self . errors : <EOL> blurb = self . errors [ <NUM_LIT:0> ] <EOL> elif self . warnings : <EOL> blurb = self . warnings [ <NUM_LIT:0> ] <EOL> else : <EOL> blurb = "<STR_LIT>" % ( self . request_type , self . resultCount , self . content [ '<STR_LIT>' ] ) <EOL> return "<STR_LIT>" % ( self . status_code , blurb ) </s>
