<s> '''<STR_LIT>''' <EOL> __version__ = '<STR_LIT>' <EOL> try : <EOL> from urllib2 import build_opener , HTTPSHandler , Request , HTTPError <EOL> from urllib import quote as urlquote <EOL> from StringIO import StringIO <EOL> def bytes ( string , encoding = None ) : <EOL> return str ( string ) <EOL> except : <EOL> from urllib . request import build_opener , HTTPSHandler , HTTPError , Request <EOL> from urllib . parse import quote as urlquote <EOL> from io import StringIO <EOL> import re , os , time , hmac , base64 , hashlib , urllib , mimetypes , json <EOL> from collections import Iterable <EOL> from datetime import datetime , timedelta , tzinfo <EOL> TIMEOUT = <NUM_LIT> <EOL> _URL = '<STR_LIT>' <EOL> _METHOD_MAP = dict ( <EOL> GET = lambda : '<STR_LIT:GET>' , <EOL> PUT = lambda : '<STR_LIT>' , <EOL> POST = lambda : '<STR_LIT:POST>' , <EOL> PATCH = lambda : '<STR_LIT>' , <EOL> DELETE = lambda : '<STR_LIT>' ) <EOL> DEFAULT_SCOPE = None <EOL> RW_SCOPE = '<STR_LIT>' <EOL> def _encode_params ( kw ) : <EOL> '''<STR_LIT>''' <EOL> args = [ ] <EOL> for k , v in kw . items ( ) : <EOL> try : <EOL> qv = v . encode ( '<STR_LIT:utf-8>' ) if isinstance ( v , unicode ) else str ( v ) <EOL> except : <EOL> qv = v <EOL> args . append ( '<STR_LIT>' % ( k , urlquote ( qv ) ) ) <EOL> return '<STR_LIT:&>' . join ( args ) <EOL> def _encode_json ( obj ) : <EOL> '''<STR_LIT>''' <EOL> def _dump_obj ( obj ) : <EOL> if isinstance ( obj , dict ) : <EOL> return obj <EOL> d = dict ( ) <EOL> for k in dir ( obj ) : <EOL> if not k . startswith ( '<STR_LIT:_>' ) : <EOL> d [ k ] = getattr ( obj , k ) <EOL> return d <EOL> return json . dumps ( obj , default = _dump_obj ) <EOL> def _parse_json ( jsonstr ) : <EOL> def _obj_hook ( pairs ) : <EOL> o = JsonObject ( ) <EOL> for k , v in pairs . items ( ) : <EOL> o [ str ( k ) ] = v <EOL> return o <EOL> return json . loads ( jsonstr , object_hook = _obj_hook ) <EOL> class _Executable ( object ) : <EOL> def __init__ ( self , _gh , _method , _path ) : <EOL> self . _gh = _gh <EOL> self . _method = _method <EOL> self . _path = _path <EOL> def __call__ ( self , ** kw ) : <EOL> return self . _gh . _http ( self . _method , self . _path , ** kw ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . _method , self . _path ) <EOL> __repr__ = __str__ <EOL> class _Callable ( object ) : <EOL> def __init__ ( self , _gh , _name ) : <EOL> self . _gh = _gh <EOL> self . _name = _name <EOL> def __call__ ( self , * args ) : <EOL> if len ( args ) == <NUM_LIT:0> : <EOL> return self <EOL> name = '<STR_LIT>' % ( self . _name , '<STR_LIT:/>' . join ( [ str ( arg ) for arg in args ] ) ) <EOL> return _Callable ( self . _gh , name ) <EOL> def __getattr__ ( self , attr ) : <EOL> if attr == '<STR_LIT>' : <EOL> return _Executable ( self . _gh , '<STR_LIT:GET>' , self . _name ) <EOL> if attr == '<STR_LIT>' : <EOL> return _Executable ( self . _gh , '<STR_LIT>' , self . _name ) <EOL> if attr == '<STR_LIT>' : <EOL> return _Executable ( self . _gh , '<STR_LIT:POST>' , self . _name ) <EOL> if attr == '<STR_LIT>' : <EOL> return _Executable ( self . _gh , '<STR_LIT>' , self . _name ) <EOL> if attr == '<STR_LIT>' : <EOL> return _Executable ( self . _gh , '<STR_LIT>' , self . _name ) <EOL> name = '<STR_LIT>' % ( self . _name , attr ) <EOL> return _Callable ( self . _gh , name ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % self . _name <EOL> __repr__ = __str__ <EOL> class GitHub ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , username = None , password = None , access_token = None , client_id = None , client_secret = None , redirect_uri = None , scope = None ) : <EOL> self . x_ratelimit_remaining = ( - <NUM_LIT:1> ) <EOL> self . x_ratelimit_limit = ( - <NUM_LIT:1> ) <EOL> self . x_ratelimit_reset = ( - <NUM_LIT:1> ) <EOL> self . _authorization = None <EOL> if username and password : <EOL> userandpass = base64 . b64encode ( bytes ( '<STR_LIT>' % ( username , password ) , '<STR_LIT:utf-8>' ) ) <EOL> userandpass = userandpass . decode ( '<STR_LIT:ascii>' ) <EOL> self . _authorization = '<STR_LIT>' % userandpass <EOL> elif access_token : <EOL> self . _authorization = '<STR_LIT>' % access_token <EOL> self . _client_id = client_id <EOL> self . _client_secret = client_secret <EOL> self . _redirect_uri = redirect_uri <EOL> self . _scope = scope <EOL> def authorize_url ( self , state = None ) : <EOL> '''<STR_LIT>''' <EOL> if not self . _client_id : <EOL> raise ApiAuthError ( '<STR_LIT>' ) <EOL> kw = dict ( client_id = self . _client_id ) <EOL> if self . _redirect_uri : <EOL> kw [ '<STR_LIT>' ] = self . _redirect_uri <EOL> if self . _scope : <EOL> kw [ '<STR_LIT>' ] = self . _scope <EOL> if state : <EOL> kw [ '<STR_LIT:state>' ] = state <EOL> return '<STR_LIT>' % _encode_params ( kw ) <EOL> def get_access_token ( self , code , state = None ) : <EOL> '''<STR_LIT>''' <EOL> kw = dict ( client_id = self . _client_id , client_secret = self . _client_secret , code = code ) <EOL> if self . _redirect_uri : <EOL> kw [ '<STR_LIT>' ] = self . _redirect_uri <EOL> if state : <EOL> kw [ '<STR_LIT:state>' ] = state <EOL> opener = build_opener ( HTTPSHandler ) <EOL> request = Request ( '<STR_LIT>' , data = _encode_params ( kw ) ) <EOL> request . get_method = _METHOD_MAP [ '<STR_LIT:POST>' ] <EOL> request . add_header ( '<STR_LIT>' , '<STR_LIT:application/json>' ) <EOL> try : <EOL> response = opener . open ( request , timeout = TIMEOUT ) <EOL> r = _parse_json ( response . read ( ) ) <EOL> if '<STR_LIT:error>' in r : <EOL> raise ApiAuthError ( str ( r . error ) ) <EOL> return str ( r . access_token ) <EOL> except HTTPError as e : <EOL> raise ApiAuthError ( '<STR_LIT>' ) <EOL> def __getattr__ ( self , attr ) : <EOL> return _Callable ( self , '<STR_LIT>' % attr ) <EOL> def _http ( self , _method , _path , ** kw ) : <EOL> data = None <EOL> params = None <EOL> if _method == '<STR_LIT:GET>' and kw : <EOL> _path = '<STR_LIT>' % ( _path , _encode_params ( kw ) ) <EOL> if _method in [ '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> data = bytes ( _encode_json ( kw ) , '<STR_LIT:utf-8>' ) <EOL> url = '<STR_LIT>' % ( _URL , _path ) <EOL> opener = build_opener ( HTTPSHandler ) <EOL> request = Request ( url , data = data ) <EOL> request . get_method = _METHOD_MAP [ _method ] <EOL> if self . _authorization : <EOL> request . add_header ( '<STR_LIT>' , self . _authorization ) <EOL> if _method in [ '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> request . add_header ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) <EOL> try : <EOL> response = opener . open ( request , timeout = TIMEOUT ) <EOL> is_json = self . _process_resp ( response . headers ) <EOL> if is_json : <EOL> return _parse_json ( response . read ( ) . decode ( '<STR_LIT:utf-8>' ) ) <EOL> except HTTPError as e : <EOL> is_json = self . _process_resp ( e . headers ) <EOL> if is_json : <EOL> json = _parse_json ( e . read ( ) . decode ( '<STR_LIT:utf-8>' ) ) <EOL> else : <EOL> json = e . read ( ) . decode ( '<STR_LIT:utf-8>' ) <EOL> req = JsonObject ( method = _method , url = url ) <EOL> resp = JsonObject ( code = e . code , json = json ) <EOL> if resp . code == <NUM_LIT> : <EOL> raise ApiNotFoundError ( url , req , resp ) <EOL> raise ApiError ( url , req , resp ) <EOL> def _process_resp ( self , headers ) : <EOL> is_json = False <EOL> if headers : <EOL> for k in headers : <EOL> h = k . lower ( ) <EOL> if h == '<STR_LIT>' : <EOL> self . x_ratelimit_remaining = int ( headers [ k ] ) <EOL> elif h == '<STR_LIT>' : <EOL> self . x_ratelimit_limit = int ( headers [ k ] ) <EOL> elif h == '<STR_LIT>' : <EOL> self . x_ratelimit_reset = int ( headers [ k ] ) <EOL> elif h == '<STR_LIT>' : <EOL> is_json = headers [ k ] . startswith ( '<STR_LIT:application/json>' ) <EOL> return is_json <EOL> class JsonObject ( dict ) : <EOL> '''<STR_LIT>''' <EOL> def __getattr__ ( self , key ) : <EOL> try : <EOL> return self [ key ] <EOL> except KeyError : <EOL> raise AttributeError ( r"<STR_LIT>" % key ) <EOL> def __setattr__ ( self , attr , value ) : <EOL> self [ attr ] = value <EOL> class ApiError ( Exception ) : <EOL> def __init__ ( self , url , request , response ) : <EOL> super ( ApiError , self ) . __init__ ( url ) <EOL> self . request = request <EOL> self . response = response <EOL> class ApiAuthError ( ApiError ) : <EOL> def __init__ ( self , msg ) : <EOL> super ( ApiAuthError , self ) . __init__ ( msg , None , None ) <EOL> class ApiNotFoundError ( ApiError ) : <EOL> pass <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import doctest <EOL> doctest . testmod ( ) </s>
