Salome HOME
Dump Python in the High Level Parameterized Geometry API (issue #1648)
[modules/shaper.git] / src / PythonAPI / model / dump / DumpAssistant.py
1 """Package for dumping purposes.
2 """
3
4 import ModelHighAPI
5
6 import ModelAPI
7 import SketchAPI
8
9 import sys
10 import inspect
11
12 def singleton(cls):
13     instance = cls()
14     instance.__call__ = lambda: instance
15     return instance
16
17
18 ## @ingroup CPPHighAPI
19 #  Collect information about features that may be dumped and stores the model as a Python script
20 @singleton
21 class DumpAssistant(ModelHighAPI.ModelHighAPI_Dumper):
22
23     ## Constructor
24     def __init__(self):
25         ModelHighAPI.ModelHighAPI_Dumper.__init__(self)
26         ModelHighAPI.ModelHighAPI_Dumper.setInstance(self)
27         self.collectFeatures()
28
29     ## Collect feature wrappers, which allow dumping (have method dump)
30     def collectFeatures(self):
31         self.myFeatures = {}
32         for aModule in sys.modules:
33             for aName, anObj in inspect.getmembers(sys.modules[aModule], inspect.isclass):
34                 if issubclass(anObj, ModelHighAPI.ModelHighAPI_Interface) and hasattr(anObj, "ID") and anObj.dump != ModelHighAPI.ModelHighAPI_Interface.dump:
35                     self.myFeatures[anObj.ID()] = anObj
36
37     ## Create wrapper for a given feature and dump it
38     def dumpFeature(self, theFeature, theForce):
39         aFeatureKind = theFeature.getKind()
40         if aFeatureKind in self.myFeatures:
41             # Dump only feature created by user (in history).
42             # For all other features, just keep their name.
43             if theForce or aFeatureKind == "Parameter" or theFeature.isInHistory():
44                 self.myFeatures[aFeatureKind](theFeature).dump(self)
45             else:
46                 self.name(theFeature)
47         else:
48             # Probably the feature is a constraint, try to dump it with SketchAPI_Constraint.
49             # In case of theFeature is not a constraint, it will not be dumped.
50             self.myFeatures[SketchAPI.SketchAPI_Constraint.ID()](theFeature).dump(self)
51
52     ## Return getter for specified attribute
53     def attributeGetter(self, theFeature, theAttrName):
54         aFeatureKind = theFeature.getKind()
55         if aFeatureKind in self.myFeatures:
56             return self.myFeatures[aFeatureKind](theFeature).attributeGetter(theAttrName)
57         return std_string()
58
59 # Instance of dumper
60 dumper = DumpAssistant