* \brief Initializes Python bindings for MITK property types. * * Registers BaseProperty and its concrete subclasses (StringProperty, * BoolProperty, IntProperty, FloatProperty, DoubleProperty, ColorProperty), * plus the PropertyNotOwnedError exception. */
| 33 | * plus the PropertyNotOwnedError exception. |
| 34 | */ |
| 35 | void InitProperty(py::module_ &m) |
| 36 | { |
| 37 | auto propertyNotOwnedError = |
| 38 | py::register_exception<PropertyNotOwnedError>(m, "PropertyNotOwnedError", PyExc_AttributeError); |
| 39 | propertyNotOwnedError.attr("__doc__") = |
| 40 | R"(Raised when ``set_property()`` or ``remove_property()`` is called on a |
| 41 | property that is provided read-only (not owned) by this object. |
| 42 | |
| 43 | Subclass of ``AttributeError``. Use :py:meth:`property_is_owned` to check |
| 44 | before writing. |
| 45 | )"; |
| 46 | |
| 47 | py::class_<mitk::BaseProperty, mitk::BaseProperty::Pointer>(m, "BaseProperty", |
| 48 | R"(Abstract base class for typed property values. |
| 49 | |
| 50 | Every property attached to an MITK object is a ``BaseProperty`` subclass: |
| 51 | :py:class:`StringProperty`, :py:class:`BoolProperty`, |
| 52 | :py:class:`IntProperty`, :py:class:`FloatProperty`, |
| 53 | :py:class:`DoubleProperty`, :py:class:`ColorProperty`, |
| 54 | :py:class:`TemporoSpatialStringProperty`. |
| 55 | |
| 56 | When you read properties through the :py:attr:`Image.properties` mapping |
| 57 | (or via :py:meth:`get_property`), values are auto-coerced to Python-native |
| 58 | types. The raw ``BaseProperty`` is reachable with ``raw=True``. |
| 59 | )") |
| 60 | .def("__str__", &mitk::BaseProperty::GetValueAsString, |
| 61 | "Return the property value as a string.") |
| 62 | .def("__repr__", |
| 63 | [](const mitk::BaseProperty &p) |
| 64 | { return "<mitk." + std::string(p.GetNameOfClass()) + ": " + p.GetValueAsString() + ">"; }) |
| 65 | .def("__eq__", [](const mitk::BaseProperty &a, const mitk::BaseProperty &b) { return a == b; }) |
| 66 | .def("to_json", |
| 67 | [](const mitk::BaseProperty &p) { return mitk::ConvertPropertyToSelfContainedJson(&p).dump(); }, |
| 68 | R"(Serialize this property to a self-contained JSON string. |
| 69 | |
| 70 | The string carries enough type information that :py:meth:`from_json` can |
| 71 | reconstruct the correct subclass. |
| 72 | |
| 73 | Returns: |
| 74 | JSON string representation of the property. |
| 75 | )") |
| 76 | .def_static( |
| 77 | "from_json", |
| 78 | [](const std::string &json) { return mitk::ConvertPropertyFromSelfContainedJson(nlohmann::json::parse(json)); }, |
| 79 | py::arg("json"), |
| 80 | R"(Reconstruct a ``BaseProperty`` subclass from JSON. |
| 81 | |
| 82 | Args: |
| 83 | json: JSON string produced by :py:meth:`to_json`. |
| 84 | |
| 85 | Returns: |
| 86 | A new ``BaseProperty`` of the appropriate subclass. |
| 87 | )") |
| 88 | .def_property_readonly("value", nullptr, |
| 89 | "Property value (overridden by subclasses with type-specific accessors).") |
| 90 | .def("clone", [](const mitk::BaseProperty &p) { return p.Clone(); }, |
| 91 | "Return a deep copy of this property."); |
| 92 |
no test coverage detected