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
8 import sys
9 import inspect
10
11 def singleton(cls):
12     instance = cls()
13     instance.__call__ = lambda: instance
14     return instance
15
16
17 ## @ingroup CPPHighAPI
18 #  Collect information about features that may be dumped and stores the model as a Python script
19 @singleton
20 class DumpAssistant(ModelHighAPI.ModelHighAPI_Dumper):
21
22     ## Constructor
23     def __init__(self):
24         ModelHighAPI.ModelHighAPI_Dumper.__init__(self)
25         ModelHighAPI.ModelHighAPI_Dumper.setInstance(self)
26         self.collectFeatures()
27
28     ## Collect feature wrappers, which allow dumping (have method dump)
29     def collectFeatures(self):
30         self.myFeatures = {}
31         for aModule in sys.modules:
32             for aName, anObj in inspect.getmembers(sys.modules[aModule], inspect.isclass):
33                 if issubclass(anObj, ModelHighAPI.ModelHighAPI_Interface) and hasattr(anObj, "ID") and anObj.dump != ModelHighAPI.ModelHighAPI_Interface.dump:
34                     self.myFeatures[anObj.ID()] = anObj
35
36     ## Create wrapper for a given feature and dump it
37     def dumpFeature(self, theFeature, theForce):
38         aFeatureKind = theFeature.getKind()
39         if aFeatureKind in self.myFeatures:
40             # Dump only feature created by user (in history).
41             # For all other features, just keep their name.
42             if theForce or aFeatureKind == "Parameter" or theFeature.isInHistory():
43                 self.myFeatures[aFeatureKind](theFeature).dump(self)
44             else:
45                 self.name(theFeature)
46
47     ## Return getter for specified attribute
48     def attributeGetter(self, theFeature, theAttrName):
49         aFeatureKind = theFeature.getKind()
50         if aFeatureKind in self.myFeatures:
51             return self.myFeatures[aFeatureKind](theFeature).attributeGetter(theAttrName)
52         return std_string()
53
54 # Instance of dumper
55 dumper = DumpAssistant