From 795b2ef89c5953086a50677699ee214228befa2b Mon Sep 17 00:00:00 2001 From: Artem Zhidkov Date: Sat, 25 Apr 2020 17:30:43 +0300 Subject: [PATCH] Issue #3219: Improve Python dump readability --- src/ModelHighAPI/ModelHighAPI.i | 3 +++ src/ModelHighAPI/ModelHighAPI_Dumper.cpp | 19 ++++++++++++++----- src/ModelHighAPI/ModelHighAPI_Dumper.h | 3 +++ src/PythonAPI/model/dump/DumpAssistant.py | 11 +++++++++-- src/SketchAPI/SketchAPI_Constraint.cpp | 1 - src/SketchAPI/SketchAPI_ConstraintAngle.cpp | 2 -- 6 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/ModelHighAPI/ModelHighAPI.i b/src/ModelHighAPI/ModelHighAPI.i index 30fb144b3..9fea6cc19 100644 --- a/src/ModelHighAPI/ModelHighAPI.i +++ b/src/ModelHighAPI/ModelHighAPI.i @@ -54,6 +54,9 @@ // directors %feature("director") ModelHighAPI_Dumper; +// renamed methods +%rename(__print__) ModelHighAPI_Dumper::operator<<; + // shared pointers %shared_ptr(ModelHighAPI_Interface) %shared_ptr(ModelHighAPI_Folder) diff --git a/src/ModelHighAPI/ModelHighAPI_Dumper.cpp b/src/ModelHighAPI/ModelHighAPI_Dumper.cpp index 8be7dd3cd..2c8d45c01 100644 --- a/src/ModelHighAPI/ModelHighAPI_Dumper.cpp +++ b/src/ModelHighAPI/ModelHighAPI_Dumper.cpp @@ -1254,7 +1254,6 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FolderPtr& theFolder) ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity) { - *myDumpStorage << "\n### Create "<< theEntity->getKind() << "\n"; *myDumpStorage << name(theEntity); if (!myNames[theEntity].myIsDumped) { @@ -1454,10 +1453,10 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( const std::shared_ptr& theAttrSelList) { static const int aThreshold = 2; - static bool aDumpAsIs = false; + static int aNbSpaces = 0; // if number of elements in the list if greater than a threshold, // dump it in a separate line with specific name - if (aDumpAsIs || theAttrSelList->size() <= aThreshold) { + if (aNbSpaces > 0 || theAttrSelList->size() <= aThreshold) { *myDumpStorage << "["; GeomShapePtr aShape; @@ -1480,6 +1479,11 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( if(isAdded) { *myDumpStorage << ", "; + // print each attribute on separate line with the appropriate shift + if (aNbSpaces > 0) { + std::string aSpaces(aNbSpaces + 1, ' '); + *myDumpStorage << "\n" << aSpaces; + } } else { isAdded = true; } @@ -1513,9 +1517,9 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( } // reserve dumped buffer and store list "as is" myDumpStorage->reserveBuffer(); - aDumpAsIs = true; + aNbSpaces = (int)aListName.size() + 3; *this << aListName << " = " << theAttrSelList << "\n"; - aDumpAsIs = false; + aNbSpaces = 0; // append reserved data to the end of the current buffer myDumpStorage->restoreReservedBuffer(); *myDumpStorage << aListName; @@ -1540,6 +1544,11 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( return *this; } +void ModelHighAPI_Dumper::newline() +{ + *this << std::endl; +} + /// Dump std::endl ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper, std::basic_ostream& (*theEndl)(std::basic_ostream&)) diff --git a/src/ModelHighAPI/ModelHighAPI_Dumper.h b/src/ModelHighAPI/ModelHighAPI_Dumper.h index a7c63ec4e..8f480c0f1 100644 --- a/src/ModelHighAPI/ModelHighAPI_Dumper.h +++ b/src/ModelHighAPI/ModelHighAPI_Dumper.h @@ -243,6 +243,9 @@ public: ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper, std::basic_ostream& (*theEndl)(std::basic_ostream&)); + /// Print std::endl from Python + MODELHIGHAPI_EXPORT void newline(); + /// Dump GeomAPI_Pnt in the following form: /// "GeomAPI_Pnt(X, Y, Z)" MODELHIGHAPI_EXPORT diff --git a/src/PythonAPI/model/dump/DumpAssistant.py b/src/PythonAPI/model/dump/DumpAssistant.py index 3170cc857..08007616d 100644 --- a/src/PythonAPI/model/dump/DumpAssistant.py +++ b/src/PythonAPI/model/dump/DumpAssistant.py @@ -58,20 +58,27 @@ class DumpAssistant(ModelHighAPI.ModelHighAPI_Dumper): ## Create wrapper for a given feature and dump it def dumpFeature(self, theFeature, theForce): + aDumper = None aFeatureKind = theFeature.getKind() if aFeatureKind in self.myFeatures: # Dump only feature created by user (in history). # Also dump Export and RemoveResults features (hard-coded here in order not to change the data model). # For all other features, just keep their name. if theForce or theFeature.isInHistory() or aFeatureKind=="Export" or aFeatureKind=="RemoveResults": - self.myFeatures[aFeatureKind](theFeature).dump(self) + aDumper = self.myFeatures[aFeatureKind](theFeature) + # Dump comment for the operation before the dumping of the feature to improve the readability of a script. + self.__print__("\n### Create " + theFeature.getKind()) + self.newline() else: self.name(theFeature) self.clearNotDumped() else: # Probably the feature is a constraint, try to dump it with SketchAPI_Constraint. # In case of theFeature is not a constraint, it will not be dumped. - self.myFeatures[SketchAPI.SketchAPI_Constraint.ID()](theFeature).dump(self) + self.name(theFeature, False, True, True) + aDumper = self.myFeatures[SketchAPI.SketchAPI_Constraint.ID()](theFeature) + if aDumper is not None: + aDumper.dump(self) ## Create wrapper for a folder and dump it def dumpFolder(self, theFolder): diff --git a/src/SketchAPI/SketchAPI_Constraint.cpp b/src/SketchAPI/SketchAPI_Constraint.cpp index eedc31a97..5a655d067 100644 --- a/src/SketchAPI/SketchAPI_Constraint.cpp +++ b/src/SketchAPI/SketchAPI_Constraint.cpp @@ -194,7 +194,6 @@ void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const return; const std::string& aSketchName = theDumper.parentName(aBase); - theDumper.name(aBase, false, true, true); theDumper << aSketchName << "." << aSetter << "("; bool isFirstAttr = true; diff --git a/src/SketchAPI/SketchAPI_ConstraintAngle.cpp b/src/SketchAPI/SketchAPI_ConstraintAngle.cpp index e84634aaf..94e5ecf86 100644 --- a/src/SketchAPI/SketchAPI_ConstraintAngle.cpp +++ b/src/SketchAPI/SketchAPI_ConstraintAngle.cpp @@ -129,8 +129,6 @@ void SketchAPI_ConstraintAngle::dump(ModelHighAPI_Dumper& theDumper) const FeaturePtr aBase = feature(); const std::string& aSketchName = theDumper.parentName(aBase); - //theDumper << aBase << " = " << aSketchName << "." << "setAngle("; - theDumper.name(aBase, false, true, true); theDumper << aSketchName << "." << "setAngle("; bool isFirstAttr = true; -- 2.39.2