App: Apply clang format (part 2)

This commit is contained in:
wmayer
2024-11-21 12:27:35 +01:00
committed by wwmayer
parent fcea9343b3
commit 5771c94523
44 changed files with 12163 additions and 8775 deletions

View File

@@ -39,7 +39,7 @@
using namespace App;
TYPESYSTEM_SOURCE(App::PropertyPythonObject , App::Property)
TYPESYSTEM_SOURCE(App::PropertyPythonObject, App::Property)
PropertyPythonObject::PropertyPythonObject() = default;
@@ -51,11 +51,11 @@ PropertyPythonObject::~PropertyPythonObject()
this->object = Py::Object();
}
void PropertyPythonObject::setValue(Py::Object o)
void PropertyPythonObject::setValue(const Py::Object& py)
{
Base::PyGILStateLocker lock;
aboutToSetValue();
this->object = o;
this->object = py;
hasSetValue();
}
@@ -64,12 +64,12 @@ Py::Object PropertyPythonObject::getValue() const
return object;
}
PyObject *PropertyPythonObject::getPyObject()
PyObject* PropertyPythonObject::getPyObject()
{
return Py::new_reference_to(this->object);
}
void PropertyPythonObject::setPyObject(PyObject * obj)
void PropertyPythonObject::setPyObject(PyObject* obj)
{
Base::PyGILStateLocker lock;
aboutToSetValue();
@@ -82,9 +82,10 @@ std::string PropertyPythonObject::toString() const
std::string repr;
Base::PyGILStateLocker lock;
try {
Py::Module pickle(PyImport_ImportModule("json"),true);
if (pickle.isNull())
Py::Module pickle(PyImport_ImportModule("json"), true);
if (pickle.isNull()) {
throw Py::Exception();
}
Py::Callable method(pickle.getAttr(std::string("dumps")));
Py::Object dump;
if (this->object.hasAttr("dumps")) {
@@ -95,10 +96,9 @@ std::string PropertyPythonObject::toString() const
// support add-ons that use the old method names
else if (this->object.hasAttr("__getstate__")
#if PY_VERSION_HEX >= 0x030b0000
&& this->object.getAttr("__getstate__").hasAttr("__func__")
&& this->object.getAttr("__getstate__").hasAttr("__func__")
#endif
)
{
) {
Py::Tuple args;
Py::Callable state(this->object.getAttr("__getstate__"));
dump = state.apply(args);
@@ -118,8 +118,9 @@ std::string PropertyPythonObject::toString() const
}
catch (Py::Exception&) {
Py::String typestr(this->object.type().str());
Base::Console().Error("PropertyPythonObject::toString(): failed for %s\n", typestr.as_string().c_str());
Base::PyException e; // extract the Python error text
Base::Console().Error("PropertyPythonObject::toString(): failed for %s\n",
typestr.as_string().c_str());
Base::PyException e; // extract the Python error text
e.ReportException();
}
@@ -130,11 +131,13 @@ void PropertyPythonObject::fromString(const std::string& repr)
{
Base::PyGILStateLocker lock;
try {
if (repr.empty())
if (repr.empty()) {
return;
Py::Module pickle(PyImport_ImportModule("json"),true);
if (pickle.isNull())
}
Py::Module pickle(PyImport_ImportModule("json"), true);
if (pickle.isNull()) {
throw Py::Exception();
}
Py::Callable method(pickle.getAttr(std::string("loads")));
Py::Tuple args(1);
args.setItem(0, Py::String(repr));
@@ -149,10 +152,9 @@ void PropertyPythonObject::fromString(const std::string& repr)
// support add-ons that use the old method names
else if (this->object.hasAttr("__setstate__")
#if PY_VERSION_HEX >= 0x030b0000
&& this->object.getAttr("__setstate__").hasAttr("__func__")
&& this->object.getAttr("__setstate__").hasAttr("__func__")
#endif
)
{
) {
Py::Tuple args(1);
args.setItem(0, res);
Py::Callable state(this->object.getAttr("__setstate__"));
@@ -168,7 +170,7 @@ void PropertyPythonObject::fromString(const std::string& repr)
}
}
catch (Py::Exception&) {
Base::PyException e; // extract the Python error text
Base::PyException e; // extract the Python error text
e.ReportException();
}
}
@@ -194,7 +196,7 @@ void PropertyPythonObject::loadPickle(const std::string& str)
}
}
catch (Py::Exception&) {
Base::PyException e; // extract the Python error text
Base::PyException e; // extract the Python error text
e.ReportException();
}
}
@@ -203,18 +205,24 @@ std::string PropertyPythonObject::encodeValue(const std::string& str) const
{
std::string tmp;
for (char it : str) {
if (it == '<')
if (it == '<') {
tmp += "&lt;";
else if (it == '"')
}
else if (it == '"') {
tmp += "&quot;";
else if (it == '&')
}
else if (it == '&') {
tmp += "&amp;";
else if (it == '>')
}
else if (it == '>') {
tmp += "&gt";
else if (it == '\n')
}
else if (it == '\n') {
tmp += "\\n";
else
}
else {
tmp += it;
}
}
return tmp;
@@ -230,14 +238,15 @@ std::string PropertyPythonObject::decodeValue(const std::string& str) const
tmp += '\n';
}
}
else
else {
tmp += *it;
}
}
return tmp;
}
void PropertyPythonObject::saveObject(Base::Writer &writer) const
void PropertyPythonObject::saveObject(Base::Writer& writer) const
{
Base::PyGILStateLocker lock;
try {
@@ -258,19 +267,19 @@ void PropertyPythonObject::saveObject(Base::Writer &writer) const
}
}
void PropertyPythonObject::restoreObject(Base::XMLReader &reader)
void PropertyPythonObject::restoreObject(Base::XMLReader& reader)
{
Base::PyGILStateLocker lock;
try {
PropertyContainer* parent = this->getContainer();
if (reader.hasAttribute("object")) {
if (strcmp(reader.getAttribute("object"),"yes") == 0) {
if (strcmp(reader.getAttribute("object"), "yes") == 0) {
Py::Object obj = Py::asObject(parent->getPyObject());
this->object.setAttr("__object__", obj);
}
}
if (reader.hasAttribute("vobject")) {
if (strcmp(reader.getAttribute("vobject"),"yes") == 0) {
if (strcmp(reader.getAttribute("vobject"), "yes") == 0) {
Py::Object obj = Py::asObject(parent->getPyObject());
this->object.setAttr("__vobject__", obj);
}
@@ -280,65 +289,57 @@ void PropertyPythonObject::restoreObject(Base::XMLReader &reader)
e.clear();
}
catch (const Base::Exception& e) {
Base::Console().Error("%s\n",e.what());
Base::Console().Error("%s\n", e.what());
}
catch (...) {
Base::Console().Error("Critical error in PropertyPythonObject::restoreObject\n");
}
}
void PropertyPythonObject::Save (Base::Writer &writer) const
void PropertyPythonObject::Save(Base::Writer& writer) const
{
//if (writer.isForceXML()) {
std::string repr = this->toString();
repr = Base::base64_encode((const unsigned char*)repr.c_str(), repr.size());
std::string val = /*encodeValue*/(repr);
writer.Stream() << writer.ind() << "<Python value=\"" << val
<< R"(" encoded="yes")";
std::string repr = this->toString();
repr = Base::base64_encode((const unsigned char*)repr.c_str(), repr.size());
std::string val = /*encodeValue*/ (repr);
writer.Stream() << writer.ind() << "<Python value=\"" << val << R"(" encoded="yes")";
Base::PyGILStateLocker lock;
try {
if (this->object.hasAttr("__module__") && this->object.hasAttr("__class__")) {
Py::String mod(this->object.getAttr("__module__"));
Py::Object cls(this->object.getAttr("__class__"));
if (cls.hasAttr("__name__")) {
Py::String name(cls.getAttr("__name__"));
writer.Stream() << " module=\"" << (std::string)mod << "\""
<< " class=\"" << (std::string)name << "\"";
}
}
else {
writer.Stream() << " json=\"yes\"";
Base::PyGILStateLocker lock;
try {
if (this->object.hasAttr("__module__") && this->object.hasAttr("__class__")) {
Py::String mod(this->object.getAttr("__module__"));
Py::Object cls(this->object.getAttr("__class__"));
if (cls.hasAttr("__name__")) {
Py::String name(cls.getAttr("__name__"));
writer.Stream() << " module=\"" << (std::string)mod << "\""
<< " class=\"" << (std::string)name << "\"";
}
}
catch (Py::Exception&) {
Base::PyException e; // extract the Python error text
e.ReportException();
else {
writer.Stream() << " json=\"yes\"";
}
}
catch (Py::Exception&) {
Base::PyException e; // extract the Python error text
e.ReportException();
}
saveObject(writer);
writer.Stream() << "/>" << std::endl;
//}
//else {
// writer.Stream() << writer.ind() << "<Python file=\"" <<
// writer.addFile("pickle", this) << "\"/>" << std::endl;
//}
saveObject(writer);
writer.Stream() << "/>" << std::endl;
}
void PropertyPythonObject::Restore(Base::XMLReader &reader)
void PropertyPythonObject::Restore(Base::XMLReader& reader)
{
reader.readElement("Python");
if (reader.hasAttribute("file")) {
std::string file(reader.getAttribute("file"));
reader.addFile(file.c_str(),this);
reader.addFile(file.c_str(), this);
}
else {
bool load_json=false;
bool load_pickle=false;
bool load_failed=false;
bool load_json = false;
bool load_pickle = false;
bool load_failed = false;
std::string buffer = reader.getAttribute("value");
if (reader.hasAttribute("encoded") &&
strcmp(reader.getAttribute("encoded"),"yes") == 0) {
if (reader.hasAttribute("encoded") && strcmp(reader.getAttribute("encoded"), "yes") == 0) {
buffer = Base::base64_decode(buffer);
}
else {
@@ -353,14 +354,15 @@ void PropertyPythonObject::Restore(Base::XMLReader &reader)
start = buffer.begin();
end = buffer.end();
if (reader.hasAttribute("module") && reader.hasAttribute("class")) {
Py::Module mod(PyImport_ImportModule(reader.getAttribute("module")),true);
if (mod.isNull())
Py::Module mod(PyImport_ImportModule(reader.getAttribute("module")), true);
if (mod.isNull()) {
throw Py::Exception();
}
PyObject* cls = mod.getAttr(reader.getAttribute("class")).ptr();
if (!cls) {
std::stringstream s;
s << "Module " << reader.getAttribute("module")
<< " has no class " << reader.getAttribute("class");
s << "Module " << reader.getAttribute("module") << " has no class "
<< reader.getAttribute("class");
throw Py::AttributeError(s.str());
}
if (PyType_Check(cls)) {
@@ -374,9 +376,10 @@ void PropertyPythonObject::Restore(Base::XMLReader &reader)
else if (boost::regex_search(start, end, what, pickle)) {
std::string name = std::string(what[1].first, what[1].second);
std::string type = std::string(what[2].first, what[2].second);
Py::Module mod(PyImport_ImportModule(name.c_str()),true);
if (mod.isNull())
Py::Module mod(PyImport_ImportModule(name.c_str()), true);
if (mod.isNull()) {
throw Py::Exception();
}
this->object = PyObject_CallObject(mod.getAttr(type).ptr(), nullptr);
load_pickle = true;
buffer = std::string(what[2].second, end);
@@ -386,57 +389,63 @@ void PropertyPythonObject::Restore(Base::XMLReader &reader)
}
}
catch (Py::Exception&) {
Base::PyException e; // extract the Python error text
Base::PyException e; // extract the Python error text
e.ReportException();
this->object = Py::None();
load_failed = true;
}
aboutToSetValue();
if (load_json)
if (load_json) {
this->fromString(buffer);
else if (load_pickle)
}
else if (load_pickle) {
this->loadPickle(buffer);
else if (!load_failed)
Base::Console().Warning("PropertyPythonObject::Restore: unsupported serialisation: %s\n", buffer.c_str());
}
else if (!load_failed) {
Base::Console().Warning(
"PropertyPythonObject::Restore: unsupported serialisation: %s\n",
buffer.c_str());
}
restoreObject(reader);
hasSetValue();
}
}
void PropertyPythonObject::SaveDocFile (Base::Writer &writer) const
void PropertyPythonObject::SaveDocFile(Base::Writer& writer) const
{
std::string buffer = this->toString();
for (char it : buffer)
for (char it : buffer) {
writer.Stream().put(it);
}
}
void PropertyPythonObject::RestoreDocFile(Base::Reader &reader)
void PropertyPythonObject::RestoreDocFile(Base::Reader& reader)
{
aboutToSetValue();
std::string buffer;
char c;
while (reader.get(c)) {
buffer.push_back(c);
char ch {};
while (reader.get(ch)) {
buffer.push_back(ch);
}
this->fromString(buffer);
hasSetValue();
}
unsigned int PropertyPythonObject::getMemSize () const
unsigned int PropertyPythonObject::getMemSize() const
{
return sizeof(Py::Object);
}
Property *PropertyPythonObject::Copy() const
Property* PropertyPythonObject::Copy() const
{
PropertyPythonObject *p = new PropertyPythonObject();
PropertyPythonObject* p = new PropertyPythonObject();
Base::PyGILStateLocker lock;
p->object = this->object;
return p;
}
void PropertyPythonObject::Paste(const Property &from)
void PropertyPythonObject::Paste(const Property& from)
{
if (from.is<PropertyPythonObject>()) {
Base::PyGILStateLocker lock;