<s> """<STR_LIT>""" <EOL> import sys <EOL> import urllib2 <EOL> import traceback <EOL> try : <EOL> from lxml import etree <EOL> except ImportError : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> raise <EOL> try : <EOL> import json <EOL> except ImportError : <EOL> try : <EOL> import simplejson as json <EOL> except : <EOL> import gluon . contrib . simplejson as json <EOL> from gluon import * <EOL> from . . s3datetime import s3_encode_iso_datetime <EOL> from . . s3sync import S3SyncBaseAdapter <EOL> from . . s3utils import S3ModuleDebug <EOL> DEBUG = False <EOL> if DEBUG : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> _debug = S3ModuleDebug . on <EOL> else : <EOL> _debug = S3ModuleDebug . off <EOL> class S3SyncAdapter ( S3SyncBaseAdapter ) : <EOL> """<STR_LIT>""" <EOL> def register ( self ) : <EOL> """<STR_LIT>""" <EOL> repository = self . repository <EOL> if not repository . url : <EOL> return True <EOL> current . log . debug ( "<STR_LIT>" % ( repository . url ) ) <EOL> config = repository . config <EOL> url = "<STR_LIT>" % ( repository . url , config . uuid ) <EOL> current . log . debug ( "<STR_LIT>" % url ) <EOL> req = urllib2 . Request ( url = url ) <EOL> handlers = [ ] <EOL> proxy = repository . proxy or config . proxy or None <EOL> if proxy : <EOL> proxy_handler = urllib2 . ProxyHandler ( { "<STR_LIT:http>" : proxy } ) <EOL> handlers . append ( proxy_handler ) <EOL> username = repository . username <EOL> password = repository . password <EOL> if username and password : <EOL> import base64 <EOL> base64string = base64 . encodestring ( '<STR_LIT>' % <EOL> ( username , password ) ) [ : - <NUM_LIT:1> ] <EOL> req . add_header ( "<STR_LIT>" , "<STR_LIT>" % base64string ) <EOL> passwd_manager = urllib2 . HTTPPasswordMgrWithDefaultRealm ( ) <EOL> passwd_manager . add_password ( realm = None , <EOL> uri = url , <EOL> user = username , <EOL> passwd = password , <EOL> ) <EOL> auth_handler = urllib2 . HTTPBasicAuthHandler ( passwd_manager ) <EOL> handlers . append ( auth_handler ) <EOL> if handlers : <EOL> opener = urllib2 . build_opener ( * handlers ) <EOL> urllib2 . install_opener ( opener ) <EOL> log = repository . log <EOL> success = True <EOL> remote = False <EOL> try : <EOL> f = urllib2 . urlopen ( req ) <EOL> except urllib2 . HTTPError , e : <EOL> result = log . FATAL <EOL> remote = True <EOL> code = e . code <EOL> message = e . read ( ) <EOL> success = False <EOL> try : <EOL> message_json = json . loads ( message ) <EOL> message = message_json . get ( "<STR_LIT:message>" , message ) <EOL> except : <EOL> pass <EOL> except : <EOL> result = log . FATAL <EOL> code = <NUM_LIT> <EOL> message = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> success = False <EOL> else : <EOL> ruid = None <EOL> message = f . read ( ) <EOL> try : <EOL> message_json = json . loads ( message ) <EOL> message = message_json . get ( "<STR_LIT:message>" , message ) <EOL> ruid = message_json . get ( "<STR_LIT>" , None ) <EOL> except : <EOL> message = "<STR_LIT>" <EOL> result = log . SUCCESS <EOL> if ruid is not None : <EOL> db = current . db <EOL> rtable = current . s3db . sync_repository <EOL> try : <EOL> db ( rtable . id == repository . id ) . update ( uuid = ruid ) <EOL> except : <EOL> pass <EOL> log . write ( repository_id = repository . id , <EOL> transmission = log . OUT , <EOL> mode = log . PUSH , <EOL> action = "<STR_LIT>" , <EOL> remote = remote , <EOL> result = result , <EOL> message = message , <EOL> ) <EOL> return success <EOL> def login ( self ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def pull ( self , task , onconflict = None ) : <EOL> """<STR_LIT>""" <EOL> repository = self . repository <EOL> xml = current . xml <EOL> config = repository . config <EOL> resource_name = task . resource_name <EOL> current . log . debug ( "<STR_LIT>" % ( resource_name , <EOL> repository . url ) ) <EOL> url = "<STR_LIT>" % ( repository . url , resource_name , config . uuid ) <EOL> last_pull = task . last_pull <EOL> if last_pull and task . update_policy not in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> url += "<STR_LIT>" % s3_encode_iso_datetime ( last_pull ) <EOL> url += "<STR_LIT>" <EOL> filters = current . sync . get_filters ( task . id ) <EOL> filter_string = None <EOL> resource_name = task . resource_name <EOL> for tablename in filters : <EOL> prefix = "<STR_LIT>" if not tablename or tablename == resource_name else tablename <EOL> for k , v in filters [ tablename ] . items ( ) : <EOL> urlfilter = "<STR_LIT>" % ( prefix , k , v ) <EOL> url += "<STR_LIT>" % urlfilter <EOL> url_split = url . split ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> if len ( url_split ) == <NUM_LIT:2> : <EOL> protocol , path = url_split <EOL> else : <EOL> protocol , path = "<STR_LIT:http>" , None <EOL> req = urllib2 . Request ( url = url ) <EOL> handlers = [ ] <EOL> proxy = repository . proxy or config . proxy or None <EOL> if proxy : <EOL> current . log . debug ( "<STR_LIT>" % proxy ) <EOL> proxy_handler = urllib2 . ProxyHandler ( { protocol : proxy } ) <EOL> handlers . append ( proxy_handler ) <EOL> username = repository . username <EOL> password = repository . password <EOL> if username and password : <EOL> import base64 <EOL> base64string = base64 . encodestring ( '<STR_LIT>' % <EOL> ( username , password ) ) [ : - <NUM_LIT:1> ] <EOL> req . add_header ( "<STR_LIT>" , "<STR_LIT>" % base64string ) <EOL> passwd_manager = urllib2 . HTTPPasswordMgrWithDefaultRealm ( ) <EOL> passwd_manager . add_password ( realm = None , <EOL> uri = url , <EOL> user = username , <EOL> passwd = password ) <EOL> auth_handler = urllib2 . HTTPBasicAuthHandler ( passwd_manager ) <EOL> handlers . append ( auth_handler ) <EOL> if handlers : <EOL> opener = urllib2 . build_opener ( * handlers ) <EOL> urllib2 . install_opener ( opener ) <EOL> remote = False <EOL> action = "<STR_LIT>" <EOL> response = None <EOL> output = None <EOL> log = repository . log <EOL> try : <EOL> f = urllib2 . urlopen ( req ) <EOL> except urllib2 . HTTPError , e : <EOL> result = log . ERROR <EOL> remote = True <EOL> code = e . code <EOL> message = e . read ( ) <EOL> try : <EOL> message_json = json . loads ( message ) <EOL> message = message_json . get ( "<STR_LIT:message>" , message ) <EOL> except : <EOL> pass <EOL> message = "<STR_LIT>" % message <EOL> try : <EOL> markup = etree . XML ( message ) <EOL> message = markup . xpath ( "<STR_LIT>" ) <EOL> if message : <EOL> message = "<STR_LIT:U+0020>" . join ( message ) <EOL> else : <EOL> message = "<STR_LIT>" <EOL> except etree . XMLSyntaxError : <EOL> pass <EOL> output = xml . json_message ( False , code , message , tree = None ) <EOL> except : <EOL> result = log . FATAL <EOL> code = <NUM_LIT> <EOL> message = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> output = xml . json_message ( False , code , message ) <EOL> else : <EOL> result = log . SUCCESS <EOL> response = f <EOL> mtime = None <EOL> if response : <EOL> strategy = task . strategy <EOL> update_policy = task . update_policy <EOL> conflict_policy = task . conflict_policy <EOL> success = True <EOL> message = "<STR_LIT>" <EOL> action = "<STR_LIT>" <EOL> resource = current . s3db . resource ( resource_name ) <EOL> if onconflict : <EOL> onconflict_callback = lambda item : onconflict ( item , <EOL> repository , <EOL> resource ) <EOL> else : <EOL> onconflict_callback = None <EOL> count = <NUM_LIT:0> <EOL> try : <EOL> success = resource . import_xml ( <EOL> response , <EOL> ignore_errors = True , <EOL> strategy = strategy , <EOL> update_policy = update_policy , <EOL> conflict_policy = conflict_policy , <EOL> last_sync = last_pull , <EOL> onconflict = onconflict_callback , <EOL> ) <EOL> count = resource . import_count <EOL> except IOError , e : <EOL> result = log . FATAL <EOL> message = "<STR_LIT:%s>" % e <EOL> output = xml . json_message ( False , <NUM_LIT> , message ) <EOL> except Exception , e : <EOL> result = log . FATAL <EOL> message = "<STR_LIT>" % traceback . format_exc ( ) <EOL> output = xml . json_message ( False , <NUM_LIT> , sys . exc_info ( ) [ <NUM_LIT:1> ] ) <EOL> mtime = resource . mtime <EOL> if resource . error_tree is not None : <EOL> result = log . WARNING <EOL> message = "<STR_LIT:%s>" % resource . error <EOL> for element in resource . error_tree . findall ( "<STR_LIT>" ) : <EOL> for field in element . findall ( "<STR_LIT>" ) : <EOL> error_msg = field . get ( "<STR_LIT:error>" , None ) <EOL> if error_msg : <EOL> msg = "<STR_LIT>" % ( element . get ( "<STR_LIT>" , None ) , <EOL> element . get ( "<STR_LIT:name>" , None ) , <EOL> field . get ( "<STR_LIT>" , None ) , <EOL> field . get ( "<STR_LIT:value>" , field . text ) , <EOL> field . get ( "<STR_LIT:error>" , None ) ) <EOL> message = "<STR_LIT>" % ( message , msg ) <EOL> if not success : <EOL> result = log . FATAL <EOL> if not message : <EOL> message = "<STR_LIT:%s>" % resource . error <EOL> output = xml . json_message ( False , <NUM_LIT> , message ) <EOL> mtime = None <EOL> elif not message : <EOL> message = "<STR_LIT>" % count <EOL> elif result == log . SUCCESS : <EOL> result = log . ERROR <EOL> remote = True <EOL> message = "<STR_LIT>" <EOL> log . write ( repository_id = repository . id , <EOL> resource_name = task . resource_name , <EOL> transmission = log . OUT , <EOL> mode = log . PULL , <EOL> action = action , <EOL> remote = remote , <EOL> result = result , <EOL> message = message ) <EOL> current . log . debug ( "<STR_LIT>" % ( result , message ) ) <EOL> return ( output , mtime ) <EOL> def push ( self , task ) : <EOL> """<STR_LIT>""" <EOL> xml = current . xml <EOL> repository = self . repository <EOL> config = repository . config <EOL> resource_name = task . resource_name <EOL> _debug ( "<STR_LIT>" , repository . url , resource_name ) <EOL> url = "<STR_LIT>" % ( repository . url , resource_name , config . uuid ) <EOL> strategy = task . strategy <EOL> if strategy : <EOL> url += "<STR_LIT>" % "<STR_LIT:U+002C>" . join ( strategy ) <EOL> update_policy = task . update_policy <EOL> if update_policy : <EOL> url += "<STR_LIT>" % update_policy <EOL> conflict_policy = task . conflict_policy <EOL> if conflict_policy : <EOL> url += "<STR_LIT>" % conflict_policy <EOL> last_push = task . last_push <EOL> if last_push and update_policy not in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> url += "<STR_LIT>" % s3_encode_iso_datetime ( last_push ) <EOL> else : <EOL> last_push = None <EOL> _debug ( "<STR_LIT>" , url ) <EOL> resource = current . s3db . resource ( resource_name , <EOL> include_deleted = True ) <EOL> filters = current . sync . get_filters ( task . id ) <EOL> data = resource . export_xml ( filters = filters , <EOL> msince = last_push ) <EOL> count = resource . results or <NUM_LIT:0> <EOL> mtime = resource . muntil <EOL> remote = False <EOL> output = None <EOL> log = repository . log <EOL> if data and count : <EOL> url_split = url . split ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> if len ( url_split ) == <NUM_LIT:2> : <EOL> protocol , path = url_split <EOL> else : <EOL> protocol , path = "<STR_LIT:http>" , None <EOL> import urllib2 <EOL> req = urllib2 . Request ( url = url , data = data ) <EOL> req . add_header ( '<STR_LIT:Content-Type>' , "<STR_LIT>" ) <EOL> handlers = [ ] <EOL> proxy = repository . proxy or config . proxy or None <EOL> if proxy : <EOL> _debug ( "<STR_LIT>" , proxy ) <EOL> proxy_handler = urllib2 . ProxyHandler ( { protocol : proxy } ) <EOL> handlers . append ( proxy_handler ) <EOL> username = repository . username <EOL> password = repository . password <EOL> if username and password : <EOL> import base64 <EOL> base64string = base64 . encodestring ( '<STR_LIT>' % <EOL> ( username , password ) ) [ : - <NUM_LIT:1> ] <EOL> req . add_header ( "<STR_LIT>" , "<STR_LIT>" % base64string ) <EOL> passwd_manager = urllib2 . HTTPPasswordMgrWithDefaultRealm ( ) <EOL> passwd_manager . add_password ( realm = None , <EOL> uri = url , <EOL> user = username , <EOL> passwd = password ) <EOL> auth_handler = urllib2 . HTTPBasicAuthHandler ( passwd_manager ) <EOL> handlers . append ( auth_handler ) <EOL> if handlers : <EOL> opener = urllib2 . build_opener ( * handlers ) <EOL> urllib2 . install_opener ( opener ) <EOL> try : <EOL> f = urllib2 . urlopen ( req ) <EOL> except urllib2 . HTTPError , e : <EOL> result = log . FATAL <EOL> remote = True <EOL> code = e . code <EOL> message = e . read ( ) <EOL> try : <EOL> message_json = json . loads ( message ) <EOL> message = message_json . get ( "<STR_LIT:message>" , message ) <EOL> except : <EOL> pass <EOL> output = xml . json_message ( False , code , message ) <EOL> except : <EOL> result = log . FATAL <EOL> code = <NUM_LIT> <EOL> message = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> output = xml . json_message ( False , code , message ) <EOL> else : <EOL> result = log . SUCCESS <EOL> message = "<STR_LIT>" % count <EOL> else : <EOL> result = log . WARNING <EOL> message = "<STR_LIT>" <EOL> log . write ( repository_id = repository . id , <EOL> resource_name = task . resource_name , <EOL> transmission = log . OUT , <EOL> mode = log . PUSH , <EOL> action = "<STR_LIT>" , <EOL> remote = remote , <EOL> result = result , <EOL> message = message ) <EOL> if output is not None : <EOL> mtime = None <EOL> return ( output , mtime ) <EOL> def send ( self , <EOL> resource , <EOL> start = None , <EOL> limit = None , <EOL> msince = None , <EOL> filters = None , <EOL> mixed = False , <EOL> pretty_print = False ) : <EOL> """<STR_LIT>""" <EOL> if not resource or mixed : <EOL> msg = "<STR_LIT>" <EOL> return { "<STR_LIT:status>" : self . log . FATAL , <EOL> "<STR_LIT:message>" : msg , <EOL> "<STR_LIT>" : current . xml . json_message ( False , <NUM_LIT> , msg ) , <EOL> } <EOL> output = resource . export_xml ( start = start , <EOL> limit = limit , <EOL> filters = filters , <EOL> msince = msince , <EOL> pretty_print = pretty_print , <EOL> ) <EOL> count = resource . results <EOL> msg = "<STR_LIT>" % count <EOL> headers = current . response . headers <EOL> headers [ "<STR_LIT:Content-Type>" ] = "<STR_LIT>" <EOL> return { "<STR_LIT:status>" : self . log . SUCCESS , <EOL> "<STR_LIT:message>" : msg , <EOL> "<STR_LIT>" : output , <EOL> } <EOL> def receive ( self , <EOL> source , <EOL> resource , <EOL> strategy = None , <EOL> update_policy = None , <EOL> conflict_policy = None , <EOL> onconflict = None , <EOL> last_sync = None , <EOL> mixed = False ) : <EOL> """<STR_LIT>""" <EOL> if not resource or mixed : <EOL> msg = "<STR_LIT>" <EOL> return { "<STR_LIT:status>" : self . log . FATAL , <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT:message>" : msg , <EOL> "<STR_LIT>" : current . xml . json_message ( False , <NUM_LIT> , msg ) , <EOL> } <EOL> repository = self . repository <EOL> ignore_errors = True <EOL> if onconflict : <EOL> onconflict_callback = lambda item : onconflict ( item , <EOL> repository , <EOL> resource ) <EOL> else : <EOL> onconflict_callback = None <EOL> output = resource . import_xml ( source , format = "<STR_LIT>" , <EOL> ignore_errors = ignore_errors , <EOL> strategy = strategy , <EOL> update_policy = update_policy , <EOL> conflict_policy = conflict_policy , <EOL> last_sync = last_sync , <EOL> onconflict = onconflict_callback , <EOL> ) <EOL> log = self . log <EOL> if resource . error_tree is not None : <EOL> if ignore_errors : <EOL> result = log . WARNING <EOL> else : <EOL> result = log . FATAL <EOL> remote = True <EOL> message = "<STR_LIT:%s>" % resource . error <EOL> for element in resource . error_tree . findall ( "<STR_LIT>" ) : <EOL> error_msg = element . get ( "<STR_LIT:error>" , "<STR_LIT>" ) <EOL> error_fields = element . findall ( "<STR_LIT>" ) <EOL> if error_fields : <EOL> for field in error_fields : <EOL> error_msg = field . get ( "<STR_LIT:error>" , "<STR_LIT>" ) <EOL> if error_msg : <EOL> msg = "<STR_LIT>" % ( element . get ( "<STR_LIT>" , None ) , <EOL> element . get ( "<STR_LIT:name>" , None ) , <EOL> field . get ( "<STR_LIT>" , None ) , <EOL> field . get ( "<STR_LIT:value>" , field . text ) , <EOL> error_msg ) <EOL> message = "<STR_LIT>" % ( message , msg ) <EOL> else : <EOL> msg = "<STR_LIT>" % ( element . get ( "<STR_LIT>" , None ) , <EOL> element . get ( "<STR_LIT:name>" , None ) , <EOL> error_msg ) <EOL> message = "<STR_LIT>" % ( message , msg ) <EOL> else : <EOL> result = log . SUCCESS <EOL> remote = False <EOL> message = "<STR_LIT>" <EOL> return { "<STR_LIT:status>" : result , <EOL> "<STR_LIT>" : remote , <EOL> "<STR_LIT:message>" : message , <EOL> "<STR_LIT>" : output , <EOL> } </s>
