<s> import collections <EOL> import functools <EOL> from requests . compat import urlparse , urlencode <EOL> from . import exceptions <EOL> from . import models <EOL> class GitHubIterator ( models . GitHubCore , collections . Iterator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , count , url , cls , session , params = None , etag = None , <EOL> headers = None ) : <EOL> models . GitHubCore . __init__ ( self , { } , session ) <EOL> self . original = count <EOL> self . count = count <EOL> self . url = url <EOL> self . last_url = None <EOL> self . _api = self . url <EOL> self . cls = cls <EOL> self . params = params or { } <EOL> self . _remove_none ( self . params ) <EOL> self . etag = None <EOL> self . headers = headers or { } <EOL> self . last_response = None <EOL> self . last_status = <NUM_LIT:0> <EOL> if etag : <EOL> self . headers . update ( { '<STR_LIT>' : etag } ) <EOL> self . path = urlparse ( self . url ) . path <EOL> def _repr ( self ) : <EOL> return '<STR_LIT>' . format ( self . count , self . path ) <EOL> def __iter__ ( self ) : <EOL> self . last_url , params = self . url , self . params <EOL> headers = self . headers <EOL> if <NUM_LIT:0> < self . count <= <NUM_LIT:100> and self . count != - <NUM_LIT:1> : <EOL> params [ '<STR_LIT>' ] = self . count <EOL> if '<STR_LIT>' not in params and self . count == - <NUM_LIT:1> : <EOL> params [ '<STR_LIT>' ] = <NUM_LIT:100> <EOL> cls = self . cls <EOL> if issubclass ( self . cls , models . GitHubCore ) : <EOL> cls = functools . partial ( self . cls , session = self ) <EOL> while ( self . count == - <NUM_LIT:1> or self . count > <NUM_LIT:0> ) and self . last_url : <EOL> response = self . _get ( self . last_url , params = params , <EOL> headers = headers ) <EOL> self . last_response = response <EOL> self . last_status = response . status_code <EOL> if params : <EOL> params = None <EOL> if not self . etag and response . headers . get ( '<STR_LIT>' ) : <EOL> self . etag = response . headers . get ( '<STR_LIT>' ) <EOL> json = self . _get_json ( response ) <EOL> if json is None : <EOL> break <EOL> if isinstance ( json , dict ) : <EOL> if issubclass ( self . cls , models . GitHubCore ) : <EOL> raise exceptions . UnprocessableResponseBody ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , json <EOL> ) <EOL> if json . get ( '<STR_LIT>' ) : <EOL> del json [ '<STR_LIT>' ] <EOL> if json . get ( '<STR_LIT>' ) : <EOL> del json [ '<STR_LIT>' ] <EOL> json = json . items ( ) <EOL> for i in json : <EOL> yield cls ( i ) <EOL> self . count -= <NUM_LIT:1> if self . count > <NUM_LIT:0> else <NUM_LIT:0> <EOL> if self . count == <NUM_LIT:0> : <EOL> break <EOL> rel_next = response . links . get ( '<STR_LIT>' , { } ) <EOL> self . last_url = rel_next . get ( '<STR_LIT:url>' , '<STR_LIT>' ) <EOL> def __next__ ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . __i__ = self . __iter__ ( ) <EOL> return next ( self . __i__ ) <EOL> def _get_json ( self , response ) : <EOL> return self . _json ( response , <NUM_LIT:200> ) <EOL> def refresh ( self , conditional = False ) : <EOL> self . count = self . original <EOL> if conditional : <EOL> self . headers [ '<STR_LIT>' ] = self . etag <EOL> self . etag = None <EOL> self . __i__ = self . __iter__ ( ) <EOL> return self <EOL> def next ( self ) : <EOL> return self . __next__ ( ) <EOL> class SearchIterator ( GitHubIterator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , count , url , cls , session , params = None , etag = None , <EOL> headers = None ) : <EOL> super ( SearchIterator , self ) . __init__ ( count , url , cls , session , params , <EOL> etag , headers ) <EOL> self . total_count = <NUM_LIT:0> <EOL> self . items = [ ] <EOL> def _repr ( self ) : <EOL> return '<STR_LIT>' . format ( self . count , self . path , <EOL> urlencode ( self . params ) ) <EOL> def _get_json ( self , response ) : <EOL> json = self . _json ( response , <NUM_LIT:200> ) <EOL> self . total_count = json . get ( '<STR_LIT>' , self . total_count ) <EOL> self . items = json . get ( '<STR_LIT>' , [ ] ) <EOL> return json . get ( '<STR_LIT>' ) </s>
