Debian Codegraph @

src/App/MetadataPyImp.cpp

raw
1 /**************************************************************************
2 * *
3 * Copyright (c) 2022 FreeCAD Project Association *
4 * *
5 * This file is part of FreeCAD. *
6 * *
7 * FreeCAD is free software: you can redistribute it and/or modify it *
8 * under the terms of the GNU Lesser General Public License as *
9 * published by the Free Software Foundation, either version 2.1 of the *
10 * License, or (at your option) any later version. *
11 * *
12 * FreeCAD is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15 * Lesser General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with FreeCAD. If not, see *
19 * <https://www.gnu.org/licenses/>. *
20 * *
21 **************************************************************************/
22
23
24 #include "Metadata.h"
25
26 #include <Base/FileInfo.h>
27 #include <Base/Tools.h>
28
29 // inclusion of the generated files (generated out of MetadataPy.xml)
30 #include "MetadataPy.h"
31 #include "MetadataPy.cpp"
32
33 using namespace Base;
34 #ifndef XERCES_CPP_NAMESPACE_BEGIN
35 #define XERCES_CPP_NAMESPACE_QUALIFIER
36 using namespace XERCES_CPP_NAMESPACE;
37 #else
38 XERCES_CPP_NAMESPACE_USE
39 #endif
40
41 // Returns a string which represents the object e.g. when printed in Python
42 std::string MetadataPy::representation() const
43 {
44 MetadataPy::PointerType ptr = getMetadataPtr();
45 std::stringstream str;
46 str << "Metadata [Name=(";
47 str << ptr->name();
48 str << "), Description=(";
49 str << ptr->description();
50 if (!ptr->maintainer().empty()) {
51 str << "), Maintainer=(";
52 str << ptr->maintainer().front().name;
53 }
54 str << ")]";
55
56 return str.str();
57 }
58
59 PyObject* MetadataPy::PyMake(struct _typeobject*, PyObject*, PyObject*) // Python wrapper
60 {
61 return new MetadataPy(nullptr);
62 }
63
64 // constructor method
65 int MetadataPy::PyInit(PyObject* args, PyObject* /*kwd*/)
66 {
67 if (PyArg_ParseTuple(args, "")) {
68 setTwinPointer(new Metadata());
69 return 0;
70 }
71
72 // Data may be passed directly in as a bytes-like object buffer
73 PyErr_Clear();
74 Py_buffer dataBuffer;
75 if (PyArg_ParseTuple(args, "y*", &dataBuffer)) {
76 try {
77 // NB: This is making a copy of the buffer for simplicity, but that shouldn't be
78 // necessary. Use either a string_view or a span to avoid the copy in the future.
79 auto md =
80 new Metadata(std::string(static_cast<const char*>(dataBuffer.buf), dataBuffer.len));
81 setTwinPointer(md);
82 return 0;
83 }
84 catch (const Base::XMLBaseException&) {
85 // If the XML read failed, this might have been a path to a file, rather than a
86 // bytes-like object. Fall through to the next case.
87 }
88 }
89
90 // Main class constructor -- takes a file path, loads the metadata from it
91 PyErr_Clear();
92 char* filename;
93 if (PyArg_ParseTuple(args, "et", "utf-8", &filename)) {
94 try {
95 std::string utf8Name = std::string(filename);
96 PyMem_Free(filename);
97
98 auto md = new Metadata(Base::FileInfo::stringToPath(utf8Name));
99 setTwinPointer(md);
100 return 0;
101 }
102 catch (const Base::XMLBaseException& e) {
103 e.setPyException();
104 return -1;
105 }
106 catch (const XMLException& toCatch) {
107 char* message = XMLString::transcode(toCatch.getMessage());
108 std::string what = message;
109 XMLString::release(&message);
110 PyErr_SetString(Base::PyExc_FC_XMLBaseException, what.c_str());
111 return -1;
112 }
113 catch (const DOMException& toCatch) {
114 char* message = XMLString::transcode(toCatch.getMessage());
115 std::string what = message;
116 XMLString::release(&message);
117 PyErr_SetString(Base::PyExc_FC_XMLBaseException, what.c_str());
118 return -1;
119 }
120 catch (...) {
121 PyErr_SetString(Base::PyExc_FC_GeneralError, "Failed to create Metadata object");
122 return -1;
123 }
124 }
125
126 // Copy constructor
127 PyErr_Clear();
128 PyObject* o;
129 if (PyArg_ParseTuple(args, "O!", &(App::MetadataPy::Type), &o)) {
130 App::Metadata* a = static_cast<App::MetadataPy*>(o)->getMetadataPtr();
131 setTwinPointer(new Metadata(*a));
132 return 0;
133 }
134
135 PyErr_SetString(Base::PyExc_FC_GeneralError,
136 "metadata object or path to metadata file expected");
137 return -1;
138 }
139
140 Py::String MetadataPy::getName() const
141 {
142 return Py::String(getMetadataPtr()->name());
143 }
144
145 void MetadataPy::setName(Py::String args)
146 {
147 const char* name = nullptr;
148 if (!PyArg_Parse(args.ptr(), "z", &name)) {
149 throw Py::Exception();
150 }
151 if (name) {
152 getMetadataPtr()->setName(name);
153 }
154 else {
155 getMetadataPtr()->setName("");
156 }
157 }
158
159 Py::String MetadataPy::getVersion() const
160 {
161 return Py::String(getMetadataPtr()->version().str());
162 }
163
164 void MetadataPy::setVersion(Py::String args)
165 {
166 const char* name = nullptr;
167 if (!PyArg_Parse(args.ptr(), "z", &name)) {
168 throw Py::Exception();
169 }
170 if (!Base::Tools::isNullOrEmpty(name)) {
171 getMetadataPtr()->setVersion(App::Meta::Version(std::string(name)));
172 }
173 else {
174 getMetadataPtr()->setVersion(App::Meta::Version());
175 }
176 }
177
178 Py::String MetadataPy::getDate() const
179 {
180 return Py::String(getMetadataPtr()->date());
181 }
182
183 void MetadataPy::setDate(Py::String args)
184 {
185 const char* date = nullptr;
186 if (!PyArg_Parse(args.ptr(), "z", &date)) {
187 throw Py::Exception();
188 }
189 if (date) {
190 getMetadataPtr()->setDate(date);
191 }
192 else {
193 getMetadataPtr()->setDate("");
194 }
195 }
196
197 Py::String MetadataPy::getDescription() const
198 {
199 return Py::String(getMetadataPtr()->description());
200 }
201
202 void MetadataPy::setDescription(Py::String args)
203 {
204 const char* description = nullptr;
205 if (!PyArg_Parse(args.ptr(), "s", &description)) {
206 throw Py::Exception();
207 }
208 getMetadataPtr()->setDescription(description);
209 }
210
211 Py::String MetadataPy::getType() const
212 {
213 return Py::String(getMetadataPtr()->type());
214 }
215
216 void MetadataPy::setType(Py::String args)
217 {
218 const char* type = nullptr;
219 if (!PyArg_Parse(args.ptr(), "s", &type)) {
220 throw Py::Exception();
221 }
222 getMetadataPtr()->setType(type);
223 }
224
225 Py::List MetadataPy::getMaintainer() const
226 {
227 auto maintainers = getMetadataPtr()->maintainer();
228 Py::List pyMaintainers;
229 for (const auto& m : maintainers) {
230 Py::Dict pyMaintainer;
231 pyMaintainer["name"] = Py::String(m.name);
232 pyMaintainer["email"] = Py::String(m.email);
233 pyMaintainers.append(pyMaintainer);
234 }
235 return pyMaintainers;
236 }
237
238 void MetadataPy::setMaintainer(Py::List args)
239 {
240 PyObject* list = nullptr;
241 if (!PyArg_Parse(args.ptr(), "O!", &PyList_Type, &list)) {
242 throw Py::Exception();
243 }
244
245 getMetadataPtr()->clearMaintainer();
246 Py::List maintainers(list);
247 for (const auto& m : maintainers) {
248 Py::Dict pyMaintainer(m);
249 std::string name = pyMaintainer["name"].str();
250 std::string email = pyMaintainer["email"].str();
251 getMetadataPtr()->addMaintainer(App::Meta::Contact(name, email));
252 }
253 }
254
255 PyObject* MetadataPy::addMaintainer(PyObject* args)
256 {
257 const char* name = nullptr;
258 const char* email = nullptr;
259 if (!PyArg_ParseTuple(args, "ss", &name, &email)) {
260 throw Py::Exception();
261 }
262 getMetadataPtr()->addMaintainer(App::Meta::Contact(name, email));
263 Py_INCREF(Py_None);
264 return Py_None;
265 }
266
267 PyObject* MetadataPy::removeMaintainer(PyObject* args)
268 {
269 const char* name = nullptr;
270 const char* email = nullptr;
271 if (!PyArg_ParseTuple(args, "ss", &name, &email)) {
272 throw Py::Exception();
273 }
274 getMetadataPtr()->removeMaintainer(App::Meta::Contact(name, email));
275 Py_INCREF(Py_None);
276 return Py_None;
277 }
278
279
280 Py::List MetadataPy::getAuthor() const
281 {
282 auto authors = getMetadataPtr()->author();
283 Py::List pyAuthors;
284 for (const auto& a : authors) {
285 Py::Dict pyAuthor;
286 pyAuthor["name"] = Py::String(a.name);
287 pyAuthor["email"] = Py::String(a.email);
288 pyAuthors.append(pyAuthor);
289 }
290 return pyAuthors;
291 }
292
293 void MetadataPy::setAuthor(Py::List args)
294 {
295 PyObject* list = nullptr;
296 if (!PyArg_Parse(args.ptr(), "O!", &PyList_Type, &list)) {
297 throw Py::Exception();
298 }
299
300 getMetadataPtr()->clearAuthor();
301 Py::List authors(list);
302 for (const auto& a : authors) {
303 Py::Dict pyAuthor(a);
304 std::string name = pyAuthor["name"].str();
305 std::string email = pyAuthor["email"].str();
306 getMetadataPtr()->addAuthor(App::Meta::Contact(name, email));
307 }
308 }
309
310 PyObject* MetadataPy::addAuthor(PyObject* args)
311 {
312 const char* name = nullptr;
313 const char* email = nullptr;
314 if (!PyArg_ParseTuple(args, "ss", &name, &email)) {
315 throw Py::Exception();
316 }
317 getMetadataPtr()->addAuthor(App::Meta::Contact(name, email));
318 Py_INCREF(Py_None);
319 return Py_None;
320 }
321
322 PyObject* MetadataPy::removeAuthor(PyObject* args)
323 {
324 const char* name = nullptr;
325 const char* email = nullptr;
326 if (!PyArg_ParseTuple(args, "ss", &name, &email)) {
327 throw Py::Exception();
328 }
329 getMetadataPtr()->removeAuthor(App::Meta::Contact(name, email));
330 Py_INCREF(Py_None);
331 return Py_None;
332 }
333
334 Py::List MetadataPy::getLicense() const
335 {
336 auto licenses = getMetadataPtr()->license();
337 Py::List pyLicenses;
338 for (const auto& lic : licenses) {
339 Py::Dict pyLicense;
340 pyLicense["name"] = Py::String(lic.name);
341 pyLicense["file"] = Py::String(lic.file.string());
342 pyLicenses.append(pyLicense);
343 }
344 return pyLicenses;
345 }
346
347 void MetadataPy::setLicense(Py::List args)
348 {
349 PyObject* list = nullptr;
350 if (!PyArg_Parse(args.ptr(), "O!", &PyList_Type, &list)) {
351 throw Py::Exception();
352 }
353
354 getMetadataPtr()->clearLicense();
355 Py::List licenses(list);
356 for (const auto& l : licenses) {
357 Py::Dict pyLicense(l);
358 std::string name = pyLicense["name"].str();
359 std::string path = pyLicense["file"].str();
360 getMetadataPtr()->addLicense(App::Meta::License(name, path));
361 }
362 }
363
364 PyObject* MetadataPy::addLicense(PyObject* args)
365 {
366 const char* shortCode = nullptr;
367 const char* path = nullptr;
368 if (!PyArg_ParseTuple(args, "ss", &shortCode, &path)) {
369 throw Py::Exception();
370 }
371 getMetadataPtr()->addLicense(App::Meta::License(shortCode, path));
372 Py_INCREF(Py_None);
373 return Py_None;
374 }
375
376 PyObject* MetadataPy::removeLicense(PyObject* args)
377 {
378 const char* shortCode = nullptr;
379 const char* path = nullptr;
380 if (!PyArg_ParseTuple(args, "ss", &shortCode, &path)) {
381 throw Py::Exception();
382 }
383 getMetadataPtr()->removeLicense(App::Meta::License(shortCode, path));
384 Py_INCREF(Py_None);
385 return Py_None;
386 }
387
388 Py::List MetadataPy::getUrls() const
389 {
390 auto urls = getMetadataPtr()->url();
391 Py::List pyUrls;
392 for (const auto& url : urls) {
393 Py::Dict pyUrl;
394 pyUrl["location"] = Py::String(url.location);
395 switch (url.type) {
396 case Meta::UrlType::website:
397 pyUrl["type"] = Py::String("website");
398 break;
399 case Meta::UrlType::repository:
400 pyUrl["type"] = Py::String("repository");
401 break;
402 case Meta::UrlType::bugtracker:
403 pyUrl["type"] = Py::String("bugtracker");
404 break;
405 case Meta::UrlType::readme:
406 pyUrl["type"] = Py::String("readme");
407 break;
408 case Meta::UrlType::documentation:
409 pyUrl["type"] = Py::String("documentation");
410 break;
411 case Meta::UrlType::discussion:
412 pyUrl["type"] = Py::String("discussion");
413 break;
414 default:
415 pyUrl["type"] = Py::String("unknown");
416 break;
417 }
418 if (url.type == Meta::UrlType::repository) {
419 pyUrl["branch"] = Py::String(url.branch);
420 }
421 pyUrls.append(pyUrl);
422 }
423 return pyUrls;
424 }
425
426 void MetadataPy::setUrls(Py::List args)
427 {
428 PyObject* list = nullptr;
429 if (!PyArg_Parse(args.ptr(), "O!", &PyList_Type, &list)) {
430 throw Py::Exception();
431 }
432
433 getMetadataPtr()->clearUrl();
434 Py::List urls(list);
435 for (const auto& url : urls) {
436 Py::Dict pyUrl(url);
437 std::string location = pyUrl["location"].str();
438 std::string typeAsString = pyUrl["type"].str();
439 std::string branch = pyUrl["branch"].str();
440 auto newUrl = App::Meta::Url(location, Meta::UrlType::website);
441 if (typeAsString == "website") {
442 newUrl.type = Meta::UrlType::website;
443 }
444 else if (typeAsString == "repository") {
445 newUrl.type = Meta::UrlType::repository;
446 newUrl.branch = branch;
447 }
448 else if (typeAsString == "bugtracker") {
449 newUrl.type = Meta::UrlType::bugtracker;
450 }
451 else if (typeAsString == "readme") {
452 newUrl.type = Meta::UrlType::readme;
453 }
454 else if (typeAsString == "documentation") {
455 newUrl.type = Meta::UrlType::documentation;
456 }
457 else if (typeAsString == "discussion") {
458 newUrl.type = Meta::UrlType::discussion;
459 }
460 else {
461 PyErr_SetString(Base::PyExc_FC_GeneralError, "Unrecognized URL type");
462 return;
463 }
464 getMetadataPtr()->addUrl(newUrl);
465 }
466 }
467
468 App::Meta::Url urlFromStrings(const char* urlTypeCharStar, const char* link, const char* branch)
469 {
470 std::string urlTypeString(urlTypeCharStar);
471 App::Meta::UrlType urlType {App::Meta::UrlType::documentation};
472 if (urlTypeString == "repository") {
473 urlType = App::Meta::UrlType::repository;
474 }
475 else if (urlTypeString == "bugtracker") {
476 urlType = App::Meta::UrlType::bugtracker;
477 }
478 else if (urlTypeString == "documentation") {
479 urlType = App::Meta::UrlType::documentation;
480 }
481 else if (urlTypeString == "readme") {
482 urlType = App::Meta::UrlType::readme;
483 }
484 else if (urlTypeString == "website") {
485 urlType = App::Meta::UrlType::website;
486 }
487
488 App::Meta::Url url(link, urlType);
489 if (urlType == App::Meta::UrlType::repository) {
490 url.branch = std::string(branch);
491 }
492
493 return url;
494 }
495
496 PyObject* MetadataPy::addUrl(PyObject* args)
497 {
498 const char* urlTypeCharStar = nullptr;
499 const char* link = nullptr;
500 const char* branch = nullptr;
501 if (!PyArg_ParseTuple(args, "ss|s", &urlTypeCharStar, &link, &branch)) {
502 throw Py::Exception();
503 }
504
505 getMetadataPtr()->addUrl(urlFromStrings(urlTypeCharStar, link, branch));
506 Py_INCREF(Py_None);
507 return Py_None;
508 }
509
510 PyObject* MetadataPy::removeUrl(PyObject* args)
511 {
512 const char* urlTypeCharStar = nullptr;
513 const char* link = nullptr;
514 const char* branch = nullptr;
515 if (!PyArg_ParseTuple(args, "ss|s", &urlTypeCharStar, &link, &branch)) {
516 throw Py::Exception();
517 }
518
519 getMetadataPtr()->removeUrl(urlFromStrings(urlTypeCharStar, link, branch));
520 Py_INCREF(Py_None);
521 return Py_None;
522 }
523
524 Py::Object dependencyToPyObject(const Meta::Dependency& d)
525 {
526 Py::Dict pyDependency;
527 pyDependency["package"] = Py::String(d.package);
528 pyDependency["version_lt"] = Py::String(d.version_lt);
529 pyDependency["version_lte"] = Py::String(d.version_lte);
530 pyDependency["version_eq"] = Py::String(d.version_eq);
531 pyDependency["version_gt"] = Py::String(d.version_gt);
532 pyDependency["version_gte"] = Py::String(d.version_gte);
533 pyDependency["condition"] = Py::String(d.condition);
534 pyDependency["optional"] = Py::Boolean(d.optional);
535 switch (d.dependencyType) {
536 case App::Meta::DependencyType::automatic:
537 pyDependency["type"] = Py::String("automatic");
538 break;
539 case App::Meta::DependencyType::addon:
540 pyDependency["type"] = Py::String("addon");
541 break;
542 case App::Meta::DependencyType::internal:
543 pyDependency["type"] = Py::String("internal");
544 break;
545 case App::Meta::DependencyType::python:
546 pyDependency["type"] = Py::String("python");
547 break;
548 }
549 return pyDependency;
550 }
551
552 Meta::Dependency pyObjectToDependency(const Py::Object& d)
553 {
554 Py::Dict pyDependency(d);
555 Meta::Dependency result;
556 result.package = pyDependency["package"].str();
557 if (pyDependency.hasKey("version_lt")) {
558 result.version_lt = pyDependency["version_lt"].str();
559 }
560 if (pyDependency.hasKey("version_lte")) {
561 result.version_lte = pyDependency["version_lte"].str();
562 }
563 if (pyDependency.hasKey("version_eq")) {
564 result.version_eq = pyDependency["version_eq"].str();
565 }
566 if (pyDependency.hasKey("version_gt")) {
567 result.version_gt = pyDependency["version_gt"].str();
568 }
569 if (pyDependency.hasKey("version_gte")) {
570 result.version_gte = pyDependency["version_gte"].str();
571 }
572 if (pyDependency.hasKey("condition")) {
573 result.condition = pyDependency["condition"].str();
574 }
575 if (pyDependency.hasKey("optional")) {
576 result.optional = Py::Boolean(pyDependency["optional"]).as_bool();
577 }
578 if (pyDependency.hasKey("type")) {
579 if (pyDependency["type"].str() == Py::String("automatic")) {
580 result.dependencyType = App::Meta::DependencyType::automatic;
581 }
582 else if (pyDependency["type"].str() == Py::String("internal")) {
583 result.dependencyType = App::Meta::DependencyType::internal;
584 }
585 else if (pyDependency["type"].str() == Py::String("addon")) {
586 result.dependencyType = App::Meta::DependencyType::addon;
587 }
588 else if (pyDependency["type"].str() == Py::String("python")) {
589 result.dependencyType = App::Meta::DependencyType::python;
590 }
591 }
592 return result;
593 }
594
595 Py::List MetadataPy::getDepend() const
596 {
597 auto dependencies = getMetadataPtr()->depend();
598 Py::List pyDependencies;
599 for (const auto& d : dependencies) {
600 pyDependencies.append(dependencyToPyObject(d));
601 }
602 return pyDependencies;
603 }
604
605 void MetadataPy::setDepend(Py::List args)
606 {
607 PyObject* list = nullptr;
608 if (!PyArg_Parse(args.ptr(), "O!", &PyList_Type, &list)) {
609 throw Py::Exception();
610 }
611
612 getMetadataPtr()->clearDepend();
613 Py::List deps(list);
614 for (const auto& dep : deps) {
615 Py::Dict pyDep(dep);
616 getMetadataPtr()->addDepend(pyObjectToDependency(pyDep));
617 }
618 }
619
620 PyObject* MetadataPy::addDepend(PyObject* args)
621 {
622 PyObject* dictionary = nullptr;
623 if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dictionary)) {
624 throw Py::Exception();
625 }
626 Py::Dict pyDep(dictionary);
627 getMetadataPtr()->addDepend(pyObjectToDependency(pyDep));
628 Py_INCREF(Py_None);
629 return Py_None;
630 }
631
632 PyObject* MetadataPy::removeDepend(PyObject* args)
633 {
634 PyObject* dictionary = nullptr;
635 if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dictionary)) {
636 throw Py::Exception();
637 }
638 Py::Dict pyDep(dictionary);
639 getMetadataPtr()->removeDepend(pyObjectToDependency(pyDep));
640 Py_INCREF(Py_None);
641 return Py_None;
642 }
643
644 Py::List MetadataPy::getConflict() const
645 {
646 auto dependencies = getMetadataPtr()->conflict();
647 Py::List pyDependencies;
648 for (const auto& d : dependencies) {
649 pyDependencies.append(dependencyToPyObject(d));
650 }
651 return pyDependencies;
652 }
653
654 void MetadataPy::setConflict(Py::List args)
655 {
656 PyObject* list = nullptr;
657 if (!PyArg_Parse(args.ptr(), "O!", &PyList_Type, &list)) {
658 throw Py::Exception();
659 }
660
661 getMetadataPtr()->clearConflict();
662 Py::List deps(list);
663 for (const auto& dep : deps) {
664 Py::Dict pyDep(dep);
665 getMetadataPtr()->addConflict(pyObjectToDependency(pyDep));
666 }
667 }
668
669 PyObject* MetadataPy::addConflict(PyObject* args)
670 {
671 PyObject* dictionary = nullptr;
672 if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dictionary)) {
673 throw Py::Exception();
674 }
675 Py::Dict pyDep(dictionary);
676 getMetadataPtr()->addConflict(pyObjectToDependency(pyDep));
677 Py_INCREF(Py_None);
678 return Py_None;
679 }
680
681 PyObject* MetadataPy::removeConflict(PyObject* args)
682 {
683 PyObject* dictionary = nullptr;
684 if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dictionary)) {
685 throw Py::Exception();
686 }
687 Py::Dict pyDep(dictionary);
688 getMetadataPtr()->removeConflict(pyObjectToDependency(pyDep));
689 Py_INCREF(Py_None);
690 return Py_None;
691 }
692
693 Py::List MetadataPy::getReplace() const
694 {
695 auto dependencies = getMetadataPtr()->replace();
696 Py::List pyDependencies;
697 for (const auto& d : dependencies) {
698 pyDependencies.append(dependencyToPyObject(d));
699 }
700 return pyDependencies;
701 }
702
703 void MetadataPy::setReplace(Py::List args)
704 {
705 PyObject* list = nullptr;
706 if (!PyArg_Parse(args.ptr(), "O!", &PyList_Type, &list)) {
707 throw Py::Exception();
708 }
709
710 getMetadataPtr()->clearReplace();
711 Py::List deps(list);
712 for (const auto& dep : deps) {
713 Py::Dict pyDep(dep);
714 getMetadataPtr()->addReplace(pyObjectToDependency(pyDep));
715 }
716 }
717
718 PyObject* MetadataPy::addReplace(PyObject* args)
719 {
720 PyObject* dictionary = nullptr;
721 if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dictionary)) {
722 throw Py::Exception();
723 }
724 Py::Dict pyDep(dictionary);
725 getMetadataPtr()->addReplace(pyObjectToDependency(pyDep));
726 Py_INCREF(Py_None);
727 return Py_None;
728 }
729
730 PyObject* MetadataPy::removeReplace(PyObject* args)
731 {
732 PyObject* dictionary = nullptr;
733 if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dictionary)) {
734 throw Py::Exception();
735 }
736 Py::Dict pyDep(dictionary);
737 getMetadataPtr()->removeReplace(pyObjectToDependency(pyDep));
738 Py_INCREF(Py_None);
739 return Py_None;
740 }
741
742 // Tag, icon, classname, file
743
744 Py::List MetadataPy::getTag() const
745 {
746 auto tags = getMetadataPtr()->tag();
747 Py::List pyTags;
748 for (const auto& t : tags) {
749 pyTags.append(Py::String(t));
750 }
751 return pyTags;
752 }
753
754 void MetadataPy::setTag(Py::List args)
755 {
756 PyObject* list = nullptr;
757 if (!PyArg_Parse(args.ptr(), "O!", &PyList_Type, &list)) {
758 throw Py::Exception();
759 }
760
761 getMetadataPtr()->clearTag();
762 Py::List tags(list);
763 for (const auto& tag : tags) {
764 Py::String pyTag(tag);
765 getMetadataPtr()->addTag(pyTag.as_std_string());
766 }
767 }
768
769 PyObject* MetadataPy::addTag(PyObject* args)
770 {
771 const char* tag = nullptr;
772 if (!PyArg_ParseTuple(args, "s", &tag)) {
773 throw Py::Exception();
774 }
775 getMetadataPtr()->addTag(tag);
776 Py_INCREF(Py_None);
777 return Py_None;
778 }
779
780 PyObject* MetadataPy::removeTag(PyObject* args)
781 {
782 const char* tag = nullptr;
783 if (!PyArg_ParseTuple(args, "s", &tag)) {
784 throw Py::Exception();
785 }
786 getMetadataPtr()->removeTag(tag);
787 Py_INCREF(Py_None);
788 return Py_None;
789 }
790
791 Py::String MetadataPy::getIcon() const
792 {
793 return Py::String(getMetadataPtr()->icon().string());
794 }
795
796 void MetadataPy::setIcon(Py::String args)
797 {
798 const char* name;
799 if (!PyArg_Parse(args.ptr(), "s", &name)) {
800 throw Py::Exception();
801 }
802 getMetadataPtr()->setIcon(name);
803 }
804
805 Py::String MetadataPy::getClassname() const
806 {
807 return Py::String(getMetadataPtr()->classname());
808 }
809
810 void MetadataPy::setClassname(Py::String args)
811 {
812 const char* name;
813 if (!PyArg_Parse(args.ptr(), "s", &name)) {
814 throw Py::Exception();
815 }
816 getMetadataPtr()->setClassname(name);
817 }
818
819 Py::String MetadataPy::getSubdirectory() const
820 {
821 return Py::String(getMetadataPtr()->subdirectory().string());
822 }
823
824 void MetadataPy::setSubdirectory(Py::String args)
825 {
826 const char* name;
827 if (!PyArg_Parse(args.ptr(), "s", &name)) {
828 throw Py::Exception();
829 }
830 getMetadataPtr()->setSubdirectory(name);
831 }
832
833 Py::List MetadataPy::getFile() const
834 {
835 auto files = getMetadataPtr()->file();
836 Py::List pyFiles;
837 for (const auto& f : files) {
838 pyFiles.append(Py::String(f.string()));
839 }
840 return pyFiles;
841 }
842
843 void MetadataPy::setFile(Py::List args)
844 {
845 PyObject* list = nullptr;
846 if (!PyArg_Parse(args.ptr(), "O!", &PyList_Type, &list)) {
847 throw Py::Exception();
848 }
849
850 getMetadataPtr()->clearTag();
851 Py::List files(list);
852 for (const auto& file : files) {
853 Py::String pyFile(file);
854 getMetadataPtr()->addFile(pyFile.as_std_string());
855 }
856 }
857
858 PyObject* MetadataPy::addFile(PyObject* args)
859 {
860 const char* file = nullptr;
861 if (!PyArg_ParseTuple(args, "s", &file)) {
862 throw Py::Exception();
863 }
864 getMetadataPtr()->addFile(file);
865 Py_INCREF(Py_None);
866 return Py_None;
867 }
868
869 PyObject* MetadataPy::removeFile(PyObject* args)
870 {
871 const char* file = nullptr;
872 if (!PyArg_ParseTuple(args, "s", &file)) {
873 throw Py::Exception();
874 }
875 getMetadataPtr()->removeFile(file);
876 Py_INCREF(Py_None);
877 return Py_None;
878 }
879
880
881 Py::Dict MetadataPy::getContent() const
882 {
883 auto content = getMetadataPtr()->content();
884 std::set<std::string> keys;
885 for (const auto& item : content) {
886 keys.insert(item.first);
887 }
888
889 // For the Python, we'll use a dictionary of lists to store the content components:
890 Py::Dict pyContent;
891 for (const auto& key : keys) {
892 Py::List pyContentForKey;
893 auto elements = content.equal_range(key);
894 for (auto& element = elements.first; element != elements.second; ++element) {
895 auto contentMetadataItem = new MetadataPy(new Metadata(element->second));
896 pyContentForKey.append(Py::asObject(contentMetadataItem));
897 }
898 pyContent[key] = pyContentForKey;
899 }
900 return pyContent;
901 }
902
903 void MetadataPy::setContent(Py::Dict arg)
904 {
905 PyObject* obj = nullptr;
906 if (!PyArg_Parse(arg.ptr(), "O!", &PyList_Type, &obj)) {
907 throw Py::Exception();
908 }
909
910 getMetadataPtr()->clearContent();
911 Py::Dict outerDict(obj);
912 for (const auto& pyContentType : outerDict) {
913 auto contentType = Py::String(pyContentType.first).as_std_string();
914 auto contentList = Py::List(pyContentType.second);
915 for (const auto& contentItem : contentList) {
916 auto item = static_cast<MetadataPy*>(contentItem.ptr());
917 getMetadataPtr()->addContentItem(contentType, *(item->getMetadataPtr()));
918 }
919 }
920 }
921
922 PyObject* MetadataPy::getGenericMetadata(PyObject* args)
923 {
924 const char* name;
925 if (!PyArg_ParseTuple(args, "s", &name)) {
926 return nullptr;
927 }
928
929 auto gm = (*getMetadataPtr())[name];
930 Py::List pyGenericMetadata;
931 for (const auto& item : gm) {
932 Py::Dict pyItem;
933 pyItem["contents"] = Py::String(item.contents);
934 Py::Dict pyAttributes;
935 for (const auto& attribute : item.attributes) {
936 pyAttributes[attribute.first] = Py::String(attribute.second);
937 }
938 pyItem["attributes"] = pyAttributes;
939 pyGenericMetadata.append(pyItem);
940 }
941 return Py::new_reference_to(pyGenericMetadata);
942 }
943
944 Py::String MetadataPy::getFreeCADMin() const
945 {
946 return Py::String(getMetadataPtr()->freecadmin().str());
947 }
948
949 void MetadataPy::setFreeCADMin(Py::String args)
950 {
951 char* version = nullptr;
952 PyObject* p = args.ptr();
953 if (!PyArg_Parse(p, "z", &version)) {
954 throw Py::Exception();
955 }
956 if (version) {
957 getMetadataPtr()->setFreeCADMin(App::Meta::Version(version));
958 }
959 else {
960 getMetadataPtr()->setFreeCADMin(App::Meta::Version());
961 }
962 }
963
964 Py::String MetadataPy::getFreeCADMax() const
965 {
966 return Py::String(getMetadataPtr()->freecadmax().str());
967 }
968
969 void MetadataPy::setFreeCADMax(Py::String args)
970 {
971 char* version = nullptr;
972 PyObject* p = args.ptr();
973 if (!PyArg_Parse(p, "z", &version)) {
974 throw Py::Exception();
975 }
976
977 if (version) {
978 getMetadataPtr()->setFreeCADMax(App::Meta::Version(version));
979 }
980 else {
981 getMetadataPtr()->setFreeCADMax(App::Meta::Version());
982 }
983 }
984
985 Py::String MetadataPy::getPythonMin() const
986 {
987 return Py::String(getMetadataPtr()->pythonmin().str());
988 }
989
990 void MetadataPy::setPythonMin(Py::String args)
991 {
992 char* version = nullptr;
993 PyObject* p = args.ptr();
994 if (!PyArg_Parse(p, "z", &version)) {
995 throw Py::Exception();
996 }
997 if (version) {
998 getMetadataPtr()->setPythonMin(App::Meta::Version(version));
999 }
1000 else {
1001 getMetadataPtr()->setPythonMin(App::Meta::Version());
1002 }
1003 }
1004
1005 PyObject* MetadataPy::getFirstSupportedFreeCADVersion(PyObject* p)
1006 {
1007 if (!PyArg_ParseTuple(p, "")) {
1008 return nullptr;
1009 }
1010
1011 // Short-circuit: if the toplevel sets a version, then the lower-levels are overridden
1012 if (getMetadataPtr()->freecadmin() != App::Meta::Version()) {
1013 return Py::new_reference_to(Py::String(getMetadataPtr()->freecadmin().str()));
1014 }
1015
1016 auto content = getMetadataPtr()->content();
1017 auto result = App::Meta::Version();
1018 for (const auto& item : content) {
1019 auto minVersion = item.second.freecadmin();
1020 if (minVersion != App::Meta::Version()) {
1021 if (result == App::Meta::Version() || minVersion < result) {
1022 result = minVersion;
1023 }
1024 }
1025 }
1026 if (result != App::Meta::Version()) {
1027 return Py::new_reference_to(Py::String(result.str()));
1028 }
1029 else {
1030 Py_INCREF(Py_None);
1031 return Py_None;
1032 }
1033 }
1034
1035 PyObject* MetadataPy::getLastSupportedFreeCADVersion(PyObject* p)
1036 {
1037 if (!PyArg_ParseTuple(p, "")) {
1038 return nullptr;
1039 }
1040
1041 // Short-circuit: if the toplevel sets a version, then the lower-levels are overridden
1042 if (getMetadataPtr()->freecadmax() != App::Meta::Version()) {
1043 return Py::new_reference_to(Py::String(getMetadataPtr()->freecadmax().str()));
1044 }
1045
1046 auto content = getMetadataPtr()->content();
1047 auto result = App::Meta::Version();
1048 for (const auto& item : content) {
1049 auto maxVersion = item.second.freecadmax();
1050 if (maxVersion != App::Meta::Version()) {
1051 if (result == App::Meta::Version() || maxVersion > result) {
1052 result = maxVersion;
1053 }
1054 }
1055 }
1056 if (result != App::Meta::Version()) {
1057 return Py::new_reference_to(Py::String(result.str()));
1058 }
1059 else {
1060 Py_INCREF(Py_None);
1061 return Py_None;
1062 }
1063 }
1064
1065 PyObject* MetadataPy::supportsCurrentFreeCAD(PyObject* p)
1066 {
1067 if (!PyArg_ParseTuple(p, "")) {
1068 return nullptr;
1069 }
1070
1071 bool result = getMetadataPtr()->supportsCurrentFreeCAD();
1072 return Py::new_reference_to(Py::Boolean(result));
1073 }
1074
1075 PyObject* MetadataPy::addContentItem(PyObject* arg)
1076 {
1077 char* contentType = nullptr;
1078 PyObject* contentItem = nullptr;
1079 if (!PyArg_ParseTuple(arg, "sO!", &contentType, &(App::MetadataPy::Type), &contentItem)) {
1080 return nullptr;
1081 }
1082
1083 if (!contentItem || !contentType) {
1084 return nullptr;
1085 }
1086 auto item = static_cast<MetadataPy*>(contentItem);
1087 getMetadataPtr()->addContentItem(contentType, *(item->getMetadataPtr()));
1088
1089 Py_INCREF(Py_None);
1090 return Py_None;
1091 }
1092
1093 PyObject* MetadataPy::removeContentItem(PyObject* arg)
1094 {
1095 char* contentType = nullptr;
1096 char* contentName = nullptr;
1097 if (!PyArg_ParseTuple(arg, "ss", &contentType, &contentName)) {
1098 return nullptr;
1099 }
1100 if (contentType && contentName) {
1101 getMetadataPtr()->removeContentItem(contentType, contentName);
1102 }
1103
1104 Py_INCREF(Py_None);
1105 return Py_None;
1106 }
1107
1108 PyObject* MetadataPy::write(PyObject* args)
1109 {
1110 char* filename = nullptr;
1111 if (!PyArg_ParseTuple(args, "s", &filename)) {
1112 return nullptr;
1113 }
1114 getMetadataPtr()->write(filename);
1115
1116 Py_INCREF(Py_None);
1117 return Py_None;
1118 }
1119
1120
1121 PyObject* MetadataPy::getCustomAttributes(const char* /*attr*/) const
1122 {
1123 return nullptr;
1124 }
1125
1126 int MetadataPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
1127 {
1128 return 0;
1129 }