<s> """<STR_LIT>""" <EOL> import sys <EOL> from gluon import * <EOL> from . . s3sync import S3SyncBaseAdapter <EOL> from . . s3query import S3URLQuery , FS <EOL> from . . s3rest import S3Request <EOL> from . . s3export import S3Exporter <EOL> try : <EOL> from cStringIO import StringIO <EOL> except : <EOL> from StringIO import StringIO <EOL> class S3SyncAdapter ( S3SyncBaseAdapter ) : <EOL> """<STR_LIT>""" <EOL> def register ( self ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> def login ( self ) : <EOL> """<STR_LIT>""" <EOL> repository = self . repository <EOL> url = repository . url <EOL> error = None <EOL> if not url : <EOL> error = "<STR_LIT>" <EOL> else : <EOL> import ftplib <EOL> try : <EOL> ftp_connection = ftplib . FTP ( url ) <EOL> except ftplib . all_errors : <EOL> error = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> else : <EOL> try : <EOL> ftp_connection . login ( repository . username , repository . password ) <EOL> except ftplib . error_perm : <EOL> error = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> self . ftp_connection = ftp_connection <EOL> if error : <EOL> current . log . debug ( error ) <EOL> return error <EOL> def pull ( self , task , onconflict = None ) : <EOL> """<STR_LIT>""" <EOL> repository = self . repository <EOL> message = "<STR_LIT>" <EOL> log = repository . log <EOL> log . write ( repository_id = repository . id , <EOL> resource_name = task . resource_name , <EOL> transmission = log . OUT , <EOL> mode = log . PULL , <EOL> action = None , <EOL> remote = False , <EOL> result = log . FATAL , <EOL> message = message , <EOL> ) <EOL> return message , None <EOL> def push ( self , task ) : <EOL> """<STR_LIT>""" <EOL> repository = self . repository <EOL> resource_name = task . resource_name <EOL> log = repository . log <EOL> remote = False <EOL> output = None <EOL> current . log . debug ( "<STR_LIT>" % ( repository . url , <EOL> resource_name ) ) <EOL> resource = current . s3db . resource ( resource_name , <EOL> ) <EOL> filters = current . sync . get_filters ( task . id ) <EOL> table = resource . table <EOL> tablename = resource . tablename <EOL> if filters : <EOL> queries = S3URLQuery . parse ( resource , filters [ tablename ] ) <EOL> [ resource . add_filter ( q ) for a in queries for q in queries [ a ] ] <EOL> msince = task . last_push <EOL> if msince : <EOL> strategy = task . strategy <EOL> created = "<STR_LIT>" in strategy <EOL> updated = "<STR_LIT>" in strategy <EOL> if created and updated : <EOL> mtime_filter = table . modified_on > msince <EOL> elif created : <EOL> mtime_filter = table . created_on > msince <EOL> elif updated : <EOL> mtime_filter = ( table . created_on <= msince ) & ( table . modified_on > msince ) <EOL> else : <EOL> mtime_filter = None <EOL> if mtime_filter : <EOL> resource . add_filter ( mtime_filter ) <EOL> mtime = resource . muntil <EOL> resource_ids = resource . get_id ( ) <EOL> if resource_ids is None : <EOL> message = "<STR_LIT>" <EOL> result = log . WARNING <EOL> else : <EOL> settings = current . deployment_settings <EOL> placeholders = { "<STR_LIT>" : settings . get_system_name ( ) , <EOL> "<STR_LIT>" : settings . get_system_name_short ( ) , <EOL> "<STR_LIT>" : resource_name , <EOL> "<STR_LIT>" : settings . get_base_public_url ( ) , <EOL> } <EOL> from string import Template <EOL> filename = resource . get_config ( "<STR_LIT>" ) <EOL> if not filename : <EOL> filename = settings . get_sync_upload_filename ( ) <EOL> filename = Template ( filename ) . safe_substitute ( s = "<STR_LIT>" , <EOL> r = "<STR_LIT>" ) <EOL> filename = filename % placeholders <EOL> representation = task . representation <EOL> filename = ( "<STR_LIT>" ) % ( filename , representation ) <EOL> remote = True <EOL> import ftplib <EOL> ftp_connection = self . ftp_connection <EOL> if task . multiple_file : <EOL> if type ( resource_ids ) is not list : <EOL> resource_ids = [ resource_ids ] <EOL> for resource_id in resource_ids : <EOL> resource . clear_query ( ) <EOL> resource . add_filter ( FS ( "<STR_LIT:id>" ) == resource_id ) <EOL> data = self . _get_data ( resource , representation ) <EOL> try : <EOL> ftp_connection . storbinary ( "<STR_LIT>" % filename , <EOL> StringIO ( data ) ) <EOL> except ftplib . error_perm : <EOL> message = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> result = log . ERROR <EOL> output = message <EOL> else : <EOL> message = "<STR_LIT>" <EOL> result = log . SUCCESS <EOL> current . log . debug ( message ) <EOL> else : <EOL> data = self . _get_data ( resource , representation ) <EOL> try : <EOL> ftp_connection . storbinary ( "<STR_LIT>" % filename , <EOL> StringIO ( data ) ) <EOL> except ftplib . error_perm : <EOL> message = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> result = log . ERROR <EOL> output = message <EOL> else : <EOL> message = "<STR_LIT>" <EOL> result = log . SUCCESS <EOL> current . log . debug ( message ) <EOL> ftp_connection . quit ( ) <EOL> log . write ( repository_id = repository . id , <EOL> resource_name = resource_name , <EOL> transmission = log . OUT , <EOL> mode = log . PUSH , <EOL> action = "<STR_LIT>" , <EOL> remote = remote , <EOL> result = result , <EOL> message = message , <EOL> ) <EOL> if output is not None : <EOL> mtime = None <EOL> return output , mtime <EOL> def _get_data ( self , resource , representation ) : <EOL> """<STR_LIT>""" <EOL> request = S3Request ( prefix = resource . prefix , <EOL> name = resource . name , <EOL> extension = representation , <EOL> ) <EOL> if request . transformable ( ) : <EOL> return resource . export_xml ( stylesheet = request . stylesheet ( ) , <EOL> pretty_print = True , <EOL> ) <EOL> else : <EOL> if representation == "<STR_LIT>" : <EOL> exporter = S3Exporter ( ) . csv <EOL> elif representation == "<STR_LIT>" : <EOL> exporter = S3Exporter ( ) . pdf <EOL> elif representation == "<STR_LIT>" : <EOL> exporter = S3Exporter ( ) . xls <EOL> elif representation == "<STR_LIT>" : <EOL> exporter = S3Exporter ( ) . json <EOL> return exporter ( resource ) </s>
