1
/**************************************************************************
3
* Copyright (c) 2022 FreeCAD Project Association *
5
* This file is part of FreeCAD. *
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. *
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. *
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/>. *
21
**************************************************************************/
23
#include "PreCompiled.h"
26
#include <Base/FileInfo.h>
28
// inclusion of the generated files (generated out of MetadataPy.xml)
29
#include "MetadataPy.h"
30
#include "MetadataPy.cpp"
33
#ifndef XERCES_CPP_NAMESPACE_BEGIN
34
#define XERCES_CPP_NAMESPACE_QUALIFIER
35
using namespace XERCES_CPP_NAMESPACE;
37
XERCES_CPP_NAMESPACE_USE
40
// Returns a string which represents the object e.g. when printed in Python
41
std::string MetadataPy::representation() const
43
MetadataPy::PointerType ptr = getMetadataPtr();
44
std::stringstream str;
45
str << "Metadata [Name=(";
47
str << "), Description=(";
48
str << ptr->description();
49
if (!ptr->maintainer().empty()) {
50
str << "), Maintainer=(";
51
str << ptr->maintainer().front().name;
58
PyObject *MetadataPy::PyMake(struct _typeobject *, PyObject *, PyObject *)// Python wrapper
60
return new MetadataPy(nullptr);
64
int MetadataPy::PyInit(PyObject *args, PyObject * /*kwd*/)
66
if (PyArg_ParseTuple(args, "")) {
67
setTwinPointer(new Metadata());
71
// Data may be passed directly in as a bytes-like object buffer
74
if (PyArg_ParseTuple(args, "y*", &dataBuffer)) {
76
// NB: This is making a copy of the buffer for simplicity, but that shouldn't be
77
// necessary. Use either a string_view or a span to avoid the copy in the future.
78
auto md = new Metadata(
79
std::string(static_cast<const char*>(dataBuffer.buf), dataBuffer.len)
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.
90
// Main class constructor -- takes a file path, loads the metadata from it
93
if (PyArg_ParseTuple(args, "et", "utf-8", &filename)) {
95
std::string utf8Name = std::string(filename);
98
auto md = new Metadata(Base::FileInfo::stringToPath(utf8Name));
102
catch (const Base::XMLBaseException &e) {
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());
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());
121
PyErr_SetString(Base::PyExc_FC_GeneralError, "Failed to create Metadata object");
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));
135
PyErr_SetString(Base::PyExc_FC_GeneralError, "metadata object or path to metadata file expected");
139
Py::Object MetadataPy::getName() const
141
return Py::String(getMetadataPtr()->name());
144
void MetadataPy::setName(Py::Object args)
146
const char *name = nullptr;
147
if (!PyArg_Parse(args.ptr(), "z", &name)) {
148
throw Py::Exception();
151
getMetadataPtr()->setName(name);
153
getMetadataPtr()->setName("");
156
Py::Object MetadataPy::getVersion() const
158
return Py::String(getMetadataPtr()->version().str());
161
void MetadataPy::setVersion(Py::Object args)
163
const char *name = nullptr;
164
if (!PyArg_Parse(args.ptr(), "z", &name))
165
throw Py::Exception();
166
if (name && name[0] != '\0')
167
getMetadataPtr()->setVersion(App::Meta::Version(std::string(name)));
169
getMetadataPtr()->setVersion(App::Meta::Version());
172
Py::Object MetadataPy::getDate() const
174
return Py::String(getMetadataPtr()->date());
177
void MetadataPy::setDate(Py::Object args)
179
const char *date = nullptr;
180
if (!PyArg_Parse(args.ptr(), "z", &date))
181
throw Py::Exception();
182
if (date) getMetadataPtr()->setDate(date);
184
getMetadataPtr()->setDate("");
187
Py::Object MetadataPy::getDescription() const
189
return Py::String(getMetadataPtr()->description());
192
void MetadataPy::setDescription(Py::Object args)
194
const char *description = nullptr;
195
if (!PyArg_Parse(args.ptr(), "s", &description))
196
throw Py::Exception();
197
getMetadataPtr()->setDescription(description);
200
Py::Object MetadataPy::getType() const
202
return Py::String(getMetadataPtr()->type());
205
void MetadataPy::setType(Py::Object args)
207
const char *type = nullptr;
208
if (!PyArg_Parse(args.ptr(), "s", &type))
209
throw Py::Exception();
210
getMetadataPtr()->setType(type);
213
Py::Object MetadataPy::getMaintainer() const
215
auto maintainers = getMetadataPtr()->maintainer();
216
Py::List pyMaintainers;
217
for (const auto &m : maintainers) {
218
Py::Dict pyMaintainer;
219
pyMaintainer["name"] = Py::String(m.name);
220
pyMaintainer["email"] = Py::String(m.email);
221
pyMaintainers.append(pyMaintainer);
223
return pyMaintainers;
226
void MetadataPy::setMaintainer(Py::Object args)
228
PyObject *list = nullptr;
229
if (!PyArg_Parse(args.ptr(), "O!", &PyList_Type, &list))
230
throw Py::Exception();
232
getMetadataPtr()->clearMaintainer();
233
Py::List maintainers(list);
234
for (const auto &m : maintainers) {
235
Py::Dict pyMaintainer(m);
236
std::string name = pyMaintainer["name"].str();
237
std::string email = pyMaintainer["email"].str();
238
getMetadataPtr()->addMaintainer(App::Meta::Contact(name, email));
242
PyObject *MetadataPy::addMaintainer(PyObject *args)
244
const char *name = nullptr;
245
const char *email = nullptr;
246
if (!PyArg_ParseTuple(args, "ss", &name, &email))
247
throw Py::Exception();
248
getMetadataPtr()->addMaintainer(App::Meta::Contact(name, email));
253
PyObject *MetadataPy::removeMaintainer(PyObject *args)
255
const char *name = nullptr;
256
const char *email = nullptr;
257
if (!PyArg_ParseTuple(args, "ss", &name, &email))
258
throw Py::Exception();
259
getMetadataPtr()->removeMaintainer(App::Meta::Contact(name, email));
265
Py::Object MetadataPy::getAuthor() const
267
auto authors = getMetadataPtr()->author();
269
for (const auto &a : authors) {
271
pyAuthor["name"] = Py::String(a.name);
272
pyAuthor["email"] = Py::String(a.email);
273
pyAuthors.append(pyAuthor);
278
void MetadataPy::setAuthor(Py::Object args)
280
PyObject *list = nullptr;
281
if (!PyArg_Parse(args.ptr(), "O!", &PyList_Type, &list))
282
throw Py::Exception();
284
getMetadataPtr()->clearAuthor();
285
Py::List authors(list);
286
for (const auto &a : authors) {
287
Py::Dict pyAuthor(a);
288
std::string name = pyAuthor["name"].str();
289
std::string email = pyAuthor["email"].str();
290
getMetadataPtr()->addAuthor(App::Meta::Contact(name, email));
294
PyObject *MetadataPy::addAuthor(PyObject *args)
296
const char *name = nullptr;
297
const char *email = nullptr;
298
if (!PyArg_ParseTuple(args, "ss", &name, &email))
299
throw Py::Exception();
300
getMetadataPtr()->addAuthor(App::Meta::Contact(name, email));
305
PyObject *MetadataPy::removeAuthor(PyObject *args)
307
const char *name = nullptr;
308
const char *email = nullptr;
309
if (!PyArg_ParseTuple(args, "ss", &name, &email))
310
throw Py::Exception();
311
getMetadataPtr()->removeAuthor(App::Meta::Contact(name, email));
316
Py::Object MetadataPy::getLicense() const
318
auto licenses = getMetadataPtr()->license();
320
for (const auto &lic : licenses) {
322
pyLicense["name"] = Py::String(lic.name);
323
pyLicense["file"] = Py::String(lic.file.string());
324
pyLicenses.append(pyLicense);
329
void MetadataPy::setLicense(Py::Object args)
331
PyObject *list = nullptr;
332
if (!PyArg_Parse(args.ptr(), "O!", &PyList_Type, &list))
333
throw Py::Exception();
335
getMetadataPtr()->clearLicense();
336
Py::List licenses(list);
337
for (const auto &l : licenses) {
338
Py::Dict pyLicense(l);
339
std::string name = pyLicense["name"].str();
340
std::string path = pyLicense["file"].str();
341
getMetadataPtr()->addLicense(App::Meta::License(name, path));
345
PyObject *MetadataPy::addLicense(PyObject *args)
347
const char *shortCode = nullptr;
348
const char *path = nullptr;
349
if (!PyArg_ParseTuple(args, "ss", &shortCode, &path))
350
throw Py::Exception();
351
getMetadataPtr()->addLicense(App::Meta::License(shortCode, path));
356
PyObject *MetadataPy::removeLicense(PyObject *args)
358
const char *shortCode = nullptr;
359
const char *path = nullptr;
360
if (!PyArg_ParseTuple(args, "ss", &shortCode, &path))
361
throw Py::Exception();
362
getMetadataPtr()->removeLicense(App::Meta::License(shortCode, path));
367
Py::Object MetadataPy::getUrls() const
369
auto urls = getMetadataPtr()->url();
371
for (const auto &url : urls) {
373
pyUrl["location"] = Py::String(url.location);
375
case Meta::UrlType::website: pyUrl["type"] = Py::String("website"); break;
376
case Meta::UrlType::repository: pyUrl["type"] = Py::String("repository"); break;
377
case Meta::UrlType::bugtracker: pyUrl["type"] = Py::String("bugtracker"); break;
378
case Meta::UrlType::readme: pyUrl["type"] = Py::String("readme"); break;
379
case Meta::UrlType::documentation: pyUrl["type"] = Py::String("documentation"); break;
380
case Meta::UrlType::discussion: pyUrl["type"] = Py::String("discussion"); break;
381
default: pyUrl["type"] = Py::String("unknown"); break;
383
if (url.type == Meta::UrlType::repository)
384
pyUrl["branch"] = Py::String(url.branch);
385
pyUrls.append(pyUrl);
390
void MetadataPy::setUrls(Py::Object args)
392
PyObject *list = nullptr;
393
if (!PyArg_Parse(args.ptr(), "O!", &PyList_Type, &list))
394
throw Py::Exception();
396
getMetadataPtr()->clearUrl();
398
for (const auto &url : urls) {
400
std::string location = pyUrl["location"].str();
401
std::string typeAsString = pyUrl["type"].str();
402
std::string branch = pyUrl["branch"].str();
403
auto newUrl = App::Meta::Url(location, Meta::UrlType::website);
404
if (typeAsString == "website") {
405
newUrl.type = Meta::UrlType::website;
407
else if (typeAsString == "repository") {
408
newUrl.type = Meta::UrlType::repository;
409
newUrl.branch = branch;
411
else if (typeAsString == "bugtracker") {
412
newUrl.type = Meta::UrlType::bugtracker;
414
else if (typeAsString == "readme") {
415
newUrl.type = Meta::UrlType::readme;
417
else if (typeAsString == "documentation") {
418
newUrl.type = Meta::UrlType::documentation;
420
else if (typeAsString == "discussion") {
421
newUrl.type = Meta::UrlType::discussion;
424
PyErr_SetString(Base::PyExc_FC_GeneralError, "Unrecognized URL type");
427
getMetadataPtr()->addUrl(newUrl);
431
App::Meta::Url urlFromStrings(const char* urlTypeCharStar, const char* link, const char* branch)
433
std::string urlTypeString(urlTypeCharStar);
434
App::Meta::UrlType urlType{App::Meta::UrlType::documentation};
435
if (urlTypeString == "repository")
436
urlType = App::Meta::UrlType::repository;
437
else if (urlTypeString == "bugtracker")
438
urlType = App::Meta::UrlType::bugtracker;
439
else if (urlTypeString == "documentation")
440
urlType = App::Meta::UrlType::documentation;
441
else if (urlTypeString == "readme")
442
urlType = App::Meta::UrlType::readme;
443
else if (urlTypeString == "website")
444
urlType = App::Meta::UrlType::website;
446
App::Meta::Url url(link, urlType);
447
if (urlType == App::Meta::UrlType::repository)
448
url.branch = std::string(branch);
453
PyObject *MetadataPy::addUrl(PyObject *args)
455
const char *urlTypeCharStar = nullptr;
456
const char *link = nullptr;
457
const char *branch = nullptr;
458
if (!PyArg_ParseTuple(args, "ss|s", &urlTypeCharStar, &link, &branch))
459
throw Py::Exception();
461
getMetadataPtr()->addUrl(urlFromStrings(urlTypeCharStar, link, branch));
466
PyObject *MetadataPy::removeUrl(PyObject *args)
468
const char *urlTypeCharStar = nullptr;
469
const char *link = nullptr;
470
const char *branch = nullptr;
471
if (!PyArg_ParseTuple(args, "ss|s", &urlTypeCharStar, &link, &branch))
472
throw Py::Exception();
474
getMetadataPtr()->removeUrl(urlFromStrings(urlTypeCharStar, link, branch));
479
Py::Object dependencyToPyObject(const Meta::Dependency &d)
481
Py::Dict pyDependency;
482
pyDependency["package"] = Py::String(d.package);
483
pyDependency["version_lt"] = Py::String(d.version_lt);
484
pyDependency["version_lte"] = Py::String(d.version_lte);
485
pyDependency["version_eq"] = Py::String(d.version_eq);
486
pyDependency["version_gt"] = Py::String(d.version_gt);
487
pyDependency["version_gte"] = Py::String(d.version_gte);
488
pyDependency["condition"] = Py::String(d.condition);
489
pyDependency["optional"] = Py::Boolean(d.optional);
490
switch (d.dependencyType) {
491
case App::Meta::DependencyType::automatic:
492
pyDependency["type"] = Py::String ("automatic");
494
case App::Meta::DependencyType::addon:
495
pyDependency["type"] = Py::String("addon");
497
case App::Meta::DependencyType::internal:
498
pyDependency["type"] = Py::String("internal");
500
case App::Meta::DependencyType::python:
501
pyDependency["type"] = Py::String("python");
507
Meta::Dependency pyObjectToDependency(const Py::Object &d)
509
Py::Dict pyDependency(d);
510
Meta::Dependency result;
511
result.package = pyDependency["package"].str();
512
if (pyDependency.hasKey("version_lt"))
513
result.version_lt = pyDependency["version_lt"].str();
514
if (pyDependency.hasKey("version_lte"))
515
result.version_lte = pyDependency["version_lte"].str();
516
if (pyDependency.hasKey("version_eq"))
517
result.version_eq = pyDependency["version_eq"].str();
518
if (pyDependency.hasKey("version_gt"))
519
result.version_gt = pyDependency["version_gt"].str();
520
if (pyDependency.hasKey("version_gte"))
521
result.version_gte = pyDependency["version_gte"].str();
522
if (pyDependency.hasKey("condition"))
523
result.condition = pyDependency["condition"].str();
524
if (pyDependency.hasKey("optional"))
525
result.optional = Py::Boolean(pyDependency["optional"]).as_bool();
526
if (pyDependency.hasKey("type")) {
527
if (pyDependency["type"].str() == Py::String("automatic"))
528
result.dependencyType = App::Meta::DependencyType::automatic;
529
else if (pyDependency["type"].str() == Py::String("internal"))
530
result.dependencyType = App::Meta::DependencyType::internal;
531
else if (pyDependency["type"].str() == Py::String("addon"))
532
result.dependencyType = App::Meta::DependencyType::addon;
533
else if (pyDependency["type"].str() == Py::String("python"))
534
result.dependencyType = App::Meta::DependencyType::python;
539
Py::Object MetadataPy::getDepend() const
541
auto dependencies = getMetadataPtr()->depend();
542
Py::List pyDependencies;
543
for (const auto &d : dependencies) {
544
pyDependencies.append(dependencyToPyObject(d));
546
return pyDependencies;
549
void MetadataPy::setDepend(Py::Object args)
551
PyObject *list = nullptr;
552
if (!PyArg_Parse(args.ptr(), "O!", &PyList_Type, &list))
553
throw Py::Exception();
555
getMetadataPtr()->clearDepend();
557
for (const auto &dep : deps) {
559
getMetadataPtr()->addDepend(pyObjectToDependency(pyDep));
563
PyObject *MetadataPy::addDepend(PyObject *args)
565
PyObject *dictionary = nullptr;
566
if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dictionary))
567
throw Py::Exception();
568
Py::Dict pyDep(dictionary);
569
getMetadataPtr()->addDepend(pyObjectToDependency(pyDep));
574
PyObject *MetadataPy::removeDepend(PyObject *args)
576
PyObject *dictionary = nullptr;
577
if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dictionary))
578
throw Py::Exception();
579
Py::Dict pyDep(dictionary);
580
getMetadataPtr()->removeDepend(pyObjectToDependency(pyDep));
585
Py::Object MetadataPy::getConflict() const
587
auto dependencies = getMetadataPtr()->conflict();
588
Py::List pyDependencies;
589
for (const auto &d : dependencies) {
590
pyDependencies.append(dependencyToPyObject(d));
592
return pyDependencies;
595
void MetadataPy::setConflict(Py::Object args)
597
PyObject *list = nullptr;
598
if (!PyArg_Parse(args.ptr(), "O!", &PyList_Type, &list))
599
throw Py::Exception();
601
getMetadataPtr()->clearConflict();
603
for (const auto &dep : deps) {
605
getMetadataPtr()->addConflict(pyObjectToDependency(pyDep));
609
PyObject *MetadataPy::addConflict(PyObject *args)
611
PyObject *dictionary = nullptr;
612
if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dictionary))
613
throw Py::Exception();
614
Py::Dict pyDep(dictionary);
615
getMetadataPtr()->addConflict(pyObjectToDependency(pyDep));
620
PyObject *MetadataPy::removeConflict(PyObject *args)
622
PyObject *dictionary = nullptr;
623
if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dictionary))
624
throw Py::Exception();
625
Py::Dict pyDep(dictionary);
626
getMetadataPtr()->removeConflict(pyObjectToDependency(pyDep));
631
Py::Object MetadataPy::getReplace() const
633
auto dependencies = getMetadataPtr()->replace();
634
Py::List pyDependencies;
635
for (const auto &d : dependencies) {
636
pyDependencies.append(dependencyToPyObject(d));
638
return pyDependencies;
641
void MetadataPy::setReplace(Py::Object args)
643
PyObject *list = nullptr;
644
if (!PyArg_Parse(args.ptr(), "O!", &PyList_Type, &list))
645
throw Py::Exception();
647
getMetadataPtr()->clearReplace();
649
for (const auto &dep : deps) {
651
getMetadataPtr()->addReplace(pyObjectToDependency(pyDep));
655
PyObject *MetadataPy::addReplace(PyObject *args)
657
PyObject *dictionary = nullptr;
658
if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dictionary))
659
throw Py::Exception();
660
Py::Dict pyDep(dictionary);
661
getMetadataPtr()->addReplace(pyObjectToDependency(pyDep));
666
PyObject *MetadataPy::removeReplace(PyObject *args)
668
PyObject *dictionary = nullptr;
669
if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dictionary))
670
throw Py::Exception();
671
Py::Dict pyDep(dictionary);
672
getMetadataPtr()->removeReplace(pyObjectToDependency(pyDep));
677
// Tag, icon, classname, file
679
Py::Object MetadataPy::getTag() const
681
auto tags = getMetadataPtr()->tag();
683
for (const auto &t : tags) {
684
pyTags.append(Py::String(t));
689
void MetadataPy::setTag(Py::Object args)
691
PyObject *list = nullptr;
692
if (!PyArg_Parse(args.ptr(), "O!", &PyList_Type, &list))
693
throw Py::Exception();
695
getMetadataPtr()->clearTag();
697
for (const auto &tag : tags) {
698
Py::String pyTag(tag);
699
getMetadataPtr()->addTag(pyTag.as_std_string());
703
PyObject *MetadataPy::addTag(PyObject *args)
705
const char *tag = nullptr;
706
if (!PyArg_ParseTuple(args, "s", &tag))
707
throw Py::Exception();
708
getMetadataPtr()->addTag(tag);
713
PyObject *MetadataPy::removeTag(PyObject *args)
715
const char *tag = nullptr;
716
if (!PyArg_ParseTuple(args, "s", &tag))
717
throw Py::Exception();
718
getMetadataPtr()->removeTag(tag);
723
Py::Object MetadataPy::getIcon() const
725
return Py::String(getMetadataPtr()->icon().string());
728
void MetadataPy::setIcon(Py::Object args)
731
if (!PyArg_Parse(args.ptr(), "s", &name))
732
throw Py::Exception();
733
getMetadataPtr()->setIcon(name);
736
Py::Object MetadataPy::getClassname() const
738
return Py::String(getMetadataPtr()->classname());
741
void MetadataPy::setClassname(Py::Object args)
744
if (!PyArg_Parse(args.ptr(), "s", &name))
745
throw Py::Exception();
746
getMetadataPtr()->setClassname(name);
749
Py::Object MetadataPy::getSubdirectory() const
751
return Py::String(getMetadataPtr()->subdirectory().string());
754
void MetadataPy::setSubdirectory(Py::Object args)
757
if (!PyArg_Parse(args.ptr(), "s", &name))
758
throw Py::Exception();
759
getMetadataPtr()->setSubdirectory(name);
762
Py::Object MetadataPy::getFile() const
764
auto files = getMetadataPtr()->file();
766
for (const auto &f : files) {
767
pyFiles.append(Py::String(f.string()));
772
void MetadataPy::setFile(Py::Object args)
774
PyObject *list = nullptr;
775
if (!PyArg_Parse(args.ptr(), "O!", &PyList_Type, &list))
776
throw Py::Exception();
778
getMetadataPtr()->clearTag();
779
Py::List files(list);
780
for (const auto &file : files) {
781
Py::String pyFile(file);
782
getMetadataPtr()->addFile(pyFile.as_std_string());
786
PyObject *MetadataPy::addFile(PyObject *args)
788
const char *file = nullptr;
789
if (!PyArg_ParseTuple(args, "s", &file))
790
throw Py::Exception();
791
getMetadataPtr()->addFile(file);
796
PyObject *MetadataPy::removeFile(PyObject *args)
798
const char *file = nullptr;
799
if (!PyArg_ParseTuple(args, "s", &file))
800
throw Py::Exception();
801
getMetadataPtr()->removeFile(file);
807
Py::Object MetadataPy::getContent() const
809
auto content = getMetadataPtr()->content();
810
std::set<std::string> keys;
811
for (const auto &item : content) {
812
keys.insert(item.first);
815
// For the Python, we'll use a dictionary of lists to store the content components:
817
for (const auto &key : keys) {
818
Py::List pyContentForKey;
819
auto elements = content.equal_range(key);
820
for (auto &element = elements.first; element != elements.second; ++element) {
821
auto contentMetadataItem = new MetadataPy(new Metadata(element->second));
822
pyContentForKey.append(Py::asObject(contentMetadataItem));
824
pyContent[key] = pyContentForKey;
829
void MetadataPy::setContent(Py::Object arg)
831
PyObject *obj = nullptr;
832
if (!PyArg_Parse(arg.ptr(), "O!", &PyList_Type, &obj))
833
throw Py::Exception();
835
getMetadataPtr()->clearContent();
836
Py::Dict outerDict(obj);
837
for (const auto &pyContentType : outerDict) {
838
auto contentType = Py::String(pyContentType.first).as_std_string();
839
auto contentList = Py::List(pyContentType.second);
840
for (const auto& contentItem : contentList) {
841
auto item = static_cast<MetadataPy *>(contentItem.ptr());
842
getMetadataPtr()->addContentItem(contentType, *(item->getMetadataPtr()));
848
PyObject *MetadataPy::getGenericMetadata(PyObject *args)
851
if (!PyArg_ParseTuple(args, "s", &name))
854
auto gm = (*getMetadataPtr())[name];
855
Py::List pyGenericMetadata;
856
for (const auto &item : gm) {
858
pyItem["contents"] = Py::String(item.contents);
859
Py::Dict pyAttributes;
860
for (const auto &attribute : item.attributes) {
861
pyAttributes[attribute.first] = Py::String(attribute.second);
863
pyItem["attributes"] = pyAttributes;
864
pyGenericMetadata.append(pyItem);
866
return Py::new_reference_to(pyGenericMetadata);
869
Py::Object MetadataPy::getFreeCADMin() const
871
return Py::String(getMetadataPtr()->freecadmin().str());
874
void MetadataPy::setFreeCADMin(Py::Object args)
876
char *version = nullptr;
877
PyObject *p = args.ptr();
878
if (!PyArg_Parse(p, "z", &version))
879
throw Py::Exception();
881
getMetadataPtr()->setFreeCADMin(App::Meta::Version(version));
883
getMetadataPtr()->setFreeCADMin(App::Meta::Version());
886
Py::Object MetadataPy::getFreeCADMax() const
888
return Py::String(getMetadataPtr()->freecadmax().str());
891
void MetadataPy::setFreeCADMax(Py::Object args)
893
char *version = nullptr;
894
PyObject *p = args.ptr();
895
if (!PyArg_Parse(p, "z", &version))
896
throw Py::Exception();
899
getMetadataPtr()->setFreeCADMax(App::Meta::Version(version));
901
getMetadataPtr()->setFreeCADMax(App::Meta::Version());
904
Py::Object MetadataPy::getPythonMin() const
906
return Py::String(getMetadataPtr()->pythonmin().str());
909
void MetadataPy::setPythonMin(Py::Object args)
911
char *version = nullptr;
912
PyObject *p = args.ptr();
913
if (!PyArg_Parse(p, "z", &version)) throw Py::Exception();
914
if (version) getMetadataPtr()->setPythonMin(App::Meta::Version(version));
916
getMetadataPtr()->setPythonMin(App::Meta::Version());
919
PyObject *MetadataPy::getFirstSupportedFreeCADVersion(PyObject *p)
921
if (!PyArg_ParseTuple(p, ""))
924
// Short-circuit: if the toplevel sets a version, then the lower-levels are overridden
925
if (getMetadataPtr()->freecadmin() != App::Meta::Version())
926
return Py::new_reference_to(Py::String(getMetadataPtr()->freecadmin().str()));
928
auto content = getMetadataPtr()->content();
929
auto result = App::Meta::Version();
930
for (const auto &item : content) {
931
auto minVersion = item.second.freecadmin();
932
if (minVersion != App::Meta::Version())
933
if (result == App::Meta::Version() || minVersion < result)
936
if (result != App::Meta::Version()) {
937
return Py::new_reference_to(Py::String(result.str()));
945
PyObject *MetadataPy::getLastSupportedFreeCADVersion(PyObject *p)
947
if (!PyArg_ParseTuple(p, ""))
950
// Short-circuit: if the toplevel sets a version, then the lower-levels are overridden
951
if (getMetadataPtr()->freecadmax() != App::Meta::Version())
952
return Py::new_reference_to(Py::String(getMetadataPtr()->freecadmax().str()));
954
auto content = getMetadataPtr()->content();
955
auto result = App::Meta::Version();
956
for (const auto &item : content) {
957
auto maxVersion = item.second.freecadmax();
958
if (maxVersion != App::Meta::Version())
959
if (result == App::Meta::Version() || maxVersion > result)
962
if (result != App::Meta::Version()) {
963
return Py::new_reference_to(Py::String(result.str()));
971
PyObject *MetadataPy::supportsCurrentFreeCAD(PyObject *p)
973
if (!PyArg_ParseTuple(p, ""))
976
bool result = getMetadataPtr()->supportsCurrentFreeCAD();
977
return Py::new_reference_to(Py::Boolean(result));
980
PyObject* MetadataPy::addContentItem(PyObject* arg)
982
char *contentType = nullptr;
983
PyObject *contentItem = nullptr;
984
if (!PyArg_ParseTuple(arg, "sO!", &contentType, &(App::MetadataPy::Type), &contentItem))
987
if (!contentItem || !contentType)
989
auto item = static_cast<MetadataPy *>(contentItem);
990
getMetadataPtr()->addContentItem(contentType, *(item->getMetadataPtr()));
996
PyObject *MetadataPy::removeContentItem(PyObject *arg)
998
char *contentType = nullptr;
999
char *contentName = nullptr;
1000
if (!PyArg_ParseTuple(arg, "ss", &contentType, &contentName))
1002
if (contentType && contentName)
1003
getMetadataPtr()->removeContentItem(contentType, contentName);
1009
PyObject* MetadataPy::write(PyObject* args)
1011
char *filename = nullptr;
1012
if (!PyArg_ParseTuple(args, "s", &filename))
1014
getMetadataPtr()->write(filename);
1021
PyObject *MetadataPy::getCustomAttributes(const char * /*attr*/) const
1026
int MetadataPy::setCustomAttributes(const char * /*attr*/, PyObject * /*obj*/)