Debian Codegraph @

src/Base/ParameterPy.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2002 Jürgen Riegel <[email protected]> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Library General Public License (LGPL) *
10 * as published by the Free Software Foundation; either version 2 of *
11 * the License, or (at your option) any later version. *
12 * for detail see the LICENCE text file. *
13 * *
14 * FreeCAD is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU Library General Public License for more details. *
18 * *
19 * You should have received a copy of the GNU Library General Public *
20 * License along with FreeCAD; if not, write to the Free Software *
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
22 * USA *
23 * *
24 ***************************************************************************/
25
26 #include <FCConfig.h>
27
28 #ifdef FC_OS_WIN32
29 # include <xercesc/sax/SAXParseException.hpp>
30 #endif
31 #include <list>
32 #include <sstream>
33 #include <string>
34 #include <utility>
35
36 #ifdef FC_OS_LINUX
37 # include <unistd.h>
38 #endif
39
40 #include "Parameter.h"
41 #include "Exception.h"
42 #include "Interpreter.h"
43
44 #include <Tools.h>
45
46
47 namespace Base
48 {
49
50 class ParameterGrpObserver: public ParameterGrp::ObserverType // NOLINT
51 {
52 public:
53 explicit ParameterGrpObserver(const Py::Object& obj)
54 : inst {obj}
55 {}
56 ParameterGrpObserver(const Py::Object& obj, const Py::Object& callable, ParameterGrp* target)
57 : callable(callable)
58 , _target(target)
59 , inst(obj)
60 {}
61 ~ParameterGrpObserver() override
62 {
63 Base::PyGILStateLocker lock;
64 inst = Py::None();
65 callable = Py::None();
66 }
67 void OnChange(ParameterGrp::SubjectType& rCaller, ParameterGrp::MessageType Reason) override
68 {
69 Base::PyGILStateLocker lock;
70 try {
71 auto& rGrp = dynamic_cast<ParameterGrp&>(rCaller);
72 ParameterGrp::handle hGrp(&rGrp);
73 Py::Callable method(this->inst.getAttr(std::string("onChange")));
74 Py::Tuple args(2);
75 args.setItem(0, Py::asObject(GetPyObject(hGrp)));
76 // A Reason of null indicates to clear the parameter group
77 if (!Base::Tools::isNullOrEmpty(Reason)) {
78 args.setItem(1, Py::String(Reason));
79 }
80 method.apply(args);
81 }
82 catch (Py::Exception&) {
83 Base::PyException e; // extract the Python error text
84 e.reportException();
85 }
86 }
87 bool isEqual(const Py::Object& obj) const
88 {
89 return this->inst.is(obj);
90 }
91
92 public:
93 // NOLINTBEGIN
94 Py::Object callable;
95 boost::signals2::scoped_connection conn;
96 ParameterGrp* _target = nullptr; // no reference counted, do not access
97 // NOLINTEND
98
99 private:
100 Py::Object inst;
101 };
102
103 using ParameterGrpObserverList = std::list<ParameterGrpObserver*>;
104
105 class ParameterGrpPy: public Py::PythonExtension<ParameterGrpPy> // NOLINT
106 {
107 public:
108 static void init_type(); // announce properties and methods
109
110 explicit ParameterGrpPy(const Base::Reference<ParameterGrp>& rcParamGrp);
111 ~ParameterGrpPy() override;
112
113 Py::Object repr() override;
114
115 // NOLINTBEGIN
116 Py::Object getGroup(const Py::Tuple&);
117 Py::Object getGroupName(const Py::Tuple&);
118 Py::Object getGroups(const Py::Tuple&);
119 Py::Object remGroup(const Py::Tuple&);
120 Py::Object hasGroup(const Py::Tuple&);
121 Py::Object renameGroup(const Py::Tuple&);
122 Py::Object copyTo(const Py::Tuple&);
123
124 Py::Object getManager(const Py::Tuple&);
125 Py::Object getParent(const Py::Tuple&);
126
127 Py::Object isEmpty(const Py::Tuple&);
128 Py::Object clear(const Py::Tuple&);
129
130 Py::Object attach(const Py::Tuple&);
131 Py::Object attachManager(const Py::Tuple& args);
132 Py::Object detach(const Py::Tuple&);
133 Py::Object notify(const Py::Tuple&);
134 Py::Object notifyAll(const Py::Tuple&);
135
136 Py::Object setBool(const Py::Tuple&);
137 Py::Object getBool(const Py::Tuple&);
138 Py::Object getBools(const Py::Tuple&);
139 Py::Object remBool(const Py::Tuple&);
140
141 Py::Object setInt(const Py::Tuple&);
142 Py::Object getInt(const Py::Tuple&);
143 Py::Object getInts(const Py::Tuple&);
144 Py::Object remInt(const Py::Tuple&);
145
146 Py::Object setUnsigned(const Py::Tuple&);
147 Py::Object getUnsigned(const Py::Tuple&);
148 Py::Object getUnsigneds(const Py::Tuple&);
149 Py::Object remUnsigned(const Py::Tuple&);
150
151 Py::Object setFloat(const Py::Tuple&);
152 Py::Object getFloat(const Py::Tuple&);
153 Py::Object getFloats(const Py::Tuple&);
154 Py::Object remFloat(const Py::Tuple&);
155
156 Py::Object setString(const Py::Tuple&);
157 Py::Object getString(const Py::Tuple&);
158 Py::Object getStrings(const Py::Tuple&);
159 Py::Object remString(const Py::Tuple&);
160
161 Py::Object importFrom(const Py::Tuple&);
162 Py::Object insert(const Py::Tuple&);
163 Py::Object exportTo(const Py::Tuple&);
164
165 Py::Object getContents(const Py::Tuple&);
166 // NOLINTEND
167
168 private:
169 void tryCall(
170 ParameterGrpObserver* obs,
171 ParameterGrp* Param,
172 ParameterGrp::ParamType Type,
173 const char* Name,
174 const char* Value
175 );
176
177 private:
178 ParameterGrp::handle _cParamGrp;
179 ParameterGrpObserverList _observers;
180 };
181
182 // ---------------------------------------------------------
183
184 void ParameterGrpPy::init_type()
185 {
186 behaviors().name("ParameterGrp");
187 behaviors().doc("Python interface class to set parameters");
188 // you must have overwritten the virtual functions
189 behaviors().supportRepr();
190 behaviors().supportGetattr();
191 behaviors().supportSetattr();
192 behaviors().readyType();
193
194 add_varargs_method("GetGroup", &ParameterGrpPy::getGroup, "GetGroup(str)");
195 add_varargs_method("GetGroupName", &ParameterGrpPy::getGroupName, "GetGroupName()");
196 add_varargs_method("GetGroups", &ParameterGrpPy::getGroups, "GetGroups()");
197 add_varargs_method("RemGroup", &ParameterGrpPy::remGroup, "RemGroup(str)");
198 add_varargs_method("HasGroup", &ParameterGrpPy::hasGroup, "HasGroup(str)");
199 add_varargs_method("RenameGroup", &ParameterGrpPy::renameGroup, "RenameGroup(str, str)");
200 add_varargs_method("CopyTo", &ParameterGrpPy::copyTo, "copyTo(ParameterGrp)");
201
202 add_varargs_method("Manager", &ParameterGrpPy::getManager, "Manager()");
203 add_varargs_method("Parent", &ParameterGrpPy::getParent, "Parent()");
204
205 add_varargs_method("IsEmpty", &ParameterGrpPy::isEmpty, "IsEmpty()");
206 add_varargs_method("Clear", &ParameterGrpPy::clear, "Clear()");
207
208 add_varargs_method("Attach", &ParameterGrpPy::attach, "Attach()");
209 add_varargs_method(
210 "AttachManager",
211 &ParameterGrpPy::attachManager,
212 "AttachManager(observer) -- attach parameter manager for notification\n\n"
213 "This method attaches a user defined observer to the manager (i.e. the root)\n"
214 "of the current parameter group to receive notification of all its parameters\n"
215 "and those from its sub-groups\n\n"
216 "The method expects the observer to have a callable attribute as shown below\n"
217 " slotParamChanged(param, tp, name, value)\n"
218 "where 'param' is the parameter group causing the change, 'tp' is the type of\n"
219 "the parameter, 'name' is the name of the parameter, and 'value' is the current\n"
220 "value.\n\n"
221 "The possible value of type are, 'FCBool', 'FCInt', 'FCUint', 'FCFloat', 'FCText',\n"
222 "and 'FCParamGroup'. The notification is triggered when value is changed, in which\n"
223 "case 'value' contains the new value in text form, or, when the parameter is removed,\n"
224 "in which case 'value' is empty.\n\n"
225 "For 'FCParamGroup' type, the observer will be notified in the following events.\n"
226 "* Group creation: both 'name' and 'value' contain the name of the new group\n"
227 "* Group removal: both 'name' and 'value' are empty\n"
228 "* Group rename: 'name' is the new name, and 'value' is the old name"
229 );
230 add_varargs_method("Detach", &ParameterGrpPy::detach, "Detach()");
231 add_varargs_method("Notify", &ParameterGrpPy::notify, "Notify()");
232 add_varargs_method("NotifyAll", &ParameterGrpPy::notifyAll, "NotifyAll()");
233
234 add_varargs_method("SetBool", &ParameterGrpPy::setBool, "SetBool()");
235 add_varargs_method("GetBool", &ParameterGrpPy::getBool, "GetBool()");
236 add_varargs_method("GetBools", &ParameterGrpPy::getBools, "GetBools()");
237 add_varargs_method("RemBool", &ParameterGrpPy::remBool, "RemBool()");
238
239 add_varargs_method("SetInt", &ParameterGrpPy::setInt, "SetInt()");
240 add_varargs_method("GetInt", &ParameterGrpPy::getInt, "GetInt()");
241 add_varargs_method("GetInts", &ParameterGrpPy::getInts, "GetInts()");
242 add_varargs_method("RemInt", &ParameterGrpPy::remInt, "RemInt()");
243
244 add_varargs_method("SetUnsigned", &ParameterGrpPy::setUnsigned, "SetUnsigned()");
245 add_varargs_method("GetUnsigned", &ParameterGrpPy::getUnsigned, "GetUnsigned()");
246 add_varargs_method("GetUnsigneds", &ParameterGrpPy::getUnsigneds, "GetUnsigneds()");
247 add_varargs_method("RemUnsigned", &ParameterGrpPy::remUnsigned, "RemUnsigned()");
248
249 add_varargs_method("SetFloat", &ParameterGrpPy::setFloat, "SetFloat()");
250 add_varargs_method("GetFloat", &ParameterGrpPy::getFloat, "GetFloat()");
251 add_varargs_method("GetFloats", &ParameterGrpPy::getFloats, "GetFloats()");
252 add_varargs_method("RemFloat", &ParameterGrpPy::remFloat, "RemFloat()");
253
254 add_varargs_method("SetString", &ParameterGrpPy::setString, "SetString()");
255 add_varargs_method("GetString", &ParameterGrpPy::getString, "GetString()");
256 add_varargs_method("GetStrings", &ParameterGrpPy::getStrings, "GetStrings()");
257 add_varargs_method("RemString", &ParameterGrpPy::remString, "RemString()");
258
259 add_varargs_method("Import", &ParameterGrpPy::importFrom, "Import()");
260 add_varargs_method("Insert", &ParameterGrpPy::insert, "Insert()");
261 add_varargs_method("Export", &ParameterGrpPy::exportTo, "Export()");
262
263 add_varargs_method("GetContents", &ParameterGrpPy::getContents, "GetContents()");
264 }
265
266 ParameterGrpPy::ParameterGrpPy(const Base::Reference<ParameterGrp>& rcParamGrp)
267 : _cParamGrp(rcParamGrp)
268 {}
269
270 ParameterGrpPy::~ParameterGrpPy()
271 {
272 try {
273 for (ParameterGrpObserver* obs : _observers) {
274 if (!obs->_target) {
275 _cParamGrp->Detach(obs);
276 }
277 delete obs;
278 }
279 }
280 catch (...) {
281 }
282 }
283
284 Py::Object ParameterGrpPy::repr()
285 {
286 std::stringstream s;
287 s << "<ParameterGrp at " << this << ">";
288 return Py::String(s.str()); // NOLINT
289 }
290
291 Py::Object ParameterGrpPy::importFrom(const Py::Tuple& args)
292 {
293 char* pstr = nullptr;
294 if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
295 throw Py::Exception();
296 }
297
298 try {
299 _cParamGrp->importFrom(pstr);
300 return Py::None();
301 }
302 catch (const Base::Exception& e) {
303 e.setPyException();
304 throw Py::Exception();
305 }
306 }
307
308 Py::Object ParameterGrpPy::insert(const Py::Tuple& args)
309 {
310 char* pstr = nullptr;
311 if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
312 throw Py::Exception();
313 }
314
315 try {
316 _cParamGrp->insert(pstr);
317 return Py::None();
318 }
319 catch (const Base::Exception& e) {
320 e.setPyException();
321 throw Py::Exception();
322 }
323 }
324
325 Py::Object ParameterGrpPy::exportTo(const Py::Tuple& args)
326 {
327 char* pstr = nullptr;
328 if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
329 throw Py::Exception();
330 }
331
332 try {
333 _cParamGrp->exportTo(pstr);
334 return Py::None();
335 }
336 catch (const Base::Exception& e) {
337 e.setPyException();
338 throw Py::Exception();
339 }
340 }
341
342 Py::Object ParameterGrpPy::getGroup(const Py::Tuple& args)
343 {
344 char* pstr = nullptr;
345 if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
346 throw Py::Exception();
347 }
348
349 try {
350 // get the Handle of the wanted group
351 Base::Reference<ParameterGrp> handle = _cParamGrp->GetGroup(pstr);
352 if (handle.isValid()) {
353 // create a python wrapper class
354 auto pcParamGrp = new ParameterGrpPy(handle);
355 // increment the ref count
356 return Py::asObject(pcParamGrp);
357 }
358
359 throw Py::RuntimeError("GetGroup failed");
360 }
361 catch (const Base::Exception& e) {
362 e.setPyException();
363 throw Py::Exception();
364 }
365 }
366
367 Py::Object ParameterGrpPy::getManager(const Py::Tuple& args)
368 {
369 if (!PyArg_ParseTuple(args.ptr(), "")) {
370 throw Py::Exception();
371 }
372
373 // get the Handle of the wanted group
374 Base::Reference<ParameterGrp> handle = _cParamGrp->Manager();
375 if (handle.isValid()) {
376 // create a python wrapper class
377 auto pcParamGrp = new ParameterGrpPy(handle);
378 // increment the ref count
379 return Py::asObject(pcParamGrp);
380 }
381
382 return Py::None();
383 }
384
385 Py::Object ParameterGrpPy::getParent(const Py::Tuple& args)
386 {
387 if (!PyArg_ParseTuple(args.ptr(), "")) {
388 throw Py::Exception();
389 }
390
391 // get the Handle of the wanted group
392 Base::Reference<ParameterGrp> handle = _cParamGrp->Parent();
393 if (handle.isValid()) {
394 // create a python wrapper class
395 auto pcParamGrp = new ParameterGrpPy(handle);
396 // increment the ref count
397 return Py::asObject(pcParamGrp);
398 }
399
400 return Py::None();
401 }
402
403 Py::Object ParameterGrpPy::getGroupName(const Py::Tuple& args)
404 {
405 if (!PyArg_ParseTuple(args.ptr(), "")) {
406 throw Py::Exception();
407 }
408
409 // get the Handle of the wanted group
410 std::string name = _cParamGrp->GetGroupName();
411 return Py::String(name); // NOLINT
412 }
413
414 Py::Object ParameterGrpPy::getGroups(const Py::Tuple& args)
415 {
416 if (!PyArg_ParseTuple(args.ptr(), "")) {
417 throw Py::Exception();
418 }
419
420 // get the Handle of the wanted group
421 std::vector<Base::Reference<ParameterGrp>> handle = _cParamGrp->GetGroups();
422 Py::List list;
423 for (const auto& it : handle) {
424 list.append(Py::String(it->GetGroupName()));
425 }
426
427 return list; // NOLINT
428 }
429
430 Py::Object ParameterGrpPy::setBool(const Py::Tuple& args)
431 {
432 char* pstr = nullptr;
433 int Bool = 0;
434 if (!PyArg_ParseTuple(args.ptr(), "si", &pstr, &Bool)) {
435 throw Py::Exception();
436 }
437
438 _cParamGrp->SetBool(pstr, Bool != 0);
439 return Py::None();
440 }
441
442 Py::Object ParameterGrpPy::getBool(const Py::Tuple& args)
443 {
444 char* pstr = nullptr;
445 int Bool = 0;
446 if (!PyArg_ParseTuple(args.ptr(), "s|i", &pstr, &Bool)) {
447 throw Py::Exception();
448 }
449
450 return Py::Boolean(_cParamGrp->GetBool(pstr, Bool != 0)); // NOLINT
451 }
452
453 Py::Object ParameterGrpPy::getBools(const Py::Tuple& args)
454 {
455 char* filter = nullptr;
456 if (!PyArg_ParseTuple(args.ptr(), "|s", &filter)) {
457 throw Py::Exception();
458 }
459
460 std::vector<std::pair<std::string, bool>> map = _cParamGrp->GetBoolMap(filter);
461 Py::List list;
462 for (const auto& it : map) {
463 list.append(Py::String(it.first));
464 }
465
466 return list; // NOLINT
467 }
468
469 Py::Object ParameterGrpPy::setInt(const Py::Tuple& args)
470 {
471 char* pstr = nullptr;
472 int Int = 0;
473 if (!PyArg_ParseTuple(args.ptr(), "si", &pstr, &Int)) {
474 throw Py::Exception();
475 }
476
477 _cParamGrp->SetInt(pstr, Int);
478 return Py::None();
479 }
480
481 Py::Object ParameterGrpPy::getInt(const Py::Tuple& args)
482 {
483 char* pstr = nullptr;
484 int Int = 0;
485 if (!PyArg_ParseTuple(args.ptr(), "s|i", &pstr, &Int)) {
486 throw Py::Exception();
487 }
488 return Py::Long(_cParamGrp->GetInt(pstr, Int)); // NOLINT
489 }
490
491 Py::Object ParameterGrpPy::getInts(const Py::Tuple& args)
492 {
493 char* filter = nullptr;
494 if (!PyArg_ParseTuple(args.ptr(), "|s", &filter)) {
495 throw Py::Exception();
496 }
497
498 std::vector<std::pair<std::string, long>> map = _cParamGrp->GetIntMap(filter);
499 Py::List list;
500 for (const auto& it : map) {
501 list.append(Py::String(it.first));
502 }
503
504 return list; // NOLINT
505 }
506
507 Py::Object ParameterGrpPy::setUnsigned(const Py::Tuple& args)
508 {
509 char* pstr = nullptr;
510 unsigned int UInt = 0;
511 if (!PyArg_ParseTuple(args.ptr(), "sI", &pstr, &UInt)) {
512 throw Py::Exception();
513 }
514
515 _cParamGrp->SetUnsigned(pstr, UInt);
516 return Py::None();
517 }
518
519 Py::Object ParameterGrpPy::getUnsigned(const Py::Tuple& args)
520 {
521 char* pstr = nullptr;
522 unsigned int UInt = 0;
523 if (!PyArg_ParseTuple(args.ptr(), "s|I", &pstr, &UInt)) {
524 throw Py::Exception();
525 }
526 return Py::Long(_cParamGrp->GetUnsigned(pstr, UInt)); // NOLINT
527 }
528
529 Py::Object ParameterGrpPy::getUnsigneds(const Py::Tuple& args)
530 {
531 char* filter = nullptr;
532 if (!PyArg_ParseTuple(args.ptr(), "|s", &filter)) {
533 throw Py::Exception();
534 }
535
536 std::vector<std::pair<std::string, unsigned long>> map = _cParamGrp->GetUnsignedMap(filter);
537 Py::List list;
538 for (const auto& it : map) {
539 list.append(Py::String(it.first));
540 }
541
542 return list; // NOLINT
543 }
544
545 Py::Object ParameterGrpPy::setFloat(const Py::Tuple& args)
546 {
547 char* pstr = nullptr;
548 double Float {};
549 if (!PyArg_ParseTuple(args.ptr(), "sd", &pstr, &Float)) {
550 throw Py::Exception();
551 }
552
553 _cParamGrp->SetFloat(pstr, Float);
554 return Py::None();
555 }
556
557 Py::Object ParameterGrpPy::getFloat(const Py::Tuple& args)
558 {
559 char* pstr = nullptr;
560 double Float = 0.0;
561 if (!PyArg_ParseTuple(args.ptr(), "s|d", &pstr, &Float)) {
562 throw Py::Exception();
563 }
564
565 return Py::Float(_cParamGrp->GetFloat(pstr, Float)); // NOLINT
566 }
567
568 Py::Object ParameterGrpPy::getFloats(const Py::Tuple& args)
569 {
570 char* filter = nullptr;
571 if (!PyArg_ParseTuple(args.ptr(), "|s", &filter)) {
572 throw Py::Exception();
573 }
574
575 std::vector<std::pair<std::string, double>> map = _cParamGrp->GetFloatMap(filter);
576 Py::List list;
577 for (const auto& it : map) {
578 list.append(Py::String(it.first));
579 }
580
581 return list; // NOLINT
582 }
583
584 Py::Object ParameterGrpPy::setString(const Py::Tuple& args)
585 {
586 char* pstr = nullptr;
587 char* str = nullptr;
588 if (!PyArg_ParseTuple(args.ptr(), "ss", &pstr, &str)) {
589 throw Py::Exception();
590 }
591
592 _cParamGrp->SetASCII(pstr, str);
593 return Py::None();
594 }
595
596 Py::Object ParameterGrpPy::getString(const Py::Tuple& args)
597 {
598 char* pstr = nullptr;
599 const char* str = "";
600 if (!PyArg_ParseTuple(args.ptr(), "s|s", &pstr, &str)) {
601 throw Py::Exception();
602 }
603
604 return Py::String(_cParamGrp->GetASCII(pstr, str)); // NOLINT
605 }
606
607 Py::Object ParameterGrpPy::getStrings(const Py::Tuple& args)
608 {
609 char* filter = nullptr;
610 if (!PyArg_ParseTuple(args.ptr(), "|s", &filter)) {
611 throw Py::Exception();
612 }
613
614 std::vector<std::pair<std::string, std::string>> map = _cParamGrp->GetASCIIMap(filter);
615 Py::List list;
616 for (const auto& it : map) {
617 list.append(Py::String(it.first));
618 }
619
620 return list; // NOLINT
621 }
622
623 Py::Object ParameterGrpPy::remInt(const Py::Tuple& args)
624 {
625 char* pstr = nullptr;
626 if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
627 throw Py::Exception();
628 }
629
630 _cParamGrp->RemoveInt(pstr);
631 return Py::None();
632 }
633
634 Py::Object ParameterGrpPy::remUnsigned(const Py::Tuple& args)
635 {
636 char* pstr = nullptr;
637 if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
638 throw Py::Exception();
639 }
640
641 _cParamGrp->RemoveUnsigned(pstr);
642 return Py::None();
643 }
644
645 Py::Object ParameterGrpPy::remBool(const Py::Tuple& args)
646 {
647 char* pstr = nullptr;
648 if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
649 throw Py::Exception();
650 }
651
652 _cParamGrp->RemoveBool(pstr);
653 return Py::None();
654 }
655
656 Py::Object ParameterGrpPy::remGroup(const Py::Tuple& args)
657 {
658 char* pstr = nullptr;
659 if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
660 throw Py::Exception();
661 }
662
663 _cParamGrp->RemoveGrp(pstr);
664 return Py::None();
665 }
666
667 Py::Object ParameterGrpPy::remFloat(const Py::Tuple& args)
668 {
669 char* pstr = nullptr;
670 if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
671 throw Py::Exception();
672 }
673
674 _cParamGrp->RemoveFloat(pstr);
675 return Py::None();
676 }
677
678 Py::Object ParameterGrpPy::remString(const Py::Tuple& args)
679 {
680 char* pstr = nullptr;
681 if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
682 throw Py::Exception();
683 }
684
685 _cParamGrp->RemoveASCII(pstr);
686 return Py::None();
687 }
688
689 Py::Object ParameterGrpPy::clear(const Py::Tuple& args)
690 {
691 if (!PyArg_ParseTuple(args.ptr(), "")) {
692 throw Py::Exception();
693 }
694
695 _cParamGrp->Clear();
696 return Py::None();
697 }
698
699 Py::Object ParameterGrpPy::isEmpty(const Py::Tuple& args)
700 {
701 if (!PyArg_ParseTuple(args.ptr(), "")) {
702 throw Py::Exception();
703 }
704
705 return Py::Boolean(_cParamGrp->IsEmpty()); // NOLINT
706 }
707
708 Py::Object ParameterGrpPy::hasGroup(const Py::Tuple& args)
709 {
710 char* pstr = nullptr;
711 if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
712 throw Py::Exception();
713 }
714
715 return Py::Boolean(_cParamGrp->HasGroup(pstr)); // NOLINT
716 }
717
718 Py::Object ParameterGrpPy::renameGroup(const Py::Tuple& args)
719 {
720 char* oldname = nullptr;
721 char* newname = nullptr;
722 if (!PyArg_ParseTuple(args.ptr(), "ss", &oldname, &newname)) {
723 throw Py::Exception();
724 }
725
726 return Py::Boolean(_cParamGrp->RenameGrp(oldname, newname)); // NOLINT
727 }
728
729 Py::Object ParameterGrpPy::copyTo(const Py::Tuple& args)
730 {
731 PyObject* pygrp {};
732 if (!PyArg_ParseTuple(args.ptr(), "O!", ParameterGrpPy::type_object(), &pygrp)) {
733 throw Py::Exception();
734 }
735
736 auto grp = static_cast<ParameterGrpPy*>(pygrp); // NOLINT
737 _cParamGrp->copyTo(grp->_cParamGrp);
738 return Py::None();
739 }
740
741 Py::Object ParameterGrpPy::attach(const Py::Tuple& args)
742 {
743 PyObject* obj = nullptr;
744 if (!PyArg_ParseTuple(args.ptr(), "O", &obj)) {
745 throw Py::Exception();
746 }
747
748 Py::Object o(obj);
749 if (!o.hasAttr(std::string("onChange"))) {
750 throw Py::TypeError("Object has no onChange attribute");
751 }
752
753 for (ParameterGrpObserver* it : _observers) {
754 if (it->isEqual(o)) {
755 throw Py::RuntimeError("Object is already attached.");
756 }
757 }
758
759 auto obs = new ParameterGrpObserver(o);
760 _cParamGrp->Attach(obs);
761 _observers.push_back(obs);
762
763 return Py::None();
764 }
765
766 void ParameterGrpPy::tryCall(
767 ParameterGrpObserver* obs,
768 ParameterGrp* Param,
769 ParameterGrp::ParamType Type,
770 const char* Name,
771 const char* Value
772 )
773 {
774 Base::PyGILStateLocker lock;
775 Py::TupleN args(
776 Py::asObject(new ParameterGrpPy(Param)),
777 Py::String(ParameterGrp::TypeName(Type)),
778 Py::String(Name ? Name : ""),
779 Py::String(Value ? Value : "")
780 );
781 try {
782 Py::Callable(obs->callable).apply(args);
783 }
784 catch (Py::Exception&) {
785 Base::PyException e;
786 e.reportException();
787 }
788 }
789
790 Py::Object ParameterGrpPy::attachManager(const Py::Tuple& args)
791 {
792 PyObject* obj = nullptr;
793 if (!PyArg_ParseTuple(args.ptr(), "O", &obj)) {
794 throw Py::Exception();
795 }
796
797 if (!_cParamGrp->Manager()) {
798 throw Py::RuntimeError("Parameter has no manager");
799 }
800
801 Py::Object o(obj);
802 if (!o.hasAttr(std::string("slotParamChanged"))) {
803 throw Py::TypeError("Object has no slotParamChanged attribute");
804 }
805
806 Py::Object attr(o.getAttr("slotParamChanged"));
807 if (!attr.isCallable()) {
808 throw Py::TypeError("Object has no slotParamChanged callable attribute");
809 }
810
811 for (ParameterGrpObserver* it : _observers) {
812 if (it->isEqual(o)) {
813 throw Py::RuntimeError("Object is already attached.");
814 }
815 }
816
817 auto obs = new ParameterGrpObserver(o, attr, _cParamGrp);
818 ParameterManager* man = _cParamGrp->Manager();
819 obs->conn = man->signalParamChanged.connect(
820 [obs,
821 this](ParameterGrp* Param, ParameterGrp::ParamType Type, const char* Name, const char* Value) {
822 if (!Param) {
823 return;
824 }
825 for (auto p = Param; p; p = p->Parent()) {
826 if (p == obs->_target) {
827 tryCall(obs, Param, Type, Name, Value);
828 break;
829 }
830 }
831 }
832 );
833
834 _observers.push_back(obs);
835 return Py::None();
836 }
837
838 Py::Object ParameterGrpPy::detach(const Py::Tuple& args)
839 {
840 PyObject* obj = nullptr;
841 if (!PyArg_ParseTuple(args.ptr(), "O", &obj)) {
842 throw Py::Exception();
843 }
844
845 Py::Object o(obj);
846 if (!o.hasAttr(std::string("onChange"))) {
847 throw Py::TypeError("Object has no onChange attribute");
848 }
849
850 for (auto it = _observers.begin(); it != _observers.end(); ++it) {
851 if ((*it)->isEqual(o)) {
852 ParameterGrpObserver* obs = *it;
853 _observers.erase(it);
854 _cParamGrp->Detach(obs);
855 delete obs;
856 break;
857 }
858 }
859
860 return Py::None();
861 }
862
863 Py::Object ParameterGrpPy::notify(const Py::Tuple& args)
864 {
865 char* pstr = nullptr;
866 if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
867 throw Py::Exception();
868 }
869
870 _cParamGrp->Notify(pstr);
871 return Py::None();
872 }
873
874 Py::Object ParameterGrpPy::notifyAll(const Py::Tuple& args)
875 {
876 if (!PyArg_ParseTuple(args.ptr(), "")) {
877 throw Py::Exception();
878 }
879
880 _cParamGrp->NotifyAll();
881 return Py::None();
882 }
883
884 Py::Object ParameterGrpPy::getContents(const Py::Tuple& args)
885 {
886 if (!PyArg_ParseTuple(args.ptr(), "")) {
887 throw Py::Exception();
888 }
889
890 if (_cParamGrp->IsEmpty()) {
891 return Py::None();
892 }
893
894 Py::List list;
895 // filling up Text nodes
896 std::vector<std::pair<std::string, std::string>> mcTextMap = _cParamGrp->GetASCIIMap();
897 for (const auto& it : mcTextMap) {
898 Py::Tuple t2(3);
899 t2.setItem(0, Py::String("String"));
900 t2.setItem(1, Py::String(it.first.c_str()));
901 t2.setItem(2, Py::String(it.second.c_str()));
902 list.append(t2);
903 }
904
905 // filling up Int nodes
906 std::vector<std::pair<std::string, long>> mcIntMap = _cParamGrp->GetIntMap();
907 for (const auto& it : mcIntMap) {
908 Py::Tuple t3(3);
909 t3.setItem(0, Py::String("Integer"));
910 t3.setItem(1, Py::String(it.first.c_str()));
911 t3.setItem(2, Py::Long(it.second));
912 list.append(t3);
913 }
914
915 // filling up Float nodes
916 std::vector<std::pair<std::string, double>> mcFloatMap = _cParamGrp->GetFloatMap();
917 for (const auto& it : mcFloatMap) {
918 Py::Tuple t4(3);
919 t4.setItem(0, Py::String("Float"));
920 t4.setItem(1, Py::String(it.first.c_str()));
921 t4.setItem(2, Py::Float(it.second));
922 list.append(t4);
923 }
924
925 // filling up bool nodes
926 std::vector<std::pair<std::string, bool>> mcBoolMap = _cParamGrp->GetBoolMap();
927 for (const auto& it : mcBoolMap) {
928 Py::Tuple t5(3);
929 t5.setItem(0, Py::String("Boolean"));
930 t5.setItem(1, Py::String(it.first.c_str()));
931 t5.setItem(2, Py::Boolean(it.second));
932 list.append(t5);
933 }
934
935 // filling up UInt nodes
936 std::vector<std::pair<std::string, unsigned long>> mcUIntMap = _cParamGrp->GetUnsignedMap();
937 for (const auto& it : mcUIntMap) {
938 Py::Tuple t6(3);
939 t6.setItem(0, Py::String("Unsigned Long"));
940 t6.setItem(1, Py::String(it.first.c_str()));
941 t6.setItem(2, Py::Long(it.second));
942 list.append(t6);
943 }
944
945 return list; // NOLINT
946 }
947
948 } // namespace Base
949
950 /** python wrapper function
951 */
952 PyObject* GetPyObject(const Base::Reference<ParameterGrp>& hcParamGrp)
953 {
954 static bool init = false;
955 if (!init) {
956 init = true;
957 Base::ParameterGrpPy::init_type();
958 }
959
960 return new Base::ParameterGrpPy(hcParamGrp);
961 }