Beispiel #1
0
    def save(self) -> Optional[bool]:
        """Save the file to the current :attr:`path`.

        This calls :meth:`save_as` if :attr:`path` is None, and returns
        False if the user cancels the save as dialog. None is returned
        on errors, and True is returned in all other cases. In other
        words, this returns True if saving succeeded.

        .. seealso:: The :virtevt:`Save` event.
        """
        if self.path is None:
            return self.save_as()

        self.event_generate('<<Save>>')

        encoding = self.settings.get('encoding', str)
        line_ending = self.settings.get('line_ending', settings.LineEnding)

        try:
            with utils.backup_open(self.path,
                                   'w',
                                   encoding=encoding,
                                   newline=line_ending.value) as f:
                f.write(self.textwidget.get('1.0', 'end - 1 char'))
        except (OSError, UnicodeError) as e:
            log.exception("saving '%s' failed", self.path)
            utils.errordialog(
                type(e).__name__, "Saving failed!", traceback.format_exc())
            return None

        self.mark_saved()
        return True
Beispiel #2
0
    def save(self):
        """Save the file to the current :attr:`path`.

        This calls :meth:`save_as` if :attr:`path` is None, and returns
        False if the user cancels the save as dialog. None is returned
        on errors, and True is returned in all other cases. In other
        words, this returns True if saving succeeded.

        .. seealso:: The :virtevt:`Save` event.
        """
        if self.path is None:
            return self.save_as()

        self.event_generate('<<Save>>')

        encoding = settings.get_section('General')['encoding']
        try:
            with utils.backup_open(self.path, 'w', encoding=encoding) as f:
                for chunk in self.textwidget.iter_chunks():
                    f.write(chunk)
        except (OSError, UnicodeError) as e:
            log.exception("saving '%s' failed", self.path)
            utils.errordialog(
                type(e).__name__, "Saving failed!", traceback.format_exc())
            return None

        self.mark_saved()
        return True