From 0a8dbec082c8b93e142bce5418cf233bb7dafc79 Mon Sep 17 00:00:00 2001 From: MrS0m30n3 Date: Sun, 25 Sep 2016 23:57:04 +0300 Subject: [PATCH] DownloadList add index method --- tests/test_dlist.py | 15 +++++++++++++++ youtube_dl_gui/downloadmanager.py | 7 +++++++ youtube_dl_gui/mainframe.py | 3 +-- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/tests/test_dlist.py b/tests/test_dlist.py index 8f55f6b..0e21072 100644 --- a/tests/test_dlist.py +++ b/tests/test_dlist.py @@ -236,6 +236,21 @@ class TestChangeStage(unittest.TestCase): self.assertRaises(KeyError, self.dlist.change_stage, 3, "Active") +class TestIndex(unittest.TestCase): + + """Test case for the DownloadList index method.""" + + def setUp(self): + self.mocks = [mock.Mock(object_id=i) for i in range(3)] + self.dlist = DownloadList(self.mocks) + + def test_index(self): + self.assertEqual(self.dlist.index(2), 2) + + def test_index_not_exist(self): + self.assertEqual(self.dlist.index(3), -1) + + class TestSynchronizeDecorator(unittest.TestCase): def test_synchronize(self): diff --git a/youtube_dl_gui/downloadmanager.py b/youtube_dl_gui/downloadmanager.py index 50846c1..7b2bfd0 100644 --- a/youtube_dl_gui/downloadmanager.py +++ b/youtube_dl_gui/downloadmanager.py @@ -291,6 +291,13 @@ class DownloadList(object): """Change the stage of the item with the given object_id.""" self._items_dict[object_id].stage = new_stage + @synchronized(_SYNC_LOCK) + def index(self, object_id): + """Get the zero based index of the item with the given object_id.""" + if object_id in self._items_list: + return self._items_list.index(object_id) + return -1 + @synchronized(_SYNC_LOCK) def __len__(self): return len(self._items_list) diff --git a/youtube_dl_gui/mainframe.py b/youtube_dl_gui/mainframe.py index aeda1cc..87a30b3 100644 --- a/youtube_dl_gui/mainframe.py +++ b/youtube_dl_gui/mainframe.py @@ -707,8 +707,7 @@ class MainFrame(wx.Frame): download_item = self._download_list.get_item(data["index"]) download_item.update_stats(data) - #TODO Add get index from object_id on download_list instead - row = self._download_list._items_list.index(data["index"]) + row = self._download_list.index(data["index"]) self._status_list._update_from_item(row, download_item)