예제 #1
0
 def test1_finish(self):
     mock_finish_function = MagicMock()
     download = Download("test_url",
                         "test_save_location",
                         finish_func=mock_finish_function)
     download.finish()
     exp = 2
     obs = len(mock_finish_function.mock_calls)
     self.assertEqual(exp, obs)
예제 #2
0
 def test_cancel(self):
     mock_cancel_function = MagicMock()
     download = Download("test_url",
                         "test_save_location",
                         cancel_func=mock_cancel_function)
     download.cancel()
     exp = 2
     obs = len(mock_cancel_function.mock_calls)
     self.assertEqual(exp, obs)
예제 #3
0
 def test1_set_progress(self):
     mock_progress_function = MagicMock()
     download = Download("test_url",
                         "test_save_location",
                         progress_func=mock_progress_function)
     download.set_progress(50)
     kall = mock_progress_function.mock_calls[-1]
     name, args, kwargs = kall
     exp = 50
     obs = args[0]
     self.assertEqual(exp, obs)
예제 #4
0
 def test2_set_progress(self):
     mock_progress_function = MagicMock()
     download = Download("test_url",
                         "test_save_location",
                         progress_func=mock_progress_function,
                         out_of_amount=2)
     download.set_progress(32)
     kall = mock_progress_function.mock_calls[-1]
     name, args, kwargs = kall
     exp = 16
     obs = args[0]
     self.assertEqual(exp, obs)
예제 #5
0
 def test2_finish(self):
     mock_finish_function = MagicMock()
     mock_finish_function.side_effect = FileNotFoundError(
         Mock(status="Connection Error"))
     mock_cancel_function = MagicMock()
     download = Download("test_url",
                         "test_save_location",
                         finish_func=mock_finish_function,
                         cancel_func=mock_cancel_function)
     download.finish()
     exp = 2
     obs = len(mock_cancel_function.mock_calls)
     self.assertEqual(exp, obs)
예제 #6
0
    def __download(self, download_info, finish_func, cancel_to_state):
        download_success = True
        GLib.idle_add(self.update_to_state, self.state.QUEUED)
        Config.set("current_download", self.game.id)
        # Start the download for all files
        self.download_list = []
        number_of_files = len(download_info['files'])
        total_file_size = 0
        executable_path = None
        download_files = []
        for key, file_info in enumerate(download_info['files']):
            try:
                download_url = self.api.get_real_download_link(
                    file_info["downlink"])
            except ValueError as e:
                print(e)
                GLib.idle_add(self.parent.parent.show_error,
                              _("Download error"), _(str(e)))
                download_success = False
                break
            total_file_size += self.api.get_file_size(file_info["downlink"])
            try:
                # Extract the filename from the download url (filename is between %2F and &token)
                filename = urllib.parse.unquote(
                    re.search('%2F(((?!%2F).)*)&t', download_url).group(1))
            except AttributeError:
                filename = "{}-{}.bin".format(self.game.get_stripped_name(),
                                              key)
            download_path = os.path.join(self.download_dir, filename)
            if key == 0:
                # If key = 0, denote the file as the executable's path
                executable_path = download_path
            md5sum = self.api.get_download_file_md5(file_info["downlink"])
            if md5sum:
                self.game.md5sum[os.path.basename(download_path)] = md5sum
            download = Download(
                url=download_url,
                save_location=download_path,
                finish_func=finish_func
                if download_path == executable_path else None,
                progress_func=self.set_progress,
                cancel_func=lambda: self.__cancel(to_state=cancel_to_state),
                number=number_of_files - key,
                out_of_amount=number_of_files,
                game=self.game)
            download_files.insert(0, download)
        self.download_list.extend(download_files)

        if check_diskspace(total_file_size, Config.get("install_dir")):
            DownloadManager.download(download_files)
            ds_msg_title = ""
            ds_msg_text = ""
        else:
            ds_msg_title = "Download error"
            ds_msg_text = "Not enough disk space to install game."
            download_success = False
        if ds_msg_title:
            GLib.idle_add(self.parent.parent.show_error, _(ds_msg_title),
                          _(ds_msg_text))
        return download_success
