Salome HOME
Increment version number (2.2.3)
[modules/kernel.git] / src / Batch / Batch_PyVersatile.cxx
1 /*
2  * PyVersatile.cxx : 
3  *
4  * Auteur : Ivan DUTKA-MALEN - EDF R&D
5  * Mail   : mailto:ivan.dutka-malen@der.edf.fr
6  * Date   : Mon Oct 13 12:01:12 2003
7  * Projet : Salome 2
8  *
9  */
10
11 #include <string>
12 #include <Python.h>
13 #include "Batch_TypeMismatchException.hxx"
14 #include "Batch_ListIsFullException.hxx"
15 #include "Batch_InvalidArgumentException.hxx"
16 #include "Batch_PyVersatile.hxx"
17
18 namespace Batch {
19
20   // Constructeur a partir d'un objet Versatile
21   PyVersatile::PyVersatile(const Versatile & V) : Versatile(V)
22   {
23     // Nothing to do
24   }
25
26
27   // Constructeur a partir d'un PyObject
28   // Les objets autorises sont les strings et les ints,
29   // ainsi que les listes de strings
30   PyVersatile::PyVersatile(const PyObject * PyO) throw(TypeMismatchException, ListIsFullException, InvalidArgumentException) : Versatile()
31   {
32     PyObject * _PyO = const_cast<PyObject *>(PyO);
33
34     if (PyList_Check(_PyO)) { // c'est une liste
35       _maxsize = PyList_Size(_PyO);
36       for(int i=0; i<_maxsize; i++) {
37         PyObject * val = PyList_GetItem(_PyO, i);
38         if (PyString_Check(val)) {
39           *this += PyString_AsString(val);
40                                         
41         } else if (PyTuple_Check(val) && 
42                    (PyTuple_Size(val) == 2) &&
43                    PyString_Check( PyTuple_GetItem(val,0) ) && 
44                    PyString_Check( PyTuple_GetItem(val,1) )   ) {
45           *this += Couple( PyString_AsString( PyTuple_GetItem(val,0) ),
46                            PyString_AsString( PyTuple_GetItem(val,1) )
47                            );
48                                         
49         } else {
50           PyErr_SetString(PyExc_RuntimeWarning, "PyVersatile::PyVersatile(const PyObject * PyO) : invalid PyObject");
51         }
52       }
53
54     } else if (PyString_Check(_PyO)) { // c'est une string
55       const char * s = PyString_AsString(_PyO);
56       Versatile V = string(s);
57       *this = V;
58       
59     } else if (PyInt_Check(_PyO)) { // c'est un int
60       *this = PyInt_AsLong(_PyO);
61
62     } else { // erreur
63       PyErr_SetString(PyExc_RuntimeWarning, "PyVersatile::PyVersatile(const PyObject * PyO) : invalid PyObject");
64     }
65   }
66
67
68
69   // Conversion de type vers un PyObject
70   PyVersatile::operator PyObject *() const
71   {
72     PyObject * obj;
73
74     if (_maxsize != 1) { // une liste
75       obj = PyList_New(0);
76       for(Versatile::const_iterator it=begin(); it!=end(); it++) {
77         char ch[2] = {0, 0};
78         string st;
79         Couple cp;
80 //      PyObject * tuple;
81         switch (_discriminator) {
82           //    case BOOL:
83           //      PyList_Append(obj, PyInt_FromLong(* static_cast<BoolType *>(*it)));
84           //      break;
85
86           //    case CHAR:
87           //      *ch = * static_cast<CharType *>(*it);
88           //      PyList_Append(obj, PyString_FromString(ch));
89           //      break;
90
91           //    case INT:
92           //      PyList_Append(obj, PyInt_FromLong(* static_cast<IntType *>(*it)));
93           //      break;
94
95         case LONG:
96           PyList_Append(obj, PyInt_FromLong(* static_cast<LongType *>(*it)));
97           break;
98
99         case STRING:
100           st = * static_cast<StringType *>(*it);
101           PyList_Append(obj, PyString_FromString(st.c_str()));
102           break;
103
104         case COUPLE:
105           cp = * static_cast<CoupleType *>(*it);
106 //        tuple = PyTuple_New(2);
107 //        PyTuple_SetItem(tuple, 0, PyString_FromString( cp.getLocal().c_str()  ) );
108 //        PyTuple_SetItem(tuple, 1, PyString_FromString( cp.getRemote().c_str() ) );
109 //        PyList_Append(obj, tuple);
110           PyList_Append(obj, Py_BuildValue("(ss)", cp.getLocal().c_str(), cp.getRemote().c_str() ));
111           break;
112
113         case UNDEFINED:
114           PyList_Append(obj, Py_None);
115           break;
116         }
117
118       }
119
120     } else { // un scalaire
121       char ch[2] = {0, 0};
122       string st;
123       Couple cp;
124 //       PyObject * tuple;
125       switch (_discriminator) {
126         //       case BOOL:
127         //      obj = PyInt_FromLong(* static_cast<BoolType *>(front()));
128         //      break;
129
130         //       case CHAR:
131         //      *ch = * static_cast<CharType *>(front());
132         //      obj = PyString_FromString(ch);
133         //      break;
134
135         //       case INT:
136         //      obj = PyInt_FromLong(* static_cast<IntType *>(front()));
137         //      break;
138
139       case LONG:
140         obj = PyInt_FromLong(* static_cast<LongType *>(front()));
141         break;
142
143       case STRING:
144         st = * static_cast<StringType *>(front());
145         obj = PyString_FromString(st.c_str());
146         break;
147
148       case COUPLE:
149         cp = * static_cast<CoupleType *>(front());
150 //      tuple = PyTuple_New(2);
151 //      PyTuple_SetItem(tuple, 0, PyString_FromString( cp.getLocal().c_str()  ) );
152 //      PyTuple_SetItem(tuple, 1, PyString_FromString( cp.getRemote().c_str() ) );
153 //      obj = PyList_New(0);
154 //      PyList_Append(obj, tuple);
155         obj = Py_BuildValue("[(ss)]", cp.getLocal().c_str(), cp.getRemote().c_str() );
156         break;
157
158       case UNDEFINED:
159         obj = Py_None;
160         break;
161       }
162     }
163
164     return obj;
165   }
166
167
168   // Operateur d'affectation a partir d'un objet Versatile
169   PyVersatile & PyVersatile::operator =(const Versatile & V)
170   {
171     Versatile * me = this;
172     *me = V;
173     return *this;
174   }
175
176 }
177
178
179 // COMMENTS