-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #4399. Exported the worksapce validators to python
- Loading branch information
1 parent
56be1ef
commit db206fb
Showing
15 changed files
with
207 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...id/Framework/PythonInterface/inc/MantidPythonInterface/kernel/TypedValidatorExportMacro.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#ifndef MANTID_PYTHONINTERFACE_TYPEDVALIDATOREXPORT_MACRO_H_ | ||
#define MANTID_PYTHONINTERFACE_TYPEDVALIDATOREXPORT_MACRO_H_ | ||
/* | ||
Copyright © 2012 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory | ||
This file is part of Mantid. | ||
Mantid is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 3 of the License, or | ||
(at your option) any later version. | ||
Mantid is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid> | ||
Code Documentation is available at: <http://doxygen.mantidproject.org> | ||
*/ | ||
#include "MantidKernel/TypedValidator.h" | ||
#include <boost/python/class.hpp> | ||
|
||
/** | ||
* Defines an export for a validator of the given type | ||
*/ | ||
#define EXPORT_TYPEDVALIDATOR(Type) \ | ||
boost::python::class_<Mantid::Kernel::TypedValidator<Type>,\ | ||
boost::python::bases<Mantid::Kernel::IValidator>,\ | ||
boost::noncopyable\ | ||
>("TypedValidator_"#Type, boost::python::no_init)\ | ||
.def("isValid", &Mantid::Kernel::TypedValidator<Type>::isValid, \ | ||
"Returns an empty string if the value is considered valid, otherwise a string defining the error is returned.") | ||
|
||
#endif // MANTID_PYTHONINTERFACE_TYPEDVALIDATOREXPORT_MACRO_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/InstrumentValidator.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include "MantidAPI/WorkspaceValidators.h" | ||
#include "MantidPythonInterface/kernel/TypedValidatorExportMacro.h" | ||
#include <boost/python/class.hpp> | ||
|
||
// This is typed on the ExperimentInfo class | ||
void export_InstrumentValidator() | ||
{ | ||
using namespace Mantid::API; | ||
using namespace boost::python; | ||
EXPORT_TYPEDVALIDATOR(ExperimentInfo_sptr); | ||
class_<InstrumentValidator, bases<Mantid::Kernel::TypedValidator<ExperimentInfo_sptr>>, | ||
boost::noncopyable | ||
>("InstrumentValidator", init<>("Checks that the workspace has an instrument defined")) | ||
; | ||
} |
65 changes: 65 additions & 0 deletions
65
Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/WorkspaceValidators.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include "MantidAPI/WorkspaceValidators.h" // They are all defined in one file so export them in one | ||
#include "MantidPythonInterface/kernel/TypedValidatorExportMacro.h" | ||
#include <boost/python/class.hpp> | ||
|
||
using Mantid::Kernel::TypedValidator; | ||
using namespace boost::python; | ||
|
||
/// This is the base TypedValidator for most of the WorkspaceValidators | ||
void export_MatrixWorkspaceValidator() | ||
{ | ||
using Mantid::API::MatrixWorkspace_sptr; | ||
using Mantid::API::MatrixWorkspaceValidator; | ||
EXPORT_TYPEDVALIDATOR(MatrixWorkspace_sptr); | ||
class_<MatrixWorkspaceValidator, | ||
bases<TypedValidator<MatrixWorkspace_sptr>>, | ||
boost::noncopyable>("MatrixWorkspaceValidator", no_init) | ||
; | ||
} | ||
namespace | ||
{ | ||
/// Export a validator derived from a MatrixWorkspaceValidator that has a no-arg constructor | ||
#define EXPORT_WKSP_VALIDATOR_NO_ARG(ValidatorType, DocString) \ | ||
class_<ValidatorType, \ | ||
bases<MatrixWorkspaceValidator>, \ | ||
boost::noncopyable\ | ||
>(#ValidatorType, \ | ||
init<>(DocString))\ | ||
; | ||
/// Export a validator derived from a MatrixWorkspaceValidator that has a single-arg constructor | ||
#define EXPORT_WKSP_VALIDATOR_ARG(ValidatorType, ArgType, ArgName, DocString) \ | ||
class_<ValidatorType, \ | ||
bases<MatrixWorkspaceValidator>, \ | ||
boost::noncopyable\ | ||
>(#ValidatorType, \ | ||
init<ArgType>(arg(ArgName), DocString))\ | ||
; | ||
/// Export a validator derived from a MatrixWorkspaceValidator that has a single-arg constructor | ||
/// with a default argument | ||
#define EXPORT_WKSP_VALIDATOR_DEFAULT_ARG(ValidatorType, ArgType, ArgName, DefaultValue, DocString) \ | ||
class_<ValidatorType, \ | ||
bases<MatrixWorkspaceValidator>, \ | ||
boost::noncopyable\ | ||
>(#ValidatorType, \ | ||
init<ArgType>(arg(ArgName)=DefaultValue, DocString))\ | ||
; | ||
|
||
} | ||
|
||
void export_WorkspaceValidators() | ||
{ | ||
using namespace Mantid::API; | ||
|
||
EXPORT_WKSP_VALIDATOR_ARG(WorkspaceUnitValidator, std::string, "unit", "Checks the workspace has the given unit along the X-axis"); | ||
EXPORT_WKSP_VALIDATOR_DEFAULT_ARG(HistogramValidator, bool, "mustBeHistogram", true, | ||
"If mustBeHistogram=True then the workspace must be a histogram otherwise it must be point data."); | ||
EXPORT_WKSP_VALIDATOR_DEFAULT_ARG(RawCountValidator, bool, "mustNotBeDistribution", true, | ||
"If mustNotBeDistribution=True then the workspace must not have been divided by the bin-width"); | ||
EXPORT_WKSP_VALIDATOR_NO_ARG(CommonBinsValidator, "A tentative check that the bins are common across the workspace"); | ||
EXPORT_WKSP_VALIDATOR_DEFAULT_ARG(SpectraAxisValidator, int, "axisNumber", 1, | ||
"Checks whether the axis specified by axisNumber is a SpectraAxis"); | ||
EXPORT_WKSP_VALIDATOR_DEFAULT_ARG(NumericAxisValidator, int, "axisNumber", 1, | ||
"Checks whether the axis specified by axisNumber is a NumericAxis"); | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
Code/Mantid/Framework/PythonInterface/test/python/WorkspaceValidatorsTest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
""" | ||
Test construction of the WorkspaceValidators | ||
""" | ||
import unittest | ||
import testhelpers | ||
from mantid.kernel import IValidator | ||
from mantid import (WorkspaceUnitValidator, HistogramValidator, | ||
RawCountValidator, CommonBinsValidator, | ||
SpectraAxisValidator, NumericAxisValidator, | ||
InstrumentValidator) | ||
|
||
class WorkspaceValidatorsTest(unittest.TestCase): | ||
|
||
def test_WorkspaceUnitValidator_construction(self): | ||
""" | ||
Test that the WorkspaceUnitValidator can be constructed | ||
with a single string | ||
""" | ||
testhelpers.assertRaisesNothing(self, WorkspaceUnitValidator,"DeltaE") | ||
self.assertRaises(Exception, WorkspaceUnitValidator) | ||
|
||
def test_CommonBinsValidator_construction(self): | ||
""" | ||
Test that the CommonBinsValidator can be constructed | ||
with no args | ||
""" | ||
testhelpers.assertRaisesNothing(self, CommonBinsValidator) | ||
|
||
def test_HistogramValidator_construction(self): | ||
""" | ||
Test that the HistogramValidator can be constructed | ||
with no args or a single bool | ||
""" | ||
testhelpers.assertRaisesNothing(self, HistogramValidator) | ||
testhelpers.assertRaisesNothing(self, HistogramValidator, False) | ||
|
||
def test_RawCountValidator_construction(self): | ||
""" | ||
Test that the HistogramValidator can be constructed | ||
with no args or a single bool | ||
""" | ||
testhelpers.assertRaisesNothing(self, RawCountValidator) | ||
testhelpers.assertRaisesNothing(self, RawCountValidator, False) | ||
|
||
def test_SpectraAxisValidator_construction(self): | ||
""" | ||
Test that the SpectraAxis can be constructed | ||
with no args or a single integer | ||
""" | ||
testhelpers.assertRaisesNothing(self, SpectraAxisValidator) | ||
testhelpers.assertRaisesNothing(self, SpectraAxisValidator, 0) | ||
|
||
def test_NumericAxisValidator_construction(self): | ||
""" | ||
Test that the NumericAxis can be constructed | ||
with no args or a single integer | ||
""" | ||
testhelpers.assertRaisesNothing(self, NumericAxisValidator) | ||
testhelpers.assertRaisesNothing(self, NumericAxisValidator, 0) | ||
|
||
def test_InstrumentValidator_construction(self): | ||
""" | ||
Test that the InstrumentValidator can be constructed | ||
with no args | ||
""" | ||
testhelpers.assertRaisesNothing(self, InstrumentValidator) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters