From d534818f9add6e1e5167380a022be2b450d5c9b5 Mon Sep 17 00:00:00 2001 From: Srikanth Ravipati <rsri131@gmail.com> Date: Thu, 18 Mar 2021 13:23:39 +0000 Subject: [PATCH] Made memory widget into a package and a few other changes --- qt/applications/workbench/CMakeLists.txt | 4 +- .../workbench/workbench/app/mainwindow.py | 3 +- .../workbench/plugins/memorypresenter.py | 19 ------- .../plugins/memorywidget/__init__.py | 6 +++ .../plugins/{ => memorywidget}/memoryinfo.py | 4 +- .../plugins/memorywidget/memorypresenter.py | 17 ++++++ .../plugins/{ => memorywidget}/memoryview.py | 19 +++---- .../{ => memorywidget}/memorywidget.py | 9 ++-- .../plugins/test/test_memorypresenter.py | 52 ------------------- .../test/test_memorywidget/__init__.py | 6 +++ .../test_memoryinfo.py | 7 +-- .../test_memorywidget/test_memorypresenter.py | 46 ++++++++++++++++ 12 files changed, 97 insertions(+), 95 deletions(-) delete mode 100644 qt/applications/workbench/workbench/plugins/memorypresenter.py create mode 100644 qt/applications/workbench/workbench/plugins/memorywidget/__init__.py rename qt/applications/workbench/workbench/plugins/{ => memorywidget}/memoryinfo.py (90%) create mode 100644 qt/applications/workbench/workbench/plugins/memorywidget/memorypresenter.py rename qt/applications/workbench/workbench/plugins/{ => memorywidget}/memoryview.py (71%) rename qt/applications/workbench/workbench/plugins/{ => memorywidget}/memorywidget.py (80%) delete mode 100644 qt/applications/workbench/workbench/plugins/test/test_memorypresenter.py create mode 100644 qt/applications/workbench/workbench/plugins/test/test_memorywidget/__init__.py rename qt/applications/workbench/workbench/plugins/test/{ => test_memorywidget}/test_memoryinfo.py (77%) create mode 100644 qt/applications/workbench/workbench/plugins/test/test_memorywidget/test_memorypresenter.py diff --git a/qt/applications/workbench/CMakeLists.txt b/qt/applications/workbench/CMakeLists.txt index 9a37e0cd243..90c9c782887 100644 --- a/qt/applications/workbench/CMakeLists.txt +++ b/qt/applications/workbench/CMakeLists.txt @@ -161,8 +161,8 @@ set(TEST_FILES workbench/plugins/test/test_editor.py workbench/plugins/test/test_exception_handler.py workbench/plugins/test/test_jupyterconsole.py - workbench/plugins/test/test_memoryinfo.py - workbench/plugins/test/test_memorypresenter.py + workbench/plugins/test/test_memorywidget/test_memoryinfo.py + workbench/plugins/test/test_memorywidget/test_memorypresenter.py workbench/plugins/test/test_workspacewidget.py workbench/utils/test/test_workspacehistorygeneration.py workbench/widgets/plotselector/test/test_plotselector_model.py diff --git a/qt/applications/workbench/workbench/app/mainwindow.py b/qt/applications/workbench/workbench/app/mainwindow.py index 2c22dd07c3c..9dc4a28448c 100644 --- a/qt/applications/workbench/workbench/app/mainwindow.py +++ b/qt/applications/workbench/workbench/app/mainwindow.py @@ -174,10 +174,9 @@ class MainWindow(QMainWindow): self.widgets.append(self.workspacewidget) self.set_splash("Loading memory widget") - from workbench.plugins.memorywidget import MemoryWidget + from workbench.plugins.memorywidget.memorywidget import MemoryWidget self.memorywidget = MemoryWidget(self) self.memorywidget.register_plugin() - self.memorywidget.setMaximumHeight(70) self.widgets.append(self.memorywidget) # set the link between the algorithm and workspace widget diff --git a/qt/applications/workbench/workbench/plugins/memorypresenter.py b/qt/applications/workbench/workbench/plugins/memorypresenter.py deleted file mode 100644 index cb9092a6c9b..00000000000 --- a/qt/applications/workbench/workbench/plugins/memorypresenter.py +++ /dev/null @@ -1,19 +0,0 @@ -from qtpy.QtCore import QTimer - -from workbench.plugins.memoryinfo import getMemoryUsed - -class MemoryPresenter(object): - def __init__(self, view): - - self.view = view - self.updateMemoryUsage() - self.view.updateSignal.connect(self.updateMemoryUsage) - - self.timer = QTimer() - self.timer.timeout.connect(self.view.onUpdateRequest) - self.timer.start(10) - - def updateMemoryUsage(self): - mem_used_percent, mem_used, mem_avail = getMemoryUsed() - self.view.setValue(mem_used_percent, mem_used, mem_avail) - diff --git a/qt/applications/workbench/workbench/plugins/memorywidget/__init__.py b/qt/applications/workbench/workbench/plugins/memorywidget/__init__.py new file mode 100644 index 00000000000..ffedf5d542e --- /dev/null +++ b/qt/applications/workbench/workbench/plugins/memorywidget/__init__.py @@ -0,0 +1,6 @@ +# Mantid Repository : https://github.com/mantidproject/mantid +# +# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI, +# NScD Oak Ridge National Laboratory, European Spallation Source, +# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS +# SPDX - License - Identifier: GPL - 3.0 + diff --git a/qt/applications/workbench/workbench/plugins/memoryinfo.py b/qt/applications/workbench/workbench/plugins/memorywidget/memoryinfo.py similarity index 90% rename from qt/applications/workbench/workbench/plugins/memoryinfo.py rename to qt/applications/workbench/workbench/plugins/memorywidget/memoryinfo.py index e1a331547b3..b3cb7c56d91 100644 --- a/qt/applications/workbench/workbench/plugins/memoryinfo.py +++ b/qt/applications/workbench/workbench/plugins/memorywidget/memoryinfo.py @@ -8,12 +8,12 @@ # # from psutil import virtual_memory +conversion_factor_to_GB = 1.0 / (1024 * 1024 * 1024) -def getMemoryUsed(): +def get_memory_info(): mem_used = virtual_memory().used mem_total = virtual_memory().total mem_used_percent = min(int(round(mem_used * 100 / mem_total)),100) - conversion_factor_to_GB = 1.0 / 1024 / 1024 / 1024 mem_used_GB = mem_used * conversion_factor_to_GB mem_total_GB = mem_total * conversion_factor_to_GB return mem_used_percent, mem_used_GB, mem_total_GB diff --git a/qt/applications/workbench/workbench/plugins/memorywidget/memorypresenter.py b/qt/applications/workbench/workbench/plugins/memorywidget/memorypresenter.py new file mode 100644 index 00000000000..4dfa0f6e82b --- /dev/null +++ b/qt/applications/workbench/workbench/plugins/memorywidget/memorypresenter.py @@ -0,0 +1,17 @@ +from qtpy.QtCore import QTimer + +from ..memorywidget.memoryinfo import get_memory_info + +class MemoryPresenter(object): + def __init__(self, view): + + self.view = view + self.update_memory_usage() + + self.timer = QTimer() + self.timer.timeout.connect(self.update_memory_usage) + self.timer.start(10) + + def update_memory_usage(self): + mem_used_percent, mem_used, mem_avail = get_memory_info() + self.view.set_value(mem_used_percent, mem_used, mem_avail) diff --git a/qt/applications/workbench/workbench/plugins/memoryview.py b/qt/applications/workbench/workbench/plugins/memorywidget/memoryview.py similarity index 71% rename from qt/applications/workbench/workbench/plugins/memoryview.py rename to qt/applications/workbench/workbench/plugins/memorywidget/memoryview.py index c40390d6c37..1adf5bbaed6 100644 --- a/qt/applications/workbench/workbench/plugins/memoryview.py +++ b/qt/applications/workbench/workbench/plugins/memorywidget/memoryview.py @@ -8,7 +8,6 @@ # # from qtpy.QtWidgets import QWidget, QProgressBar -from qtpy.QtCore import Signal NORMAL_STYLE = """ QProgressBar::chunk { @@ -22,15 +21,14 @@ QProgressBar::chunk { } """ -def fromNormalToCritical(critical, currentValue, newValue)->bool: +def from_normal_to_critical(critical, currentValue, newValue)->bool: return (currentValue < critical and newValue >= critical) -def fromCriticalToNormal(critical, currentValue, newValue)->bool: +def from_critical_to_normal(critical, currentValue, newValue)->bool: return (currentValue >= critical and newValue < critical) class MemoryView(QWidget): - updateSignal = Signal() def __init__(self, parent): super(MemoryView, self).__init__(parent) @@ -38,23 +36,20 @@ class MemoryView(QWidget): self.critical = 90 self.memory_bar = QProgressBar(self) - def setBarColor(self, currentValue, newValue): - if (fromNormalToCritical(self.critical, currentValue, newValue)): + def set_bar_color(self, currentValue, newValue): + if (from_normal_to_critical(self.critical, currentValue, newValue)): self.memory_bar.setStyleSheet(CRITICAL_STYLE) - elif (fromCriticalToNormal(self.critical, currentValue, newValue)): + elif (from_critical_to_normal(self.critical, currentValue, newValue)): self.memory_bar.setStyleSheet(NORMAL_STYLE) else: pass - def setValue(self, newValue, mem_used, mem_avail): + def set_value(self, newValue, mem_used, mem_avail): # newValue is the mem_used_percent(int) currentValue = self.memory_bar.value() if currentValue != newValue: - self.setBarColor(currentValue, newValue) + self.set_bar_color(currentValue, newValue) self.memory_bar.setValue(newValue) display_str = "%3.1f"%mem_used + "/" + "%3.1f"%mem_avail + " GB " + \ "(" + "%d"%newValue+"%" +")" self.memory_bar.setFormat(display_str) - - def onUpdateRequest(self): - self.updateSignal.emit() diff --git a/qt/applications/workbench/workbench/plugins/memorywidget.py b/qt/applications/workbench/workbench/plugins/memorywidget/memorywidget.py similarity index 80% rename from qt/applications/workbench/workbench/plugins/memorywidget.py rename to qt/applications/workbench/workbench/plugins/memorywidget/memorywidget.py index dbd59f67df9..0d8ff4cd165 100644 --- a/qt/applications/workbench/workbench/plugins/memorywidget.py +++ b/qt/applications/workbench/workbench/plugins/memorywidget/memorywidget.py @@ -9,9 +9,9 @@ # from qtpy.QtWidgets import QVBoxLayout -from workbench.plugins.base import PluginWidget -from workbench.plugins.memoryview import MemoryView -from workbench.plugins.memorypresenter import MemoryPresenter +from ..base import PluginWidget +from ..memorywidget.memoryview import MemoryView +from ..memorywidget.memorypresenter import MemoryPresenter class MemoryWidget(PluginWidget): @@ -26,6 +26,9 @@ class MemoryWidget(PluginWidget): self.setLayout(layout) self.setWindowTitle(self.get_plugin_title()) + # 70 is chosen as a good value after testing + # how it looks for different values + self.setMaximumHeight(70) # ----------------- Plugin API -------------------- diff --git a/qt/applications/workbench/workbench/plugins/test/test_memorypresenter.py b/qt/applications/workbench/workbench/plugins/test/test_memorypresenter.py deleted file mode 100644 index ca88fd1ab14..00000000000 --- a/qt/applications/workbench/workbench/plugins/test/test_memorypresenter.py +++ /dev/null @@ -1,52 +0,0 @@ -# Mantid Repository : https://github.com/mantidproject/mantid -# -# Copyright © 2019 ISIS Rutherford Appleton Laboratory UKRI, -# NScD Oak Ridge National Laboratory, European Spallation Source, -# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS -# SPDX - License - Identifier: GPL - 3.0 + -# This file is part of the mantid workbench. -# -# -from workbench.plugins.memoryview import MemoryView -from workbench.plugins.memoryview import fromNormalToCritical, fromCriticalToNormal -from workbench.plugins.memorypresenter import MemoryPresenter - - -import unittest -from unittest import mock - -class MemoryPresenterTest(unittest.TestCase): - def setUp(self): - self.view = mock.create_autospec(MemoryView) - - self.view.updateSignal = mock.Mock() - self.view.critical = 90 - self.view.memory_bar = mock.Mock() - self.view.setBarColor = mock.Mock() - self.view.setValue = mock.Mock() - self.view.onUpdateRequest = mock.Mock() - - self.presenter = MemoryPresenter(self.view) - - def test_doSomething(self): - self.assertTrue(fromNormalToCritical(self.presenter.view.critical, - 75, 95)) - self.assertFalse(fromCriticalToNormal(self.presenter.view.critical, - 75, 95)) - - self.assertFalse(fromNormalToCritical(self.presenter.view.critical, - 95, 75)) - self.assertTrue(fromCriticalToNormal(self.presenter.view.critical, - 95, 75)) - - self.assertEqual(self.presenter.view.setValue.call_count, 1) - - self.view.memory_bar.value.retun_value = 95 - self.presenter.view.memory_bar.value.retun_value = 95 - - self.presenter.updateMemoryUsage() - self.assertEqual(self.presenter.view.setValue.call_count, 2) - - -if __name__ == "__main__": - unittest.main() diff --git a/qt/applications/workbench/workbench/plugins/test/test_memorywidget/__init__.py b/qt/applications/workbench/workbench/plugins/test/test_memorywidget/__init__.py new file mode 100644 index 00000000000..ffedf5d542e --- /dev/null +++ b/qt/applications/workbench/workbench/plugins/test/test_memorywidget/__init__.py @@ -0,0 +1,6 @@ +# Mantid Repository : https://github.com/mantidproject/mantid +# +# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI, +# NScD Oak Ridge National Laboratory, European Spallation Source, +# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS +# SPDX - License - Identifier: GPL - 3.0 + diff --git a/qt/applications/workbench/workbench/plugins/test/test_memoryinfo.py b/qt/applications/workbench/workbench/plugins/test/test_memorywidget/test_memoryinfo.py similarity index 77% rename from qt/applications/workbench/workbench/plugins/test/test_memoryinfo.py rename to qt/applications/workbench/workbench/plugins/test/test_memorywidget/test_memoryinfo.py index 2a2abfab949..788b4ac8bc0 100644 --- a/qt/applications/workbench/workbench/plugins/test/test_memoryinfo.py +++ b/qt/applications/workbench/workbench/plugins/test/test_memorywidget/test_memoryinfo.py @@ -8,12 +8,13 @@ # # import unittest -from workbench.plugins.memoryinfo import getMemoryUsed +from workbench.plugins.memorywidget.memoryinfo import get_memory_info + class MemoryInfoTest(unittest.TestCase): - def test_MemoryInfo(self): - mem_used_percent, mem_used, mem_total = getMemoryUsed() + def test_memoryinfo(self): + mem_used_percent, mem_used, mem_total = get_memory_info() self.assertTrue(isinstance(mem_used_percent, int)) self.assertTrue(0 <= mem_used_percent <= 100) diff --git a/qt/applications/workbench/workbench/plugins/test/test_memorywidget/test_memorypresenter.py b/qt/applications/workbench/workbench/plugins/test/test_memorywidget/test_memorypresenter.py new file mode 100644 index 00000000000..7302e272663 --- /dev/null +++ b/qt/applications/workbench/workbench/plugins/test/test_memorywidget/test_memorypresenter.py @@ -0,0 +1,46 @@ +# Mantid Repository : https://github.com/mantidproject/mantid +# +# Copyright © 2019 ISIS Rutherford Appleton Laboratory UKRI, +# NScD Oak Ridge National Laboratory, European Spallation Source, +# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS +# SPDX - License - Identifier: GPL - 3.0 + +# This file is part of the mantid workbench. +# +# +from workbench.plugins.memorywidget.memoryview import MemoryView, \ + from_normal_to_critical, from_critical_to_normal +from workbench.plugins.memorywidget.memorypresenter import MemoryPresenter + +import unittest +from unittest import mock + + +class MemoryPresenterTest(unittest.TestCase): + def setUp(self): + self.view = mock.create_autospec(MemoryView) + + self.view.critical = 90 + self.view.memory_bar = mock.Mock() + self.view.set_bar_color = mock.Mock() + self.view.set_value = mock.Mock() + + self.presenter = MemoryPresenter(self.view) + + def test_presenter(self): + self.assertTrue(from_normal_to_critical(self.presenter.view.critical, + 75, 95)) + self.assertFalse(from_critical_to_normal(self.presenter.view.critical, + 75, 95)) + + self.assertFalse(from_normal_to_critical(self.presenter.view.critical, + 95, 75)) + self.assertTrue(from_critical_to_normal(self.presenter.view.critical, + 95, 75)) + + self.assertEqual(self.presenter.view.set_value.call_count, 1) + + self.presenter.update_memory_usage() + self.assertEqual(self.presenter.view.set_value.call_count, 2) + +if __name__ == "__main__": + unittest.main() -- GitLab