예제 #7
0
 def load_thumbnail(self):
     if self.gamesdb_info["cover"]:
         cover_path = os.path.join(COVER_DIR, "{}.jpg".format(self.game.id))
         if os.path.isfile(cover_path):
             pixbuf = GdkPixbuf.Pixbuf.new_from_file(cover_path)
             pixbuf = pixbuf.scale_simple(340, 480,
                                          GdkPixbuf.InterpType.BILINEAR)
             GLib.idle_add(self.image.set_from_pixbuf, pixbuf)
         else:
             url = "{}".format(self.gamesdb_info["cover"])
             download = Download(url, cover_path)
             DownloadManager.download_now(download)
             response = urllib.request.urlopen(url)
             input_stream = Gio.MemoryInputStream.new_from_data(
                 response.read(), None)
             pixbuf = GdkPixbuf.Pixbuf.new_from_stream(input_stream, None)
             pixbuf = pixbuf.scale_simple(340, 480,
                                          GdkPixbuf.InterpType.BILINEAR)
             GLib.idle_add(self.image.set_from_pixbuf, pixbuf)
     else:
         thumbnail_path = os.path.join(THUMBNAIL_DIR,
                                       "{}.jpg".format(self.game.id))
         if not os.path.isfile(thumbnail_path) and self.game.is_installed:
             thumbnail_path = os.path.join(self.game.install_dir,
                                           "thumbnail.jpg")
         GLib.idle_add(self.image.set_from_file, thumbnail_path)
예제 #8
0
 def get_async_image_dlc_icon(self, dlc_id, image, icon, title):
     dlc_icon_path = os.path.join(ICON_DIR, "{}.jpg".format(dlc_id))
     if icon:
         if os.path.isfile(dlc_icon_path):
             GLib.idle_add(image.set_from_file, dlc_icon_path)
         else:
             url = "http:{}".format(icon)
             dlc_icon = os.path.join(ICON_DIR, "{}.jpg".format(dlc_id))
             download = Download(url, dlc_icon)
             DownloadManager.download_now(download)
             GLib.idle_add(image.set_from_file, dlc_icon_path)
예제 #9
0
 def __download_file(self) -> None:
     Config.set("current_download", self.game.id)
     GLib.idle_add(self.update_to_state, self.state.QUEUED)
     download_info = self.api.get_download_info(self.game)
     file_url = download_info["downlink"]
     self.download = Download(file_url,
                              self.download_path,
                              self.__install,
                              progress_func=self.set_progress,
                              cancel_func=self.__cancel_download)
     DownloadManager.download(self.download)
예제 #10
0
    def load_thumbnail(self):
        if self.__set_image():
            return True
        if not self.game.image_url or not self.game.id:
            return False

        # Download the thumbnail
        image_url = "https:{}_196.jpg".format(self.game.image_url)
        thumbnail = os.path.join(THUMBNAIL_DIR, "{}.jpg".format(self.game.id))

        download = Download(image_url, thumbnail, finish_func=self.__set_image)
        DownloadManager.download_now(download)
        return True
