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 +
13
14#include <boost/python/class.hpp>
15#include <boost/python/copy_const_reference.hpp>
16#include <boost/python/dict.hpp>
17#include <boost/python/iterator.hpp>
18#include <boost/python/list.hpp>
19#include <boost/python/register_ptr_to_python.hpp>
20#include <boost/python/return_internal_reference.hpp>
21#include <boost/python/stl_iterator.hpp>
22#include <boost/python/str.hpp>
23
24using namespace Mantid::Kernel;
26using namespace boost::python;
27using ExtractStdString = extract<std::string>;
28
30
31namespace {
40void setProperty(IPropertyManager &self, const std::string &name, const boost::python::object &value) {
41 ExtractStdString valuecpp(value);
42 if (valuecpp.check()) {
43 self.setPropertyValue(name, valuecpp());
44 } else {
45 try {
46 Property *p = self.getProperty(name);
47 const auto &entry = Registry::TypeRegistry::retrieve(*(p->type_info()));
48 entry.set(&self, name, value);
49 } catch (std::invalid_argument &e) {
50 throw std::invalid_argument("When converting parameter \"" + name + "\": " + e.what());
51 }
52 }
53}
54
55void setProperties(IPropertyManager &self, const boost::python::dict &kwargs) {
56 const object view = kwargs.attr("items")();
57 const object objectItems(handle<>(PyObject_GetIter(view.ptr())));
58 auto begin = stl_input_iterator<object>(objectItems);
59 auto end = stl_input_iterator<object>();
60 for (auto it = begin; it != end; ++it) {
61 setProperty(self, ExtractStdString((*it)[0])(), (*it)[1]);
62 }
63}
64
72void declareProperty(IPropertyManager &self, const std::string &name, const boost::python::object &value) {
73 auto p = std::unique_ptr<Property>(Registry::PropertyWithValueFactory::create(name, value, 0));
74 self.declareProperty(std::move(p));
75}
76
85void declareOrSetProperty(IPropertyManager &self, const std::string &name, const boost::python::object &value) {
86 bool propExists = self.existsProperty(name);
87 if (propExists) {
88 setProperty(self, name, value);
89 } else {
90 declareProperty(self, name, value);
91 }
92}
93
102void setPropertySettings(IPropertyManager &self, const std::string &propName, IPropertySettings *settingsManager) {
103 self.setPropertySettings(propName, std::unique_ptr<IPropertySettings>(settingsManager->clone()));
104}
105
106void deleteProperty(IPropertyManager &self, const std::string &propName) { self.removeProperty(propName); }
107
113boost::python::list getKeys(const IPropertyManager &self) {
114 const std::vector<Property *> &props = self.getProperties();
115 const size_t numProps = props.size();
116
117 boost::python::list result;
118 for (size_t i = 0; i < numProps; ++i) {
119 result.append(props[i]->name());
120 }
121
122 return result;
123}
124
137Property *get(const IPropertyManager &self, const std::string &name, const boost::python::object &value) {
138 try {
139 return self.getPointerToProperty(name);
140 } catch (Exception::NotFoundError &) {
141 return Registry::PropertyWithValueFactory::create(name, value, 0).release();
142 }
143}
144} // namespace
145
147 register_ptr_to_python<IPropertyManager *>();
148
149 class_<IPropertyManager, boost::noncopyable>("IPropertyManager", no_init)
150 .def("propertyCount", &IPropertyManager::propertyCount, arg("self"),
151 "Returns the number of properties being managed")
152
153 .def("getProperty", &IPropertyManager::getPointerToProperty, (arg("self"), arg("name")),
154 return_internal_reference<>(),
155 "Returns the property of the given name. Use .value to give the "
156 "value")
157
158 .def("getPropertyValue", &IPropertyManager::getPropertyValue, (arg("self"), arg("name")),
159 "Returns a string representation of the named property's value")
160
161 .def("getProperties", &IPropertyManager::getProperties, arg("self"), return_value_policy<copy_const_reference>(),
162 "Returns the list of properties managed by this object")
163
164 .def("declareProperty", &declareProperty, (arg("self"), arg("name"), arg("value")), "Create a new named property")
165
166 .def("setPropertyValue", &IPropertyManager::setPropertyValue, (arg("self"), arg("name"), arg("value")),
167 "Set the value of the named property via a string")
168
169 .def("setProperty", &setProperty, (arg("self"), arg("name"), arg("value")), "Set the value of the named property")
170 .def("setProperties", &setProperties, (arg("self"), arg("kwargs")), "Set a collection of properties from a dict")
171
172 .def("setPropertySettings", &setPropertySettings, (arg("self"), arg("name"), arg("settingsManager")),
173 "Assign the given IPropertySettings object to the named property")
174
175 .def("setPropertyGroup", &IPropertyManager::setPropertyGroup, (arg("self"), arg("name"), arg("group")),
176 "Set the group for a given property")
177
178 .def("existsProperty", &IPropertyManager::existsProperty, (arg("self"), arg("name")),
179 "Returns whether a property exists")
180
181 // Special methods so that IPropertyManager acts like a dictionary
182 // __len__, __getitem__, __setitem__, __delitem__, __iter__ and
183 // __contains__
184 .def("__len__", &IPropertyManager::propertyCount, arg("self"), "Returns the number of properties being managed")
185 .def("__getitem__", &IPropertyManager::getPointerToProperty, (arg("self"), arg("name")),
186 return_value_policy<return_by_value>(),
187 "Returns the property of the given name. Use .value to give the "
188 "value")
189 .def("__setitem__", &declareOrSetProperty, (arg("self"), arg("name"), arg("value")),
190 "Set the value of the named property or create it if it doesn't "
191 "exist")
192 .def("__delitem__", &deleteProperty, (arg("self"), arg("name")), "Delete the named property")
193 // TODO .def("__iter__", iterator<std::vector<std::string> > ())
194 .def("__contains__", &IPropertyManager::existsProperty, (arg("self"), arg("name")),
195 "Returns whether a property exists")
196
197 // Bonus methods to be even more like a dict
198 .def("has_key", &IPropertyManager::existsProperty, (arg("self"), arg("name")),
199 "Returns whether a property exists")
200 .def("keys", &getKeys, arg("self"))
201 .def("values", &IPropertyManager::getProperties, arg("self"), return_value_policy<copy_const_reference>(),
202 "Returns the list of properties managed by this object")
203 .def("get", &get, (arg("self"), arg("name"), arg("value")), return_value_policy<return_by_value>(),
204 "Returns the property of the given name. Use .value to give the "
205 "value. If property with given name does not exist, returns given "
206 "default value.");
207}
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 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:70