Skip to content

Commit

Permalink
Make script usable for the current session of Mantid
Browse files Browse the repository at this point in the history
In order to make the script usable as soon as it is downloade, it is not
enough to update the pythonscripts.directories as the last commit did, but
you have also to add to the python sys path at the current running session.

Running python code is not possible inside the Framework/ScriptRepository
level. So, a notification strategy was used in order to trigger a method
of MantidUI, which can execute the python code that is necessary to add
the new paths to the python sys.

re #7097
  • Loading branch information
gesnerpassos authored and OwenArnold committed May 29, 2013
1 parent 05e0aec commit 6f53f2d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Code/Mantid/MantidPlot/src/Mantid/MantidUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ m_finishedLoadDAEObserver(*this, &MantidUI::handleLoadDAEFinishedNotification),
m_groupworkspacesObserver(*this,&MantidUI::handleGroupWorkspaces),
m_ungroupworkspaceObserver(*this,&MantidUI::handleUnGroupWorkspace),
m_workspaceGroupUpdateObserver(*this,&MantidUI::handleWorkspaceGroupUpdate),
m_configServiceObserver(*this,&MantidUI::handleConfigServiceUpdate),
m_appWindow(aw), m_vatesSubWindow(NULL)
{

Expand Down Expand Up @@ -181,6 +182,7 @@ void MantidUI::init()
dataStore.notificationCenter.addObserver(m_groupworkspacesObserver);
dataStore.notificationCenter.addObserver(m_ungroupworkspaceObserver);
dataStore.notificationCenter.addObserver(m_workspaceGroupUpdateObserver);
Mantid::Kernel::ConfigService::Instance().addObserver(m_configServiceObserver);

m_exploreAlgorithms->update();
try
Expand Down Expand Up @@ -288,6 +290,7 @@ MantidUI::~MantidUI()
{
delete m_algMonitor;

Mantid::Kernel::ConfigService::Instance().removeObserver(m_configServiceObserver);
Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_groupworkspacesObserver);
Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_ungroupworkspaceObserver);
Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_workspaceGroupUpdateObserver);
Expand Down Expand Up @@ -1876,6 +1879,25 @@ void MantidUI::handleWorkspaceGroupUpdate(Mantid::API::GroupUpdatedNotification_
emit workspace_group_updated( name );
}

void MantidUI::handleConfigServiceUpdate(Mantid::Kernel::ConfigValChangeNotification_ptr pNf){
if (pNf->key() == "pythonscripts.directories"){
// this code ad the filepaths inside the pythonscripts.directories to the
// python sys if they are not already there. This is to cope with the requirement
// at #7097 of letting python scripts usable when downloaded from Script Repository.
// This code was added because changing the pythonscripts.directories update the
// python path just after restarting MantidPlot.
QString code = QString("import sys\n\
paths = '%1'\n\
list_of_path = paths.split(';')\n\
if isinstance(list_of_path,str):\n\
list_of_path = [list_of_path,]\n\
for value in list_of_path:\n\
if value not in sys.path: sys.path.append(value)\n").arg(QString::fromStdString(pNf->curValue()));
// run this code silently
appWindow()->runPythonScript(code, false, true, true);
}
}

void MantidUI::manageMantidWorkspaces()
{
#ifdef _WIN32
Expand Down
4 changes: 4 additions & 0 deletions Code/Mantid/MantidPlot/src/Mantid/MantidUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,10 @@ public slots:
void handleWorkspaceGroupUpdate(Mantid::API::GroupUpdatedNotification_ptr pNf);
Poco::NObserver<MantidUI, Mantid::API::GroupUpdatedNotification> m_workspaceGroupUpdateObserver;

// handles notification send by ConfigService, change on pythonscripts.directories
void handleConfigServiceUpdate(Mantid::Kernel::ConfigValChangeNotification_ptr pNf);
Poco::NObserver<MantidUI, Mantid::Kernel::ConfigValChangeNotification> m_configServiceObserver;

//#678
//for savenexus algorithm
void executeSaveNexus(QString algName,int version);
Expand Down

0 comments on commit 6f53f2d

Please sign in to comment.