App: Clean up code in PropertyContainerPyImp.cpp

This commit is contained in:
marioalexis
2022-06-07 18:04:22 -03:00
committed by Chris Hennes
parent 53e02bc851
commit 10edc03ec1

View File

@@ -54,16 +54,24 @@ PyObject* PropertyContainerPy::getPropertyByName(PyObject *args)
int checkOwner=0;
if (!PyArg_ParseTuple(args, "s|i", &pstr, &checkOwner))
return nullptr;
App::Property* prop = getPropertyContainerPtr()->getPropertyByName(pstr);
if (prop) {
if(!checkOwner || (checkOwner==1 && prop->getContainer()==getPropertyContainerPtr()))
return prop->getPyObject();
Py::TupleN res(Py::asObject(prop->getContainer()->getPyObject()),
Py::asObject(prop->getPyObject()));
return Py::new_reference_to(res);
if (checkOwner < 0 || checkOwner > 2) {
PyErr_SetString(PyExc_ValueError, "'checkOwner' expected in the range [0, 2]");
return nullptr;
}
PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", pstr);
return nullptr;
App::Property* prop = getPropertyContainerPtr()->getPropertyByName(pstr);
if (!prop) {
PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", pstr);
return nullptr;
}
if (!checkOwner || (checkOwner==1 && prop->getContainer()==getPropertyContainerPtr()))
return prop->getPyObject();
Py::TupleN res(Py::asObject(prop->getContainer()->getPyObject()), Py::asObject(prop->getPyObject()));
return Py::new_reference_to(res);
}
PyObject* PropertyContainerPy::getPropertyTouchList(PyObject *args)
@@ -71,6 +79,7 @@ PyObject* PropertyContainerPy::getPropertyTouchList(PyObject *args)
char *pstr;
if (!PyArg_ParseTuple(args, "s", &pstr))
return nullptr;
App::Property* prop = getPropertyContainerPtr()->getPropertyByName(pstr);
if (prop && prop->isDerivedFrom(PropertyLists::getClassTypeId())) {
const auto &touched = static_cast<PropertyLists*>(prop)->getTouchList();
@@ -80,11 +89,14 @@ PyObject* PropertyContainerPy::getPropertyTouchList(PyObject *args)
ret.setItem(i++,Py::Long(idx));
return Py::new_reference_to(ret);
}
else if(!prop)
else if (!prop) {
PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", pstr);
else
return nullptr;
}
else {
PyErr_Format(PyExc_AttributeError, "Property '%s' is not of list type", pstr);
return nullptr;
return nullptr;
}
}
PyObject* PropertyContainerPy::getTypeOfProperty(PyObject *args)
@@ -168,7 +180,7 @@ PyObject* PropertyContainerPy::setEditorMode(PyObject *args)
status.reset(Property::ReadOnly);
status.reset(Property::Hidden);
for (Py::Sequence::iterator it = seq.begin();it!=seq.end();++it) {
std::string str = (std::string)Py::String(*it);
std::string str = static_cast<std::string>(Py::String(*it));
if (str == "ReadOnly")
status.set(Property::ReadOnly);
else if (str == "Hidden")
@@ -210,28 +222,30 @@ PyObject* PropertyContainerPy::setPropertyStatus(PyObject *args)
PyObject *pyValue;
if (!PyArg_ParseTuple(args, "sO", &name, &pyValue))
return nullptr;
App::Property* prop = getPropertyContainerPtr()->getPropertyByName(name);
if (!prop) {
PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", name);
return nullptr;
}
auto linkProp = Base::freecad_dynamic_cast<App::PropertyLinkBase>(prop);
auto linkProp = Base::freecad_dynamic_cast<App::PropertyLinkBase>(prop);
std::bitset<32> status(prop->getStatus());
size_t count = 1;
bool isSeq = false;
if(PyList_Check(pyValue) || PyTuple_Check(pyValue)) {
if (PyList_Check(pyValue) || PyTuple_Check(pyValue)) {
isSeq = true;
count = PySequence_Size(pyValue);
}
for(size_t i=0;i<count;++i) {
for(size_t i=0; i<count; ++i) {
Py::Object item;
if(isSeq)
if (isSeq)
item = Py::Object(PySequence_GetItem(pyValue,i));
else
item = Py::Object(pyValue);
bool value = true;
if(item.isString()) {
if (item.isString()) {
const auto &statusMap = getStatusMap();
auto v = (std::string)Py::String(item);
if(v.size()>1 && v[0] == '-') {
@@ -248,7 +262,8 @@ PyObject* PropertyContainerPy::setPropertyStatus(PyObject *args)
return nullptr;
}
status.set(it->second,value);
}else if(item.isNumeric()) {
}
else if (item.isNumeric()) {
int v = Py::Int(item);
if(v<0) {
value = false;
@@ -257,7 +272,8 @@ PyObject* PropertyContainerPy::setPropertyStatus(PyObject *args)
if(v==0 || v>31)
PyErr_Format(PyExc_ValueError, "Status value out of range '%d'", v);
status.set(v,value);
} else {
}
else {
PyErr_SetString(PyExc_TypeError, "Expects status type to be Int or String");
return nullptr;
}
@@ -275,31 +291,34 @@ PyObject* PropertyContainerPy::getPropertyStatus(PyObject *args)
Py::List ret;
const auto &statusMap = getStatusMap();
if(!name[0]) {
if (!name[0]) {
for(auto &v : statusMap)
ret.append(Py::String(v.first.c_str()));
}else{
}
else {
App::Property* prop = getPropertyContainerPtr()->getPropertyByName(name);
if (prop) {
if (!prop) {
PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", name);
return nullptr;
}
auto linkProp = Base::freecad_dynamic_cast<App::PropertyLinkBase>(prop);
if(linkProp && linkProp->testFlag(App::PropertyLinkBase::LinkAllowPartial))
ret.append(Py::String("AllowPartial"));
auto linkProp = Base::freecad_dynamic_cast<App::PropertyLinkBase>(prop);
if (linkProp && linkProp->testFlag(App::PropertyLinkBase::LinkAllowPartial))
ret.append(Py::String("AllowPartial"));
std::bitset<32> bits(prop->getStatus());
for(size_t i=1;i<bits.size();++i) {
if(!bits[i]) continue;
bool found = false;
for(auto &v : statusMap) {
if(v.second == (int)i) {
ret.append(Py::String(v.first.c_str()));
found = true;
break;
}
std::bitset<32> bits(prop->getStatus());
for(size_t i=1; i<bits.size(); ++i) {
if(!bits[i]) continue;
bool found = false;
for(auto &v : statusMap) {
if(v.second == static_cast<int>(i)) {
ret.append(Py::String(v.first.c_str()));
found = true;
break;
}
if(!found)
ret.append(Py::Int((long)i));
}
if (!found)
ret.append(Py::Int(static_cast<long>(i)));
}
}
return Py::new_reference_to(ret);
@@ -312,6 +331,11 @@ PyObject* PropertyContainerPy::getEditorMode(PyObject *args)
return nullptr;
App::Property* prop = getPropertyContainerPtr()->getPropertyByName(name);
if (!prop) {
PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", name);
return nullptr;
}
Py::List ret;
if (prop) {
short Type = prop->getType();
@@ -357,7 +381,8 @@ PyObject* PropertyContainerPy::setGroupOfProperty(PyObject *args)
}
prop->getContainer()->changeDynamicProperty(prop,group,nullptr);
Py_Return;
} PY_CATCH
}
PY_CATCH
}
@@ -373,9 +398,9 @@ PyObject* PropertyContainerPy::getDocumentationOfProperty(PyObject *args)
return nullptr;
}
const char* Group = getPropertyContainerPtr()->getPropertyDocumentation(prop);
if (Group)
return Py::new_reference_to(Py::String(Group));
const char* docstr = getPropertyContainerPtr()->getPropertyDocumentation(prop);
if (docstr)
return Py::new_reference_to(Py::String(docstr));
else
return Py::new_reference_to(Py::String(""));
}
@@ -395,7 +420,8 @@ PyObject* PropertyContainerPy::setDocumentationOfProperty(PyObject *args)
}
prop->getContainer()->changeDynamicProperty(prop,nullptr,doc);
Py_Return;
} PY_CATCH
}
PY_CATCH
}
PyObject* PropertyContainerPy::getEnumerationsOfProperty(PyObject *args)
@@ -411,13 +437,10 @@ PyObject* PropertyContainerPy::getEnumerationsOfProperty(PyObject *args)
}
PropertyEnumeration *enumProp = dynamic_cast<PropertyEnumeration*>(prop);
if (!enumProp) {
Py_INCREF(Py_None);
return Py_None;
}
if (!enumProp)
Py_Return;
std::vector<std::string> enumerations = enumProp->getEnumVector();
Py::List ret;
for (std::vector<std::string>::const_iterator it = enumerations.begin(); it != enumerations.end(); ++it) {
ret.append(Py::String(*it));
@@ -432,7 +455,7 @@ Py::List PropertyContainerPy::getPropertiesList(void) const
getPropertyContainerPtr()->getPropertyMap(Map);
for (std::map<std::string,Property*>::const_iterator It=Map.begin();It!=Map.end();++It)
for (std::map<std::string,Property*>::const_iterator It=Map.begin(); It!=Map.end(); ++It)
ret.append(Py::String(It->first));
return ret;
@@ -442,12 +465,11 @@ Py::List PropertyContainerPy::getPropertiesList(void) const
PyObject* PropertyContainerPy::dumpPropertyContent(PyObject *args, PyObject *kwds)
{
int compression = 3;
char* property;
static char* kwds_def[] = {"Property", "Compression",nullptr};
const char* property;
static char* kwds_def[] = {"Property", "Compression", nullptr};
PyErr_Clear();
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|i", kwds_def, &property, &compression)) {
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|i", kwds_def, &property, &compression))
return nullptr;
}
Property* prop = getPropertyContainerPtr()->getPropertyByName(property);
if (!prop) {