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