19#include <boost/python/class.hpp>
20#include <boost/python/dict.hpp>
32template <
typename BaseAlgorithm>
34 : BaseAlgorithm(),
m_self(self), m_isRunningObj(nullptr), m_wikiSummary(
"") {
46 return std::string(getSelf()->ob_type->tp_name);
55 return callMethod<int>(getSelf(),
"version");
68 return callMethod<bool>(getSelf(),
"checkGroups");
70 return BaseAlgorithm::checkGroups();
79 const static std::string defaultCategory =
"PythonAlgorithms";
80 std::string category = defaultCategory;
82 category = callMethod<std::string>(getSelf(),
"category");
85 if (category == defaultCategory) {
87 this->getLogger().warning() <<
"Python Algorithm " << this->name() <<
" v" << this->version()
88 <<
" does not have a category defined. See "
89 "http://www.mantidproject.org/Basic_PythonAlgorithm_Structure\n";
114 return callMethod<std::string>(getSelf(),
"alias");
116 return BaseAlgorithm::alias();
126 return callMethod<std::string>(getSelf(),
"aliasDeprecated");
128 return BaseAlgorithm::aliasDeprecated();
138 return callMethod<std::string>(getSelf(),
"summary");
140 return m_wikiSummary;
149 return callMethod<std::string>(getSelf(),
"helpURL");
151 return std::string();
159 if (!m_isRunningObj) {
160 return SuperClass::isRunning();
165 PyObject *result = PyObject_CallObject(m_isRunningObj,
nullptr);
166 if (PyErr_Occurred())
168 if (PyBool_Check(result)) {
169 return static_cast<bool>(PyLong_AsLong(result));
171 throw std::runtime_error(
"Algorithm.isRunning - Expected bool return type.");
178 return callMethod<void>(getSelf(),
"cancel");
180 SuperClass::cancel();
188 using boost::python::dict;
189 std::map<std::string, std::string> resultMap;
193 dict resultDict = callMethod<dict>(getSelf(),
"validateInputs");
195 boost::python::list keys = resultDict.keys();
196 size_t numItems = boost::python::len(keys);
197 for (
size_t i = 0; i < numItems; ++i) {
198 boost::python::object key = keys[i];
199 boost::python::object
value = resultDict[key];
202 std::string keyAsString = boost::python::extract<std::string>(key);
203 std::string valueAsString = boost::python::extract<std::string>(
value);
204 resultMap[std::move(keyAsString)] = std::move(valueAsString);
205 }
catch (boost::python::error_already_set &) {
206 this->getLogger().error() <<
"In validateInputs(self): Invalid type for key/value pair "
207 <<
"detected in dict.\n"
208 <<
"All keys and values must be strings\n";
222 std::string msg =
"self.setWikiSummary() is deprecated and will be removed in a future "
224 "To ensure continued functionality remove the line containing "
225 "'self.setWikiSummary'\n"
226 "and add a new function outside of the current one defined like so:\n"
227 "def summary(self):\n"
231 PyErr_Warn(PyExc_DeprecationWarning, msg.c_str());
232 m_wikiSummary = summary;
241template <
typename BaseAlgorithm>
243 const std::string &doc) {
244 BaseAlgorithm &caller = extract<BaseAlgorithm &>(self);
248 caller.declareProperty(std::unique_ptr<Kernel::Property>(prop->
clone()), doc);
263template <
typename BaseAlgorithm>
265 const boost::python::object &defaultValue,
266 const boost::python::object &validator,
267 const std::string &doc,
const int direction) {
268 BaseAlgorithm &caller = extract<BaseAlgorithm &>(self);
269 auto prop = std::unique_ptr<Kernel::Property>(
271 caller.declareProperty(std::move(prop), doc);
284template <
typename BaseAlgorithm>
286 const boost::python::object &defaultValue,
287 const std::string &doc,
const int direction) {
288 BaseAlgorithm &caller = extract<BaseAlgorithm &>(self);
291 caller.declareProperty(std::move(prop), doc);
302template <
typename BaseAlgorithm>
304 const boost::python::object &defaultValue,
305 const int direction) {
306 declarePyAlgProperty(self, name, defaultValue,
"", direction);
318 callMethod<void>(getSelf(),
"PyInit");
327 callMethod<void>(getSelf(),
"PyExec");
329 if (BaseAlgorithm::getCancel())
double value
The value of the point.
std::unique_ptr< ConceptT > m_self
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
CancelException is thrown to cancel execution of the algorithm.
Base class for properties.
virtual Property * clone() const =0
'Virtual copy constructor'
Provides a layer class for boost::python to allow C++ virtual functions to be overridden in a Python ...
const std::string helpURL() const override
Optional documentation URL of the algorithm, empty string if not overridden.
void setWikiSummary(const std::string &summary)
Set the summary text.
void cancel() override
Allow the cancel method to be overridden.
std::map< std::string, std::string > validateInputs() override
Returns the validateInputs result of the algorithm.
AlgorithmAdapter()=delete
Disable default constructor - The PyObject must be supplied to construct the object.
const std::string alias() const override
Allow the method returning the algorithm aliases to be overridden.
const std::string name() const override
Returns the name of the algorithm.
PyObject * m_isRunningObj
A pointer to an overridden isRunning method.
bool checkGroups() override
A return of false will allow processing workspace groups as a whole.
const std::string aliasDeprecated() const override
Returns optional documentation URL of the algorithm.
const std::string summary() const override
Returns the summary for the algorithm.
void exec() override
Private exec for this algorithm.
int version() const override
Returns a version of the algorithm.
const std::vector< std::string > seeAlso() const override
Returns seeAlso related algorithms.
static void declarePyAlgProperty(boost::python::object &self, Kernel::Property *prop, const std::string &doc="")
Declare a preconstructed property.
const std::string category() const override
Returns a category of the algorithm.
void init() override
Private init for this algorithm.
bool isRunning() const override
Allow the isRunning method to be overridden.
Defines a structure for acquiring/releasing the Python GIL using the RAII pattern.
Exception type that captures the current Python error state as a generic C++ exception for any genera...
static std::unique_ptr< Kernel::Property > create(const std::string &name, const boost::python::object &defaultValue, const boost::python::object &validator, const unsigned int direction)
Creates a PropertyWithValue<Type> instance from the given information.
bool MANTID_PYTHONINTERFACE_CORE_DLL typeHasAttribute(PyObject *obj, const char *attr)
This namespace contains helper functions for classes that are overridden in Python.
Converts a Python sequence type to a C++ std::vector, where the element type is defined by the templa...
Defines an exception for an undefined attribute.