Example #1
0
    def autosave(self, index):
        """
        Autosave a file.

        Do nothing if the `changed_since_autosave` flag is not set or the file
        is newly created (and thus not named by the user). Otherwise, save a
        copy of the file with the name given by `self.get_autosave_filename()`
        and clear the `changed_since_autosave` flag. Errors raised when saving
        are silently ignored.

        Args:
            index (int): index into self.stack.data
        """
        finfo = self.stack.data[index]
        document = finfo.editor.document()
        if not document.changed_since_autosave or finfo.newly_created:
            return
        autosave_filename = self.get_autosave_filename(finfo.filename)
        logger.debug('Autosaving %s to %s', finfo.filename, autosave_filename)
        try:
            self.stack._write_to_file(finfo, autosave_filename)
            document.changed_since_autosave = False
        except EnvironmentError as error:
            action = (_('Error while autosaving {} to {}')
                      .format(finfo.filename, autosave_filename))
            msgbox = AutosaveErrorDialog(action, error)
            msgbox.exec_if_enabled()
Example #2
0
    def get_autosave_filename(self, filename):
        """
        Get name of autosave file for specified file name.

        This function uses the dict in `self.name_mapping`. If `filename` is
        in the mapping, then return the corresponding autosave file name.
        Otherwise, construct a unique file name and update the mapping.

        Args:
            filename (str): original file name
        """
        try:
            autosave_filename = self.name_mapping[filename]
        except KeyError:
            autosave_dir = get_conf_path('autosave')
            if not osp.isdir(autosave_dir):
                try:
                    os.mkdir(autosave_dir)
                except EnvironmentError as error:
                    action = _('Error while creating autosave directory')
                    msgbox = AutosaveErrorDialog(action, error)
                    msgbox.exec_if_enabled()
            autosave_filename = self.create_unique_autosave_filename(
                filename, autosave_dir)
            self.name_mapping[filename] = autosave_filename
            self.save_autosave_mapping()
            logger.debug('New autosave file name')
        return autosave_filename
Example #3
0
    def get_autosave_filename(self, filename):
        """
        Get name of autosave file for specified file name.

        This function uses the dict in `self.name_mapping`. If `filename` is
        in the mapping, then return the corresponding autosave file name.
        Otherwise, construct a unique file name and update the mapping.

        Args:
            filename (str): original file name
        """
        try:
            autosave_filename = self.name_mapping[filename]
        except KeyError:
            autosave_dir = get_conf_path('autosave')
            if not osp.isdir(autosave_dir):
                try:
                    os.mkdir(autosave_dir)
                except EnvironmentError as error:
                    action = _('Error while creating autosave directory')
                    msgbox = AutosaveErrorDialog(action, error)
                    msgbox.exec_if_enabled()
            autosave_filename = self.create_unique_autosave_filename(
                    filename, autosave_dir)
            self.name_mapping[filename] = autosave_filename
            self.stack.sig_option_changed.emit(
                    'autosave_mapping', self.name_mapping)
            logger.debug('New autosave file name')
        return autosave_filename
Example #4
0
    def autosave(self, index):
        """
        Autosave a file.

        Do nothing if the `changed_since_autosave` flag is not set or the file
        is newly created (and thus not named by the user). Otherwise, save a
        copy of the file with the name given by `self.get_autosave_filename()`
        and clear the `changed_since_autosave` flag. Errors raised when saving
        are silently ignored.

        Args:
            index (int): index into self.stack.data
        """
        finfo = self.stack.data[index]
        document = finfo.editor.document()
        if not document.changed_since_autosave or finfo.newly_created:
            return
        autosave_filename = self.get_autosave_filename(finfo.filename)
        logger.debug('Autosaving %s to %s', finfo.filename, autosave_filename)
        try:
            self.stack._write_to_file(finfo, autosave_filename)
            document.changed_since_autosave = False
        except EnvironmentError as error:
            action = (_('Error while autosaving {} to {}')
                      .format(finfo.filename, autosave_filename))
            msgbox = AutosaveErrorDialog(action, error)
            msgbox.exec_if_enabled()
Example #5
0
def test_autosave_error_message_box(qtbot, mocker):
    """Test that AutosaveErrorDialog exec's at first, but that after the
    'do not show anymore' checkbox is clicked, it does not exec anymore."""
    mock_exec = mocker.patch.object(AutosaveErrorDialog, 'exec_')
    box = AutosaveErrorDialog('action', 'error')
    box.exec_if_enabled()
    assert mock_exec.call_count == 1
    box.dismiss_box.click()
    ok_button = box.findChild(QPushButton)
    ok_button.click()
    box2 = AutosaveErrorDialog('action', 'error')
    box2.exec_if_enabled()
    assert mock_exec.call_count == 1
