Skip to content

Commit

Permalink
Fix inconsistent path handling in WS history. Refs #5885
Browse files Browse the repository at this point in the history
Load is now not a special case and any FileProperty/MultipleFileProperty
value is prefixed with r to turn then into raw strings.
  • Loading branch information
martyngigg committed Mar 28, 2013
1 parent ce8b6f7 commit 1a5cea6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 42 deletions.
39 changes: 10 additions & 29 deletions Code/Mantid/Framework/Algorithms/src/GeneratePythonScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -200,34 +200,15 @@ std::string GeneratePythonScript::genParamString(
params += ",";
}

if(algHistName == "Load")
{
if(propHist.type() == "string")
{
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<FileProperty*> ((p));
if(NULL != fp)
{
params += name + "=r'" + value + "'";
}
else
{
params += name + "='" + value + "'";
}
}
// 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<FileProperty*>(p);
auto* mp = dynamic_cast<MultipleFileProperty*>(p);
if(fp || mp) params += name + "=r'" + value + "'";
else params += name + "='" + value + "'";
}
}

Expand Down
36 changes: 23 additions & 13 deletions Code/Mantid/Framework/Algorithms/test/GeneratePythonScriptTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,21 @@ class GeneratePythonScriptTest : public CxxTest::TestSuite
"######################################################################",
"#Python Script Generated by GeneratePythonScript Algorithm",
"######################################################################",
"LoadRaw(Filename='G:/Spencer/Science/Raw/irs26173.raw',OutputWorkspace='IPG',SpectrumMin='3',SpectrumMax='53')", // Not tested.
"LoadRaw(Filename=r'G:/Spencer/Science/Raw/irs26173.raw',OutputWorkspace='IPG',SpectrumMin='3',SpectrumMax='53')", // Not tested.
"ConvertUnits(InputWorkspace='IPG',OutputWorkspace='Spec',Target='Wavelength')",
"LoadRaw(Filename='G:/Spencer/Science/Raw/irs26173.raw',OutputWorkspace='Mon_in',SpectrumMax='1')", // Not tested.
"LoadRaw(Filename=r'G:/Spencer/Science/Raw/irs26173.raw',OutputWorkspace='Mon_in',SpectrumMax='1')", // Not tested.
"Unwrap(InputWorkspace='Mon_in',OutputWorkspace='Mon',LRef='37.86')",
"RemoveBins(InputWorkspace='Mon',OutputWorkspace='Mon',XMin='6.14600063416',XMax='6.14800063416',Interpolation='Linear')",
"FFTSmooth(InputWorkspace='Mon',OutputWorkspace='Mon')",
"RebinToWorkspace(WorkspaceToRebin='Spec',WorkspaceToMatch='Mon',OutputWorkspace='Spec')",
"LoadRaw(Filename='G:/Spencer/Science/Raw/irs26173.raw',OutputWorkspace='Mon_in',SpectrumMax='1')", // Not tested.
"LoadRaw(Filename=r'G:/Spencer/Science/Raw/irs26173.raw',OutputWorkspace='Mon_in',SpectrumMax='1')", // Not tested.
"Unwrap(InputWorkspace='Mon_in',OutputWorkspace='Mon',LRef='37.86')",
"RemoveBins(InputWorkspace='Mon',OutputWorkspace='Mon',XMin='6.14600063416',XMax='6.14800063416',Interpolation='Linear')",
"FFTSmooth(InputWorkspace='Mon',OutputWorkspace='Mon')",
"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='G:/Spencer/Science/Mantid/IRIS/PG1op3.map')", // Not tested.
"Load(Filename='C:/Mantid/Test/AutoTestData/IRS26173_ipg.nxs',OutputWorkspace='IRS26173_ipg')", // Not tested.
"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.
""
};

Expand Down Expand Up @@ -93,23 +93,33 @@ class GeneratePythonScriptTest : public CxxTest::TestSuite

int lineCount = 0;

// Compare the contents of the file to the expected result line-by-line,
// but only include the lines that do not contain a "Filename" or "MapFile" property.
// This way we still test most lines, but dont concern ourselves with
// the problem of forwardslashes or backslashes, or different directory structures.
// 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)
{
if(!(result[lineCount].find("Filename=") != std::string::npos ||
result[lineCount].find("MapFile=") != std::string::npos))
const std::string & algLine = *lineIter;
std::string::size_type filenamePos = algLine.find("Filename=");
std::string::size_type mapfilePos = algLine.find("MapFile=");
if(filenamePos != std::string::npos)
{
TS_ASSERT_EQUALS((*lineIter),result[lineCount]);
TS_ASSERT_EQUALS(algLine[filenamePos+9], 'r');
}
else if(mapfilePos != std::string::npos)
{
TS_ASSERT_EQUALS(algLine[mapfilePos+8], 'r');
}
else
{
TS_ASSERT_EQUALS(algLine,result[lineCount]);
}
lineCount++;
}

// Remove workspace from the data service.
// AnalysisDataService::Instance().remove(outWSName);
file.close();
file.close();
if (Poco::File(filename).exists()) Poco::File(filename).remove();
}
};
Expand Down

0 comments on commit 1a5cea6

Please sign in to comment.