Пример #1
0
    def _finish_file(self):
        """Save the file to the filename given in __init__."""
        if self._finished_file:
            log.downloads.debug("finish_file called twice, ignored!")
            return
        self._finished_file = True
        log.downloads.debug("All assets downloaded, ready to finish off!")

        if isinstance(self.target, downloads.FileDownloadTarget):
            fobj = open(self.target.filename, 'wb')
        elif isinstance(self.target, downloads.FileObjDownloadTarget):
            fobj = self.target.fileobj
        elif isinstance(self.target, downloads.OpenFileDownloadTarget):
            try:
                fobj = downloads.temp_download_manager.get_tmpfile(
                    self.tab.title() + '.mhtml')
            except OSError as exc:
                msg = "Download error: {}".format(exc)
                message.error(msg)
                return
        else:
            raise ValueError("Invalid DownloadTarget given: {!r}".format(
                self.target))

        try:
            with fobj:
                self.writer.write_to(fobj)
        except OSError as error:
            message.error("Could not save file: {}".format(error))
            return
        log.downloads.debug("File successfully written.")
        message.info("Page saved as {}".format(self.target))

        if isinstance(self.target, downloads.OpenFileDownloadTarget):
            utils.open_file(fobj.name, self.target.cmdline)
Пример #2
0
    def _finish_file(self):
        """Save the file to the filename given in __init__."""
        if self._finished_file:
            log.downloads.debug("finish_file called twice, ignored!")
            return
        self._finished_file = True
        log.downloads.debug("All assets downloaded, ready to finish off!")

        if isinstance(self.target, downloads.FileDownloadTarget):
            fobj = open(self.target.filename, 'wb')
        elif isinstance(self.target, downloads.FileObjDownloadTarget):
            fobj = self.target.fileobj
        elif isinstance(self.target, downloads.OpenFileDownloadTarget):
            try:
                fobj = downloads.temp_download_manager.get_tmpfile(
                    self.tab.title() + '.mhtml')
            except OSError as exc:
                msg = "Download error: {}".format(exc)
                message.error(msg)
                return
        else:
            raise ValueError("Invalid DownloadTarget given: {!r}"
                             .format(self.target))

        try:
            with fobj:
                self.writer.write_to(fobj)
        except OSError as error:
            message.error("Could not save file: {}".format(error))
            return
        log.downloads.debug("File successfully written.")
        message.info("Page saved as {}".format(self.target))

        if isinstance(self.target, downloads.OpenFileDownloadTarget):
            utils.open_file(fobj.name, self.target.cmdline)
Пример #3
0
def start_download_checked(target, tab):
    """First check if dest is already a file, then start the download.

    Args:
        target: The DownloadTarget where the resulting file should be saved.
        tab: Specify the tab whose page should be loaded.
    """
    if not isinstance(target, downloads.FileDownloadTarget):
        _start_download(target, tab)
        return
    # The default name is 'page title.mhtml'
    title = tab.title()
    default_name = utils.sanitize_filename(title + '.mhtml')

    # Remove characters which cannot be expressed in the file system encoding
    encoding = sys.getfilesystemencoding()
    default_name = utils.force_encoding(default_name, encoding)
    dest = utils.force_encoding(target.filename, encoding)

    dest = os.path.expanduser(dest)

    # See if we already have an absolute path
    path = downloads.create_full_filename(default_name, dest)
    if path is None:
        # We still only have a relative path, prepend download_dir and
        # try again.
        path = downloads.create_full_filename(
            default_name, os.path.join(downloads.download_dir(), dest))
    downloads.last_used_directory = os.path.dirname(path)

    # Avoid downloading files if we can't save the output anyway...
    # Yes, this is prone to race conditions, but we're checking again before
    # saving the file anyway.
    if not os.path.isdir(os.path.dirname(path)):
        folder = os.path.dirname(path)
        message.error("Directory {} does not exist.".format(folder))
        return

    target = downloads.FileDownloadTarget(path)
    if not os.path.isfile(path):
        _start_download(target, tab=tab)
        return

    q = usertypes.Question()
    q.mode = usertypes.PromptMode.yesno
    q.title = "Overwrite existing file?"
    q.text = "<b>{}</b> already exists. Overwrite?".format(
        html.escape(path))
    q.completed.connect(q.deleteLater)
    q.answered_yes.connect(functools.partial(
        _start_download, target, tab=tab))
    message.global_bridge.ask(q, blocking=False)
