1 // Copyright (C) 2006-2016 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include "py2yacs.hxx"
22 #include "RuntimeSALOME.hxx"
24 #include "InlineNode.hxx"
25 #include "AutoGIL.hxx"
26 #include "InputPort.hxx"
27 #include "Container.hxx"
29 Py2yacsException::Py2yacsException(const std::string& what)
35 Py2yacsException::~Py2yacsException()throw ()
39 const char * Py2yacsException::what() const throw ()
46 : _python_parser_module("py2yacs"),
47 _python_parser_function("get_properties"),
54 Py2yacs::Py2yacs(const std::string& python_parser_module,
55 const std::string& python_parser_function)
56 : _python_parser_module(python_parser_module),
57 _python_parser_function(python_parser_function),
64 const std::list<std::string>& Py2yacs::getGlobalErrors() const
66 return _global_errors;
69 const std::list<FunctionProperties>& Py2yacs::getFunctionProperties()const
74 // Copy a python list of strings to a c++ list of strings.
75 // Return an error string. An empty string means no error.
77 std::string copyList(PyObject *pyList, std::list<std::string>& cppList)
80 if(!PyList_Check(pyList))
82 error = "Not a python list.\n";
83 //throw Py2yacsException("Not a python list.");
87 int n = PyList_Size(pyList);
88 for(int i=0; i<n; i++)
90 PyObject *elem = PyList_GetItem(pyList,i);
91 if(!PyString_Check(elem))
93 std::stringstream message;
94 message << "List element number " << i << " is not a string.\n";
95 error += message.str();
96 // throw Py2yacsException(message.str());
100 const char * portName = PyString_AsString(elem);
101 cppList.push_back(portName);
109 std::string getPyErrorText()
111 std::string result="";
112 if (PyErr_Occurred())
114 PyObject *ptype, *pvalue, *ptraceback;
115 PyObject *pystr, *module_name, *pyth_module, *pyth_func;
116 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
117 pystr = PyObject_Str(pvalue);
118 result = PyString_AsString(pystr);
122 /* See if we can get a full traceback */
125 module_name = PyString_FromString("traceback");
126 pyth_module = PyImport_Import(module_name);
127 Py_DECREF(module_name);
130 pyth_func = PyObject_GetAttrString(pyth_module, "format_exception");
131 if (pyth_func && PyCallable_Check(pyth_func))
134 pyList = PyObject_CallFunctionObjArgs(pyth_func, ptype, pvalue, ptraceback, NULL);
137 int n = PyList_Size(pyList);
138 for(int i=0; i<n; i++)
140 pystr = PyList_GetItem(pyList,i);
141 result += PyString_AsString(pystr);
146 Py_XDECREF(pyth_func);
147 Py_DECREF(pyth_module);
152 Py_XDECREF(ptraceback);
158 PyObject* checkAndGetAttribute(PyObject *p,
159 const char* attribute,
162 PyObject *pAttribute = PyObject_GetAttrString(p, attribute);
165 error += "Attribute '";
167 error += "' not found in the returned value of the parsing function.\n";
168 error += getPyErrorText();
173 void Py2yacs::load(const std::string& python_code)
175 PyObject *pModule, *pDict, *pFunc;
176 PyObject *pArgs, *pValue;
178 std::string errorMessage="";
179 _python_code = python_code;
181 _global_errors.clear();
184 YACS::ENGINE::AutoGIL agil;
185 pValue = PyString_FromString(_python_parser_module.c_str());
186 pModule = PyImport_Import(pValue);
191 errorMessage = getPyErrorText();
192 errorMessage += "\nFailed to load ";
193 errorMessage += _python_parser_module;
194 errorMessage += ".\n";
198 pFunc = PyObject_GetAttrString(pModule, _python_parser_function.c_str());
200 if (pFunc && PyCallable_Check(pFunc))
202 pArgs = PyTuple_New(1);
203 pValue = PyString_FromString(python_code.c_str());
204 PyTuple_SetItem(pArgs, 0, pValue);
206 pValue = PyObject_CallObject(pFunc, pArgs);
209 errorMessage = getPyErrorText();
212 if (!PyTuple_Check(pValue))
214 errorMessage += "Parsing function should return a tuple of two string lists.\n";
216 int n = PyTuple_Size(pValue);
219 errorMessage += "Parsing function should return two string lists.\n";
221 PyObject *pyList = PyTuple_GetItem(pValue, 0);
222 if(!PyList_Check(pyList))
224 errorMessage += "The first returned value of the parsing function"
225 " should be a python list.\n";
229 n = PyList_Size(pyList);
230 for(int i=0; i<n; i++)
232 PyObject *fpy = PyList_GetItem(pyList,i);
233 PyObject *pAttribute;
235 if(pAttribute = checkAndGetAttribute(fpy, "name", errorMessage))
237 if(!PyString_Check(pAttribute))
239 errorMessage += "Attribute 'name' should be a string.\n";
240 Py_DECREF(pAttribute);
244 _functions.push_back(FunctionProperties());
245 FunctionProperties& fcpp = _functions.back();
246 fcpp._name=PyString_AsString(pAttribute);
247 Py_DECREF(pAttribute);
249 if(pAttribute = checkAndGetAttribute(fpy, "inputs", errorMessage))
250 errorMessage += copyList(pAttribute, fcpp._input_ports);
251 Py_XDECREF(pAttribute);
253 if(pAttribute = checkAndGetAttribute(fpy, "outputs", errorMessage))
254 // None value means no return statement in the function
255 if(pAttribute != Py_None)
256 errorMessage += copyList(pAttribute,fcpp._output_ports);
257 Py_XDECREF(pAttribute);
259 if(pAttribute = checkAndGetAttribute(fpy, "errors", errorMessage))
260 errorMessage += copyList(pAttribute, fcpp._errors);
261 Py_XDECREF(pAttribute);
263 if(pAttribute = checkAndGetAttribute(fpy, "imports", errorMessage))
264 errorMessage += copyList(pAttribute, fcpp._imports);
265 Py_XDECREF(pAttribute);
270 errorMessage += copyList(PyTuple_GetItem(pValue, 1), _global_errors);
276 errorMessage = getPyErrorText();
277 errorMessage += "\nCannot find the parsing function '";
278 errorMessage += _python_parser_function;
279 errorMessage += "' in python module '";
280 errorMessage += _python_parser_module;
281 errorMessage += "'.\n";
287 if(!errorMessage.empty())
288 throw Py2yacsException(errorMessage);
292 void Py2yacs::save(const std::string& file_path,
293 const std::string& python_function)const
295 YACS::ENGINE::Proc* schema = createProc(python_function);
296 schema->saveSchema(file_path);
300 YACS::ENGINE::Proc* Py2yacs::createProc(const std::string& python_function)const
302 if(!_global_errors.empty())
304 std::string error_message = "The python script contains errors.\n";
305 std::list<std::string>::const_iterator it;
306 for(it = _global_errors.begin(); it != _global_errors.end(); it++)
307 error_message += (*it) + "\n";
308 throw Py2yacsException(error_message);
311 // find function properties
312 std::list<FunctionProperties>::const_iterator fn_prop = _functions.begin();
313 while(fn_prop != _functions.end() && fn_prop->_name != python_function)
316 if(fn_prop == _functions.end())
318 throw Py2yacsException(std::string("Function not found: ")+python_function);
321 if(!fn_prop->_errors.empty())
323 std::string error_message = "Function contains errors.\n";
324 std::list<std::string>::const_iterator it;
325 for(it = fn_prop->_errors.begin(); it != fn_prop->_errors.end(); it++)
326 error_message += (*it) + "\n";
327 throw Py2yacsException(error_message);
330 // add the call to the function at the end of the script
331 std::stringstream fn_call;
332 fn_call << std::endl;
333 std::list<std::string>::const_iterator it;
335 for(it=fn_prop->_output_ports.begin();
336 it!=fn_prop->_output_ports.end();
344 fn_call << "=" << python_function << "(";
346 for(it = fn_prop->_input_ports.begin();
347 it != fn_prop->_input_ports.end();
355 fn_call << ")" << std::endl;
356 std::string node_body = _python_code + fn_call.str();
358 YACS::ENGINE::Proc* schema;
359 YACS::ENGINE::RuntimeSALOME::setRuntime();
360 YACS::ENGINE::RuntimeSALOME* runtime = YACS::ENGINE::getSALOMERuntime();
362 // build the YACS schema
363 const char * node_name = "default_name";
364 schema = runtime->createProc("Schema");
365 YACS::ENGINE::InlineNode* node = runtime->createScriptNode("", node_name);
366 schema->edAddChild(node);
367 node->setScript(node_body);
368 YACS::ENGINE::TypeCode *tc_double = runtime->getTypeCode("double");
370 for(it = fn_prop->_input_ports.begin();
371 it != fn_prop->_input_ports.end();
374 YACS::ENGINE::InputPort *newport = node->edAddInputPort(*it, tc_double);
375 newport->edInit(0.0);
378 for(it = fn_prop->_output_ports.begin();
379 it != fn_prop->_output_ports.end();
381 node->edAddOutputPort(*it, tc_double);
383 node->setExecutionMode(YACS::ENGINE::InlineNode::REMOTE_STR);
384 YACS::ENGINE::Container* cont=schema->createContainer("Py2YacsContainer");
385 node->setContainer(cont);
390 std::string Py2yacs::getAllErrors()const
392 std::stringstream buffer;
394 if(! _global_errors.empty())
396 buffer << "Global errors:" << std::endl;
397 std::list<std::string>::const_iterator it;
398 for(it=_global_errors.begin(); it!=_global_errors.end(); it++)
400 buffer << *it << std::endl;
402 buffer << "-----------------------------------------" << std::endl;
405 std::list<FunctionProperties>::const_iterator it_fp;
406 for(it_fp=_functions.begin();it_fp!=_functions.end();it_fp++)
408 if(! it_fp->_errors.empty())
410 buffer << "Function " << it_fp->_name << " has errors:" << std::endl;
411 std::list<std::string>::const_iterator it;
412 buffer << "Errors :" ;
413 for(it=it_fp->_errors.begin();it!=it_fp->_errors.end();it++)
414 buffer << *it << std::endl;
415 buffer << "-----------------------------------------" << std::endl;
421 std::string Py2yacs::getFunctionErrors(const std::string& functionName)const
423 std::stringstream buffer;
425 if(! _global_errors.empty())
427 buffer << "Global errors:" << std::endl;
428 std::list<std::string>::const_iterator it;
429 for(it=_global_errors.begin(); it!=_global_errors.end(); it++)
431 buffer << *it << std::endl;
433 buffer << "-----------------------------------------" << std::endl;
436 bool nameFound = false;
437 std::list<FunctionProperties>::const_iterator it_fp;
438 for(it_fp=_functions.begin(); it_fp!=_functions.end() && !nameFound; it_fp++)
440 if(it_fp->_name == functionName)
443 if(! it_fp->_errors.empty())
445 buffer << "Function " << it_fp->_name << " has errors:" << std::endl;
446 std::list<std::string>::const_iterator it;
447 buffer << "Errors :" ;
448 for(it=it_fp->_errors.begin();it!=it_fp->_errors.end();it++)
449 buffer << *it << std::endl;
450 buffer << "-----------------------------------------" << std::endl;
457 buffer << "Function " << functionName << " not found." << std::endl;