Esempio n. 1
0
class Client:
    """Base client class for syncing."""

    def __init__(self, host, port, uri, database, controller, config, log, \
        ui_controller):
        self.config = config
        self.log = log
        self.host = host
        self.port = port
        self.uri = uri
        self.ui_controller = ui_controller
        self.eman = EventManager(database, log, controller, \
            self.config.mediadir(), self.get_media_file, self.ui_controller)
        self.login = ''
        self.passwd = ''
        self.id = hex(uuid.getnode())
        self.name = 'Mnemosyne'
        self.version = mnemosyne.version.version
        self.deck = 'default'
        self.protocol = PROTOCOL_VERSION
        self.cardtypes = N_SIDED_CARD_TYPE
        self.extra = ''
        self.stopped = False

    def set_user(self, login, passwd):
        """Sets user login and password."""

        self.login, self.passwd = login, passwd

    def start(self):
        """Start syncing."""
       
        try:
            self.ui_controller.update_status("Authorization. Please, wait...")
            self.login_()

            self.ui_controller.update_status("Handshaking. Please, wait...")
            self.handshake()

            self.ui_controller.update_status("Creating backup. Please, wait...")
            backup_file = self.eman.make_backup()

            server_media_count = self.get_server_media_count()
            if server_media_count:
                self.ui_controller.update_status(\
                    "Applying server media. Please, wait...")
                self.eman.apply_media(self.get_media_history(), \
                    server_media_count)

            client_media_count = self.eman.get_media_count()
            if client_media_count:
                self.ui_controller.update_status(\
                    "Sending client media to the server. Please, wait...")
                self.send_client_media(self.eman.get_media_history(), \
                    client_media_count)

            server_history_length = self.get_server_history_length()
            if server_history_length:
                self.ui_controller.update_status(\
                    "Applying server history. Please, wait...")
                self.get_server_history(server_history_length)

            # save current database and open backuped database
            # to get history for server
            self.eman.replace_database(backup_file)
            
            client_history_length = self.eman.get_history_length()
            if client_history_length:
                self.ui_controller.update_status(\
                    "Sending client history to the server. Please, wait...")
                self.send_client_history(self.eman.get_history(), \
                    client_history_length)

            # close temp database and return worked database
            self.eman.return_databases()

            self.ui_controller.update_status(\
                "Waiting for the server complete. Please, wait...")
    
            self.send_finish_request()

            if self.stopped:
                raise SyncError("Aborted!")
        except SyncError, exception:
            self.eman.restore_backup()
            self.ui_controller.show_message("Error: " + str(exception))
        else: