From b7c6c8b2a7bbc98fedaa5334ae28156e08efaf1b Mon Sep 17 00:00:00 2001 From: Vesna Tanko Date: Wed, 2 Nov 2016 12:56:50 +0100 Subject: [PATCH] OWFile: Do not load large files automatically --- Orange/widgets/data/owfile.py | 16 +++++++++++++--- Orange/widgets/data/tests/test_owfile.py | 6 +++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Orange/widgets/data/owfile.py b/Orange/widgets/data/owfile.py index fd2ca9e4247..52113d9dae3 100644 --- a/Orange/widgets/data/owfile.py +++ b/Orange/widgets/data/owfile.py @@ -91,7 +91,7 @@ class OWFile(widget.OWWidget, RecentPathsWComboMixin): want_main_area = False SEARCH_PATHS = [("sample-datasets", get_sample_datasets_dir())] - + SIZE_LIMIT = 1e7 LOCAL_FILE, URL = range(2) settingsHandler = PerfectDomainContextHandler() @@ -118,6 +118,10 @@ class OWFile(widget.OWWidget, RecentPathsWComboMixin): for f in sorted(set(FileFormat.readers.values()), key=list(FileFormat.readers.values()).index))) + class Warning(widget.OWWidget.Warning): + file_too_big = widget.Msg("The file is too large to load automatically." + " Press Reload to load.") + def __init__(self): super().__init__() RecentPathsWComboMixin.__init__(self) @@ -214,10 +218,16 @@ def __init__(self): self.set_file_list() # Must not call open_file from within __init__. open_file # explicitly re-enters the event loop (by a progress bar) - QTimer.singleShot(0, self.load_data) self.setAcceptDrops(True) + if self.source == self.LOCAL_FILE and \ + os.path.getsize(self.last_path()) > self.SIZE_LIMIT: + self.Warning.file_too_big() + return + + QTimer.singleShot(0, self.load_data) + def sizeHint(self): return QSize(600, 550) @@ -263,7 +273,7 @@ def load_data(self): # file readers # pylint: disable=broad-except self.editor_model.set_domain(None) - + self.Warning.file_too_big.clear() error = None try: self.reader = self._get_reader() diff --git a/Orange/widgets/data/tests/test_owfile.py b/Orange/widgets/data/tests/test_owfile.py index 8fae90a8318..761ace701eb 100644 --- a/Orange/widgets/data/tests/test_owfile.py +++ b/Orange/widgets/data/tests/test_owfile.py @@ -64,4 +64,8 @@ def _drop_event(self, url): QPoint(0, 0), Qt.MoveAction, data, Qt.NoButton, Qt.NoModifier, QDropEvent.Drop) - + def test_check_file_size(self): + self.assertFalse(self.widget.Warning.file_too_big.is_shown()) + self.widget.SIZE_LIMIT = 4000 + self.widget.__init__() + self.assertTrue(self.widget.Warning.file_too_big.is_shown())