Skip to content

Commit

Permalink
Make Download into a dataclass
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkwouter committed Jul 16, 2023
1 parent bf23ce0 commit a99547a
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 36 deletions.
6 changes: 4 additions & 2 deletions minigalaxy/download_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
Example:
>>> import os
>>> from minigalaxy.download import Download, DownloadType
>>> from minigalaxy.entity.download import Download
>>> from minigalaxy.entity.download_type import DownloadType
>>> from minigalaxy.download_manager import DownloadManager
>>> def your_function():
>>> image_url = "https://www.gog.com/bundles/gogwebsitestaticpages/images/icon_section1-header.png"
Expand All @@ -25,7 +26,8 @@
from requests import Session
from requests.exceptions import RequestException
from minigalaxy.constants import DOWNLOAD_CHUNK_SIZE, MINIMUM_RESUME_SIZE, GAME_DOWNLOAD_THREADS, UI_DOWNLOAD_THREADS
from minigalaxy.download import Download, DownloadType
from minigalaxy.entity.download import Download
from minigalaxy.entity.download_type import DownloadType
import minigalaxy.logger # noqa: F401

module_logger = logging.getLogger("minigalaxy.download_manager")
Expand Down
Empty file added minigalaxy/entity/__init__.py
Empty file.
48 changes: 20 additions & 28 deletions minigalaxy/download.py → minigalaxy/entity/download.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
from enum import Enum
from dataclasses import dataclass
from typing import Callable, Optional
from zipfile import BadZipFile


# Enums were added in Python 3.4
class DownloadType(Enum):
ICON = 1
THUMBNAIL = 2
GAME = 3
GAME_UPDATE = 4
GAME_DLC = 5
from minigalaxy.entity.download_type import DownloadType
from minigalaxy.game import Game


@dataclass
class Download:
"""
A class to easily download from URLs and save the file.
Expand All @@ -25,42 +21,38 @@ class Download:
>>> download = Download(image_url, thumbnail, DownloadType.THUMBNAIL, finish_func=lambda x: print("Done downloading {}!".format(x))) # noqa: E501
>>> your_function() # doctest: +SKIP
"""
def __init__(self, url, save_location, download_type=None, finish_func=None,
progress_func=None, cancel_func=None, number=1,
out_of_amount=1, game=None):
self.url = url
self.save_location = save_location
self.__finish_func = finish_func
self.__progress_func = progress_func
self.__cancel_func = cancel_func
self.number = number
self.out_of_amount = out_of_amount
self.game = game
# Type of object, e.g. icon, thumbnail, game, dlc,
self.download_type = download_type
url: str
save_location: str
download_type: Optional[DownloadType] = None
finish_func: Optional[Callable[[str], None]] = None
progress_func: Optional[Callable[[int], None]] = None
cancel_func: Optional[Callable[[], None]] = None
number: int = 1
out_of_amount: int = 1
game: Optional[Game] = None

def set_progress(self, percentage: int) -> None:
"Set the download progress of the Download"
if self.__progress_func:
if self.progress_func:
if self.out_of_amount > 1:
# Change the percentage based on which number we are
progress_start = 100 / self.out_of_amount * (self.number - 1)
percentage = progress_start + percentage / self.out_of_amount
percentage = int(percentage)
self.__progress_func(percentage)
self.progress_func(percentage)

def finish(self):
"""
finish is called when the download has completed
If a finish_func was specified when the Download was created, call the function
"""
if self.__finish_func:
if self.finish_func:
try:
self.__finish_func(self.save_location)
self.finish_func(self.save_location)
except (FileNotFoundError, BadZipFile):
self.cancel()

def cancel(self):
"Cancel the download, calling a cancel_func if one was specified"
if self.__cancel_func:
self.__cancel_func()
if self.cancel_func:
self.cancel_func()
9 changes: 9 additions & 0 deletions minigalaxy/entity/download_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from enum import Enum


class DownloadType(Enum):
ICON = 1
THUMBNAIL = 2
GAME = 3
GAME_UPDATE = 4
GAME_DLC = 5
4 changes: 2 additions & 2 deletions minigalaxy/ui/gametile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import re
import time
import urllib.parse
from enum import Enum

from minigalaxy.config import Config
from minigalaxy.entity.state import State
from minigalaxy.game import Game
from minigalaxy.logger import logger
from minigalaxy.translation import _
from minigalaxy.paths import CACHE_DIR, THUMBNAIL_DIR, ICON_DIR, UI_DIR
from minigalaxy.download import Download, DownloadType
from minigalaxy.entity.download_type import DownloadType
from minigalaxy.entity.download import Download
from minigalaxy.download_manager import DownloadManager
from minigalaxy.launcher import start_game
from minigalaxy.installer import uninstall_game, install_game, check_diskspace
Expand Down
4 changes: 2 additions & 2 deletions minigalaxy/ui/gametilelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import re
import time
import urllib.parse
from enum import Enum

from minigalaxy.config import Config
from minigalaxy.entity.state import State
from minigalaxy.game import Game
from minigalaxy.logger import logger
from minigalaxy.translation import _
from minigalaxy.paths import CACHE_DIR, THUMBNAIL_DIR, ICON_DIR, UI_DIR
from minigalaxy.download import Download, DownloadType
from minigalaxy.entity.download_type import DownloadType
from minigalaxy.entity.download import Download
from minigalaxy.download_manager import DownloadManager
from minigalaxy.launcher import start_game
from minigalaxy.installer import uninstall_game, install_game, check_diskspace
Expand Down
2 changes: 1 addition & 1 deletion minigalaxy/ui/information.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from minigalaxy.paths import UI_DIR, THUMBNAIL_DIR, COVER_DIR
from minigalaxy.translation import _
from minigalaxy.config import Config
from minigalaxy.download import Download
from minigalaxy.entity.download import Download
from minigalaxy.download_manager import DownloadManager
from minigalaxy.ui.gtk import Gtk, GLib, Gio, GdkPixbuf

Expand Down
2 changes: 1 addition & 1 deletion tests/test_download.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest import TestCase
from unittest.mock import MagicMock, Mock

from minigalaxy.download import Download
from minigalaxy.entity.download import Download


class TestDownload(TestCase):
Expand Down

0 comments on commit a99547a

Please sign in to comment.