Debian Codegraph @

src/App/LinkBaseExtensionPyImp.cpp

raw
1 /****************************************************************************
2 * Copyright (c) 2017 Zheng Lei (realthunder) <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ****************************************************************************/
22
23 #include <sstream>
24
25 #include "DocumentObjectPy.h"
26 #include "LinkBaseExtensionPy.h"
27 #include "LinkBaseExtensionPy.cpp"
28
29 using namespace App;
30
31 // returns a string which represent the object e.g. when printed in python
32 std::string LinkBaseExtensionPy::representation() const
33 {
34 std::ostringstream str;
35 str << "<" << getLinkBaseExtensionPtr()->getExtensionClassTypeId().getName() << ">";
36 return str.str();
37 }
38
39 using PropTmpMap = std::map<std::string, std::pair<int, Property*>>;
40 using PropMap = std::map<std::string, Property*>;
41
42 static bool getProperty(PropTmpMap& props,
43 const LinkBaseExtension::PropInfoMap& infoMap,
44 const PropMap& propMap,
45 PyObject* key,
46 PyObject* value)
47 {
48 std::ostringstream str;
49
50 if (!PyUnicode_Check(key)) {
51 PyErr_SetString(PyExc_TypeError, "key must be a unicode string");
52 return false;
53 }
54 const char* keyStr = PyUnicode_AsUTF8(key);
55 auto it = infoMap.find(keyStr);
56 if (it == infoMap.end()) {
57 str << "unknown key '" << keyStr << "'";
58 PyErr_SetString(PyExc_KeyError, str.str().c_str());
59 return false;
60 }
61
62 const char* valStr = nullptr;
63 if (key == value) {
64 valStr = keyStr;
65 }
66 else if (value != Py_None) {
67 if (!PyUnicode_Check(value)) {
68 PyErr_SetString(PyExc_TypeError, "value must be unicode string");
69 return false;
70 }
71 valStr = PyUnicode_AsUTF8(value);
72 }
73
74 App::Property* prop = nullptr;
75 auto& info = it->second;
76 if (valStr) {
77 auto pIt = propMap.find(valStr);
78 if (pIt == propMap.end()) {
79 str << "cannot find property '" << valStr << "'";
80 PyErr_SetString(PyExc_ValueError, str.str().c_str());
81 return false;
82 }
83 prop = pIt->second;
84 if (!prop->isDerivedFrom(info.type)) {
85 str << "expect property '" << keyStr << "(" << valStr << ") to be derived from '"
86 << info.type.getName() << "', instead of '" << prop->getTypeId().getName() << "'";
87 PyErr_SetString(PyExc_TypeError, str.str().c_str());
88 }
89 }
90 props[keyStr] = std::make_pair(info.index, prop);
91 return true;
92 }
93
94 PyObject* LinkBaseExtensionPy::configLinkProperty(PyObject* args, PyObject* keywds)
95 {
96 auto ext = getLinkBaseExtensionPtr();
97 const auto& info = ext->getPropertyInfoMap();
98
99 PropMap propMap;
100 ext->getExtendedContainer()->getPropertyMap(propMap);
101
102 PropTmpMap props;
103
104 if (args && PyTuple_Check(args)) {
105 for (Py_ssize_t pos = 0; pos < PyTuple_GET_SIZE(args); ++pos) {
106 auto key = PyTuple_GET_ITEM(args, pos);
107 if (!getProperty(props, info, propMap, key, key)) {
108 return nullptr;
109 }
110 }
111 }
112 if (keywds && PyDict_Check(keywds)) {
113 PyObject *key, *value;
114 Py_ssize_t pos = 0;
115 while (PyDict_Next(keywds, &pos, &key, &value)) {
116 if (!getProperty(props, info, propMap, key, value)) {
117 return nullptr;
118 }
119 }
120 }
121 for (auto& v : props) {
122 ext->setProperty(v.second.first, v.second.second);
123 }
124 Py_Return;
125 }
126
127 PyObject* LinkBaseExtensionPy::getLinkExtProperty(PyObject* args)
128 {
129 const char* name;
130 if (!PyArg_ParseTuple(args, "s", &name)) {
131 return nullptr;
132 }
133 auto prop = getLinkBaseExtensionPtr()->getProperty(name);
134 if (!prop) {
135 PyErr_SetString(PyExc_AttributeError, "unknown property name");
136 return nullptr;
137 }
138 return prop->getPyObject();
139 }
140
141 PyObject* LinkBaseExtensionPy::getLinkExtPropertyName(PyObject* args)
142 {
143 const char* name;
144 if (!PyArg_ParseTuple(args, "s", &name)) {
145 return nullptr;
146 }
147 auto prop = getLinkBaseExtensionPtr()->getProperty(name);
148 if (!prop) {
149 PyErr_SetString(PyExc_AttributeError, "unknown property name");
150 return nullptr;
151 }
152 auto container = getLinkBaseExtensionPtr()->getExtendedContainer();
153 if (!container) {
154 PyErr_SetString(PyExc_RuntimeError, "no extended container");
155 return nullptr;
156 }
157 name = container->getPropertyName(prop);
158 if (!name) {
159 PyErr_SetString(PyExc_RuntimeError, "cannot find property name");
160 return nullptr;
161 }
162 return Py::new_reference_to(Py::String(name));
163 }
164
165 PyObject* LinkBaseExtensionPy::getLinkPropertyInfo(PyObject* args)
166 {
167 auto ext = getLinkBaseExtensionPtr();
168
169 const auto& infos = ext->getPropertyInfo();
170
171 if (PyArg_ParseTuple(args, "")) {
172 Py::Tuple ret(infos.size());
173 int i = 0;
174 for (const auto& info : infos) {
175 ret.setItem(i++,
176 Py::TupleN(Py::String(info.name),
177 Py::String(info.type.getName()),
178 Py::String(info.doc)));
179 }
180 return Py::new_reference_to(ret);
181 }
182
183 short index = 0;
184 if (PyArg_ParseTuple(args, "h", &index)) {
185 if (index < 0 || index >= (int)infos.size()) {
186 PyErr_SetString(PyExc_ValueError, "index out of range");
187 return nullptr;
188 }
189 Py::TupleN ret(Py::String(infos[index].name),
190 Py::String(infos[index].type.getName()),
191 Py::String(infos[index].doc));
192 return Py::new_reference_to(ret);
193 }
194
195 char* name;
196 if (PyArg_ParseTuple(args, "s", &name)) {
197 for (const auto& info : infos) {
198 if (strcmp(info.name, name) == 0) {
199 Py::TupleN ret(Py::String(info.type.getName()), Py::String(info.doc));
200 return Py::new_reference_to(ret);
201 }
202 }
203 PyErr_SetString(PyExc_ValueError, "unknown property name");
204 return nullptr;
205 }
206
207 PyErr_SetString(PyExc_ValueError, "invalid arguments");
208 return nullptr;
209 }
210
211 void parseLink(LinkBaseExtension* ext, int index, PyObject* value)
212 {
213 App::DocumentObject* obj = nullptr;
214 PropertyStringList subs;
215 PropertyString sub;
216 if (value != Py_None) {
217 if (PyObject_TypeCheck(value, &DocumentObjectPy::Type)) {
218 obj = static_cast<DocumentObjectPy*>(value)->getDocumentObjectPtr();
219 }
220 else if (!PySequence_Check(value)) {
221 throw Base::TypeError("Expects type of DocumentObject or sequence");
222 }
223 else {
224 Py::Sequence seq(value);
225 if (seq[0].ptr() != Py_None) {
226 if (!PyObject_TypeCheck(seq[0].ptr(), &DocumentObjectPy::Type)) {
227 throw Base::TypeError(
228 "Expects the first argument to be DocumentObject in sequence");
229 }
230 obj = static_cast<DocumentObjectPy*>(seq[0].ptr())->getDocumentObjectPtr();
231 if (seq.size() > 1) {
232 sub.setPyObject(seq[1].ptr());
233 if (seq.size() > 2) {
234 subs.setPyObject(seq[2].ptr());
235 }
236 }
237 }
238 }
239 }
240 ext->setLink(index, obj, sub.getValue(), subs.getValue());
241 }
242
243 PyObject* LinkBaseExtensionPy::setLink(PyObject* _args)
244 {
245 Py::Sequence args(_args);
246 PY_TRY
247 {
248 auto ext = getLinkBaseExtensionPtr();
249 PyObject* pcObj = args.size() ? args[0].ptr() : Py_None;
250 if (pcObj == Py_None) {
251 ext->setLink(-1, nullptr);
252 }
253 else if (PyDict_Check(pcObj)) {
254 PyObject *key, *value;
255 Py_ssize_t pos = 0;
256 while (PyDict_Next(pcObj, &pos, &key, &value)) {
257 parseLink(ext, Py::Long(key), value);
258 }
259 }
260 else if (PySequence_Check(pcObj)) {
261 ext->setLink(-1, nullptr);
262 Py::Sequence seq(pcObj);
263 for (Py_ssize_t i = 0; i < seq.size(); ++i) {
264 parseLink(ext, i, seq[i].ptr());
265 }
266 }
267 else {
268 parseLink(ext, -1, _args);
269 }
270
271 Py_Return;
272 }
273 PY_CATCH
274 }
275
276 PyObject* LinkBaseExtensionPy::cacheChildLabel(PyObject* args)
277 {
278 PyObject* enable = Py_True;
279 if (!PyArg_ParseTuple(args, "|O", &enable)) {
280 return nullptr;
281 }
282 PY_TRY
283 {
284 getLinkBaseExtensionPtr()->cacheChildLabel(Base::asBoolean(enable) ? -1 : 0);
285 Py_Return;
286 }
287 PY_CATCH;
288 }
289
290 PyObject* LinkBaseExtensionPy::flattenSubname(PyObject* args)
291 {
292 const char* subname;
293 if (!PyArg_ParseTuple(args, "s", &subname)) {
294 return nullptr;
295 }
296 PY_TRY
297 {
298 return Py::new_reference_to(Py::String(getLinkBaseExtensionPtr()->flattenSubname(subname)));
299 }
300 PY_CATCH;
301 }
302
303 PyObject* LinkBaseExtensionPy::expandSubname(PyObject* args)
304 {
305 const char* subname;
306 if (!PyArg_ParseTuple(args, "s", &subname)) {
307 return nullptr;
308 }
309 PY_TRY
310 {
311 std::string sub(subname);
312 getLinkBaseExtensionPtr()->expandSubname(sub);
313 return Py::new_reference_to(Py::String(sub));
314 }
315 PY_CATCH;
316 }
317
318 Py::List LinkBaseExtensionPy::getLinkedChildren() const
319 {
320 Py::List ret;
321 for (auto o : getLinkBaseExtensionPtr()->getLinkedChildren(true)) {
322 ret.append(Py::asObject(o->getPyObject()));
323 }
324 return ret;
325 }
326
327 PyObject* LinkBaseExtensionPy::getCustomAttributes(const char* /*attr*/) const
328 {
329 return nullptr;
330 }
331
332 int LinkBaseExtensionPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
333 {
334 return 0;
335 }