<s> from . error import TweepError <EOL> class Cursor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , method , * args , ** kargs ) : <EOL> if hasattr ( method , '<STR_LIT>' ) : <EOL> if method . pagination_mode == '<STR_LIT>' : <EOL> self . iterator = CursorIterator ( method , args , kargs ) <EOL> elif method . pagination_mode == '<STR_LIT:id>' : <EOL> self . iterator = IdIterator ( method , args , kargs ) <EOL> elif method . pagination_mode == '<STR_LIT>' : <EOL> self . iterator = PageIterator ( method , args , kargs ) <EOL> else : <EOL> raise TweepError ( '<STR_LIT>' ) <EOL> else : <EOL> raise TweepError ( '<STR_LIT>' ) <EOL> def pages ( self , limit = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if limit > <NUM_LIT:0> : <EOL> self . iterator . limit = limit <EOL> return self . iterator <EOL> def items ( self , limit = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> i = ItemIterator ( self . iterator ) <EOL> i . limit = limit <EOL> return i <EOL> class BaseIterator ( object ) : <EOL> def __init__ ( self , method , args , kargs ) : <EOL> self . method = method <EOL> self . args = args <EOL> self . kargs = kargs <EOL> self . limit = <NUM_LIT:0> <EOL> def next ( self ) : <EOL> raise NotImplementedError <EOL> def prev ( self ) : <EOL> raise NotImplementedError <EOL> def __iter__ ( self ) : <EOL> return self <EOL> class CursorIterator ( BaseIterator ) : <EOL> def __init__ ( self , method , args , kargs ) : <EOL> BaseIterator . __init__ ( self , method , args , kargs ) <EOL> self . next_cursor = - <NUM_LIT:1> <EOL> self . prev_cursor = <NUM_LIT:0> <EOL> self . count = <NUM_LIT:0> <EOL> def next ( self ) : <EOL> if self . next_cursor == <NUM_LIT:0> or ( self . limit and self . count == self . limit ) : <EOL> raise StopIteration <EOL> data , cursors = self . method ( <EOL> cursor = self . next_cursor , * self . args , ** self . kargs <EOL> ) <EOL> self . prev_cursor , self . next_cursor = cursors <EOL> if len ( data ) == <NUM_LIT:0> : <EOL> raise StopIteration <EOL> self . count += <NUM_LIT:1> <EOL> return data <EOL> def prev ( self ) : <EOL> if self . prev_cursor == <NUM_LIT:0> : <EOL> raise TweepError ( '<STR_LIT>' ) <EOL> data , self . next_cursor , self . prev_cursor = self . method ( <EOL> cursor = self . prev_cursor , * self . args , ** self . kargs <EOL> ) <EOL> self . count -= <NUM_LIT:1> <EOL> return data <EOL> class IdIterator ( BaseIterator ) : <EOL> def __init__ ( self , method , args , kargs ) : <EOL> BaseIterator . __init__ ( self , method , args , kargs ) <EOL> self . max_id = kargs . get ( '<STR_LIT>' ) <EOL> self . since_id = kargs . get ( '<STR_LIT>' ) <EOL> self . count = <NUM_LIT:0> <EOL> def next ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . limit and self . limit == self . count : <EOL> raise StopIteration <EOL> max_id = self . since_id - <NUM_LIT:1> if self . max_id else None <EOL> data = self . method ( max_id = max_id , * self . args , ** self . kargs ) <EOL> if len ( data ) == <NUM_LIT:0> : <EOL> raise StopIteration <EOL> self . max_id = data . max_id <EOL> self . since_id = data . since_id <EOL> self . count += <NUM_LIT:1> <EOL> return data <EOL> def prev ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . limit and self . limit == self . count : <EOL> raise StopIteration <EOL> since_id = self . max_id <EOL> data = self . method ( since_id = since_id , * self . args , ** self . kargs ) <EOL> if len ( data ) == <NUM_LIT:0> : <EOL> raise StopIteration <EOL> self . max_id = data . max_id <EOL> self . since_id = data . since_id <EOL> self . count += <NUM_LIT:1> <EOL> return data <EOL> class PageIterator ( BaseIterator ) : <EOL> def __init__ ( self , method , args , kargs ) : <EOL> BaseIterator . __init__ ( self , method , args , kargs ) <EOL> self . current_page = <NUM_LIT:0> <EOL> def next ( self ) : <EOL> self . current_page += <NUM_LIT:1> <EOL> items = self . method ( page = self . current_page , * self . args , ** self . kargs ) <EOL> if len ( items ) == <NUM_LIT:0> or ( self . limit > <NUM_LIT:0> and self . current_page > self . limit ) : <EOL> raise StopIteration <EOL> return items <EOL> def prev ( self ) : <EOL> if ( self . current_page == <NUM_LIT:1> ) : <EOL> raise TweepError ( '<STR_LIT>' ) <EOL> self . current_page -= <NUM_LIT:1> <EOL> return self . method ( page = self . current_page , * self . args , ** self . kargs ) <EOL> class ItemIterator ( BaseIterator ) : <EOL> def __init__ ( self , page_iterator ) : <EOL> self . page_iterator = page_iterator <EOL> self . limit = <NUM_LIT:0> <EOL> self . current_page = None <EOL> self . page_index = - <NUM_LIT:1> <EOL> self . count = <NUM_LIT:0> <EOL> def next ( self ) : <EOL> if self . limit > <NUM_LIT:0> and self . count == self . limit : <EOL> raise StopIteration <EOL> if self . current_page is None or self . page_index == len ( self . current_page ) - <NUM_LIT:1> : <EOL> self . current_page = self . page_iterator . next ( ) <EOL> self . page_index = - <NUM_LIT:1> <EOL> self . page_index += <NUM_LIT:1> <EOL> self . count += <NUM_LIT:1> <EOL> return self . current_page [ self . page_index ] <EOL> def prev ( self ) : <EOL> if self . current_page is None : <EOL> raise TweepError ( '<STR_LIT>' ) <EOL> if self . page_index == <NUM_LIT:0> : <EOL> self . current_page = self . page_iterator . prev ( ) <EOL> self . page_index = len ( self . current_page ) <EOL> if self . page_index == <NUM_LIT:0> : <EOL> raise TweepError ( '<STR_LIT>' ) <EOL> self . page_index -= <NUM_LIT:1> <EOL> self . count -= <NUM_LIT:1> <EOL> return self . current_page [ self . page_index ] </s>
