Salome HOME
Issue #3219: Improve Python dump readability
authorArtem Zhidkov <Artem.Zhidkov@opencascade.com>
Sat, 25 Apr 2020 14:30:43 +0000 (17:30 +0300)
committerArtem Zhidkov <Artem.Zhidkov@opencascade.com>
Sat, 25 Apr 2020 14:30:43 +0000 (17:30 +0300)
src/ModelHighAPI/ModelHighAPI.i
src/ModelHighAPI/ModelHighAPI_Dumper.cpp
src/ModelHighAPI/ModelHighAPI_Dumper.h
src/PythonAPI/model/dump/DumpAssistant.py
src/SketchAPI/SketchAPI_Constraint.cpp
src/SketchAPI/SketchAPI_ConstraintAngle.cpp

index 30fb144b34a2ac9c177abcfb15c99bc4067aa1d0..9fea6cc192553a0a87b5938d277f78d037cde963 100644 (file)
@@ -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)
index 8be7dd3cd08911c576788f37b57dad4fe57ba704..2c8d45c0175b93d386c3416a43b6e7a06b641c9d 100644 (file)
@@ -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<ModelAPI_AttributeSelectionList>& 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<char>& (*theEndl)(std::basic_ostream<char>&))
index a7c63ec4e455e5a32a73f6976a2ccb996166d1f4..8f480c0f16441009b0fac907633dd444a2fae161 100644 (file)
@@ -243,6 +243,9 @@ public:
   ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
                                 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&));
 
+  /// Print std::endl from Python
+  MODELHIGHAPI_EXPORT void newline();
+
   /// Dump GeomAPI_Pnt in the following form:
   /// "GeomAPI_Pnt(X, Y, Z)"
   MODELHIGHAPI_EXPORT
index 3170cc8570794ee6d7be7beeb43b2d38fb071a4e..08007616d957acdd2d34c2abca27eb84ceb50aa2 100644 (file)
@@ -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):
index eedc31a9701b04caf42bbbc69d65eb1eeef74497..5a655d0672b52bc357d3d7ad459c9cc61675962e 100644 (file)
@@ -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;
index e84634aaf453395094588f03b4e2ac2710749dba..94e5ecf86e330ff7bcdb9c1807b644dc046b1f6f 100644 (file)
@@ -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;