| 119 | } |
| 120 | |
| 121 | std::optional<message> py_context::py_build_message(const py::args &xs) { |
| 122 | if (xs.size() < 2) { |
| 123 | set_py_exception("Too few arguments to call build_message"); |
| 124 | return {}; |
| 125 | } |
| 126 | auto i = xs.begin(); |
| 127 | ++i; |
| 128 | message_builder mb; |
| 129 | for (; i != xs.end(); ++i) { |
| 130 | std::string type_name = PyEval_GetFuncName((*i).ptr()); |
| 131 | if (type_name == "dict") { |
| 132 | // special case for dicts - we want to convert them to JsonStore |
| 133 | try { |
| 134 | py::object json_py_module = py::module_::import("json"); |
| 135 | py::object as_json_str = json_py_module.attr("dumps")(*i); |
| 136 | const xstudio::utility::JsonStore js( |
| 137 | nlohmann::json::parse(as_json_str.cast<std::string>())); |
| 138 | mb.append(js); |
| 139 | continue; |
| 140 | } catch (const std::exception &err) { |
| 141 | set_py_exception( |
| 142 | "Attempt to send a message to xSTUDIO with dict data that is not 'json' " |
| 143 | "compatible: ", |
| 144 | err.what()); |
| 145 | return {}; |
| 146 | } |
| 147 | } /*else if (type_name == "list") { |
| 148 | // special case for lists - can we convert to std::vector<ElemType> ? |
| 149 | py::list lst = (*i).cast<py::list>(); |
| 150 | if (lst.size() > 0) { |
| 151 | std::string elem_type_name = PyEval_GetFuncName(lst[0].ptr()); |
| 152 | type_name = fmt::format("list({})", elem_type_name); |
| 153 | } |
| 154 | }*/ |
| 155 | auto kvp = bindings().find(type_name); |
| 156 | if (kvp == bindings().end()) { |
| 157 | set_py_exception( |
| 158 | R"(Unable to add element of type A ")", |
| 159 | type_name, |
| 160 | R"(" to message: type is unknown to CAF)"); |
| 161 | return {}; |
| 162 | } |
| 163 | kvp->second->append(mb, *i); |
| 164 | } |
| 165 | return mb.move_to_message(); |
| 166 | } |
| 167 | |
| 168 | void py_context::py_send(const py::args &xs) { |
| 169 | if (xs.size() < 2) { |