예제 #11
0
    def __download_file(self) -> None:
        GLib.idle_add(self.update_to_state, self.state.QUEUED)
        try:
            download_info = self.api.get_download_info(self.game)
        except NoDownloadLinkFound:
            if Config.get("current_download") == self.game.id:
                Config.unset("current_download")
            GLib.idle_add(
                self.parent.parent.show_error, _("Download error"),
                _("There was an error when trying to fetch the download link!")
            )
            GLib.idle_add(self.update_to_state, self.state.DOWNLOADABLE)
            return

        Config.set("current_download", self.game.id)
        # Start the download for all files
        self.download = []
        download_path = self.download_path
        finish_func = self.__install
        number_of_files = len(download_info['files'])
        for key, file_info in enumerate(download_info['files']):
            download_url = self.api.get_real_download_link(
                file_info["downlink"])
            try:
                # Extract the filename from the download url (filename is between %2F and &token)
                download_path = os.path.join(
                    self.download_dir,
                    urllib.parse.unquote(
                        re.search('%2F(((?!%2F).)*)&t',
                                  download_url).group(1)))
                if key == 0:
                    # If key = 0, denote the file as the executable's path
                    self.download_path = download_path
            except AttributeError:
                if key > 0:
                    download_path = "{}-{}.bin".format(self.download_path, key)
            download = Download(url=download_url,
                                save_location=download_path,
                                finish_func=finish_func,
                                progress_func=self.set_progress,
                                cancel_func=self.__cancel_download,
                                number=key + 1,
                                out_of_amount=number_of_files)
            self.download.append(download)

        DownloadManager.download(self.download)
예제 #12
0
    def load_thumbnail(self):
        set_result = self.__set_image()
        if not set_result:
            tries = 10
            performed_try = 0
            while performed_try < tries:
                if self.game.image_url and self.game.id:
                    # Download the thumbnail
                    image_url = "https:{}_196.jpg".format(self.game.image_url)
                    thumbnail = os.path.join(THUMBNAIL_DIR, "{}.jpg".format(self.game.id))

                    download = Download(image_url, thumbnail, finish_func=self.__set_image)
                    DownloadManager.download_now(download)
                    set_result = True
                    break
                performed_try += 1
                time.sleep(1)
        return set_result
예제 #13
0
    def __download(self, download_info, finish_func, cancel_to_state):
        download_success = True
        GLib.idle_add(self.update_to_state, self.state.QUEUED)
        Config.set("current_download", self.game.id)
        # Start the download for all files
        self.download = []
        number_of_files = len(download_info['files'])
        for key, file_info in enumerate(download_info['files']):
            try:
                download_url = self.api.get_real_download_link(
                    file_info["downlink"])
                self.game.md5sum = self.api.get_download_file_md5(
                    file_info["downlink"])
            except ValueError as e:
                print(e)
                GLib.idle_add(self.parent.parent.show_error,
                              _("Download error"), _(str(e)))
                download_success = False
                break
            try:
                # Extract the filename from the download url (filename is between %2F and &token)
                download_path = os.path.join(
                    self.download_dir,
                    urllib.parse.unquote(
                        re.search('%2F(((?!%2F).)*)&t',
                                  download_url).group(1)))
                if key == 0:
                    # If key = 0, denote the file as the executable's path
                    self.download_path = download_path
            except AttributeError:
                if key > 0:
                    download_path = "{}-{}.bin".format(self.download_path, key)
            download = Download(
                url=download_url,
                save_location=download_path,
                finish_func=finish_func,
                progress_func=self.set_progress,
                cancel_func=lambda: self.__cancel(to_state=cancel_to_state),
                number=key + 1,
                out_of_amount=number_of_files)
            self.download.append(download)

        DownloadManager.download(self.download)
        return download_success
예제 #14
0
    def __download_file(self) -> None:
        Config.set("current_download", self.game.id)
        GLib.idle_add(self.update_to_state, self.state.QUEUED)
        download_info = self.api.get_download_info(self.game)

        # Start the download for all files
        self.download = []
        download_path = self.download_path
        finish_func = self.__install
        for key, file_info in enumerate(download_info['files']):
            if key > 0:
                download_path = "{}-{}.bin".format(self.download_path, key)
            download = Download(url=self.api.get_real_download_link(
                file_info["downlink"]),
                                save_location=download_path,
                                finish_func=finish_func,
                                progress_func=self.set_progress,
                                cancel_func=self.__cancel_download,
                                number=key + 1,
                                out_of_amount=len(download_info['files']))
            self.download.append(download)

        DownloadManager.download(self.download)