Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/8599_improve_python_hist…
Browse files Browse the repository at this point in the history
…_script'
  • Loading branch information
PeterParker committed Dec 13, 2013
2 parents d02a75f + 27eb18a commit fba37bc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
27 changes: 14 additions & 13 deletions Code/Mantid/Framework/Algorithms/src/GeneratePythonScript.cpp
Expand Up @@ -58,8 +58,9 @@ void GeneratePythonScript::init()
std::vector<std::string> exts;
exts.push_back(".py");

declareProperty(new API::FileProperty("Filename","", API::FileProperty::Save, exts),
declareProperty(new API::FileProperty("Filename","", API::FileProperty::OptionalSave, exts),
"The file into which the Python script will be generated.");
declareProperty("ScriptText", "",Direction::Output);
}

//----------------------------------------------------------------------------------------------
Expand All @@ -68,15 +69,6 @@ void GeneratePythonScript::init()
void GeneratePythonScript::exec()
{
const Workspace_const_sptr ws = getProperty("InputWorkspace");
const std::string filename = getPropertyValue("Filename");
std::ofstream file(filename.c_str(), std::ofstream::trunc);


if (NULL == file)
{
g_log.error("Unable to create file: " + filename);
throw Exception::FileError("Unable to create file: " , filename);
}

// Get the algorithm histories of the workspace.
const WorkspaceHistory wsHistory = ws->getHistory();
Expand Down Expand Up @@ -104,9 +96,18 @@ void GeneratePythonScript::exec()
generatedScript += *m3_pIter + "\n";
}

file << generatedScript;
file.flush();
file.close();
setPropertyValue("ScriptText", generatedScript);

const std::string filename = getPropertyValue("Filename");

if (!filename.empty())
{
std::ofstream file(filename.c_str(), std::ofstream::trunc);
file << generatedScript;
file.flush();
file.close();
}

}
//----------------------------------------------------------------------------------------------
/** Generate the line of script corresponding to the given AlgorithmHistory
Expand Down
Expand Up @@ -52,21 +52,26 @@ class GeneratePythonScriptTest : public CxxTest::TestSuite
TS_ASSERT( alg.isInitialized() );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("InputWorkspace", workspaceName) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Filename", "GeneratePythonScriptTest.py") );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("ScriptText", "") );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );


// Compare the contents of the file to the expected result line-by-line.
std::string filename = alg.getProperty("Filename");
std::ifstream file(filename.c_str(), std::ifstream::in);
std::string scriptLine;
int lineCount(0);

while(std::getline(file, scriptLine))
{
TS_ASSERT_EQUALS(scriptLine,result[lineCount]);
lineCount++;
}

// Verify that if we set the content of ScriptText that it is set correctly.
alg.setPropertyValue("ScriptText", result[4]);
TS_ASSERT_EQUALS(alg.getPropertyValue("ScriptText"), "CropWorkspace(InputWorkspace='testGeneratePython',OutputWorkspace='testGeneratePython',XMin='2',XMax='5')");

file.close();
if (Poco::File(filename).exists()) Poco::File(filename).remove();
}
Expand Down

0 comments on commit fba37bc

Please sign in to comment.