<s> from __future__ import unicode_literals <EOL> import logging <EOL> from json import dumps <EOL> from urllib import urlencode <EOL> from urllib2 import Request , urlopen <EOL> from datetime import datetime , timedelta <EOL> from flask_protorpc . proto import message_from_json , message_to_json <EOL> from werkzeug import exceptions <EOL> from client . orgs import orgs <EOL> from client . repos import repos <EOL> from client . users import users <EOL> from client . events import events <EOL> from client . issues import issues <EOL> from client . gitdata import gitdata <EOL> from client . pullreqs import pullreqs <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class Github : <EOL> def __init__ ( self , app , config , access_token , username = None , password = None ) : <EOL> if not app : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not access_token : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . app = app <EOL> self . config = config <EOL> self . access_token = access_token <EOL> self . _username = username <EOL> self . _password = password <EOL> self . orgs = orgs . Orgs ( self ) <EOL> self . repos = repos . Repos ( self ) <EOL> self . users = users . Users ( self ) <EOL> self . events = events . Events ( self ) <EOL> self . issues = issues . Issues ( self ) <EOL> self . gitdata = gitdata . GitData ( self ) <EOL> self . pullreqs = pullreqs . PullRequests ( self ) <EOL> def set_auth_type ( self ) : <EOL> return '<STR_LIT>' <EOL> def auth ( self ) : <EOL> return ( self . username ( ) , self . password ( ) ) <EOL> def request ( self , http_verb , path , data = None ) : <EOL> verb = http_verb . upper ( ) <EOL> methodmap = dict ( <EOL> GET = self . get , <EOL> PUT = self . put , <EOL> HEAD = self . head , <EOL> POST = self . post , <EOL> PATCH = self . patch , <EOL> DELETE = self . delete ) <EOL> if verb in methodmap : <EOL> return methodmap [ verb ] ( self . _url ( path ) ) <EOL> else : <EOL> raise ValueError ( <EOL> '<STR_LIT>' ) <EOL> def call ( self , path = None , query = None , data = None , method = '<STR_LIT:GET>' ) : <EOL> if method == '<STR_LIT:POST>' and ( data is None or len ( data ) < <NUM_LIT:1> ) : <EOL> data = '<STR_LIT:{}>' <EOL> result = '<STR_LIT:{}>' <EOL> status = <NUM_LIT:200> <EOL> request = Request ( self . _url ( self . app . base_url + path , query ) , <EOL> data = data , headers = { <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , <EOL> '<STR_LIT>' : '<STR_LIT>' + self . access_token } ) <EOL> rate_limit = <NUM_LIT:0> <EOL> rate_limit_remaining = <NUM_LIT:0> <EOL> user_scopes = [ ] <EOL> accepted_scopes = [ ] <EOL> try : <EOL> response = urlopen ( request ) <EOL> result = response . read ( ) <EOL> response . close ( ) <EOL> status = response . code <EOL> rate_limit = int ( response . headers . dict . get ( <EOL> '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> rate_limit_remaining = int ( response . headers . dict . get ( <EOL> '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> user_scopes = response . headers . dict . get ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) <EOL> accepted_scopes = response . headers . dict . get ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) <EOL> print '<STR_LIT>' % ( rate_limit ) <EOL> print '<STR_LIT>' % ( rate_limit_remaining ) <EOL> print '<STR_LIT>' % ( user_scopes ) <EOL> print '<STR_LIT>' % ( accepted_scopes ) <EOL> except : <EOL> import traceback <EOL> print '<STR_LIT>' % traceback . format_exc ( ) <EOL> if status == <NUM_LIT> : <EOL> raise exceptions . Unauthorized ( '''<STR_LIT>''' % result ) <EOL> elif status == <NUM_LIT> : <EOL> raise exceptions . MethodNotAllowed ( '''<STR_LIT>''' ) <EOL> elif status == <NUM_LIT> : <EOL> raise exceptions . NotFound ( '''<STR_LIT>''' % result ) <EOL> elif status > <NUM_LIT> : <EOL> raise exceptions . BadRequest ( <EOL> '<STR_LIT>' % ( status , result ) ) <EOL> return '<STR_LIT>' + result + '<STR_LIT:}>' <EOL> def head ( self , path , query = None , msg_type = None ) : <EOL> response = self . call ( path , query = query , method = '<STR_LIT>' ) <EOL> return self . _remote ( response , msg_type ) <EOL> def get ( self , path , query = None , msg_type = None ) : <EOL> response = self . call ( path , query = query , method = '<STR_LIT:GET>' ) <EOL> return self . _remote ( response , msg_type ) <EOL> def post ( self , path , query = None , data = None , msg_type = None ) : <EOL> response = self . call ( path , query = query , data = self . msg ( data ) , method = '<STR_LIT:POST>' ) <EOL> return self . _remote ( response , msg_type ) <EOL> def patch ( self , path , query = None , data = None , msg_type = None ) : <EOL> response = self . call ( path , query = query , data = self . msg ( data ) , method = '<STR_LIT>' ) <EOL> return self . _remote ( response , msg_type ) <EOL> def put ( self , path , query = None , data = None , msg_type = None ) : <EOL> response = self . call ( path , query = query , data = self . msg ( data ) , method = '<STR_LIT>' ) <EOL> return self . _remote ( response , msg_type ) <EOL> def delete ( self , path , query = None , data = None , msg_type = None ) : <EOL> response = self . call ( path , query = query , data = self . msg ( data ) , method = '<STR_LIT>' ) <EOL> return self . _remote ( response , msg_type ) <EOL> def _url ( self , path , query = None ) : <EOL> params = { } <EOL> if query and isinstance ( query , dict ) : <EOL> params . update ( query ) <EOL> path += '<STR_LIT>' % urlencode ( params ) <EOL> print '<STR_LIT>' % path <EOL> return path <EOL> def _remote ( self , response , msg_type = None ) : <EOL> if msg_type is not None : <EOL> try : <EOL> return message_from_json ( msg_type , response ) <EOL> except AttributeError , e : <EOL> print '<STR_LIT>' % ( e , response ) <EOL> return msg_type ( ) <EOL> else : <EOL> return dict ( <EOL> headers = dumps ( { } ) , body = response ) <EOL> def msg ( self , msg ) : <EOL> if msg : <EOL> return message_to_json ( msg ) <EOL> def message_to_json ( self , * args , ** kwargs ) : <EOL> return message_to_json ( * args , ** kwargs ) <EOL> def message_from_json ( self , * args , ** kwargs ) : <EOL> return message_from_json ( * args , ** kwargs ) <EOL> def username ( self , user ) : <EOL> return self . user ( user ) <EOL> def user ( self , user ) : <EOL> return self . _username if user is None else user <EOL> def password ( self ) : <EOL> return self . _password <EOL> def to_dt ( self , value ) : <EOL> '''<STR_LIT>''' <EOL> dt , _ , us = value . partition ( '<STR_LIT:.>' ) <EOL> dt = datetime . strptime ( dt , '<STR_LIT>' ) <EOL> return dt + timedelta ( microseconds = int ( us . rstrip ( '<STR_LIT>' ) , <NUM_LIT:10> ) ) <EOL> def from_dt ( self , value ) : <EOL> '''<STR_LIT>''' <EOL> return value . isoformat ( ) </s>
