Skip to content

Commit

Permalink
Refs #4399. Add constructors to FileProperty export.
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Mar 6, 2012
1 parent 0985167 commit eef6951
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,77 @@
#include "MantidAPI/FileProperty.h"
#include "MantidKernel/PropertyWithValue.h"
#include "MantidPythonInterface/kernel/Converters/PySequenceToVector.h"
#include <boost/python/class.hpp>
#include <boost/python/bases.hpp>
#include <boost/python/enum.hpp>
#include <boost/python/args.hpp>
#include <boost/python/make_constructor.hpp>
#include <boost/python/default_call_policies.hpp>
#include <boost/python/overloads.hpp>
#include <boost/make_shared.hpp>


using Mantid::API::FileProperty;
using Mantid::Kernel::PropertyWithValue;
using Mantid::PythonInterface::Converters::PySequenceToVectorConverter;
namespace bpl = boost::python;

void export_ActionEnum()
{
bpl::enum_<FileProperty::FileAction>("FileAction")
.value("Save", FileProperty::FileAction::Save)
.value("OptionalSave", FileProperty::FileAction::OptionalSave)
.value("Load", FileProperty::FileAction::Load)
.value("OptionalLoad", FileProperty::FileAction::OptionalLoad)
.value("Directory", FileProperty::FileAction::Directory)
.value("OptionalDirectory", FileProperty::FileAction::OptionalDirectory)
;
}

using boost::python::class_;
using boost::python::no_init;
using boost::python::bases;
namespace
{
/**
* The FileProperty constructor can take a list of extensions but we want users to be
* able to pass in a python list so we need a proxy function to act as a constructor
* @param name :: The name of the property
* @param defaultValue :: A default value
* @param action :: A file action defined by FileProperty::FileAction
* @param extensions :: A list of possible extensions (default = [])
* @param The direction of the property (default = input)
*/
boost::shared_ptr<FileProperty> createFileProperty(const std::string & name,const std::string& defaultValue,
unsigned int action,
const bpl::object & extensions = bpl::object(),
unsigned direction = Mantid::Kernel::Direction::Input)
{
std::vector<std::string> extsAsVector;
if( !extensions.is_none() )
{
bpl::extract<std::string> extractor(extensions);
if(extractor.check())
{
extsAsVector = std::vector<std::string>(1, extractor());
}
else
{
extsAsVector = PySequenceToVectorConverter<std::string>(extensions)();
}
}
return boost::make_shared<FileProperty>(name, defaultValue, action, extsAsVector, direction);
}

}

void export_FileProperty()
{
class_<FileProperty, bases<PropertyWithValue<std::string> >, boost::noncopyable>("FileProperty", no_init)
;
bpl::class_<FileProperty, bpl::bases<PropertyWithValue<std::string> >, boost::noncopyable>("FileProperty", bpl::no_init)
.def("__init__",
bpl::make_constructor(&createFileProperty, bpl::default_call_policies(),
(bpl::arg("name"), bpl::arg("defaultValue"), bpl::arg("action"),
bpl::arg("extensions") = bpl::object(),
bpl::arg("direction") = Mantid::Kernel::Direction::Input)
)
)
;
}

Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ void export_Property()
"The name of the property")
.add_property("documentation", make_function(&Mantid::Kernel::Property::documentation, return_value_policy<copy_const_reference>()),
"The property's doc string")
.add_property("direction", &Mantid::Kernel::Property::direction, "Input, Output, InOut or Unknown. See the Direction enum")
.add_property("direction", &Mantid::Kernel::Property::direction,
"Input, Output, InOut or Unknown. See the Direction class")
.add_property("units", &Mantid::Kernel::Property::units, "The units attached to this property")
.add_property("valueAsStr", &Mantid::Kernel::Property::value, "The value of the property as a string. "
"For some property types, e.g. Workspaces, it is useful to be able to refer to the string value directly")
"For some property types, e.g. Workspaces, it is useful to be able to refer to the string value directly")
.add_property("allowedValues", &Mantid::Kernel::Property::allowedValues, "A list of allowed values")
.def("isValid", &Mantid::Kernel::Property::isValid, "An empty string if the property is valid, otherwise it contains an error message.")
.def("allowedValues", &Mantid::Kernel::Property::allowedValues, "A list of allowed values")
.def("isDefault", &Mantid::Kernel::Property::isDefault, "Is the property set at the default value")
.def("getGroup", &Mantid::Kernel::Property::getGroup, return_value_policy<copy_const_reference>(),
"Return the 'group' of the property, that is, the header in the algorithm's list of properties.")
"Return the 'group' of the property, that is, the header in the algorithm's list of properties.")
;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
import unittest
from testhelpers import run_algorithm
from mantid.api import FileProperty
from mantid.api import FileProperty, FileAction
from mantid.kernel import Direction

class FilePropertyTest(unittest.TestCase):

def test_constructor_with_name_and_default_and_action(self):
prop = FileProperty("LoadProperty", "", FileAction.Load)
self.assertNotEquals("", prop.isValid())
self.assertEquals(Direction.Input, prop.direction)

def test_constructor_with_name_and_default_and_action_and_exts_list(self):
prop = FileProperty("LoadProperty", "", FileAction.Load, ['.nxs', '.raw'])
self.assertNotEquals("", prop.isValid())
self.assertEquals(Direction.Input, prop.direction)
allowed = prop.allowedValues
self.assertTrue('.nxs' in allowed)
self.assertTrue('.raw' in allowed)

def test_constructor_with_name_and_default_and_action_and_single_ext(self):
prop = FileProperty("LoadProperty", "", FileAction.Load, '.nxs')
self.assertNotEquals("", prop.isValid())
self.assertEquals(Direction.Input, prop.direction)
allowed = prop.allowedValues
self.assertTrue('.nxs' in allowed)

def test_constructor_with_name_and_default_and_action_and_single_ext_and_direction(self):
prop = FileProperty("LoadProperty", "", FileAction.Load, ['.nxs'], Direction.InOut)
self.assertNotEquals("", prop.isValid())
self.assertEquals(Direction.InOut, prop.direction)

def test_alg_get_property_converts_to_this(self):
alg = run_algorithm('LoadRaw', Filename='LOQ48127.raw', OutputWorkspace='tmp', SpectrumMax=1)
prop = alg.getProperty("Filename")
Expand Down

0 comments on commit eef6951

Please sign in to comment.