16#include <boost/python/class.hpp>
17#include <boost/python/copy_const_reference.hpp>
18#include <boost/python/def.hpp>
19#include <boost/python/dict.hpp>
20#include <boost/python/enum.hpp>
21#include <boost/python/implicit.hpp>
22#include <boost/python/list.hpp>
23#include <boost/python/make_function.hpp>
24#include <boost/python/overloads.hpp>
25#include <boost/python/register_ptr_to_python.hpp>
26#include <boost/python/return_value_policy.hpp>
59PyObject *unitAsUnicode(
const Property &self) {
60 static constexpr std::array<const char *, 2> codecs{
"utf-8",
"windows-1252"};
61 const auto &unitsBytes = self.
units();
62 for (
const auto &encoding : codecs) {
63 auto unitsPy = PyUnicode_Decode(unitsBytes.c_str(), unitsBytes.size(), encoding,
"strict");
70 const std::string allCodecsStr = []() {
71 auto it = codecs.begin();
72 std::string result = *it++;
73 for (; it != codecs.end(); ++it) {
79 const std::string helpMessage = std::string(
"Can't decode units string. Tried codecs=")
81 .append(
"\nTo try other codecs use Property.unitsAsBytes to retrieve the "
82 "original bytes object and use .decode().");
83 PyErr_SetString(PyExc_RuntimeError, helpMessage.c_str());
84 throw error_already_set();
92PyObject *unitsAsBytes(
const Property &self) {
return PyBytes_FromString(self.
units().c_str()); }
104 valueAsPrettyStr, 0, 2)
108void export_Property() {
109 register_ptr_to_python<Property *>();
115 enum_<Direction::Type>(
"Direction")
122 class_<Property, boost::noncopyable>(
"Property", no_init)
123 .add_property(
"name", make_function(&
Property::name, return_value_policy<copy_const_reference>()),
124 "The name of the property")
127 "An empty string if the property is valid, otherwise it "
128 "contains an error message.")
130 .add_property(
"isDefault", make_function(&
Property::isDefault),
"Is the property set at the default value")
132 .add_property(
"getDefault", make_function(&
Property::getDefault),
"Get the default value as a string")
134 .add_property(
"direction", &
Property::direction,
"Input, Output, InOut or Unknown. See the Direction class")
136 .add_property(
"documentation",
138 "The property's doc string")
140 .add_property(
"type", make_function(&
Property::type),
"Returns a string identifier for the type")
142 .add_property(
"units", &unitAsUnicode, &
Property::setUnits,
"The units attached to this property")
144 .add_property(
"unitsAsBytes", &unitsAsBytes,
145 "The units attached to this property as a encoded bytes object. It "
146 "is assumed the caller knows the correct endcoding used.")
149 "The value of the property as a string. "
150 "For some property types, e.g. Workspaces, it is useful to "
151 "be able to refer to the string value directly")
154 valueAsPrettyStrOverloader((arg(
"maxLength") = 0, arg(
"collapseLists") =
true),
155 "The value of the property as a formatted string. "
156 "If maxLength is defined then the output may not contain the "
158 "contents of the property. The maxLength and collapseLists "
160 "do not work for all property types"))
164 .add_property(
"getGroup", make_function(&
Property::getGroup, return_value_policy<copy_const_reference>()),
165 "Return the 'group' of the property, that is, the header "
166 "in the algorithm's list of properties.")
168 .add_property(
"settings", make_function(&
Property::getSettings, return_value_policy<return_by_value>()),
169 "Return the object managing this property's settings")
171 .add_static_property(
"EMPTY_DBL", emptyDouble)
172 .add_static_property(
"EMPTY_INT", emptyInt)
173 .add_static_property(
"EMPTY_LONG", emptyLong)
175 .def(
"setAutoTrim", &
Property::setAutoTrim, (arg(
"setting")),
"Setting automatic trimming of whitespaces.")
176 .def(
"getAutoTrim", &
Property::autoTrim,
"Gets the setting of automatic trimming of whitespaces.");
#define GET_POINTER_SPECIALIZATION(TYPE)
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(valueAsPrettyStrOverloader, valueAsPrettyStr, 0, 2) void export_Property()
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
Base class for properties.
virtual const std::string & units() const
Returns the units of the property, if any, as a string.
IPropertySettings * getSettings()
virtual std::string valueAsPrettyStr(const size_t maxLength=0, const bool collapseLists=true) const
Returns the value of the property as a pretty printed string.
bool autoTrim() const
Returns if the property is set to automatically trim string unput values of whitespace.
unsigned int direction() const
returns the direction of the property
const std::string & documentation() const
Get the property's documentation string.
virtual std::string setValue(const std::string &)=0
Set the value of the property via a string.
virtual void setUnits(const std::string &unit)
Sets the units of the property, as a string.
virtual std::string isValid() const
Overridden function that checks whether the property, if not overriden returns "".
virtual bool isDefault() const =0
Overriden function that returns if property has the same value that it was initialised with,...
const std::string & name() const
Get the property's name.
void setAutoTrim(const bool &setting)
Sets if the property is set to automatically trim string unput values of whitespace.
const std::string & getGroup()
virtual std::vector< std::string > allowedValues() const
Returns the set of valid values for this property, if such a set exists.
const std::string type() const
Returns the type of the property as a string.
virtual std::string getDefault() const =0
Get the default value for the property which is the value the property was initialised with.
virtual std::string value() const =0
Returns the value of the property as a string.
constexpr int EMPTY_INT() noexcept
Returns what we consider an "empty" integer within a property.
constexpr long EMPTY_LONG() noexcept
Returns what we consider an "empty" long within a property.
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
Describes the direction (within an algorithm) of a Property.
@ InOut
Both an input & output workspace.
@ Input
An input workspace.
@ Output
An output workspace.
A struct to help export std::vector types.
static void wrap(std::string const &python_name)
a python wrapper