Skip to content

Commit

Permalink
Add a check for the psutil package
Browse files Browse the repository at this point in the history
The system monitor is only made available if this package is present.
Refs #9843
  • Loading branch information
martyngigg committed Sep 19, 2014
1 parent c938574 commit e59fb62
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
63 changes: 44 additions & 19 deletions Code/Mantid/MantidPlot/src/ApplicationWindow.cpp
Expand Up @@ -436,7 +436,7 @@ void ApplicationWindow::init(bool factorySettings, const QStringList& args)

// Other docked widgets
m_interpreterDock = new QDockWidget(this);
m_sysMonitorDock = new QDockWidget(this);
if(psutilPresent()) m_sysMonitorDock = new QDockWidget(this);

// Needs to be done after initialization of dock windows,
// because we now use QDockWidget::toggleViewAction()
Expand Down Expand Up @@ -586,23 +586,26 @@ void ApplicationWindow::init(bool factorySettings, const QStringList& args)
{
addDockWidget(Qt::RightDockWidgetArea, mantidUI->m_exploreAlgorithms);
}
m_sysMonitorDock->setObjectName("systemMonitor"); // this is needed for QMainWindow::restoreState()
m_sysMonitorDock->setWindowTitle("System Monitor");
runPythonScript("from SysMon import sysmon\n"
"w = sysmon.SysMon(_qti.app._getSysMonitorDock())\n"
"_qti.app._getSysMonitorDock().setWidget(w)",
false,true,true);
if ( !restoreDockWidget(m_sysMonitorDock))
if(psutilPresent())
{
// Setting the max width to 300 and then to -1 later seems to
// be the only way that I found to get the dock to have a decent initial
// size but then still be resizable.
m_sysMonitorDock->setMaximumWidth(300);
addDockWidget(Qt::RightDockWidgetArea, m_sysMonitorDock);
m_sysMonitorDock->setMaximumWidth(-1);
m_sysMonitorDock->setObjectName("systemMonitor"); // this is needed for QMainWindow::restoreState()
m_sysMonitorDock->setWindowTitle("System Monitor");
runPythonScript("from SysMon import sysmon\n"
"w = sysmon.SysMon(_qti.app._getSysMonitorDock())\n"
"_qti.app._getSysMonitorDock().setWidget(w)",
false, true, true);
if ( !restoreDockWidget(m_sysMonitorDock))
{
// Setting the max width to 300 and then to -1 later seems to
// be the only way that I found to get the dock to have a decent initial
// size but then still be resizable.
m_sysMonitorDock->setMaximumWidth(300);
addDockWidget(Qt::RightDockWidgetArea, m_sysMonitorDock);
m_sysMonitorDock->setMaximumWidth(-1);
}
tabifyDockWidget(mantidUI->m_exploreAlgorithms, m_sysMonitorDock); // first, second in that order on tabs
mantidUI->m_exploreAlgorithms->raise();
}
tabifyDockWidget(mantidUI->m_exploreAlgorithms, m_sysMonitorDock); // first, second in that order on tabs
mantidUI->m_exploreAlgorithms->raise();

loadCustomActions();

Expand Down Expand Up @@ -1230,9 +1233,12 @@ void ApplicationWindow::initMainMenu()

mantidUI->addMenuItems(view);

view->insertSeparator();
m_sysMonitorDock->toggleViewAction()->setChecked(false);
view->addAction(m_sysMonitorDock->toggleViewAction());
if(psutilPresent())
{
view->insertSeparator();
m_sysMonitorDock->toggleViewAction()->setChecked(false);
view->addAction(m_sysMonitorDock->toggleViewAction());
}

view->insertSeparator();
toolbarsMenu = view->addMenu(tr("&Toolbars"));
Expand Down Expand Up @@ -16662,6 +16668,25 @@ bool ApplicationWindow::runPythonScript(const QString & code, bool async,
return success;
}

/// @return True if the psuitl python module is present and importable otherwise return false
bool ApplicationWindow::psutilPresent()
{
static bool checkPerformed(false);
static bool pkgPresent(false);

if(!checkPerformed)
{
g_log.debug("Checking for psutil\n");
using Mantid::Kernel::Logger;
bool verbose = g_log.is(Logger::Priority::PRIO_DEBUG);
pkgPresent = runPythonScript("import psutil", false, false, verbose);
if(pkgPresent) g_log.debug() << "Found psutil package";
else g_log.debug() << "Unable to find psutil package";
checkPerformed = true;
}

return pkgPresent;
}


bool ApplicationWindow::validFor2DPlot(Table *table)
Expand Down
2 changes: 2 additions & 0 deletions Code/Mantid/MantidPlot/src/ApplicationWindow.h
Expand Up @@ -238,6 +238,8 @@ public slots:
void onScriptExecuteError(const QString & message, const QString & scriptName, int lineNumber);
/// Runs an arbitrary lump of python code, return true/false on success/failure.
bool runPythonScript(const QString & code, bool async = false, bool quiet=false, bool redirect=true);
/// Checks for the presence of the Python psutil module for the system monitor widget
bool psutilPresent();

QList<MdiSubWindow *> windowsList() const;
QList<MdiSubWindow *> getAllWindows() const;
Expand Down

0 comments on commit e59fb62

Please sign in to comment.