diff --git a/Framework/PythonInterface/mantid/api/src/Exports/MultipleFileProperty.cpp b/Framework/PythonInterface/mantid/api/src/Exports/MultipleFileProperty.cpp index 2483eab62bcec70d783d95b9060f8a0c5b152559..994b1a98c51c1199261424f27d62d2d7eac6c2ae 100644 --- a/Framework/PythonInterface/mantid/api/src/Exports/MultipleFileProperty.cpp +++ b/Framework/PythonInterface/mantid/api/src/Exports/MultipleFileProperty.cpp @@ -1,11 +1,15 @@ #include "MantidAPI/MultipleFileProperty.h" +#include "MantidPythonInterface/kernel/Converters/PySequenceToVector.h" +#include "MantidPythonInterface/kernel/IsNone.h" #include "MantidPythonInterface/kernel/PropertyWithValueExporter.h" #include <boost/python/class.hpp> #include <boost/python/list.hpp> +#include <boost/python/make_constructor.hpp> #include <boost/python/str.hpp> using Mantid::API::MultipleFileProperty; using Mantid::Kernel::PropertyWithValue; +using Mantid::PythonInterface::Converters::PySequenceToVector; using Mantid::PythonInterface::PropertyWithValueExporter; using namespace boost::python; @@ -22,29 +26,37 @@ typedef std::vector<std::vector<std::string>> HeldType; */ boost::python::object valueAsPyObject(MultipleFileProperty &self) { const HeldType &propValue = self(); - boost::python::object result; - if (propValue.size() == 1 && - propValue[0].size() == 1) // Special case, unwrap the vector - { - result = boost::python::str(propValue[0][0]); - } else { - // Build a list of lists to mimic the behaviour of MultipleFileProperty - boost::python::list fileList; - for (const auto &filenames : propValue) { - if (filenames.size() == 1) { - fileList.append(filenames.front()); - } else { - boost::python::list groupList; - for (const auto &filename : filenames) { - groupList.append(filename); - } - fileList.append(groupList); + + // Build a list of lists to mimic the behaviour of MultipleFileProperty + boost::python::list fileList; + for (const auto &filenames : propValue) { + if (filenames.size() == 1) { + fileList.append(filenames.front()); + } else { + boost::python::list groupList; + for (const auto &filename : filenames) { + groupList.append(filename); } + fileList.append(groupList); } - result = fileList; } - return result; + return fileList; +} + +MultipleFileProperty * +createMultipleFileProperty(const std::string &name, + const object &extensions = object()) { + std::vector<std::string> extsAsVector; + if (!Mantid::PythonInterface::isNone(extensions)) { + extract<std::string> extractor(extensions); + if (extractor.check()) { + extsAsVector = std::vector<std::string>(1, extractor()); + } else { + extsAsVector = PySequenceToVector<std::string>(extensions)(); + } + } + return new MultipleFileProperty(name, extsAsVector); } } @@ -55,6 +67,9 @@ void export_MultipleFileProperty() { class_<MultipleFileProperty, bases<BaseClass>, boost::noncopyable>( "MultipleFileProperty", no_init) + .def("__init__", make_constructor( + &createMultipleFileProperty, default_call_policies(), + (arg("name"), arg("extensions") = object()))) // Override the base class one to do something more appropriate .add_property("value", &valueAsPyObject); } diff --git a/Framework/PythonInterface/test/python/mantid/api/MultipleFilePropertyTest.py b/Framework/PythonInterface/test/python/mantid/api/MultipleFilePropertyTest.py index a1b5c17c3a3d7b11a0fce4a4e2c1248b609181eb..a8e6e0dff18020cc4a5756929bc7a6edf4d0c70d 100644 --- a/Framework/PythonInterface/test/python/mantid/api/MultipleFilePropertyTest.py +++ b/Framework/PythonInterface/test/python/mantid/api/MultipleFilePropertyTest.py @@ -12,7 +12,10 @@ class MultipleFilePropertyTest(unittest.TestCase): algorithm = create_algorithm('Load', Filename='LOQ48127.raw',OutputWorkspace='w', SpectrumMin=1,SpectrumMax=1,child=True) prop = algorithm.getProperty("Filename") - self.assertTrue(isinstance(prop.value, str)) + filenames = prop.value + self.assertTrue(isinstance(filenames, list)) + self.assertTrue(isinstance(filenames[0], str)) + self.assertEquals(len(filenames), 1) def test_value_member_returns_python_list_for_multiple_files(self): algorithm = create_algorithm('Load', Filename='MUSR15189,15190,15191.nxs',OutputWorkspace='w', diff --git a/scripts/Interface/reduction_gui/reduction/inelastic/dgs_reduction_script.py b/scripts/Interface/reduction_gui/reduction/inelastic/dgs_reduction_script.py index 4f358fd8caadde5fba358983789e5637395f53cf..e4752e909d2fd080595b611294d55a79276262b1 100644 --- a/scripts/Interface/reduction_gui/reduction/inelastic/dgs_reduction_script.py +++ b/scripts/Interface/reduction_gui/reduction/inelastic/dgs_reduction_script.py @@ -180,9 +180,4 @@ class DgsReductionScripter(BaseReductionScripter): alg = mantid.api.AlgorithmManager.createUnmanaged("Load") alg.initialize() alg.setPropertyValue('Filename',str(filename)) - fnames=alg.getProperty('Filename').value - if not isinstance(fnames,list): - fnames=[fnames] #make a list with one element - return fnames - - + return alg.getProperty('Filename').value