예제 #1
0
 def __init__( self, logger = None ):
     self.logger = logger or logging.getLogger( __name__ )
     self._shutdown = ShutdownEnum.no
     self._job_manager = JobManagerFactory().make("UploadJobManager")
     self._uploads_list = []
예제 #2
0
class Control(IControl):
    __name__ = "Control"
    __version__ = "0.1"
    __description__ = """Control"""
    __author_name__ = "Juan Manuel Fernández García-Minguillán"
    __author_mail__ = "*****@*****.**"
    __metaclass__ = Singleton

    def __init__( self, logger = None ):
        self.logger = logger or logging.getLogger( __name__ )
        self._shutdown = ShutdownEnum.no
        self._job_manager = JobManagerFactory().make("UploadJobManager")
        self._uploads_list = []

    def upload( self, host_name, file_path_to_upload, file_path_remote, accounts_id_list = [],  priority = 5 ):
        self.logger.info( "Adjuntada la subida del fichero '%s' al host '%s'", file_path_to_upload, host_name )

        account_manager = AccountDBManager()

        account_list = []
        if accounts_id_list == []:
            #TODO get all the accounts
            for account in account_manager.get_account_list_of_host( host_name ):
                account_list.append( account )
        else:
            for id in accounts_id_list:
                account_list.append( account_manager.get_account_with_hostname_user( id ) )

        uploader = UploaderFactory().make( host_name + "Uploader", account_list, file_path_to_upload, file_path_remote )

        job = JobFactory().make("UploadJob", uploader, priority = priority )

        db_job = UploadJobDBManager().add( job )
        resultado = self._job_manager.add( db_job )

        if resultado:
            self.logger.info( "Adjuntada con exito la subida '%s' al host '%s'" % ( file_path_to_upload, host_name ) )
            self._uploads_list.append( db_job )
        else:
            self.logger.error( "Error al adjuntar la subida '%s' al host '%s'" % ( file_path_to_upload, host_name ) )

        return resultado

    def info( self ):
        for upload_job in UploadJobDBManager().get_upload_job_list():
            resultado = {
                    InfoEnum.id: upload_job.get_id(),
                    InfoEnum.servicio: upload_job.get_upload().get_host_name(),
                    InfoEnum.fichero: upload_job.get_upload()._orig_filename,
                    InfoEnum.estado: upload_job.state(),
                    InfoEnum.progreso: upload_job.progress(),
                    InfoEnum.prioridad: upload_job.get_priority(),
                    InfoEnum.cuentas: map( lambda x: x.user, upload_job.get_upload()._account_list )
            }

            yield resultado
        # hacer para el id, servicios, ... un enum
        #esqueleto = { 
                #id: 0,
                #servicio: 'google',
                #cuentas: [],
                #fichero: '',
                #estado: 0, # toca hacer un enum
                #progreso: 1.0,
                #prioridad: 5 # a saber si se pone la tupla
        #}

    def modify( self ):
        raise NotImplementedError

    def start( self ):
        self.logger.info( "Leyendo las tareas pendientes de la BD" )
        upload_job_manager = UploadJobDBManager()

        states = [ JobStateEnum.wait, JobStateEnum.active ]

        for state in states:
            for job in upload_job_manager.get_upload_job_list_with_state( state ):
                job.reset()
                resultado = self._job_manager.add( job )
                if resultado:
                    self._uploads_list.append( job )

    def shutdown( self, al_terminar=True ):
        self._shutdown = ShutdownEnum.shutdown_on_complete if al_terminar else ShutdownEnum.shutdown_on_complete

        if al_terminar:
            self._job_manager.stop()
        else:
            self._job_manager.stop_now()

    def addAccount( self, host_name, user, password ):
        account_manager = AccountDBManager()

        account = AccountBase( host_name )
        account.user = user
        account.password = password

        db_account = account_manager.add( account )

        return db_account != None

    def delAccount( self, host_name, user ):
        account_manager = AccountDBManager()

        db_account = account_manager.get_account_with_hostname_user( "{}:{}".format( host_name, user ) )
        if db_account == None:
            return False

        db_account.delete()
        return True

    def listAccounts( self ):
        account_manager = AccountDBManager()

        for x in account_manager.get_account_list():
            d = {}

            d[ AccountEnum.host ] = x.account_host
            d[ AccountEnum.user ] = x.user

            yield d