Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/yacs.git] / src / LifeCycleCORBA_SWIG / libSALOME_LifeCycleCORBA.i
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21
22 // ----------------------------------------------------------------------------
23
24 %module libSALOME_LifeCycleCORBA
25
26 %include <std_except.i>
27
28
29 // ----------------------------------------------------------------------------
30
31 %{
32 #include "utilities.h"
33 #include "SALOME_LifeCycleCORBA.hxx"
34 #include "SALOME_FileTransferCORBA.hxx"
35 #include "SALOME_NamingService.hxx"
36 #include "ServiceUnreachable.hxx"
37 #include "Utils_SALOME_Exception.hxx"
38
39   using namespace std;
40
41 //--- from omniORBpy.h (not present on Debian Sarge packages)
42
43 struct omniORBpyAPI {
44
45   PyObject* (*cxxObjRefToPyObjRef)(const CORBA::Object_ptr cxx_obj,
46                                    CORBA::Boolean hold_lock);
47   // Convert a C++ object reference to a Python object reference.
48   // If <hold_lock> is true, caller holds the Python interpreter lock.
49
50   CORBA::Object_ptr (*pyObjRefToCxxObjRef)(PyObject* py_obj,
51                                            CORBA::Boolean hold_lock);
52   // Convert a Python object reference to a C++ object reference.
53   // Raises BAD_PARAM if the Python object is not an object reference.
54   // If <hold_lock> is true, caller holds the Python interpreter lock.
55
56
57   omniORBpyAPI();
58   // Constructor for the singleton. Sets up the function pointers.
59 };
60
61   omniORBpyAPI* api;
62
63 %}
64
65
66 // ----------------------------------------------------------------------------
67
68
69 %init
70 %{
71   // init section
72
73   PyObject* omnipy = PyImport_ImportModule((char*)"_omnipy");
74   if (!omnipy)
75   {
76     PyErr_SetString(PyExc_ImportError,
77                     (char*)"Cannot import _omnipy");
78     return;
79   }
80   PyObject* pyapi = PyObject_GetAttrString(omnipy, (char*)"API");
81   api = (omniORBpyAPI*)PyCObject_AsVoidPtr(pyapi);
82   Py_DECREF(pyapi);
83 %}
84
85
86
87 // ----------------------------------------------------------------------------
88
89 %typemap(python,out) Engines::Container_ptr, Engines::Component_ptr, Engines::fileRef_ptr
90 {
91   MESSAGE("typemap out on CORBA object ptr");
92   SCRUTE($1);
93   $result = api->cxxObjRefToPyObjRef($1, 1);
94   SCRUTE($result);
95 }
96
97 %typemap(python,in) Engines::fileRef_ptr aFileRef
98 {
99   MESSAGE("typemap in on CORBA object ptr");
100   try {
101      CORBA::Object_ptr obj = api->pyObjRefToCxxObjRef($input,1);
102      $1 = Engines::fileRef::_narrow(obj);
103      SCRUTE($1);
104   }
105   catch (...) {
106      PyErr_SetString(PyExc_RuntimeError, "not a valid CORBA object ptr");
107   }
108 }
109
110
111 %typemap(python,out) std::string, 
112                     string
113 {
114   MESSAGE("typemap out on std::string");
115   SCRUTE($1);
116   $result = PyString_FromString($1.c_str());
117 }
118
119 %typemap(typecheck) const Engines::MachineParameters &,
120                     Engines::MachineParameters const &
121 {
122   $1 = PyDict_Check($input);
123 }
124
125 %typemap(typecheck) std::string, 
126                     string
127 {
128   $1 = PyString_Check($input);
129 }
130
131 %typemap(python,in) std::string, 
132                     string
133 {
134   MESSAGE("typemap in on std::string");
135   std::string str;
136   if (PyString_Check($input) == 1)
137     {
138       char* value = PyString_AsString($input);
139       str = value;
140       $1 = str;
141     }
142   else 
143     {
144        MESSAGE("Not a string");
145        PyErr_SetString(PyExc_TypeError,"Must Be a Python string");
146        return NULL;
147     }
148 }
149
150
151 %typemap(python,in) const Engines::MachineParameters &
152 {
153   //printf("typemap in on Engines::MachineParameters\n");
154   MESSAGE("typemap in on Engines::MachineParameters");
155   if (PyDict_Check($input) == 1)
156     {
157       Engines::MachineParameters *param = new Engines::MachineParameters ;
158       param->container_name = CORBA::string_dup("");
159       param->hostname = CORBA::string_dup("");
160       param->OS = CORBA::string_dup("");
161       param->mem_mb = 0;
162       param->cpu_clock = 0;
163       param->nb_proc_per_node = 0;
164       param->nb_node = 0;
165       param->isMPI = false;
166       param->parallelLib = CORBA::string_dup("");
167       param->nb_component_nodes = 0;
168       PyObject *key, *value;
169       int pos = 0;
170       while (PyDict_Next($input, &pos, &key, &value))
171         {
172           char* keystr = PyString_AsString(key);
173           printf("key: %s\n", keystr);
174           if (strcmp(keystr,"container_name")==0)
175             {
176               param->container_name = CORBA::string_dup(PyString_AsString(value));
177             }
178           else if (strcmp(keystr,"hostname")==0)
179             {
180               param->hostname = CORBA::string_dup(PyString_AsString(value));
181             }
182           else if (strcmp(keystr,"OS")==0)
183             {
184               param->OS = CORBA::string_dup(PyString_AsString(value));
185             }
186           else if (strcmp(keystr,"mem_mb")==0)
187             {
188               param->mem_mb = PyLong_AsLong(value);
189             }
190           else if (strcmp(keystr,"cpu_clock")==0)
191             {
192               param->cpu_clock = PyLong_AsLong(value);
193             }
194           else if (strcmp(keystr,"nb_proc_per_node")==0)
195             {
196               param->nb_proc_per_node = PyLong_AsLong(value);
197             }
198           else if (strcmp(keystr,"nb_node")==0)
199             {
200               param->nb_node = PyLong_AsLong(value);
201             }
202           else if (strcmp(keystr,"isMPI")==0)
203             {
204               param->isMPI = PyLong_AsLong(value);
205             }
206           else if (strcmp(keystr,"parallelLib")==0)
207             {
208               param->parallelLib = CORBA::string_dup(PyString_AsString(value));
209             }
210           else if (strcmp(keystr,"nb_component_nodes")==0)
211             {
212               param->nb_component_nodes = PyLong_AsLong(value);
213             }
214         }
215       $1 = param;
216     }
217   else 
218     {
219        MESSAGE("Not a dictionnary");
220        PyErr_SetString(PyExc_TypeError,"Must Be a Python Dictionnary");
221        return NULL;
222     }
223 }
224
225
226 %typemap(python,freearg) const Engines::MachineParameters &
227 {
228   MESSAGE("delete $1");
229   delete $1;
230 }
231
232 // ----------------------------------------------------------------------------
233
234 %include <Utils_SALOME_Exception.hxx>
235
236 %exception {
237     Py_BEGIN_ALLOW_THREADS
238     try {
239       $action
240     }
241     catch (ServiceUnreachable) {
242        Py_BLOCK_THREADS
243        PyErr_SetString(PyExc_RuntimeError,"Naming Service Unreacheable");
244        return NULL;
245     }
246     catch (SALOME_Exception &e) {
247        Py_BLOCK_THREADS
248        PyErr_SetString(PyExc_RuntimeError,e.what());
249        return NULL;
250     }
251     catch (SALOME::SALOME_Exception &e) {
252        Py_BLOCK_THREADS
253        PyErr_SetString(PyExc_RuntimeError,e.details.text);
254        return NULL;
255     }
256     catch (...) {
257        Py_BLOCK_THREADS
258        PyErr_SetString(PyExc_RuntimeError, "unknown exception");
259        return NULL;
260     }
261     Py_END_ALLOW_THREADS
262 }
263
264
265 %include <SALOME_LifeCycleCORBA.hxx>
266 %include <SALOME_FileTransferCORBA.hxx>
267