Browse Source

Add reset method in DownloadItem

doc-issue-template
MrS0m30n3 8 years ago
parent
commit
720437cfcb
2 changed files with 85 additions and 15 deletions
  1. 65
      tests/test_ditem.py
  2. 35
      youtube_dl_gui/downloadmanager.py

65
tests/test_ditem.py

@ -200,6 +200,71 @@ class TestDownloadItemPrivate(unittest.TestCase):
self.assertEqual(ditem.stage, "Completed")
class TestReset(unittest.TestCase):
"""Test case for the DownloadItem reset method."""
def setUp(self):
self.ditem = DownloadItem("url", ["-f", "flv"])
def test_reset_error_status(self):
self.ditem._stage = "Completed"
self.ditem.path = os.path.join("/home", "user")
self.ditem.filenames = ["file1", "file2", "file"]
self.ditem.extensions = [".mp4", ".m4a", ".mp4"]
self.ditem.progress_stats = {
"filename": "file",
"extension": ".mp4",
"filsize": "9.45MiB",
"percent": "100%",
"speed": "-",
"eta": "00:00",
"status": "Error"
}
self.ditem.reset()
self.assertEqual(self.ditem._stage, "Queued")
self.assertEqual(self.ditem.path, "")
self.assertEqual(self.ditem.filenames, [])
self.assertEqual(self.ditem.extensions, [])
self.assertEqual(
self.ditem.progress_stats,
{"filename": "url",
"extension": "-",
"filesize": "-",
"percent": "0%",
"speed": "-",
"eta": "-",
"status": "Queued"}
)
def test_reset_paused_stage(self):
self.ditem._stage = "Paused"
# No need to change filanames, extension, etc
# since everything in pause state has the default value
self.ditem.reset()
self.assertEqual(self.ditem._stage, "Queued")
def test_reset_active_stage(self):
self.ditem._stage = "Active"
self.ditem.path = os.path.join("/home", "user")
self.ditem.filenames = ["file1"]
self.ditem.extensions = [".mp4"]
self.ditem.progress_stats = {
"filename": "file1",
"extension": ".mp4",
"filsize": "9.45MiB",
"percent": "75.5%",
"speed": "200.00KiB/s",
"eta": "00:10",
"status": "Downloading"
}
self.assertRaises(RuntimeError, self.ditem.reset)
def main():
unittest.main()

35
youtube_dl_gui/downloadmanager.py

@ -89,23 +89,9 @@ class DownloadItem(object):
def __init__(self, url, options):
self.url = url
self.options = options
self._stage = self.STAGES[0]
self.object_id = hash(url + to_string(options))
self.path = ""
self.filenames = []
self.extensions = []
self.progress_stats = {
"filename": url,
"extension": "-",
"filesize": "-",
"percent": "0%",
"speed": "-",
"eta": "-",
"status": self.stage
}
self.reset()
@property
def stage(self):
@ -127,6 +113,25 @@ class DownloadItem(object):
self._stage = value
def reset(self):
if hasattr(self, "_stage") and self._stage == self.STAGES[1]:
raise RuntimeError("Cannot reset an 'Active' item")
self._stage = self.STAGES[0]
self.path = ""
self.filenames = []
self.extensions = []
self.progress_stats = {
"filename": self.url,
"extension": "-",
"filesize": "-",
"percent": "0%",
"speed": "-",
"eta": "-",
"status": self.stage
}
def get_files(self):
"""Returns a list that contains all the system files bind to this object."""
files = []

Loading…
Cancel
Save