From db6e84f9524a268981e970f4ccf5792b12601487 Mon Sep 17 00:00:00 2001 From: Ales Erjavec Date: Tue, 19 Jan 2021 10:12:09 +0100 Subject: [PATCH] owdataset: Do not capture self in closure Parameterize and move the functions out of the class. --- Orange/widgets/data/owdatasets.py | 24 ++++++++++---------- Orange/widgets/data/tests/test_owdatasets.py | 24 ++++++++++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/Orange/widgets/data/owdatasets.py b/Orange/widgets/data/owdatasets.py index af8a55e8571..21c0c1309b0 100644 --- a/Orange/widgets/data/owdatasets.py +++ b/Orange/widgets/data/owdatasets.py @@ -44,6 +44,15 @@ def ensure_local(index_url, file_path, local_cache_path, return localfiles.localpath_download(*file_path, callback=progress_advance) +def list_remote(server: str) -> Dict[Tuple[str, ...], dict]: + client = ServerFiles(server) + return client.allinfo() + + +def list_local(path: str) -> Dict[Tuple[str, ...], dict]: + return LocalFiles(path).allinfo() + + def format_exception(error): # type: (BaseException) -> str return "\n".join(traceback.format_exception_only(type(error), error)) @@ -256,7 +265,7 @@ def __init__(self): self.setStatusMessage("Initializing") self._executor = ThreadPoolExecutor(max_workers=1) - f = self._executor.submit(self.list_remote) + f = self._executor.submit(list_remote, self.INDEX_URL) w = FutureWatcher(f, parent=self) w.done.connect(self.__set_index) @@ -349,7 +358,7 @@ def __set_index(self, f): assert f.done() self.setBlocking(False) self.setStatusMessage("") - self.allinfo_local = self.list_local() + self.allinfo_local = list_local(self.local_cache_path) try: self.allinfo_remote = f.result() @@ -385,7 +394,7 @@ def __set_index(self, f): def __update_cached_state(self): model = self.view.model().sourceModel() - localinfo = self.list_local() + localinfo = list_local(self.local_cache_path) assert isinstance(model, QStandardItemModel) allinfo = [] for i in range(model.rowCount()): @@ -552,15 +561,6 @@ def load_and_output(self, path): def load_data(path): return Orange.data.Table(path) - def list_remote(self): - # type: () -> Dict[Tuple[str, ...], dict] - client = ServerFiles(server=self.INDEX_URL) - return client.allinfo() - - def list_local(self): - # type: () -> Dict[Tuple[str, ...], dict] - return LocalFiles(self.local_cache_path).allinfo() - class FutureWatcher(QObject): done = Signal(object) diff --git a/Orange/widgets/data/tests/test_owdatasets.py b/Orange/widgets/data/tests/test_owdatasets.py index c485b17d922..45c3d7e3a18 100644 --- a/Orange/widgets/data/tests/test_owdatasets.py +++ b/Orange/widgets/data/tests/test_owdatasets.py @@ -11,9 +11,9 @@ class TestOWDataSets(WidgetTest): - @patch("Orange.widgets.data.owdatasets.OWDataSets.list_remote", + @patch("Orange.widgets.data.owdatasets.list_remote", Mock(side_effect=requests.exceptions.ConnectionError)) - @patch("Orange.widgets.data.owdatasets.OWDataSets.list_local", + @patch("Orange.widgets.data.owdatasets.list_local", Mock(return_value={})) @patch("Orange.widgets.data.owdatasets.log", Mock()) def test_no_internet_connection(self): @@ -21,9 +21,9 @@ def test_no_internet_connection(self): self.wait_until_stop_blocking(w) self.assertTrue(w.Error.no_remote_datasets.is_shown()) - @patch("Orange.widgets.data.owdatasets.OWDataSets.list_remote", + @patch("Orange.widgets.data.owdatasets.list_remote", Mock(side_effect=requests.exceptions.ConnectionError)) - @patch("Orange.widgets.data.owdatasets.OWDataSets.list_local", + @patch("Orange.widgets.data.owdatasets.list_local", Mock(return_value={('core', 'foo.tab'): {}})) @patch("Orange.widgets.data.owdatasets.log", Mock()) def test_only_local(self): @@ -32,9 +32,9 @@ def test_only_local(self): self.assertTrue(w.Warning.only_local_datasets.is_shown()) self.assertEqual(w.view.model().rowCount(), 1) - @patch("Orange.widgets.data.owdatasets.OWDataSets.list_remote", + @patch("Orange.widgets.data.owdatasets.list_remote", Mock(side_effect=requests.exceptions.ConnectionError)) - @patch("Orange.widgets.data.owdatasets.OWDataSets.list_local", + @patch("Orange.widgets.data.owdatasets.list_local", Mock(return_value={('core', 'foo.tab'): {}, ('core', 'bar.tab'): {}})) @patch("Orange.widgets.data.owdatasets.log", Mock()) @@ -49,9 +49,9 @@ def test_filtering(self): w.filterLineEdit.setText("") self.assertEqual(w.view.model().rowCount(), 2) - @patch("Orange.widgets.data.owdatasets.OWDataSets.list_remote", + @patch("Orange.widgets.data.owdatasets.list_remote", Mock(return_value={('core', 'iris.tab'): {}})) - @patch("Orange.widgets.data.owdatasets.OWDataSets.list_local", + @patch("Orange.widgets.data.owdatasets.list_local", Mock(return_value={})) @patch("Orange.widgets.data.owdatasets.ensure_local", Mock(return_value="iris.tab")) @@ -65,9 +65,9 @@ def test_download_iris(self): iris = self.get_output(w.Outputs.data, w) self.assertEqual(len(iris), 150) - @patch("Orange.widgets.data.owdatasets.OWDataSets.list_remote", + @patch("Orange.widgets.data.owdatasets.list_remote", Mock(side_effect=requests.exceptions.ConnectionError)) - @patch("Orange.widgets.data.owdatasets.OWDataSets.list_local", + @patch("Orange.widgets.data.owdatasets.list_local", Mock(return_value={('dir1', 'dir2', 'foo.tab'): {}, ('bar.tab',): {}})) @patch("Orange.widgets.data.owdatasets.log", Mock()) @@ -76,9 +76,9 @@ def test_dir_depth(self): self.wait_until_stop_blocking(w) self.assertEqual(w.view.model().rowCount(), 2) - @patch("Orange.widgets.data.owdatasets.OWDataSets.list_remote", + @patch("Orange.widgets.data.owdatasets.list_remote", Mock(return_value={('core', 'iris.tab'): {}})) - @patch("Orange.widgets.data.owdatasets.OWDataSets.list_local", + @patch("Orange.widgets.data.owdatasets.list_local", Mock(return_value={})) @patch("Orange.widgets.data.owdatasets.ensure_local", Mock(return_value="iris.tab"))