<s> from . error import InvalidTokenError <EOL> import time <EOL> class PageIterator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , client , path , query , options ) : <EOL> self . client = client <EOL> self . path = path <EOL> self . query = query <EOL> self . options = client . _merge_options ( options , { '<STR_LIT>' : True } ) <EOL> self . item_limit = float ( '<STR_LIT>' ) if self . options . get ( '<STR_LIT>' , None ) == None else self . options [ '<STR_LIT>' ] <EOL> self . page_size = self . options [ '<STR_LIT>' ] <EOL> self . count = <NUM_LIT:0> <EOL> self . continuation = False <EOL> def __getattr__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name == self . CONTINUATION_KEY : <EOL> return self . continuation <EOL> raise AttributeError ( "<STR_LIT>" % ( self . __class__ , attr ) ) <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self <EOL> def __next__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . options [ '<STR_LIT>' ] = min ( self . page_size , self . item_limit - self . count ) <EOL> if self . continuation == None or self . options [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> raise StopIteration <EOL> elif self . continuation == False : <EOL> result = self . get_initial ( ) <EOL> else : <EOL> result = self . get_next ( ) <EOL> self . continuation = result . get ( self . CONTINUATION_KEY , None ) <EOL> data = result . get ( '<STR_LIT:data>' , None ) <EOL> if data != None : <EOL> self . count += len ( data ) <EOL> return data <EOL> def next ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __next__ ( ) <EOL> def items ( self ) : <EOL> """<STR_LIT>""" <EOL> for page in self : <EOL> for item in page : <EOL> yield item <EOL> raise StopIteration <EOL> class CollectionPageIterator ( PageIterator ) : <EOL> """<STR_LIT>""" <EOL> CONTINUATION_KEY = '<STR_LIT>' <EOL> def get_initial ( self ) : <EOL> return self . client . get ( self . path , self . query , ** self . options ) <EOL> def get_next ( self ) : <EOL> self . options [ '<STR_LIT>' ] = self . continuation [ '<STR_LIT>' ] <EOL> return self . client . get ( self . path , self . query , ** self . options ) <EOL> class EventsPageIterator ( PageIterator ) : <EOL> """<STR_LIT>""" <EOL> CONTINUATION_KEY = '<STR_LIT>' <EOL> def get_initial ( self ) : <EOL> if '<STR_LIT>' not in self . query : <EOL> try : <EOL> self . client . events . get ( self . query , ** self . options ) <EOL> except InvalidTokenError as e : <EOL> self . continuation = e . sync <EOL> else : <EOL> self . continuation = self . query [ '<STR_LIT>' ] <EOL> return self . get_next ( ) <EOL> def get_next ( self ) : <EOL> self . query [ '<STR_LIT>' ] = self . continuation <EOL> return self . client . events . get ( self . query , ** self . options ) <EOL> def __next__ ( self ) : <EOL> while True : <EOL> results = super ( EventsPageIterator , self ) . __next__ ( ) <EOL> if len ( results ) > <NUM_LIT:0> : <EOL> return results <EOL> else : <EOL> time . sleep ( self . options [ '<STR_LIT>' ] ) </s>
