Salome HOME
Remove execute() from partition feature.
[modules/shaper.git] / src / PythonAPI / model / roots.py
index 62288ebb3dfb67a121adfa932a3489e8907ec685..a5ac3c2014234874c733336619b588704301eca1 100644 (file)
@@ -3,39 +3,76 @@ Author: Daniel Brunier-Coulin
 Copyright (C) 2014-20xx CEA/DEN, EDF R&D
 """
 
-from ModelAPI import *
+import ModelAPI
 
+from model import tools
 
-class Feature(ModelAPI_Feature):
-  """Base class of user-defined Python features."""
 
-  def __init__(self):
-    ModelAPI_Feature.__init__(self)
+class Feature(ModelAPI.ModelAPI_Feature):
+    """Base class of user-defined Python features."""
 
-  def addRealInput (self, inputid):
-    self.data().addAttribute(inputid, ModelAPI_AttributeDouble_typeId())
+    def __init__(self):
+        ModelAPI.ModelAPI_Feature.__init__(self)
 
-  def getRealInput (self, inputid):
-    return self.data().real(inputid).value()
+    def addRealInput (self, inputid):
+        self.data().addAttribute(inputid,
+                                 ModelAPI.ModelAPI_AttributeDouble_typeId())
 
-  def addResult (self, result):
-    shape = result.shape()
-    body  = self.document().createBody( self.data() )
-    body.store(shape)
-    self.setResult(body)
+    def getRealInput (self, inputid):
+        return self.data().real(inputid).value()
+
+    def addResult (self, result):
+        shape = result.shape()
+        body = self.document().createBody(self.data())
+        body.store(shape)
+        self.setResult(body)
 
 
 class Interface():
-  """Base class of hight level Python interfaces to features."""
+    """Base class of high level Python interfaces to features."""
+
+    def __init__(self, feature):
+        self._feature = feature
+        self._attribute_white_list = [
+            "getKind", "results", "firstResult", "lastResult",
+            ]
+
+    def __getattr__(self, name):
+        """Process missing attributes.
+
+        Add get*() methods for access feature attributes.
+        Redirect some missing attributes to the feature.
+        """
+        if name.startswith("get"):
+            possible_names = [
+                "_" + name[3:],
+                "_" + tools.convert_to_underscore(name[3:]),
+                ]
+            for possible_name in possible_names:
+                if hasattr(self, possible_name):
+                    def getter():
+                        return getattr(self, possible_name)
+                    return getter
+
+        if name in self._attribute_white_list:
+            return getattr(self._feature, name)
+
+        raise AttributeError()
+
+    def feature(self):
+        """Return ModelAPI_Feature."""
+        return self._feature
 
-  def __init__(self, container, fid):
-    self.my = container.addFeature(fid)
+    def _fill_attribute(self, attribute, value):
+        """Fill ModelAPI_Attribute* with value."""
+        tools.fill_attribute(attribute, value)
 
-  def setRealInput (self, inputid, value):
-    self.my.data().real(inputid).setValue(value)
+    def setRealInput(self, inputid, value):
+        self._feature.data().real(inputid).setValue(value)
 
-  def areInputValid (self):
-    return ModelAPI_Session.get().validators().validate(self.my)
+    def areInputValid(self):
+        validators = ModelAPI.ModelAPI_Session.get().validators()
+        return validators.validate(self._feature)
 
-  def execute (self):
-    self.my.execute()
\ No newline at end of file
+    def execute(self):
+        self._feature.execute()