Mantid
Loading...
Searching...
No Matches
IPropertyManager.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
14
15#include <boost/python/class.hpp>
16#include <boost/python/copy_const_reference.hpp>
17#include <boost/python/dict.hpp>
18#include <boost/python/iterator.hpp>
19#include <boost/python/list.hpp>
20#include <boost/python/register_ptr_to_python.hpp>
21#include <boost/python/return_internal_reference.hpp>
22#include <boost/python/stl_iterator.hpp>
23#include <boost/python/str.hpp>
24
25using namespace Mantid::Kernel;
27using namespace boost::python;
28using ExtractStdString = extract<std::string>;
29
31
32namespace {
41void setProperty(IPropertyManager &self, const std::string &name, const boost::python::object &value) {
42 ExtractStdString valuecpp(value);
43 if (valuecpp.check()) {
44 self.setPropertyValue(name, valuecpp());
45 } else {
46 try {
47 const Property *p = self.getProperty(name);
48 const auto &entry = Registry::TypeRegistry::retrieve(*(p->type_info()));
49 entry.set(&self, name, value);
50 } catch (std::invalid_argument &e) {
51 throw std::invalid_argument("When converting parameter \"" + name + "\": " + e.what());
52 }
53 }
54}
55
56void setProperties(IPropertyManager &self, const boost::python::dict &kwargs) {
57 const object view = kwargs.attr("items")();
58 const object objectItems(handle<>(PyObject_GetIter(view.ptr())));
59 auto begin = stl_input_iterator<object>(objectItems);
60 auto end = stl_input_iterator<object>();
61 for (auto it = begin; it != end; ++it) {
62 setProperty(self, ExtractStdString((*it)[0])(), (*it)[1]);
63 }
64}
65
73void declareProperty(IPropertyManager &self, const std::string &name, const boost::python::object &value) {
74 auto p = std::unique_ptr<Property>(Registry::PropertyWithValueFactory::create(name, value, 0));
75 self.declareProperty(std::move(p));
76}
77
86void declareOrSetProperty(IPropertyManager &self, const std::string &name, const boost::python::object &value) {
87 bool propExists = self.existsProperty(name);
88 if (propExists) {
89 setProperty(self, name, value);
90 } else {
91 declareProperty(self, name, value);
92 }
93}
94
104void declareOrReplaceProperty(IPropertyManager &self, const std::string &name,
105 const boost::python::object &defaultValue, const boost::python::object &validator,
106 const std::string &doc, const int direction) {
107 auto prop =
108 std::unique_ptr<Property>(Registry::PropertyWithValueFactory::create(name, defaultValue, validator, direction));
109 self.declareOrReplaceProperty(std::move(prop), doc);
110}
111
120void setPropertySettings(IPropertyManager &self, const std::string &propName,
121 const IPropertySettings *settingsManager) {
122 self.setPropertySettings(propName, std::unique_ptr<IPropertySettings>(settingsManager->clone()));
123}
124
125void deleteProperty(IPropertyManager &self, const std::string &propName) { self.removeProperty(propName); }
126
132boost::python::list getKeys(const IPropertyManager &self) {
133 const std::vector<Property *> &props = self.getProperties();
134 const size_t numProps = props.size();
135
136 boost::python::list result;
137 for (size_t i = 0; i < numProps; ++i) {
138 result.append(props[i]->name());
139 }
140
141 return result;
142}
143
156Property *get(const IPropertyManager &self, const std::string &name, const boost::python::object &value) {
157 try {
158 return self.getPointerToProperty(name);
159 } catch (Exception::NotFoundError &) {
160 return Registry::PropertyWithValueFactory::create(name, value, 0).release();
161 }
162}
163} // namespace
164
166 register_ptr_to_python<IPropertyManager *>();
167
168 class_<IPropertyManager, boost::noncopyable>("IPropertyManager", no_init)
169 .def("propertyCount", &IPropertyManager::propertyCount, arg("self"),
170 "Returns the number of properties being managed")
171
172 .def("getProperty", &IPropertyManager::getPointerToProperty, (arg("self"), arg("name")),
173 return_internal_reference<>(),
174 "Returns the property of the given name. Use .value to give the "
175 "value")
176
177 .def("getPropertyValue", &IPropertyManager::getPropertyValue, (arg("self"), arg("name")),
178 "Returns a string representation of the named property's value")
179
180 .def("getProperties", &IPropertyManager::getProperties, arg("self"), return_value_policy<copy_const_reference>(),
181 "Returns the list of properties managed by this object")
182
183 .def("declareProperty", &declareProperty, (arg("self"), arg("name"), arg("value")), "Create a new named property")
184
185 .def("declareOrReplaceProperty", &declareOrReplaceProperty,
186 (arg("self"), arg("name"), arg("defaultValue"), arg("validator") = object(), arg("doc") = "",
187 arg("direction") = Direction::Input),
188 "Declares or replaces a named property where the type is taken from "
189 "the type of the defaultValue and mapped to an appropriate C++ "
190 "type")
191
192 .def("setPropertyValue", &IPropertyManager::setPropertyValue, (arg("self"), arg("name"), arg("value")),
193 "Set the value of the named property via a string")
194
195 .def("setProperty", &setProperty, (arg("self"), arg("name"), arg("value")), "Set the value of the named property")
196 .def("setProperties", &setProperties, (arg("self"), arg("kwargs")), "Set a collection of properties from a dict")
197
198 .def("setPropertySettings", &setPropertySettings, (arg("self"), arg("name"), arg("settingsManager")),
199 "Assign the given IPropertySettings object to the named property")
200
201 .def("setPropertyGroup", &IPropertyManager::setPropertyGroup, (arg("self"), arg("name"), arg("group")),
202 "Set the group for a given property")
203
204 .def("existsProperty", &IPropertyManager::existsProperty, (arg("self"), arg("name")),
205 "Returns whether a property exists")
206
207 // Special methods so that IPropertyManager acts like a dictionary
208 // __len__, __getitem__, __setitem__, __delitem__, __iter__ and
209 // __contains__
210 .def("__len__", &IPropertyManager::propertyCount, arg("self"), "Returns the number of properties being managed")
211 .def("__getitem__", &IPropertyManager::getPointerToProperty, (arg("self"), arg("name")),
212 return_value_policy<return_by_value>(),
213 "Returns the property of the given name. Use .value to give the "
214 "value")
215 .def("__setitem__", &declareOrSetProperty, (arg("self"), arg("name"), arg("value")),
216 "Set the value of the named property or create it if it doesn't "
217 "exist")
218 .def("__delitem__", &deleteProperty, (arg("self"), arg("name")), "Delete the named property")
219 // TODO .def("__iter__", iterator<std::vector<std::string> > ())
220 .def("__contains__", &IPropertyManager::existsProperty, (arg("self"), arg("name")),
221 "Returns whether a property exists")
222
223 // Bonus methods to be even more like a dict
224 .def("has_key", &IPropertyManager::existsProperty, (arg("self"), arg("name")),
225 "Returns whether a property exists")
226 .def("keys", &getKeys, arg("self"))
227 .def("values", &IPropertyManager::getProperties, arg("self"), return_value_policy<copy_const_reference>(),
228 "Returns the list of properties managed by this object")
229 .def("get", &get, (arg("self"), arg("name"), arg("value")), return_value_policy<return_by_value>(),
230 "Returns the property of the given name. Use .value to give the "
231 "value. If property with given name does not exist, returns given "
232 "default value.");
233}
std::string name
Definition Run.cpp:60
double value
The value of the point.
Definition FitMW.cpp:51
#define GET_POINTER_SPECIALIZATION(TYPE)
Definition GetPointer.h:17
boost::python::extract< std::string > ExtractStdString
void export_IPropertyManager()
Exception for when an item is not found in a collection.
Definition Exception.h:145
Interface to PropertyManager.
virtual void setPropertyValue(const std::string &name, const std::string &value)=0
Sets property value from a string.
void setPropertySettings(const std::string &name, std::unique_ptr< IPropertySettings > settings)
virtual bool existsProperty(const std::string &name) const =0
Checks whether the named property is already in the list of managed property.
virtual void removeProperty(const std::string &name, const bool delproperty=true)=0
Removes the property from management.
virtual void declareProperty(std::unique_ptr< Property > p, const std::string &doc="")=0
Function to declare properties (i.e. store them)
virtual void declareOrReplaceProperty(std::unique_ptr< Property > p, const std::string &doc="")=0
Function to declare properties (i.e. store them)
virtual Property * getPointerToProperty(const std::string &name) const =0
Get a pointer to property by name.
virtual TypedValue getProperty(const std::string &name) const =0
Get the value of a property.
virtual std::string getPropertyValue(const std::string &name) const =0
Get the value of a property as a string.
virtual size_t propertyCount() const =0
Returns the number of properties under management.
void setPropertyGroup(const std::string &name, const std::string &group)
Set the group for a given property.
virtual const std::vector< Property * > & getProperties() const =0
Get the list of managed properties.
Interface for modifiers to Property's that specify if they should be enabled or visible in a GUI.
virtual IPropertySettings * clone() const =0
Make a copy of the present type of IPropertySettings.
Base class for properties.
Definition Property.h:94
const std::type_info * type_info() const
Get the property type_info.
Definition Property.cpp:81
@ Input
An input workspace.
Definition Property.h:53