From 5a126c0614dd0f1f02ed969cffcf76eb1ec91dad Mon Sep 17 00:00:00 2001 From: Jay Rainey Date: Fri, 13 Dec 2013 12:07:32 +0000 Subject: [PATCH 1/4] Make filename optional & add ScriptText property. Refs #8599. --- .../Algorithms/src/GeneratePythonScript.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Code/Mantid/Framework/Algorithms/src/GeneratePythonScript.cpp b/Code/Mantid/Framework/Algorithms/src/GeneratePythonScript.cpp index eec03cd04c85..def422709e07 100644 --- a/Code/Mantid/Framework/Algorithms/src/GeneratePythonScript.cpp +++ b/Code/Mantid/Framework/Algorithms/src/GeneratePythonScript.cpp @@ -58,8 +58,9 @@ void GeneratePythonScript::init() std::vector 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); } //---------------------------------------------------------------------------------------------- @@ -104,9 +105,15 @@ void GeneratePythonScript::exec() generatedScript += *m3_pIter + "\n"; } - file << generatedScript; - file.flush(); - file.close(); + setPropertyValue("ScriptText", generatedScript); + + if (!filename.empty()) + { + file << generatedScript; + file.flush(); + file.close(); + } + } //---------------------------------------------------------------------------------------------- /** Generate the line of script corresponding to the given AlgorithmHistory From ec75e47ae2a8418998877977c3ae41eed563430b Mon Sep 17 00:00:00 2001 From: Jay Rainey Date: Fri, 13 Dec 2013 12:08:41 +0000 Subject: [PATCH 2/4] Updated unit test to verify ScriptText is correct. Refs #8599. --- .../Framework/Algorithms/test/GeneratePythonScriptTest.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Code/Mantid/Framework/Algorithms/test/GeneratePythonScriptTest.h b/Code/Mantid/Framework/Algorithms/test/GeneratePythonScriptTest.h index a7e49bfdbcf7..e93ae3f49679 100644 --- a/Code/Mantid/Framework/Algorithms/test/GeneratePythonScriptTest.h +++ b/Code/Mantid/Framework/Algorithms/test/GeneratePythonScriptTest.h @@ -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(); } From 9e620c25c53a33dc7b9b9869d3185857bcf9712b Mon Sep 17 00:00:00 2001 From: Jay Rainey Date: Fri, 13 Dec 2013 12:47:43 +0000 Subject: [PATCH 3/4] Remove file error checking. Refs #8599. --- .../Framework/Algorithms/src/GeneratePythonScript.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Code/Mantid/Framework/Algorithms/src/GeneratePythonScript.cpp b/Code/Mantid/Framework/Algorithms/src/GeneratePythonScript.cpp index def422709e07..f753dc5e021c 100644 --- a/Code/Mantid/Framework/Algorithms/src/GeneratePythonScript.cpp +++ b/Code/Mantid/Framework/Algorithms/src/GeneratePythonScript.cpp @@ -72,13 +72,6 @@ void GeneratePythonScript::exec() 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(); From 27eb18a3467adf49cd317aa585b999eb002a637e Mon Sep 17 00:00:00 2001 From: Jay Rainey Date: Fri, 13 Dec 2013 12:53:43 +0000 Subject: [PATCH 4/4] Only attempt to open file if property set. Refs #8599. --- .../Mantid/Framework/Algorithms/src/GeneratePythonScript.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Code/Mantid/Framework/Algorithms/src/GeneratePythonScript.cpp b/Code/Mantid/Framework/Algorithms/src/GeneratePythonScript.cpp index f753dc5e021c..073b92f855c6 100644 --- a/Code/Mantid/Framework/Algorithms/src/GeneratePythonScript.cpp +++ b/Code/Mantid/Framework/Algorithms/src/GeneratePythonScript.cpp @@ -69,8 +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); // Get the algorithm histories of the workspace. const WorkspaceHistory wsHistory = ws->getHistory(); @@ -100,8 +98,11 @@ void GeneratePythonScript::exec() 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();