From 73d6288bc527c9f344de92a83a93153d317a4c52 Mon Sep 17 00:00:00 2001 From: Martyn Gigg Date: Fri, 29 Mar 2013 08:31:15 +0000 Subject: [PATCH] Fix GeneratePythonScript for Load algorithm Refs #6812 It does not currently handle properties that don't exist when the algorithm is initialized very well. --- .../Algorithms/src/GeneratePythonScript.cpp | 41 ++++++++++++----- .../test/GeneratePythonScriptTest.h | 45 ++++++++----------- 2 files changed, 49 insertions(+), 37 deletions(-) diff --git a/Code/Mantid/Framework/Algorithms/src/GeneratePythonScript.cpp b/Code/Mantid/Framework/Algorithms/src/GeneratePythonScript.cpp index f93a133f1f3c..96199c8616b7 100644 --- a/Code/Mantid/Framework/Algorithms/src/GeneratePythonScript.cpp +++ b/Code/Mantid/Framework/Algorithms/src/GeneratePythonScript.cpp @@ -21,8 +21,8 @@ Example format: *WIKI*/ #include "MantidAlgorithms/GeneratePythonScript.h" +#include "MantidKernel/System.h" #include "MantidAPI/FileProperty.h" -#include "MantidAPI/MultipleFileProperty.h" #include "MantidAPI/AlgorithmManager.h" #include "MantidAPI/AlgorithmHistory.h" @@ -200,15 +200,36 @@ std::string GeneratePythonScript::genParamString( params += ","; } - // Try and cast the property to a MultiFileProperty or FileProperty type. If successful, we have to guard against - // the the occurence of directory separators (forward or back slashes) in the file path that the - // property contains. We do this by appending "r" onto the parameter in the output, to make it - // a Python "raw" string. - Property *p = ialg_Sptr->getPointerToProperty(name); - auto* fp = dynamic_cast(p); - auto* mp = dynamic_cast(p); - if(fp || mp) params += name + "=r'" + value + "'"; - else params += name + "='" + value + "'"; + // Needs to deal with properties that don't exist + // yet in a much better way + if(algHistName == "Load") + { + if(name == "Filename") + { + params += name + "=r'" + value + "'"; + } + else + { + params += name + "='" + value + "'"; + } + } + else + { + // Try and cast the property to a FileProperty type. If successful, we have to guard against + // the the occurence of directory separators (forward or back slashes) in the file path that the + // property contains. We do this by appending "r" onto the parameter in the output, to make it + // a Python "raw" string. + Property *p = ialg_Sptr->getPointerToProperty(name); + FileProperty* fp = dynamic_cast ((p)); + if(NULL != fp) + { + params += name + "=r'" + value + "'"; + } + else + { + params += name + "='" + value + "'"; + } + } } } diff --git a/Code/Mantid/Framework/Algorithms/test/GeneratePythonScriptTest.h b/Code/Mantid/Framework/Algorithms/test/GeneratePythonScriptTest.h index 9a7cdc145018..6efa04d9aa6e 100644 --- a/Code/Mantid/Framework/Algorithms/test/GeneratePythonScriptTest.h +++ b/Code/Mantid/Framework/Algorithms/test/GeneratePythonScriptTest.h @@ -36,6 +36,7 @@ class GeneratePythonScriptTest : public CxxTest::TestSuite // a valid choice to test the output of the Algorithm against. loader.setPropertyValue("Filename", "IRS26173_ipg.nxs"); loader.setPropertyValue("OutputWorkspace","LoadedWorkspace"); + loader.setPropertyValue("SpectrumMax","1"); loader.setRethrows(true); std::string result[] = { @@ -56,7 +57,7 @@ class GeneratePythonScriptTest : public CxxTest::TestSuite "Divide(LHSWorkspace='Spec',RHSWorkspace='Mon',OutputWorkspace='Spec')", "ConvertUnits(InputWorkspace='Spec',OutputWorkspace='Spec',Target='DeltaE',EMode='Indirect',EFixed='1.84')", "GroupDetectors(InputWorkspace='Spec',OutputWorkspace='IPG_3',MapFile=r'G:/Spencer/Science/Mantid/IRIS/PG1op3.map')", // Not tested. - "Load(Filename=r'C:/Mantid/Test/AutoTestData/IRS26173_ipg.nxs',OutputWorkspace='IRS26173_ipg')", // Not tested. + "Load(Filename=r'C:/Mantid/Test/AutoTestData/IRS26173_ipg.nxs',OutputWorkspace='IRS26173_ipg',SpectrumMax='1')", // Not tested. "" }; @@ -76,51 +77,41 @@ class GeneratePythonScriptTest : public CxxTest::TestSuite TS_ASSERT_THROWS_NOTHING( alg.execute(); ); TS_ASSERT( alg.isExecuted() ); - // Read in the file, and parse each line into a vector of strings. - std::string filename = alg.getProperty("Filename"); - std::ifstream file(filename.c_str(), std::ifstream::in); - std::vector lines; - - while (file.good()) - { - char testArray[256]; - file.getline(testArray, 256); - std::string line = testArray; - lines.push_back(line); - } - - std::vector::iterator lineIter = lines.begin(); - - int lineCount = 0; // Compare the contents of the file to the expected result line-by-line. // If the line contains Filename or MapFile then just check that the string // is prefixed with r to convert it to a Python raw string and not the actual content // as the file paths are different - for( ; lineIter != lines.end(); ++lineIter) + 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)) { - const std::string & algLine = *lineIter; - std::string::size_type filenamePos = algLine.find("Filename="); - std::string::size_type mapfilePos = algLine.find("MapFile="); + std::string::size_type filenamePos = scriptLine.find("Filename="); + std::string::size_type mapfilePos = scriptLine.find("MapFile="); if(filenamePos != std::string::npos) { - TS_ASSERT_EQUALS(algLine[filenamePos+9], 'r'); + TS_ASSERT_EQUALS(scriptLine[filenamePos+9], 'r'); + if(scriptLine.find("Load(") != std::string::npos) + { + // Check Load call has SpectrumMax + TS_ASSERT(scriptLine.find("SpectrumMax='1'") != std::string::npos); + } } else if(mapfilePos != std::string::npos) { - TS_ASSERT_EQUALS(algLine[mapfilePos+8], 'r'); + TS_ASSERT_EQUALS(scriptLine[mapfilePos+8], 'r'); } else { - TS_ASSERT_EQUALS(algLine,result[lineCount]); + TS_ASSERT_EQUALS(scriptLine,result[lineCount]); } lineCount++; } - // Remove workspace from the data service. - // AnalysisDataService::Instance().remove(outWSName); file.close(); - if (Poco::File(filename).exists()) Poco::File(filename).remove(); + //if (Poco::File(filename).exists()) Poco::File(filename).remove(); } };