Salome HOME
[PY3] 2to3 -w -n results
[modules/yacs.git] / src / py2yacs / py2yacs.cxx
1 // Copyright (C) 2006-2016  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include <Python.h>
20 #include <sstream>
21 #include "py2yacs.hxx"
22 #include "RuntimeSALOME.hxx"
23 #include "Proc.hxx"
24 #include "InlineNode.hxx"
25 #include "AutoGIL.hxx"
26
27 Py2yacsException::Py2yacsException(const std::string& what)
28 : std::exception(),
29   _what(what)
30 {
31 }
32
33 Py2yacsException::~Py2yacsException()throw ()
34 {
35 }
36
37 const char * Py2yacsException::what() const throw ()
38 {
39   return _what.c_str();
40 }
41
42
43 Py2yacs::Py2yacs()
44 : _python_parser_module("py2yacs"),
45   _python_parser_function("get_properties"),
46   _functions(),
47   _global_errors(),
48   _python_code()
49 {
50 }
51
52 Py2yacs::Py2yacs(const std::string& python_parser_module,
53         const std::string& python_parser_function)
54 : _python_parser_module(python_parser_module),
55   _python_parser_function(python_parser_function),
56   _functions(),
57   _global_errors(),
58   _python_code()
59 {
60 }
61
62 const std::list<std::string>& Py2yacs::getGlobalErrors() const
63 {
64   return _global_errors;
65 }
66
67 const std::list<FunctionProperties>& Py2yacs::getFunctionProperties()const
68 {
69   return _functions;
70 }
71
72 // Copy a python list of strings to a c++ list of strings.
73 // Return an error string. An empty string means no error.
74 static
75 std::string copyList(PyObject *pyList, std::list<std::string>& cppList)
76 {
77   std::string error;
78   if(not PyList_Check(pyList))
79   {
80     error = "Not a python list.\n";
81     //throw Py2yacsException("Not a python list.");
82   }
83   else
84   {
85     int n = PyList_Size(pyList);
86     for(int i=0; i<n; i++)
87     {
88       PyObject *elem = PyList_GetItem(pyList,i);
89       if(not PyString_Check(elem))
90       {
91         std::stringstream message;
92         message << "List element number " << i << " is not a string.\n";
93         error += message.str();
94         // throw Py2yacsException(message.str());
95       }
96       else
97       {
98         const char * portName = PyString_AsString(elem);
99         cppList.push_back(portName);
100       }
101     }
102   }
103   return error;
104 }
105
106 static
107 std::string getPyErrorText()
108 {
109   std::string result="";
110   if (PyErr_Occurred())
111   {
112     PyObject *ptype, *pvalue, *ptraceback;
113     PyObject *pystr, *module_name, *pyth_module, *pyth_func;
114     PyErr_Fetch(&ptype, &pvalue, &ptraceback);
115     pystr = PyObject_Str(pvalue);
116     result = PyString_AsString(pystr);
117     result += "\n";
118     Py_DECREF(pystr);
119     
120     /* See if we can get a full traceback */
121     if(ptraceback)
122     {
123       module_name = PyString_FromString("traceback");
124       pyth_module = PyImport_Import(module_name);
125       Py_DECREF(module_name);
126       if (pyth_module)
127       {
128         pyth_func = PyObject_GetAttrString(pyth_module, "format_exception");
129         if (pyth_func && PyCallable_Check(pyth_func))
130         {
131           PyObject *pyList;
132           pyList = PyObject_CallFunctionObjArgs(pyth_func, ptype, pvalue, ptraceback, NULL);
133           if(pyList)
134           {
135             int n = PyList_Size(pyList);
136             for(int i=0; i<n; i++)
137             {
138               pystr = PyList_GetItem(pyList,i);
139               result += PyString_AsString(pystr);
140             }
141             Py_DECREF(pyList);
142           }
143         }
144         Py_XDECREF(pyth_func);
145         Py_DECREF(pyth_module);
146       }
147     }
148     Py_XDECREF(ptype);
149     Py_XDECREF(pvalue);
150     Py_XDECREF(ptraceback);
151   }
152   return result;
153 }
154
155 static
156 PyObject* checkAndGetAttribute(PyObject *p,
157                                const char* attribute,
158                                std::string& error)
159 {
160   PyObject *pAttribute = PyObject_GetAttrString(p, attribute);
161   if(not pAttribute)
162   {
163     error += "Attribute '";
164     error += attribute;
165     error += "' not found in the returned value of the parsing function.\n";
166     error += getPyErrorText();
167   }
168   return pAttribute;
169 }
170
171 void Py2yacs::load(const std::string& python_code)
172 {
173     PyObject *pModule, *pDict, *pFunc;
174     PyObject *pArgs, *pValue;
175     int i;
176     std::string errorMessage="";
177     _python_code = python_code;
178     _functions.clear();
179     _global_errors.clear();
180     
181     // Py_Initialize();
182     YACS::ENGINE::AutoGIL agil;
183     pValue = PyString_FromString(_python_parser_module.c_str());
184     pModule = PyImport_Import(pValue);
185     Py_DECREF(pValue);
186
187     if (not pModule)
188     {
189       errorMessage  = getPyErrorText();
190       errorMessage += "\nFailed to load ";
191       errorMessage += _python_parser_module;
192       errorMessage += ".\n";
193     }
194     else
195     {
196       pFunc = PyObject_GetAttrString(pModule, _python_parser_function.c_str());
197
198       if (pFunc && PyCallable_Check(pFunc))
199       {
200         pArgs = PyTuple_New(1);
201         pValue = PyString_FromString(python_code.c_str());
202         PyTuple_SetItem(pArgs, 0, pValue);
203         
204         pValue = PyObject_CallObject(pFunc, pArgs);
205         Py_DECREF(pArgs);
206         if (not pValue)
207             errorMessage = getPyErrorText();
208         else
209         {
210           if (not PyTuple_Check(pValue))
211           {
212             errorMessage += "Parsing function should return a tuple of two string lists.\n";
213           }
214           int n = PyTuple_Size(pValue);
215           if(n != 2)
216           {
217             errorMessage += "Parsing function should return two string lists.\n";
218           }
219           PyObject *pyList = PyTuple_GetItem(pValue, 0);
220           if(not PyList_Check(pyList))
221           {
222             errorMessage += "The first returned value of the parsing function"
223                             " should be a python list.\n";
224           }
225           else
226           {
227             n = PyList_Size(pyList);
228             for(int i=0; i<n; i++)
229             {
230               PyObject *fpy = PyList_GetItem(pyList,i);
231               PyObject *pAttribute;
232               
233               if(pAttribute = checkAndGetAttribute(fpy, "name", errorMessage))
234               {
235                 if(not PyString_Check(pAttribute))
236                 {
237                   errorMessage += "Attribute 'name' should be a string.\n";
238                   Py_DECREF(pAttribute);
239                 }
240                 else
241                 {
242                   _functions.push_back(FunctionProperties());
243                   FunctionProperties& fcpp = _functions.back();
244                   fcpp._name=PyString_AsString(pAttribute);
245                   Py_DECREF(pAttribute);
246                   
247                   if(pAttribute = checkAndGetAttribute(fpy, "inputs", errorMessage))
248                     errorMessage += copyList(pAttribute, fcpp._input_ports);
249                   Py_XDECREF(pAttribute);
250                   
251                   if(pAttribute = checkAndGetAttribute(fpy, "outputs", errorMessage))
252                     // None value means no return statement in the function
253                     if(pAttribute != Py_None)
254                       errorMessage += copyList(pAttribute,fcpp._output_ports);
255                   Py_XDECREF(pAttribute);
256                     
257                   if(pAttribute = checkAndGetAttribute(fpy, "errors", errorMessage))
258                     errorMessage += copyList(pAttribute, fcpp._errors);
259                   Py_XDECREF(pAttribute);
260                   
261                   if(pAttribute = checkAndGetAttribute(fpy, "imports", errorMessage))
262                     errorMessage += copyList(pAttribute, fcpp._imports);
263                   Py_XDECREF(pAttribute);
264                 }
265               }
266             }
267           }
268           errorMessage += copyList(PyTuple_GetItem(pValue, 1), _global_errors);
269           Py_DECREF(pValue);
270         }
271       }
272       else
273       {
274         errorMessage  = getPyErrorText();
275         errorMessage += "\nCannot find the parsing function '";
276         errorMessage += _python_parser_function;
277         errorMessage += "' in python module '";
278         errorMessage += _python_parser_module;
279         errorMessage += "'.\n";
280       }
281       Py_XDECREF(pFunc);
282       Py_DECREF(pModule);
283     }
284     
285     if(not errorMessage.empty())
286       throw Py2yacsException(errorMessage);
287     // Py_Finalize();
288 }
289
290 void Py2yacs::save(const std::string& file_path,
291                    const std::string& python_function)const
292 {
293   YACS::ENGINE::Proc* schema = createProc(python_function);
294   schema->saveSchema(file_path);
295   delete schema;
296 }
297
298 YACS::ENGINE::Proc* Py2yacs::createProc(const std::string& python_function)const
299 {
300   if(not _global_errors.empty())
301   {
302     std::string error_message = "The python script contains errors.\n";
303     std::list<std::string>::const_iterator it;
304     for(it = _global_errors.begin(); it != _global_errors.end(); it++)
305       error_message += (*it) + "\n";
306     throw Py2yacsException(error_message);
307   }
308   
309   // find function properties
310   std::list<FunctionProperties>::const_iterator fn_prop = _functions.begin();
311   while(fn_prop != _functions.end() and fn_prop->_name != python_function)
312     fn_prop++;
313   
314   if(fn_prop == _functions.end())
315   {
316     throw Py2yacsException(std::string("Function not found: ")+python_function);
317   }
318   
319   if(not fn_prop->_errors.empty())
320   {
321     std::string error_message = "Function contains errors.\n";
322     std::list<std::string>::const_iterator it;
323     for(it = fn_prop->_errors.begin(); it != fn_prop->_errors.end(); it++)
324       error_message += (*it) + "\n";
325     throw Py2yacsException(error_message);
326   }
327   
328   // add the call to the function at the end of the script
329   std::stringstream fn_call;
330   fn_call << std::endl;
331   std::list<std::string>::const_iterator it;
332   bool first = true;
333   for(it=fn_prop->_output_ports.begin();
334       it!=fn_prop->_output_ports.end();
335       it++)
336   {
337     if (not first)
338       fn_call << ",";
339     first = false;
340     fn_call << *it;
341   }
342   fn_call << "=" << python_function << "(";
343   first = true;
344   for(it = fn_prop->_input_ports.begin();
345       it != fn_prop->_input_ports.end();
346       it++)
347   {
348     if (not first)
349       fn_call << ",";
350     first = false;
351     fn_call << *it;
352   }
353   fn_call << ")" << std::endl;
354   std::string node_body = _python_code + fn_call.str();
355   
356   YACS::ENGINE::Proc* schema;
357   YACS::ENGINE::RuntimeSALOME::setRuntime();
358   YACS::ENGINE::RuntimeSALOME* runtime = YACS::ENGINE::getSALOMERuntime();
359   
360   // build the YACS schema
361   const char * node_name = "default_name";
362   schema = runtime->createProc("Schema");
363   YACS::ENGINE::InlineNode* node = runtime->createScriptNode("", node_name);
364   schema->edAddChild(node);
365   node->setScript(node_body);
366   YACS::ENGINE::TypeCode *tc_double = runtime->getTypeCode("double");
367   
368   for(it = fn_prop->_input_ports.begin();
369       it != fn_prop->_input_ports.end();
370       it++)
371     node->edAddInputPort(*it, tc_double);
372   
373   for(it = fn_prop->_output_ports.begin();
374       it != fn_prop->_output_ports.end();
375       it++)
376     node->edAddOutputPort(*it, tc_double);
377   
378   return schema;
379 }