<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import StringIO <EOL> import pickle <EOL> import atom . http_interface <EOL> import atom . token_store <EOL> from google . appengine . api import urlfetch <EOL> from google . appengine . ext import db <EOL> from google . appengine . api import users <EOL> from google . appengine . api import memcache <EOL> def run_on_appengine ( gdata_service , store_tokens = True , <EOL> single_user_mode = False ) : <EOL> """<STR_LIT>""" <EOL> gdata_service . http_client = AppEngineHttpClient ( ) <EOL> gdata_service . token_store = AppEngineTokenStore ( ) <EOL> gdata_service . auto_store_tokens = store_tokens <EOL> gdata_service . auto_set_current_token = single_user_mode <EOL> return gdata_service <EOL> class AppEngineHttpClient ( atom . http_interface . GenericHttpClient ) : <EOL> def __init__ ( self , headers = None ) : <EOL> self . debug = False <EOL> self . headers = headers or { } <EOL> def request ( self , operation , url , data = None , headers = None ) : <EOL> """<STR_LIT>""" <EOL> all_headers = self . headers . copy ( ) <EOL> if headers : <EOL> all_headers . update ( headers ) <EOL> data_str = data <EOL> if data : <EOL> if isinstance ( data , list ) : <EOL> converted_parts = [ _convert_data_part ( x ) for x in data ] <EOL> data_str = '<STR_LIT>' . join ( converted_parts ) <EOL> else : <EOL> data_str = _convert_data_part ( data ) <EOL> if data and '<STR_LIT>' not in all_headers : <EOL> all_headers [ '<STR_LIT>' ] = str ( len ( data_str ) ) <EOL> if '<STR_LIT:Content-Type>' not in all_headers : <EOL> all_headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> if operation == '<STR_LIT:GET>' : <EOL> method = urlfetch . GET <EOL> elif operation == '<STR_LIT:POST>' : <EOL> method = urlfetch . POST <EOL> elif operation == '<STR_LIT>' : <EOL> method = urlfetch . PUT <EOL> elif operation == '<STR_LIT>' : <EOL> method = urlfetch . DELETE <EOL> else : <EOL> method = None <EOL> return HttpResponse ( urlfetch . Fetch ( url = str ( url ) , payload = data_str , <EOL> method = method , headers = all_headers , follow_redirects = False ) ) <EOL> def _convert_data_part ( data ) : <EOL> if not data or isinstance ( data , str ) : <EOL> return data <EOL> elif hasattr ( data , '<STR_LIT>' ) : <EOL> return data . read ( ) <EOL> return str ( data ) <EOL> class HttpResponse ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , urlfetch_response ) : <EOL> self . body = StringIO . StringIO ( urlfetch_response . content ) <EOL> self . headers = urlfetch_response . headers <EOL> self . status = urlfetch_response . status_code <EOL> self . reason = '<STR_LIT>' <EOL> def read ( self , length = None ) : <EOL> if not length : <EOL> return self . body . read ( ) <EOL> else : <EOL> return self . body . read ( length ) <EOL> def getheader ( self , name ) : <EOL> if not self . headers . has_key ( name ) : <EOL> return self . headers [ name . lower ( ) ] <EOL> return self . headers [ name ] <EOL> class TokenCollection ( db . Model ) : <EOL> """<STR_LIT>""" <EOL> user = db . UserProperty ( ) <EOL> pickled_tokens = db . BlobProperty ( ) <EOL> class AppEngineTokenStore ( atom . token_store . TokenStore ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . user = None <EOL> def add_token ( self , token ) : <EOL> """<STR_LIT>""" <EOL> tokens = load_auth_tokens ( self . user ) <EOL> if not hasattr ( token , '<STR_LIT>' ) or not token . scopes : <EOL> return False <EOL> for scope in token . scopes : <EOL> tokens [ str ( scope ) ] = token <EOL> key = save_auth_tokens ( tokens , self . user ) <EOL> if key : <EOL> return True <EOL> return False <EOL> def find_token ( self , url ) : <EOL> """<STR_LIT>""" <EOL> if url is None : <EOL> return None <EOL> if isinstance ( url , ( str , unicode ) ) : <EOL> url = atom . url . parse_url ( url ) <EOL> tokens = load_auth_tokens ( self . user ) <EOL> if url in tokens : <EOL> token = tokens [ url ] <EOL> if token . valid_for_scope ( url ) : <EOL> return token <EOL> else : <EOL> del tokens [ url ] <EOL> save_auth_tokens ( tokens , self . user ) <EOL> for scope , token in tokens . iteritems ( ) : <EOL> if token . valid_for_scope ( url ) : <EOL> return token <EOL> return atom . http_interface . GenericToken ( ) <EOL> def remove_token ( self , token ) : <EOL> """<STR_LIT>""" <EOL> token_found = False <EOL> scopes_to_delete = [ ] <EOL> tokens = load_auth_tokens ( self . user ) <EOL> for scope , stored_token in tokens . iteritems ( ) : <EOL> if stored_token == token : <EOL> scopes_to_delete . append ( scope ) <EOL> token_found = True <EOL> for scope in scopes_to_delete : <EOL> del tokens [ scope ] <EOL> if token_found : <EOL> save_auth_tokens ( tokens , self . user ) <EOL> return token_found <EOL> def remove_all_tokens ( self ) : <EOL> """<STR_LIT>""" <EOL> save_auth_tokens ( { } , self . user ) <EOL> def save_auth_tokens ( token_dict , user = None ) : <EOL> """<STR_LIT>""" <EOL> if user is None : <EOL> user = users . get_current_user ( ) <EOL> if user is None : <EOL> return None <EOL> memcache . set ( '<STR_LIT>' % user , pickle . dumps ( token_dict ) ) <EOL> user_tokens = TokenCollection . all ( ) . filter ( '<STR_LIT>' , user ) . get ( ) <EOL> if user_tokens : <EOL> user_tokens . pickled_tokens = pickle . dumps ( token_dict ) <EOL> return user_tokens . put ( ) <EOL> else : <EOL> user_tokens = TokenCollection ( <EOL> user = user , <EOL> pickled_tokens = pickle . dumps ( token_dict ) ) <EOL> return user_tokens . put ( ) <EOL> def load_auth_tokens ( user = None ) : <EOL> """<STR_LIT>""" <EOL> if user is None : <EOL> user = users . get_current_user ( ) <EOL> if user is None : <EOL> return { } <EOL> pickled_tokens = memcache . get ( '<STR_LIT>' % user ) <EOL> if pickled_tokens : <EOL> return pickle . loads ( pickled_tokens ) <EOL> user_tokens = TokenCollection . all ( ) . filter ( '<STR_LIT>' , user ) . get ( ) <EOL> if user_tokens : <EOL> memcache . set ( '<STR_LIT>' % user , user_tokens . pickled_tokens ) <EOL> return pickle . loads ( user_tokens . pickled_tokens ) <EOL> return { } </s>