Пример #4
0
def start_download_checked(target, tab):
    """First check if dest is already a file, then start the download.

    Args:
        target: The DownloadTarget where the resulting file should be saved.
        tab: Specify the tab whose page should be loaded.
    """
    if not isinstance(target, downloads.FileDownloadTarget):
        _start_download(target, tab)
        return
    # The default name is 'page title.mhtml'
    title = tab.title()
    default_name = utils.sanitize_filename(title + '.mhtml')

    # Remove characters which cannot be expressed in the file system encoding
    encoding = sys.getfilesystemencoding()
    default_name = utils.force_encoding(default_name, encoding)
    dest = utils.force_encoding(target.filename, encoding)

    dest = os.path.expanduser(dest)

    # See if we already have an absolute path
    path = downloads.create_full_filename(default_name, dest)
    if path is None:
        # We still only have a relative path, prepend download_dir and
        # try again.
        path = downloads.create_full_filename(
            default_name, os.path.join(downloads.download_dir(), dest))
    downloads.last_used_directory = os.path.dirname(path)

    # Avoid downloading files if we can't save the output anyway...
    # Yes, this is prone to race conditions, but we're checking again before
    # saving the file anyway.
    if not os.path.isdir(os.path.dirname(path)):
        folder = os.path.dirname(path)
        message.error("Directory {} does not exist.".format(folder))
        return

    target = downloads.FileDownloadTarget(path)
    if not os.path.isfile(path):
        _start_download(target, tab=tab)
        return

    q = usertypes.Question()
    q.mode = usertypes.PromptMode.yesno
    q.title = "Overwrite existing file?"
    q.text = "<b>{}</b> already exists. Overwrite?".format(
        html.escape(path))
    q.completed.connect(q.deleteLater)
    q.answered_yes.connect(functools.partial(
        _start_download, target, tab=tab))
    message.global_bridge.ask(q, blocking=False)
Пример #5
0
def start_download_checked(dest, web_view):
    """First check if dest is already a file, then start the download.

    Args:
        dest: The filename where the resulting file should be saved.
        web_view: Specify the webview whose page should be loaded.
    """
    # The default name is 'page title.mht'
    title = web_view.title()
    default_name = utils.sanitize_filename(title + '.mht')

    # Remove characters which cannot be expressed in the file system encoding
    encoding = sys.getfilesystemencoding()
    default_name = utils.force_encoding(default_name, encoding)
    dest = utils.force_encoding(dest, encoding)

    dest = os.path.expanduser(dest)

    # See if we already have an absolute path
    path = downloads.create_full_filename(default_name, dest)
    if path is None:
        # We still only have a relative path, prepend download_dir and
        # try again.
        path = downloads.create_full_filename(
            default_name, os.path.join(downloads.download_dir(), dest))
    downloads.last_used_directory = os.path.dirname(path)

    # Avoid downloading files if we can't save the output anyway...
    # Yes, this is prone to race conditions, but we're checking again before
    # saving the file anyway.
    if not os.path.isdir(os.path.dirname(path)):
        folder = os.path.dirname(path)
        message.error(web_view.win_id,
                      "Directory {} does not exist.".format(folder))
        return

    if not os.path.isfile(path):
        _start_download(path, web_view=web_view)
        return

    q = usertypes.Question()
    q.mode = usertypes.PromptMode.yesno
    q.text = "{} exists. Overwrite?".format(path)
    q.completed.connect(q.deleteLater)
    q.answered_yes.connect(
        functools.partial(_start_download, path, web_view=web_view))
    message_bridge = objreg.get('message-bridge',
                                scope='window',
                                window=web_view.win_id)
    message_bridge.ask(q, blocking=False)
Пример #6
0
def start_download_checked(dest, web_view):
    """First check if dest is already a file, then start the download.

    Args:
        dest: The filename where the resulting file should be saved.
        web_view: Specify the webview whose page should be loaded.
    """
    # The default name is 'page title.mht'
    title = web_view.title()
    default_name = utils.sanitize_filename(title + '.mht')

    # Remove characters which cannot be expressed in the file system encoding
    encoding = sys.getfilesystemencoding()
    default_name = utils.force_encoding(default_name, encoding)
    dest = utils.force_encoding(dest, encoding)

    dest = os.path.expanduser(dest)

    # See if we already have an absolute path
    path = downloads.create_full_filename(default_name, dest)
    if path is None:
        # We still only have a relative path, prepend download_dir and
        # try again.
        path = downloads.create_full_filename(
            default_name, os.path.join(downloads.download_dir(), dest))
    downloads.last_used_directory = os.path.dirname(path)

    # Avoid downloading files if we can't save the output anyway...
    # Yes, this is prone to race conditions, but we're checking again before
    # saving the file anyway.
    if not os.path.isdir(os.path.dirname(path)):
        folder = os.path.dirname(path)
        message.error(web_view.win_id,
                      "Directory {} does not exist.".format(folder))
        return

    if not os.path.isfile(path):
        _start_download(path, web_view=web_view)
        return

    q = usertypes.Question()
    q.mode = usertypes.PromptMode.yesno
    q.text = "{} exists. Overwrite?".format(path)
    q.completed.connect(q.deleteLater)
    q.answered_yes.connect(functools.partial(
        _start_download, path, web_view=web_view))
    message_bridge = objreg.get('message-bridge', scope='window',
                                window=web_view.win_id)
    message_bridge.ask(q, blocking=False)
Пример #7
0
 def _finish_file(self):
     """Save the file to the filename given in __init__."""
     if self._finished_file:
         log.downloads.debug("finish_file called twice, ignored!")
         return
     self._finished_file = True
     log.downloads.debug("All assets downloaded, ready to finish off!")
     try:
         with open(self.dest, 'wb') as file_output:
             self.writer.write_to(file_output)
     except OSError as error:
         message.error("Could not save file: {}".format(error))
         return
     log.downloads.debug("File successfully written.")
     message.info("Page saved as {}".format(self.dest))