Salome HOME
Copyright update 2022
[modules/kernel.git] / src / LifeCycleCORBA_SWIG / libSALOME_LifeCycleCORBA.i
1 // Copyright (C) 2007-2022  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, or (at your option) any later version.
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 //
25 %module libSALOME_LifeCycleCORBA
26
27 %feature("autodoc", "1");
28
29 %include <std_except.i>
30 %include "std_string.i"
31
32
33 // ----------------------------------------------------------------------------
34
35 %{
36 #include "utilities.h"
37 #include "SALOME_LifeCycleCORBA.hxx"
38 #include "SALOME_FileTransferCORBA.hxx"
39 #include "SALOME_NamingService.hxx"
40 #include "ServiceUnreachable.hxx"
41 #include "Utils_SALOME_Exception.hxx"
42 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
43 typedef int Py_ssize_t;
44 #define PY_SSIZE_T_MAX INT_MAX
45 #define PY_SSIZE_T_MIN INT_MIN
46 #endif
47
48 #include <Python.h>
49 #include <omniORBpy.h>
50 omniORBpyAPI* api=0;
51
52 %}
53
54
55 // ----------------------------------------------------------------------------
56
57
58 %init
59 %{
60   // init section
61
62   PyObject* omnipy = PyImport_ImportModule((char*)"_omnipy");
63   if (!omnipy)
64   {
65     PyErr_SetString(PyExc_ImportError, (char*)"Cannot import _omnipy");
66     return NULL;
67   }
68   PyObject* pyapi = PyObject_GetAttrString(omnipy, (char*)"API");
69   api = (omniORBpyAPI*)PyCapsule_GetPointer(pyapi,"_omnipy.API");
70   Py_DECREF(pyapi);
71 %}
72
73
74
75 // ----------------------------------------------------------------------------
76
77 %typemap(out) Engines::Container_ptr, Engines::EngineComponent_ptr, Engines::fileRef_ptr,
78               Engines::ContainerManager_ptr, Engines::ResourcesManager_ptr 
79 {
80   MESSAGE("typemap out on CORBA object ptr");
81   SCRUTE($1);
82   $result = api->cxxObjRefToPyObjRef($1, 1);
83   SCRUTE($result);
84 }
85
86 %typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) Engines::fileRef_ptr
87 {
88   $1=PyObject_HasAttrString($input, "__omni_obj");
89 }
90
91 %typemap(in) Engines::fileRef_ptr aFileRef
92 {
93   MESSAGE("typemap in on CORBA object ptr");
94   try {
95      CORBA::Object_ptr obj = api->pyObjRefToCxxObjRef($input,1);
96      $1 = Engines::fileRef::_narrow(obj);
97      SCRUTE($1);
98   }
99   catch (...) {
100      PyErr_SetString(PyExc_RuntimeError, "not a valid CORBA object ptr");
101   }
102 }
103
104 // We use a dictionary to represent ContainerParameters structure instead of the CORBA
105 // structure itself because it would require a transformation from the Python
106 // ContainerParameters structure, generated by omniOrbPy, to a C++ ContainerParameters
107 // structure, generated by omniOrb, and this would be quite complex.
108 %typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) const Engines::ContainerParameters &
109 {
110   $1 = PyDict_Check($input)? 1 : 0;
111 }
112
113 %typemap(in) const Engines::ContainerParameters &
114 {
115   MESSAGE("typemap in on Engines::ContainerParameters");
116   if (PyDict_Check($input) == 1)
117     {
118       Engines::ContainerParameters *param = new Engines::ContainerParameters ;
119       SALOME_LifeCycleCORBA::preSet(*param);
120
121       PyObject *key, *value;
122       Py_ssize_t pos = 0;
123       while (PyDict_Next($input, &pos, &key, &value))
124        {
125          char* keystr = PyString_AsString(key);
126          if (strcmp(keystr,"container_name")==0)
127            {
128              param->container_name = CORBA::string_dup(PyString_AsString(value));
129            }
130          else if (strcmp(keystr,"resource_name")==0)
131            {
132              param->resource_params.name = CORBA::string_dup(PyString_AsString(value));
133            }
134          else if (strcmp(keystr,"hostname")==0)
135            {
136              param->resource_params.hostname = CORBA::string_dup(PyString_AsString(value));
137            }
138          else if (strcmp(keystr,"OS")==0)
139            {
140              param->resource_params.OS = CORBA::string_dup(PyString_AsString(value));
141            }
142          else if (strcmp(keystr,"mem_mb")==0)
143            {
144              param->resource_params.mem_mb = PyLong_AsLong(value);
145            }
146          else if (strcmp(keystr,"cpu_clock")==0)
147            {
148              param->resource_params.cpu_clock = PyLong_AsLong(value);
149            }
150          else if (strcmp(keystr,"nb_proc_per_node")==0)
151            {
152              param->resource_params.nb_proc_per_node = PyLong_AsLong(value);
153            }
154          else if (strcmp(keystr,"nb_node")==0)
155            {
156              param->resource_params.nb_node = PyLong_AsLong(value);
157            }
158          else if (strcmp(keystr,"isMPI")==0)
159            {
160              param->isMPI = PyLong_AsLong(value);
161            }
162          else if (strcmp(keystr,"workingdir")==0)
163              param->workingdir = CORBA::string_dup(PyString_AsString(value));
164          else if (strcmp(keystr,"mode")==0)
165              param->mode = CORBA::string_dup(PyString_AsString(value));
166          else if (strcmp(keystr,"policy")==0)
167              param->resource_params.policy = CORBA::string_dup(PyString_AsString(value));
168          else if (strcmp(keystr,"parallelLib")==0)
169            {
170              param->parallelLib = CORBA::string_dup(PyString_AsString(value));
171            }
172        }
173       $1 = param;
174     }
175   else 
176     {
177        MESSAGE("Not a ContainerParameters struct");
178        PyErr_SetString(PyExc_TypeError,"Must Be a Python ContainerParameters object");
179        return NULL;
180     }
181 }
182
183
184 %typemap(freearg) const Engines::ContainerParameters &
185 {
186   MESSAGE("delete $1");
187   delete $1;
188 }
189
190 // ----------------------------------------------------------------------------
191
192 %include <Utils_SALOME_Exception.hxx>
193
194 %exception {
195     Py_BEGIN_ALLOW_THREADS
196     try {
197       $action
198     }
199     catch (ServiceUnreachable&) {
200        Py_BLOCK_THREADS
201        PyErr_SetString(PyExc_RuntimeError,"Naming Service Unreacheable");
202        return NULL;
203     }
204     catch (SALOME_Exception &e) {
205        Py_BLOCK_THREADS
206        PyErr_SetString(PyExc_RuntimeError,e.what());
207        return NULL;
208     }
209     catch (SALOME::SALOME_Exception &e) {
210        Py_BLOCK_THREADS
211        PyErr_SetString(PyExc_RuntimeError,e.details.text);
212        return NULL;
213     }
214     catch (...) {
215        Py_BLOCK_THREADS
216        PyErr_SetString(PyExc_RuntimeError, "unknown exception");
217        return NULL;
218     }
219     Py_END_ALLOW_THREADS
220 }
221
222
223 %include <SALOME_LifeCycleCORBA.hxx>
224 %include <SALOME_FileTransferCORBA.hxx>