Salome HOME
Remove get_values() function from tools.py
[modules/shaper.git] / src / PythonAPI / model / roots.py
1 """Abstract root classes of user-defined Python features producing a Body
2 Author: Daniel Brunier-Coulin
3 Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 """
5
6 import ModelAPI
7
8 from model import tools
9
10
11 class Feature(ModelAPI.ModelAPI_Feature):
12     """Base class of user-defined Python features."""
13
14     def __init__(self):
15         ModelAPI.ModelAPI_Feature.__init__(self)
16
17     def addRealInput (self, inputid):
18         self.data().addAttribute(inputid,
19                                  ModelAPI.ModelAPI_AttributeDouble_typeId())
20
21     def getRealInput (self, inputid):
22         return self.data().real(inputid).value()
23
24     def addResult (self, result):
25         shape = result.shape()
26         body = self.document().createBody(self.data())
27         body.store(shape)
28         self.setResult(body)
29
30
31 class Interface():
32     """Base class of high level Python interfaces to features."""
33
34     def __init__(self, feature):
35         self._feature = feature
36         self._attribute_white_list = [
37             "execute",
38             "getKind", "results", "firstResult", "lastResult",
39             ]
40
41     def __getattr__(self, name):
42         """Process missing attributes.
43
44         Add get*() methods for access feature attributes.
45         Redirect some missing attributes to the feature.
46         """
47         if name.startswith("get"):
48             possible_names = [
49                 "_" + name[3:],
50                 "_" + tools.convert_to_underscore(name[3:]),
51                 ]
52             for possible_name in possible_names:
53                 if hasattr(self, possible_name):
54                     def getter():
55                         return getattr(self, possible_name)
56                     return getter
57
58         if name in self._attribute_white_list:
59             return getattr(self._feature, name)
60
61         raise AttributeError()
62
63     def feature(self):
64         """Return ModelAPI_Feature."""
65         return self._feature
66
67     def _fill_attribute(self, attribute, value):
68         """Fill ModelAPI_Attribute* with value."""
69         tools.fill_attribute(attribute, value)
70
71     def setRealInput(self, inputid, value):
72         self._feature.data().real(inputid).setValue(value)
73
74     def areInputValid(self):
75         validators = ModelAPI.ModelAPI_Session.get().validators()
76         return validators.validate(self._feature)
77
78     def _execute(self):
79         if self.areInputValid():
80             self._feature.execute()
81         else:
82             raise RuntimeError("Can not execute %s: %s" %
83                                (self._feature.getKind(), self._feature.error())
84                                )