Example #6
0
    def remove_autosave_file(self, filename):
        """
        Remove autosave file for specified file.

        This function also updates `self.name_mapping` and `self.file_hashes`.
        """
        if filename not in self.name_mapping:
            return
        autosave_filename = self.name_mapping[filename]
        try:
            os.remove(autosave_filename)
        except EnvironmentError as error:
            action = (_('Error while removing autosave file {}')
                      .format(autosave_filename))
            msgbox = AutosaveErrorDialog(action, error)
            msgbox.exec_if_enabled()
        del self.name_mapping[filename]
        del self.file_hashes[autosave_filename]
        self.stack.sig_option_changed.emit(
                'autosave_mapping', self.name_mapping)
        logger.debug('Removing autosave file %s', autosave_filename)
Example #7
0
    def remove_autosave_file(self, fileinfo):
        """
        Remove autosave file for specified file.

        This function also updates `self.autosave_mapping` and clears the
        `changed_since_autosave` flag.
        """
        filename = fileinfo.filename
        if filename not in self.name_mapping:
            return
        autosave_filename = self.name_mapping[filename]
        try:
            os.remove(autosave_filename)
        except EnvironmentError as error:
            action = (_('Error while removing autosave file {}')
                      .format(autosave_filename))
            msgbox = AutosaveErrorDialog(action, error)
            msgbox.exec_if_enabled()
        del self.name_mapping[filename]
        self.stack.sig_option_changed.emit(
                'autosave_mapping', self.name_mapping)
        logger.debug('Removing autosave file %s', autosave_filename)
Example #8
0
    def autosave(self, finfo):
        """
        Autosave a file.

        Save a copy in a file with name `self.get_autosave_filename()` and
        update the cached hash of the autosave file. An error dialog notifies
        the user of any errors raised when saving.

        Args:
            fileinfo (FileInfo): file that is to be autosaved.
        """
        autosave_filename = self.get_autosave_filename(finfo.filename)
        logger.debug('Autosaving %s to %s', finfo.filename, autosave_filename)
        try:
            self.stack._write_to_file(finfo, autosave_filename)
            autosave_hash = self.stack.compute_hash(finfo)
            self.file_hashes[autosave_filename] = autosave_hash
        except EnvironmentError as error:
            action = (_('Error while autosaving {} to {}').format(
                finfo.filename, autosave_filename))
            msgbox = AutosaveErrorDialog(action, error)
            msgbox.exec_if_enabled()
Example #9
0
    def remove_autosave_file(self, filename):
        """
        Remove autosave file for specified file.

        This function also updates `self.name_mapping` and `self.file_hashes`.
        If there is no autosave file, then the function returns without doing
        anything.
        """
        if filename not in self.name_mapping:
            return
        autosave_filename = self.name_mapping[filename]
        try:
            os.remove(autosave_filename)
        except EnvironmentError as error:
            action = (_('Error while removing autosave file {}').format(
                autosave_filename))
            msgbox = AutosaveErrorDialog(action, error)
            msgbox.exec_if_enabled()
        del self.name_mapping[filename]
        del self.file_hashes[autosave_filename]
        self.save_autosave_mapping()
        logger.debug('Removing autosave file %s', autosave_filename)
Example #10
0
    def remove_autosave_file(self, filename, errors='raise'):
        """
        Remove autosave file for specified file.

        This function also updates `self.autosave_mapping` and clears the
        `changed_since_autosave` flag.
        """
        if filename not in self.name_mapping:
            return
        autosave_filename = self.name_mapping[filename]
        try:
            os.remove(autosave_filename)
        except EnvironmentError as error:
            error_message = (_('{} while removing autosave file {}')
                             .format(type(error), autosave_filename))
            if errors == 'ignore':
                logger.debug('%s : %s', error_message, str(error))
            else:
                msgbox = AutosaveErrorDialog(error_message, error)
                msgbox.exec_if_enabled()
        del self.name_mapping[filename]
        self.stack.sig_option_changed.emit(
            'autosave_mapping', self.name_mapping)
        logger.debug('Removed autosave file %s', autosave_filename)